Advertisement
Guest User

Untitled

a guest
Apr 17th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BlitzMax 12.05 KB | None | 0 0
  1. ' stencil_shadows.bmx
  2. ' static and dynamic stencil shadows
  3.  
  4. Strict
  5.  
  6. Framework Openb3d.B3dglgraphics
  7. Import Brl.Random
  8.  
  9. Graphics3D 800,600,0,2
  10.  
  11. Local camera:TCamera=CreateCamera()
  12. PositionEntity camera,-20,10,0
  13. CameraClsColor camera,0,0,255
  14.  
  15. ' set light mode
  16. Local lightmode%=2
  17. Local light:TLight=CreateLight(lightmode)
  18. Local light2:TLight=CreateLight(lightmode)
  19.  
  20. Local light_piv:TPivot=CreatePivot()
  21. PositionEntity light_piv,50,0,50
  22. Local light_piv2:TPivot=CreatePivot()
  23. PositionEntity light_piv2,50,0,50
  24.  
  25. Local sphere:TMesh=CreateSphere(8,light_piv)
  26. PositionEntity sphere,0,25,50
  27. EntityColor sphere,255,0,0
  28.  
  29. Local sphere2:TMesh=CreateSphere(8,light_piv2)
  30. PositionEntity sphere2,0,25,50
  31. EntityColor sphere2,0,255,0
  32.  
  33. Local plane:TMesh=CreatePlane(16)
  34. MoveEntity plane,0,-1,0
  35. Local tex:TTexture=LoadTexture("media/Moss.bmp")
  36. EntityTexture plane,tex
  37.  
  38. PointEntity camera,plane
  39. PositionEntity camera,-40,25,55
  40.  
  41. ' set shadow vars
  42. Local static%=0 ' global static or dynamic shadows
  43. Local numtypes%=25 ' number of cubes or cylinders
  44. Local size%=100 ' size of area
  45. Local numshadows%=0 ' shadow counter
  46. Local animnumshadows%=0
  47. Global scolor:Int[]=[100,0,0,100] ' global shadow colors 0..255
  48. SetShadowColor(scolor[0],scolor[1],scolor[2],scolor[3])
  49.  
  50. Global lightcasters:TMesh[] ' all shadow casters
  51. Global light1shadows:TShadowObject[] ' all lights if dynamic
  52. Global light2shadows:TShadowObject[] ' second shadow array for static shadows
  53. Global animlightcasters:TMesh[1]
  54. Global animlight1shadows:TShadowObject[1]
  55. Global animlight2shadows:TShadowObject[1]
  56.  
  57. ' alphamapped quads
  58. Local quad:TMesh=CreateQuad()
  59. RotateMesh quad,90,0,0
  60. ScaleMesh quad,15,15,15
  61. PositionEntity quad,15,5,55
  62.  
  63. Local tex2:TTexture=LoadTexture("media/alpha_map.png")
  64. EntityTexture(quad,tex2)
  65. EntityFX(quad,32)
  66. HideEntity quad
  67.  
  68. ' load anim mesh
  69. Local anim_time#=0
  70. Local animmode%=1
  71. Local ent:TMesh=LoadAnimMesh("media/zombie.b3d")
  72. PositionEntity ent,55,0,50
  73.  
  74. animlightcasters[animnumshadows]=ent
  75. animlight1shadows[animnumshadows]=CreateShadow(animlightcasters[animnumshadows],static)
  76. If static=1
  77.     animlight2shadows[animnumshadows]=CreateShadow(animlightcasters[animnumshadows],static)
  78. EndIf
  79. animnumshadows:+1 ' increment array index
  80.  
  81. ' load cubes - static shadows need a separate shadow array for each light
  82. For Local i%=0 To numtypes-1
  83.     lightcasters=lightcasters[..numshadows+1]
  84.     light1shadows=light1shadows[..numshadows+1]
  85.     light2shadows=light2shadows[..numshadows+1]
  86.    
  87.     lightcasters[numshadows]=CreateCube()
  88.     PositionEntity lightcasters[numshadows],Rnd(size),0,Rnd(size)
  89.     EntityColor lightcasters[numshadows],Rnd(255),Rnd(255),Rnd(255)
  90.     light1shadows[numshadows]=CreateShadow(lightcasters[numshadows],static)
  91.     If static=1
  92.         light2shadows[numshadows]=CreateShadow(lightcasters[numshadows],static)
  93.     EndIf
  94.    
  95.     numshadows:+1
  96. Next
  97.  
  98. ' load cylinders
  99. For Local i%=0 To numtypes-1
  100.     lightcasters=lightcasters[..numshadows+1]
  101.     light1shadows=light1shadows[..numshadows+1]
  102.     light2shadows=light2shadows[..numshadows+1]
  103.    
  104.     lightcasters[numshadows]=CreateCylinder()
  105.     PositionEntity lightcasters[numshadows],Rnd(size),1.5,Rnd(size)
  106.     EntityColor lightcasters[numshadows],Rnd(255),Rnd(255),Rnd(255)
  107.     FitMesh lightcasters[numshadows],-1,-1,-1,2,5,2 ' use ScaleMesh/FitMesh but not ScaleEntity
  108.     'ScaleEntity cylinder[i],2,5,2
  109.     light1shadows[numshadows]=CreateShadow(lightcasters[numshadows],static)
  110.     If static=1
  111.         light2shadows[numshadows]=CreateShadow(lightcasters[numshadows],static)
  112.     EndIf
  113.    
  114.     numshadows:+1
  115. Next
  116.  
  117. ' initial static shadows render
  118. TurnEntity light_piv2,0,-90,0
  119. PositionEntity light,EntityX(sphere,1),EntityY(sphere,1),EntityZ(sphere,1) 
  120. PositionEntity light2,EntityX(sphere2,1),EntityY(sphere2,1),EntityZ(sphere2,1)
  121. If static=1 Then CastStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  122.  
  123. Local wiretoggle%=-1
  124. Local lightmove%=1
  125. Local cylindermove%=1
  126. Local hidelight1%=0
  127. Local hidelight2%=0
  128. Local max2dmode%=0
  129. Local hidealpha%=1
  130.  
  131. ' fps code
  132. Local old_ms%=MilliSecs()
  133. Local renders%, fps%, ticks%=0
  134.  
  135.  
  136. Local mesh1:TMesh , mesh2:TMesh
  137. Local surface1:TSurface , surface2:TSurface
  138. mesh1    = CreateMesh()
  139. surface1 = CreateSurface( mesh1 )
  140. mesh2    = CreateMesh()
  141. surface2 = CreateSurface( mesh2 )
  142. EntityBlend( mesh1, 1 )
  143. EntityBlend( mesh2, 3 )
  144. EntityFX   ( mesh1, 59 )
  145. EntityFX   ( mesh2, 59 )
  146.  
  147.  
  148. While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
  149.    
  150.     If KeyHit(KEY_SPACE) Then wiretoggle=-wiretoggle
  151.     If wiretoggle=1 Then Wireframe True Else Wireframe False
  152.    
  153.     ' move camera
  154.     If KeyDown(KEY_PAGEUP) Then TranslateEntity camera,1,0,0
  155.     If KeyDown(KEY_PAGEDOWN) Then TranslateEntity camera,-1,0,0
  156.     If KeyDown(KEY_UP) Then MoveEntity camera,0,0,1
  157.     If KeyDown(KEY_DOWN) Then MoveEntity camera,0,0,-1
  158.     If KeyDown(KEY_LEFT) Then MoveEntity camera,-1,0,0
  159.     If KeyDown(KEY_RIGHT) Then MoveEntity camera,1,0,0 
  160.    
  161.     ' start/stop light movement
  162.     If KeyHit(KEY_L) Then lightmove=-lightmove
  163.     If lightmove=1 Then TurnEntity light_piv,0,1,0 ; TurnEntity light_piv2,0,-1,0
  164.    
  165.     ' start/stop cube/cylinder movement
  166.     If KeyHit(KEY_C) Then cylindermove=-cylindermove
  167.     If static=0 '  static shadows only work for static casters - causes 'wrong' casting on casters
  168.         For Local j%=1 To numshadows-1
  169.             If cylindermove=1 Then TurnEntity lightcasters[j],0,0.25,-2.5
  170.         Next
  171.     EndIf
  172.    
  173.     ' reset static shadows
  174.     If KeyHit(KEY_R)
  175.         If static=1 Then CastStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  176.     EndIf
  177.    
  178.     ' hide/show light1 - hide unwanted static shadows by freeing/re-creating
  179.     If KeyHit(KEY_1)
  180.         hidelight1=Not hidelight1
  181.         If hidelight1
  182.             HideEntity light
  183.             HideEntity sphere
  184.             If static=1
  185.                 FreeStaticShadows(numshadows,animnumshadows,camera,light,light2,1)
  186.             EndIf
  187.         Else
  188.             ShowEntity light
  189.             ShowEntity sphere
  190.             If static=1
  191.                 CreateStaticShadows(numshadows,animnumshadows,camera,light,light2,1)
  192.             EndIf
  193.         EndIf
  194.     EndIf
  195.    
  196.     ' hide/show light2
  197.     If KeyHit(KEY_2)
  198.         hidelight2=Not hidelight2
  199.         If hidelight2
  200.             HideEntity light2
  201.             HideEntity sphere2
  202.             If static=1
  203.                 FreeStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  204.             EndIf
  205.         Else
  206.             ShowEntity light2
  207.             ShowEntity sphere2
  208.             If static=1
  209.                 CreateStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  210.             EndIf
  211.         EndIf
  212.     EndIf
  213.    
  214.     ' max2d mode
  215.     If KeyHit(KEY_M) Then max2dmode=Not max2dmode
  216.    
  217.     ' hide alpha quad
  218.     If KeyHit(KEY_Q)
  219.         hidealpha=Not hidealpha
  220.         If hidealpha Then HideEntity quad Else ShowEntity quad
  221.     EndIf
  222.    
  223.     ' anim mode
  224.     If KeyHit(KEY_A) Then animmode:+1
  225.     If animmode>2 Then animmode=0
  226.     If animmode>0 Then anim_time#:+-0.5
  227.     SetAnimTime(ent,anim_time#)
  228.    
  229.     If static=1 And animmode>0 ' shows dynamic with static shadows - anim is just set at array index 0
  230.         CastStaticShadows(numshadows,animnumshadows,camera,light,light2,animmode)
  231.     EndIf
  232.    
  233.     PositionEntity light,EntityX(sphere,1),EntityY(sphere,1),EntityZ(sphere,1)
  234.     PointEntity light,plane
  235.     PositionEntity light2,EntityX(sphere2,1),EntityY(sphere2,1),EntityZ(sphere2,1)
  236.     PointEntity light2,plane
  237.    
  238.     If static=1 ' must have only one light on for main render or get doubled static shadows
  239.         HideStaticLights(light,light2,hidelight1,hidelight2)
  240.     EndIf
  241.    
  242.     If static=0 ' render first shadow color
  243.         SetShadowColor(scolor[0],scolor[1],scolor[2],100) ' light1 color
  244.     EndIf
  245.    
  246.     RenderWorld
  247.    
  248.     If static=0 ' render second shadow color
  249.         SetShadowColor(scolor[0],scolor[1],scolor[2],250) ' light2 color
  250.         If light2.Hidden() Then SetShadowColor(scolor[0],scolor[1],scolor[2],100)
  251.         If Not light.Hidden() Or Not light2.Hidden() ' don't render if no lights
  252.             TShadowObject.ShadowRenderWorldZFail() ' re-render shadows
  253.         EndIf
  254.     EndIf
  255.    
  256.     If static=1
  257.         ShowStaticLights(light,light2,hidelight1,hidelight2)
  258.     EndIf
  259.    
  260.     ' calculate fps
  261.     renders=renders+1
  262.     If MilliSecs()-old_ms>=1000
  263.         old_ms=MilliSecs()
  264.         fps=renders
  265.         renders=0
  266.     EndIf
  267.    
  268.     Text 0,0,"FPS: "+fps
  269.     Text 0,20,"Arrows: move camera, L: light movement, C: cube/cylinder movement"
  270.     Text 0,40,"R: reset static shadows, 1/2: hide lights, light mode = "+lightmode
  271.     Text 0,60,"M: Max2d mode, Q: hide alpha quad, A: anim mode = "+animmode
  272.     Text 0,80,"camera position = "+EntityX(camera)+" "+EntityY(camera)+" "+EntityZ(camera)
  273.    
  274.     If max2dmode
  275.         BeginMax2D()
  276.         DrawText "Testing Max2d",0,100
  277.         EndMax2D()
  278.     EndIf
  279.    
  280.     Flip
  281.    
  282. Wend
  283.  
  284.  
  285. Function HideStaticLights( light:TLight,light2:TLight,light1hid%,light2hid% )
  286.  
  287.     If light1hid=1 Then HideEntity light ' light1 off
  288.     If light2hid=1 Then HideEntity light2 ' light2 off
  289.     If light1hid=0 And light2hid=0 Then HideEntity light2 ' light1 on, light2 on - so hide light2
  290.    
  291. End Function
  292.  
  293. Function ShowStaticLights( light:TLight,light2:TLight,light1hid%,light2hid% )
  294.  
  295.     If light1hid=1 Then ShowEntity light ' light1 off
  296.     If light2hid=1 Then ShowEntity light2 ' light2 off
  297.     If light1hid=0 And light2hid=0 Then ShowEntity light2 ' light1 on, light2 on
  298.    
  299. End Function
  300.  
  301. Function FreeStaticShadows( numshadows%,animnumshadows%,camera:TCamera,light:TLight,light2:TLight,lightid% )
  302.  
  303.     For Local i%=0 To numshadows-1
  304.         If lightid=1 Then FreeShadow(light1shadows[i])
  305.         If lightid=2 Then FreeShadow(light2shadows[i])
  306.     Next
  307.     CastStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  308.    
  309.     For Local i%=0 To animnumshadows-1
  310.         If lightid=1 Then FreeShadow(animlight1shadows[i])
  311.         If lightid=2 Then FreeShadow(animlight2shadows[i])
  312.     Next
  313.     CastStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  314.    
  315. End Function
  316.  
  317. Function CreateStaticShadows( numshadows%,animnumshadows%,camera:TCamera,light:TLight,light2:TLight,lightid% )
  318.  
  319.     For Local i%=0 To numshadows-1
  320.         If lightid=1 Then light1shadows[i]=CreateShadow(lightcasters[i],1)
  321.         If lightid=2 Then light2shadows[i]=CreateShadow(lightcasters[i],1)
  322.     Next
  323.     CastStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  324.    
  325.     For Local i%=0 To animnumshadows-1
  326.         If lightid=1 Then animlight1shadows[i]=CreateShadow(animlightcasters[i],1)
  327.         If lightid=2 Then animlight2shadows[i]=CreateShadow(animlightcasters[i],1)
  328.     Next
  329.     CastStaticShadows(numshadows,animnumshadows,camera,light,light2,2)
  330.    
  331. End Function
  332.  
  333. Function CastStaticShadows( numshadows%,animnumshadows%,camera:TCamera,light:TLight,light2:TLight,animmode% )
  334.  
  335.     Local currdist#, maxdist#=0
  336.     Local light1hid%=light.hide[0]
  337.     Local light2hid%=light2.hide[0]
  338.    
  339.     For Local i%=0 To numshadows-1 ' calculate furthest caster from camera to set camera range
  340.         currdist=EntityDistance(camera,lightcasters[i])
  341.         If currdist>maxdist Then maxdist=currdist
  342.     Next
  343.     For Local i%=0 To animnumshadows-1 ' calculate furthest caster from camera to set camera range
  344.         currdist=EntityDistance(camera,animlightcasters[i])
  345.         If currdist>maxdist Then maxdist=currdist
  346.     Next
  347.    
  348.     CameraRange camera,1,maxdist+100 ' shorthen camera range to cap any 'detached' static shadows
  349.    
  350.     If light2hid=0 And light1hid=0 ' light2 on, light1 on - 2 lights, 3 possible states
  351.         ShowEntity light2
  352.         HideEntity light
  353.         For Local i%=0 To animnumshadows-1
  354.             animlight2shadows[i].ResetShadow()
  355.         Next
  356.         If animmode=2
  357.             For Local i%=0 To numshadows-1
  358.                 light2shadows[i].ResetShadow()
  359.             Next
  360.         EndIf
  361.         RenderWorld
  362.        
  363.         HideEntity light2
  364.         ShowEntity light
  365.         For Local i%=0 To animnumshadows-1
  366.             animlight1shadows[i].ResetShadow()
  367.         Next
  368.         If animmode=2
  369.             For Local i%=0 To numshadows-1
  370.                 light1shadows[i].ResetShadow()
  371.             Next
  372.         EndIf
  373.         RenderWorld
  374.        
  375.         ShowEntity light
  376.         ShowEntity light2
  377.     EndIf
  378.    
  379.     If light2hid=1 And light1hid=0 ' light2 off, light1 on
  380.         HideEntity light2
  381.         ShowEntity light
  382.         For Local i%=0 To animnumshadows-1
  383.             animlight1shadows[i].ResetShadow()
  384.         Next
  385.         If animmode=2
  386.             For Local i%=0 To numshadows-1
  387.                 light1shadows[i].ResetShadow()
  388.             Next
  389.         EndIf      
  390.         RenderWorld
  391.        
  392.         ShowEntity light
  393.     EndIf
  394.    
  395.     If light2hid=0 And light1hid=1 ' light2 on, light1 off
  396.         ShowEntity light2
  397.         HideEntity light
  398.         For Local i%=0 To animnumshadows-1
  399.             animlight2shadows[i].ResetShadow()
  400.         Next
  401.         If animmode=2
  402.             For Local i%=0 To numshadows-1
  403.                 light2shadows[i].ResetShadow()
  404.             Next
  405.         EndIf
  406.         RenderWorld
  407.        
  408.         ShowEntity light2
  409.     EndIf
  410.    
  411.     CameraRange camera,1,1000
  412.    
  413. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement