Advertisement
Guest User

Untitled

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