2
0

shadows.bb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;=================
  2. ;Geometric Shadows
  3. ;=================
  4. ;by Joshua 'H A L O' Klint
  5. ;[email protected]
  6. Include "../start.bb"
  7. camera=CreateCamera()
  8. PositionEntity camera,40,0,-200
  9. pivot=CreatePivot()
  10. ;Make our cube object
  11. mesh=CreateCube()
  12. t=LoadTexture("blitzlogo.bmp")
  13. EntityTexture mesh,t
  14. FreeTexture t
  15. ScaleMesh mesh,10,10,10
  16. ;Create light and flare
  17. l=CreateLight()
  18. piv=CreatePivot()
  19. PointEntity camera,piv
  20. LightColor l,255,255,255
  21. LightRange l,10
  22. s=CreateSprite()
  23. t=LoadTexture("flare0.bmp")
  24. ScaleSprite s,10,10
  25. EntityBlend s,3
  26. EntityTexture s,t
  27. FreeTexture t
  28. EntityColor s,255,255,255
  29. EntityParent s,l
  30. PositionEntity l,30,0,-30
  31. PointEntity l,piv
  32. EntityParent l,piv
  33. ;Create wall
  34. plane=CreatePlane()
  35. t=LoadTexture("wall.bmp")
  36. ScaleTexture t,100,100
  37. EntityTexture plane,t
  38. FreeTexture t
  39. PositionEntity plane,0,0,100
  40. RotateEntity plane,270,35,0
  41. EntityType plane,10
  42. EntityFX plane,1
  43. Collisions 1,10,2,1
  44. Dim tri(2)
  45. Dim shadow(999,999,5)
  46. ;Create a pivot to keep track of the position of each vertex
  47. mesh2=CreateCube()
  48. EntityColor mesh2,0,0,0
  49. EntityAlpha mesh2,0.2
  50. EntityFX mesh2,1
  51. EntityParent mesh2,0
  52. For s=1 To CountSurfaces(mesh2)
  53. surf=GetSurface(mesh,s)
  54. For k=0 To CountTriangles(surf)-1
  55. tri(0)=TriangleVertex(surf,k,0)
  56. tri(1)=TriangleVertex(surf,k,1)
  57. tri(2)=TriangleVertex(surf,k,2)
  58. For i=0 To 2
  59. PositionEntity pivot,VertexX(surf,tri(i)),VertexY(surf,tri(i)),VertexZ(surf,tri(i))
  60. shadow(s,k,i)=CopyEntity(pivot);CreatePivot()
  61. shadow(s,k,i+3)=CopyEntity(pivot);CreatePivot()
  62. PositionEntity shadow(s,k,i+3),EntityX(pivot),EntityY(pivot),EntityZ(pivot)
  63. EntityParent shadow(s,k,i+3),mesh
  64. EntityType shadow(s,k,i),1
  65. Next
  66. Next
  67. Next
  68. ;Eliminate redundant pivots
  69. For s=1 To CountSurfaces(mesh2)
  70. surf=GetSurface(mesh,s)
  71. For k=0 To CountTriangles(surf)-1
  72. For i=0 To 2
  73. For sd=1 To CountSurfaces(mesh2)
  74. surfd=GetSurface(mesh,s)
  75. For kd=0 To CountTriangles(surfd)-1
  76. For id=0 To 2
  77. If EntityX(shadow(s,k,i+3))=EntityX(shadow(sd,kd,id+3))
  78. If EntityY(shadow(s,k,i+3))=EntityY(shadow(sd,kd,id+3))
  79. If EntityZ(shadow(s,k,i+3))=EntityZ(shadow(sd,kd,id+3))
  80. If shadow(s,k,i+3)<>shadow(sd,kd,id+3)
  81. FreeEntity shadow(sd,kd,id+3)
  82. shadow(sd,kd,id+3)=shadow(s,k,i+3)
  83. EndIf
  84. EndIf
  85. EndIf
  86. EndIf
  87. Next
  88. Next
  89. Next
  90. Next
  91. Next
  92. Next
  93. While Not KeyHit(1)
  94. TurnEntity mesh,1,1,0
  95. TurnEntity piv,0,-1,0
  96. RotateEntity piv,0,EntityYaw(piv),Cos(MilliSecs()/5)*20
  97. ;Here's how it works. We have 6 pivots per triangle. We already attached
  98. ;3 to each triangle. Now we are going to take 3 more for each triangle,
  99. ;position them at the light, point them at their corresponding vertices
  100. ;we attached to the model, and move them until they hit the wall.
  101. For s=1 To CountSurfaces(mesh)
  102. surf=GetSurface(mesh,s)
  103. For k=0 To CountTriangles(surf)-1
  104. For i=0 To 2
  105. PositionEntity shadow(s,k,i),EntityX(l,1),EntityY(l,1),EntityZ(l,1)
  106. PositionEntity pivot,EntityX(shadow(s,k,i+3),1),EntityY(shadow(s,k,i+3),1),EntityZ(shadow(s,k,i+3),1)
  107. PointEntity shadow(s,k,i),pivot
  108. ResetEntity shadow(s,k,i)
  109. MoveEntity shadow(s,k,i),0,0,9999
  110. Next
  111. Next
  112. Next
  113. UpdateWorld
  114. If KeyDown(30) MoveEntity l,0,0,2
  115. If KeyDown(44) MoveEntity l,0,0,-2
  116. If KeyDown(203) TurnEntity mesh,0,1,0
  117. If KeyDown(205) TurnEntity mesh,0,-1,0
  118. ;Now that are pivots have collided with the wall, we set the shadow mesh's
  119. ;vertices to their positions.
  120. For s=1 To CountSurfaces(mesh2)
  121. surf=GetSurface(mesh2,s)
  122. For k=0 To CountTriangles(surf)-1
  123. For i=0 To 2
  124. v=TriangleVertex(surf,k,i)
  125. VertexCoords surf,v,EntityX(shadow(s,k,i)),EntityY(shadow(s,k,i)),EntityZ(shadow(s,k,i))
  126. Next
  127. Next
  128. Next
  129. RenderWorld
  130. Flip
  131. Wend
  132. End