Ooops
November 28, 2020, 01:47:26 PM

Author Topic: [bb] Simple shadows by nawi [ 1+ years ago ]  (Read 556 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Simple shadows by nawi [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Simple shadows
Author : nawi
Posted : 1+ years ago

Description : This is a simple shadow system I made for fun. It's not that fast, and alpha shadows don't work properly.. but you might learn something from this.

Pic:
[img]img146.imageshack.us/img146/8527/shadow2ti1.html">


Code :
Code: BlitzBasic
  1. Graphics3D 800,600,0,2
  2. SetBuffer BackBuffer()
  3.  
  4.  
  5. Global Cam = CreateCamera()
  6. MoveEntity Cam,0,5,-2
  7.  
  8.  
  9.  
  10. NewObject = GetObject()
  11.  
  12.  
  13.  
  14. ;Position our entity
  15. MoveEntity NewObject,0,2.5,0
  16.  
  17. ;White plane
  18. Plane = CreatePlane()
  19. EntityColor Plane,255,255,255
  20. MoveEntity Plane,0,-0.01,0
  21.  
  22. ;Create light
  23. Light = CreateSphere(5)
  24. Spot = CreateLight(2,Light)
  25. ScaleEntity Light,0.1,0.1,0.1
  26. EntityColor Light,255,255,0
  27.  
  28.  
  29.  
  30. Repeat
  31.         ;Camera movement
  32.         If KeyDown(200) Then MoveEntity Cam,0,0,1
  33.         If KeyDown(208) Then MoveEntity Cam,0,0,-1
  34.         If KeyDown(203) Then MoveEntity Cam,-1,0,0
  35.         If KeyDown(205) Then MoveEntity Cam,1,0,0
  36.  
  37.  
  38.         ;Shadow object must be created and destroyed every frame
  39.         Shadow = CreateMesh()
  40.        
  41.         EntityAlpha Shadow,1
  42.         ;There is sadly a bug with alpha shadows.. Didn't figure out a proper solution to this one
  43.         ;You could use a camera, but would be a major slowdown
  44.        
  45. ;       EntityColor Shadow,0,0,0
  46.        
  47.  
  48.         ;Light movement
  49.         GY# = 7+Sin(MilliSecs()/10)*0.5
  50.         GX# = Cos(MilliSecs()/10)*3
  51.        
  52.         ;Draw shadow
  53.         DrawShadow(NewObject,GX#,GY#,GZ#,Shadow)
  54.         PositionEntity Light,GX#,GY#,GZ#
  55.        
  56.        
  57.         ;FPS
  58.         FPSC=FPSC+1
  59.         If MilliSecs()>FPSTimer+999 Then
  60.                 FPSTimer = MilliSecs()
  61.                 FPS=FPSC
  62.                 FPSC=0
  63.         EndIf
  64.        
  65.        
  66.  
  67.  
  68.         RenderWorld
  69.  
  70.         Text 0,0,"FPS: " + FPS
  71.         ;Text 0,20,"Trisrendered: " + TrisRendered()
  72.         Flip 0
  73.  
  74.         ;Destroy shadow
  75.         FreeEntity Shadow
  76.  
  77. Until KeyHit(1)
  78.  
  79. Function DrawShadow(Entity,SX#,SY#,SZ#,Shadow)
  80.         ;This works by looping through all triangles, then calculating a line for each vertex to ground (y=0)
  81.  
  82.         EX#=EntityX#(Entity,1)
  83.         EY#=EntityY#(Entity,1)
  84.         EZ#=EntityZ#(Entity,1)
  85.         ShadowSurface = CreateSurface(Shadow)
  86.         Surfaces=CountSurfaces(Entity)
  87.         For s=1 To Surfaces
  88.                 Surface = GetSurface(Entity,s)
  89.                
  90.  
  91.                 Triangles=CountTriangles(Surface)-1
  92.                 For t=0 To Triangles
  93.                         v1 = TriangleVertex(Surface,t,0)
  94.                         v2 = TriangleVertex(Surface,t,1)
  95.                         v3 = TriangleVertex(Surface,t,2)
  96.                        
  97.                         v1X# = VertexX#(Surface,v1)+EX#
  98.                         v1Y# = VertexY#(Surface,v1)+EY#
  99.                         v1Z# = VertexZ#(Surface,v1)+EZ#
  100.                         K#=-SY#/(SY#-v1Y#)
  101.                         v1XR# = SX# + (SX#-v1X#)*K#
  102.                         v1ZR# = SZ# + (SZ#-v1Z#)*K#
  103.  
  104.                         v2X# = VertexX#(Surface,v2)+EX#
  105.                         v2Y# = VertexY#(Surface,v2)+EY#
  106.                         v2Z# = VertexZ#(Surface,v2)+EZ#
  107.                         K#=-SY#/(SY#-v2Y#)
  108.                         v2XR# = SX# + (SX#-v2X#)*K#
  109.                         v2ZR# = SZ# + (SZ#-v2Z#)*K#
  110.                        
  111.                         v3X# = VertexX#(Surface,v3)+EX#
  112.                         v3Y# = VertexY#(Surface,v3)+EY#
  113.                         v3Z# = VertexZ#(Surface,v3)+EZ#
  114.                         K#=-SY#/(SY#-v3Y#)
  115.                         v3XR# = SX# + (SX#-v3X#)*K#
  116.                         v3ZR# = SZ# + (SZ#-v3Z#)*K#
  117.                        
  118.                         ;Backface culling works by calculating a camera vector and a surface normal
  119.                         ;If the dot product is > 0 then draw
  120.                        
  121.                         ;Camera vector
  122.                         CX#=SX#-(v1X#+v2X#+v3X#)*0.33333
  123.                         CY#=SY#-(v1Y#+v2Y#+v3Y#)*0.33333
  124.                         CZ#=SZ#-(v1Z#+v2Z#+v3Z#)*0.33333
  125.                        
  126.                         ;Surface normal, create 2 vectors and then create vector W from their cross product
  127.                         UX# = V2X#-V1X#
  128.                         UY# = V2Y#-V1Y#
  129.                         UZ# = V2Z#-V1Z#
  130.                         VX# = V3X#-V1X#
  131.                         VY# = V3Y#-V1Y#
  132.                         VZ# = V3Z#-V1Z#
  133.                         WX# = UY#*VZ#-UZ#*VY#
  134.                         WY# = -(UX#*VZ#-UZ#*VX#)
  135.                         WZ# = UX#*VY#-UY#*VX#
  136.                        
  137.                         ;Add triangle
  138.                         If (SY#>v1Y#) And (SY#>v2Y#) And (SY#>v3Y#) And (CX#*WX#+CY#*WY#+CZ#*WZ#)>0.0 Then
  139.                                 AddTriangle(ShadowSurface,AddVertex(ShadowSurface,v1XR#,0,v1ZR#),AddVertex(ShadowSurface,v2XR#,0,v2ZR#),AddVertex(ShadowSurface,v3XR#,0,v3ZR#))
  140.                         EndIf
  141.  
  142.                 Next
  143.         Next
  144. End Function
  145.  
  146. ;Create a test object
  147. Function GetObject()
  148.         NewObject = CreateMesh()
  149.         Cube = CreateCube()
  150.         ScaleMesh Cube,0.6,2.5,0.6
  151.        
  152.         PositionMesh Cube,-3,0,3
  153.         AddMesh Cube,NewObject
  154.        
  155.         PositionMesh Cube,6,0,0
  156.         AddMesh Cube,NewObject
  157.        
  158.         PositionMesh Cube,-6,0,-6
  159.         AddMesh Cube,NewObject
  160.        
  161.         PositionMesh Cube,6,0,0
  162.         AddMesh Cube,NewObject
  163.        
  164.         Sphere = CreateSphere(16)
  165.  
  166.        
  167.         PositionMesh Sphere,-3,2.5,3
  168.         AddMesh Sphere,NewObject
  169.  
  170.         PositionMesh Sphere,6,0,0
  171.         AddMesh Sphere,NewObject
  172.        
  173.         PositionMesh Sphere,-6,0,-6
  174.         AddMesh Sphere,NewObject
  175.        
  176.         PositionMesh Sphere,6,0,0
  177.         AddMesh Sphere,NewObject
  178.        
  179.         FreeEntity Cube
  180.         FreeEntity Sphere
  181.        
  182.         EntityColor NewObject,0,200,0
  183.         Return NewObject
  184. End Function


Comments :


Kryzon(Posted 1+ years ago)

 Neat! Did you test it with animated meshes?


Dabhand(Posted 1+ years ago)

 Thats pretty good nawi! :)


Nate the Great(Posted 1+ years ago)

 I doesn't work when I use rotateentity() but it works when I use turnmesh().Why could that be?


Stevie G(Posted 1+ years ago)

 Replace this ..
Code: [Select]
v1X# = VertexX#(Surface,v1)+EX#
v1Y# = VertexY#(Surface,v1)+EY#
v1Z# = VertexZ#(Surface,v1)+EZ#
K#=-SY#/(SY#-v1Y#)
v1XR# = SX# + (SX#-v1X#)*K#
v1ZR# = SZ# + (SZ#-v1Z#)*K#

v2X# = VertexX#(Surface,v2)+EX#
v2Y# = VertexY#(Surface,v2)+EY#
v2Z# = VertexZ#(Surface,v2)+EZ#
K#=-SY#/(SY#-v2Y#)
v2XR# = SX# + (SX#-v2X#)*K#
v2ZR# = SZ# + (SZ#-v2Z#)*K#

v3X# = VertexX#(Surface,v3)+EX#
v3Y# = VertexY#(Surface,v3)+EY#
v3Z# = VertexZ#(Surface,v3)+EZ#
K#=-SY#/(SY#-v3Y#)
v3XR# = SX# + (SX#-v3X#)*K#
v3ZR# = SZ# + (SZ#-v3Z#)*K#
With this ...
Code: [Select]
TFormPoint VertexX( Surface, v1 ), VertexY( Surface, v1 ) , VertexZ( Surface, v1 ) , Entity, 0
v1X# = TFormedX()
v1Y# = TFormedY()
v1Z# = TFormedZ()
K#=-SY#/(SY#-v1Y#)
v1XR# = SX# + (SX#-v1X#)*K#
v1ZR# = SZ# + (SZ#-v1Z#)*K#

TFormPoint VertexX( Surface, v2 ), VertexY( Surface, v2 ) , VertexZ( Surface, v2 ) , Entity, 0
v2X# = TFormedX()
v2Y# = TFormedY()
v2Z# = TFormedZ()
K#=-SY#/(SY#-v2Y#)
v2XR# = SX# + (SX#-v2X#)*K#
v2ZR# = SZ# + (SZ#-v2Z#)*K#

TFormPoint VertexX( Surface, v3 ), VertexY( Surface, v3 ) , VertexZ( Surface, v3 ) , Entity, 0
v3X# = TFormedX()
v3Y# = TFormedY()
v3Z# = TFormedZ()
K#=-SY#/(SY#-v3Y#)
v3XR# = SX# + (SX#-v3X#)*K#
v3ZR# = SZ# + (SZ#-v3Z#)*K#
Will ensure it works when using rotateentity.Stevie


Ben(t)(Posted 1+ years ago)

 edit: nevermind


Ryudin(Posted 1+ years ago)

 Very nice! [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal