November 28, 2020, 11:29:31 AM

Author Topic: [bb] Light a scene with DirectX7 vertex lighting or with LightMesh vertex lighting by RemiD [ 1+ years ago ]  (Read 679 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Light a scene with DirectX7 vertex lighting or with LightMesh vertex lighting
Author : RemiD
Posted : 1+ years ago

Description : LightMesh can be useful if you want to light a scene with many olights (more than 8)
LightMesh can be useful if you want to light different meshes with different olights, but with only one olight per mesh. For example if you have different rooms or if you have the interior and the exterior of a building, you want to light each mesh with a different olight but you don't want the light to go through walls...


Code :
Code: BlitzBasic
  1. ;Light a scene with DirectX7 vertex lighting or with LightMesh vertex lighting
  2. Graphics3D(800,600,32,2)
  3.  
  4. SeedRnd(MilliSecs())
  5.  
  6. Global Arial14Font = LoadFont("Arial",14,False,False,False)
  7.  
  8. Camera = CreateCamera()
  9. CameraRange(Camera,0.1,100)
  10. CameraClsColor(Camera,000,000,000)
  11.  
  12. TTile = CreateMesh()
  13. TSurface = CreateSurface(TTile)
  14. AddVertex(TSurface, 0.0, 0.0, 1.0)
  15. AddVertex(TSurface, 1.0, 0.0, 1.0)
  16. AddVertex(TSurface, 0.0, 0.0, 0.0)
  17. AddVertex(TSurface, 1.0, 0.0, 0.0)
  18. AddTriangle(TSurface, 0, 1, 2)
  19. AddTriangle(TSurface, 2, 1, 3)
  20. UpdateNormals(TTile)
  21. FloorMesh = CreateMesh()
  22. For GX# = 0 To 10-1 Step 1
  23.  For GZ# = 0 To 10-1 Step 1
  24.   GY# = 0
  25.   TPart = CopyMesh(TTile)
  26.   PositionMesh(TPart,GX,GY,GZ)
  27.   AddMesh(TPart,FloorMesh)
  28.   FreeEntity(TPart)
  29.  Next
  30. Next
  31. FreeEntity(TTile)
  32.  
  33. TTile = CreateMesh()
  34. TSurface = CreateSurface(TTile)
  35. AddVertex(TSurface, 0.0, 1.0, 0.0)
  36. AddVertex(TSurface, 1.0, 1.0, 0.0)
  37. AddVertex(TSurface, 0.0, 0.0, 0.0)
  38. AddVertex(TSurface, 1.0, 0.0, 0.0)
  39. AddTriangle(TSurface, 0, 1, 2)
  40. AddTriangle(TSurface, 2, 1, 3)
  41. UpdateNormals(TTile)
  42. WallFrontMesh = CreateMesh()
  43. For GX# = 0 To 10-1 Step 1
  44.  For GY# = 0 To 3-1 Step 1
  45.   GZ# = 10
  46.   TPart = CopyMesh(TTile)
  47.   PositionMesh(TPart,GX,GY,GZ)
  48.   AddMesh(TPart,WallFrontMesh)
  49.   FreeEntity(TPart)
  50.  Next
  51. Next
  52. FreeEntity(TTile)
  53.  
  54. TTile = CreateMesh()
  55. TSurface = CreateSurface(TTile)
  56. AddVertex(TSurface, 1.0, 1.0, 0.0)
  57. AddVertex(TSurface, 0.0, 1.0, 0.0)
  58. AddVertex(TSurface, 1.0, 0.0, 0.0)
  59. AddVertex(TSurface, 0.0, 0.0, 0.0)
  60. AddTriangle(TSurface, 0, 1, 2)
  61. AddTriangle(TSurface, 2, 1, 3)
  62. UpdateNormals(TTile)
  63. WallBackMesh = CreateMesh()
  64. For GX# = 0 To 10-1 Step 1
  65.  For GY# = 0 To 3-1 Step 1
  66.   GZ# = 0
  67.   TPart = CopyMesh(TTile)
  68.   PositionMesh(TPart,GX,GY,GZ)
  69.   AddMesh(TPart,WallBackMesh)
  70.   FreeEntity(TPart)
  71.  Next
  72. Next
  73. FreeEntity(TTile)
  74.  
  75. TTile = CreateMesh()
  76. TSurface = CreateSurface(TTile)
  77. AddVertex(TSurface, 0.0, 1.0, 0.0)
  78. AddVertex(TSurface, 0.0, 1.0, 1.0)
  79. AddVertex(TSurface, 0.0, 0.0, 0.0)
  80. AddVertex(TSurface, 0.0, 0.0, 1.0)
  81. AddTriangle(TSurface, 0, 1, 2)
  82. AddTriangle(TSurface, 2, 1, 3)
  83. UpdateNormals(TTile)
  84. WallLeftMesh = CreateMesh()
  85. GX# = 0
  86. For GZ# = 0 To 10-1 Step 1
  87.  For GY# = 0 To 3-1 Step 1
  88.   TPart = CopyMesh(TTile)
  89.   PositionMesh(TPart,GX,GY,GZ)
  90.   AddMesh(TPart,WallLeftMesh)
  91.   FreeEntity(TPart)
  92.  Next
  93. Next
  94. FreeEntity(TTile)
  95.  
  96. TTile = CreateMesh()
  97. TSurface = CreateSurface(TTile)
  98. AddVertex(TSurface, 0.0, 1.0, 1.0)
  99. AddVertex(TSurface, 0.0, 1.0, 0.0)
  100. AddVertex(TSurface, 0.0, 0.0, 1.0)
  101. AddVertex(TSurface, 0.0, 0.0, 0.0)
  102. AddTriangle(TSurface, 0, 1, 2)
  103. AddTriangle(TSurface, 2, 1, 3)
  104. UpdateNormals(TTile)
  105. WallRightMesh = CreateMesh()
  106. GX# = 10
  107. For GZ# = 0 To 10-1 Step 1
  108.  For GY# = 0 To 3-1 Step 1
  109.   TPart = CopyMesh(TTile)
  110.   PositionMesh(TPart,GX,GY,GZ)
  111.   AddMesh(TPart,WallRightMesh)
  112.   FreeEntity(TPart)
  113.  Next
  114. Next
  115. FreeEntity(TTile)
  116.  
  117. TTile = CreateMesh()
  118. TSurface = CreateSurface(TTile)
  119. AddVertex(TSurface, 0.0, 0.0, 0.0)
  120. AddVertex(TSurface, 1.0, 0.0, 0.0)
  121. AddVertex(TSurface, 0.0, 0.0, 1.0)
  122. AddVertex(TSurface, 1.0, 0.0, 1.0)
  123. AddTriangle(TSurface, 0, 1, 2)
  124. AddTriangle(TSurface, 2, 1, 3)
  125. UpdateNormals(TTile)
  126. CeilingMesh = CreateMesh()
  127. For GX# = 0 To 10-1 Step 1
  128.  For GZ# = 0 To 10-1 Step 1
  129.   GY# = 3
  130.   TPart = CopyMesh(TTile)
  131.   PositionMesh(TPart,GX,GY,GZ)
  132.   AddMesh(TPart,CeilingMesh)
  133.   FreeEntity(TPart)
  134.  Next
  135. Next
  136. FreeEntity(TTile)
  137.  
  138. Global ThingsCount%
  139. Dim ThingMesh(10)
  140.  
  141. For i% = 1 To 10 Step 1
  142.  ThingsCount = ThingsCount + 1
  143.  Id% = ThingsCount
  144.  ThingMesh(Id) = CreateCube()
  145.  ScaleMesh(ThingMesh(Id),1.0/2,1.0/2,1.0/2)
  146.  PositionMesh(ThingMesh(Id),0,1.0/2,0)
  147.  PositionEntity(ThingMesh(Id),Rnd(0+0.5,10-0.5),0,Rnd(0+0.5,10-0.5),True)
  148. Next
  149.  
  150. LightIntensity# = 4.242
  151. LightR% = Rand(025,255)
  152. LightG% = Rand(025,255)
  153. LightB% = Rand(025,255)
  154. LightSource=CreateSphere(8)
  155. ScaleMesh(LightSource,0.05/2,0.05/2,0.05/2)
  156. EntityColor(LightSource,LightR,LightG,LightB)
  157. EntityFX(LightSource,1)
  158. PositionEntity(LightSource,Rnd(1,10-1),Rand(1.0,2.0),Rnd(1,10-1),True)
  159.  
  160. DX7Omni = CreateLight(2)
  161. LightRange(DX7Omni,LightIntensity)
  162. LightColor(DX7Omni,LightR,LightG,LightB) ;olight color
  163. PositionEntity(DX7Omni,EntityX(LightSource,True),EntityY(LightSource,True),EntityZ(LightSource,True),True)
  164. EntityParent(DX7Omni,LightSource,True)
  165.  
  166. Rotor = CreatePivot()
  167. PositionEntity(Rotor,5,0,5,True)
  168. EntityParent(LightSource,Rotor,True)
  169.  
  170. AmbientLight(015,015,015) ;ambientlight color
  171.  
  172. PositionEntity(Camera,5,1.65,-2.5)
  173.  
  174. Global LightingMode%
  175. Const DX7VL% = 1
  176. Const LMVL% = 2
  177.  
  178. While( KeyDown(1) <> 1 )
  179.  
  180.  TurnEntity(Rotor,0,1,0)
  181.  
  182.  If( KeyDown(30)=1 )
  183.   TurnEntity(Camera,0,1,0)
  184.  Else If( KeyDown(32)=1)
  185.   TurnEntity(Camera,0,-1,0)
  186.  EndIf
  187.  If( KeyDown(17)=1 )
  188.   MoveEntity(Camera,0,0,0.1)
  189.  Else If( KeyDown(31)=1 )
  190.   MoveEntity(Camera,0,0,-0.1)
  191.  EndIf
  192.  
  193.  LightingMode = 0
  194.  LMmsTime% = 0
  195.  
  196.  ;reset vertex colors
  197.  EntityFX(FloorMesh,0)
  198.  For VId% = 0 To CountVertices(GetSurface(FloorMesh,1))-1 Step 1
  199.   VertexColor(GetSurface(FloorMesh,1),VId,255,255,255,1.0)
  200.  Next
  201.  EntityFX(WallFrontMesh,0)
  202.  For VId% = 0 To CountVertices(GetSurface(WallFrontMesh,1))-1 Step 1
  203.   VertexColor(GetSurface(WallFrontMesh,1),VId,255,255,255,1.0)
  204.  Next
  205.  EntityFX(WallBackMesh,0)
  206.  For VId% = 0 To CountVertices(GetSurface(WallBackMesh,1))-1 Step 1
  207.   VertexColor(GetSurface(WallBackMesh,1),VId,255,255,255,1.0)
  208.  Next
  209.  EntityFX(WallLeftMesh,0)
  210.  For VId% = 0 To CountVertices(GetSurface(WallLeftMesh,1))-1 Step 1
  211.   VertexColor(GetSurface(WallLeftMesh,1),VId,255,255,255,1.0)
  212.  Next
  213.  EntityFX(WallRightMesh,0)
  214.  For VId% = 0 To CountVertices(GetSurface(WallRightMesh,1))-1 Step 1
  215.   VertexColor(GetSurface(WallRightMesh,1),VId,255,255,255,1.0)
  216.  Next
  217.  EntityFX(CeilingMesh,0)
  218.  For VId% = 0 To CountVertices(GetSurface(CeilingMesh,1))-1 Step 1
  219.   VertexColor(GetSurface(CeilingMesh,1),VId,255,255,255,1.0)
  220.  Next
  221.  For Id% = 1 To ThingsCount Step 1
  222.   EntityFX(ThingMesh(Id),0)
  223.   For VId% = 0 To CountVertices(GetSurface(ThingMesh(Id),1))-1 Step 1
  224.    VertexColor(GetSurface(ThingMesh(Id),1),VId,255,255,255,1.0)
  225.   Next
  226.  Next
  227.  
  228.  HideEntity(DX7Omni)
  229.  If( MouseDown(1)=1 )
  230.  
  231.   ;light with the DX7 vertex lighting
  232.   LightingMode = DX7VL
  233.   ShowEntity(DX7Omni)
  234.  
  235.  Else If(MouseDown(2)= 1 )
  236.  
  237.   ;light with lightmesh vertex lighting
  238.   LightingMode = LMVL
  239.   EntityFX(FloorMesh,1+2)
  240.   EntityFX(WallFrontMesh,1+2)
  241.   EntityFX(WallBackMesh,1+2)
  242.   EntityFX(WallLeftMesh,1+2)
  243.   EntityFX(WallRightMesh,1+2)
  244.   EntityFX(CeilingMesh,1+2)
  245.   For Id% = 1 To ThingsCount Step 1
  246.    EntityFX(ThingMesh(Id),1+2)
  247.   Next
  248.  
  249.   LMmsStart% = MilliSecs()
  250.  
  251.   LightMesh(FloorMesh,-255,-255,-255)
  252.   LightMesh(FloorMesh,015,015,015) ;ambientlight color
  253.   TFormPoint(0,0,0,LightSource,FloorMesh)
  254.   LightMesh(FloorMesh,LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  255.  
  256.   LightMesh(WallFrontMesh,-255,-255,-255)
  257.   LightMesh(WallFrontMesh,015,015,015) ;ambientlight color
  258.   TFormPoint(0,0,0,LightSource,WallFrontMesh)
  259.   LightMesh(WallFrontMesh,LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  260.  
  261.   LightMesh(WallBackMesh,-255,-255,-255)
  262.   LightMesh(WallBackMesh,015,015,015) ;ambientlight color
  263.   TFormPoint(0,0,0,LightSource,WallBackMesh)
  264.   LightMesh(WallBackMesh,LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  265.  
  266.   LightMesh(WallLeftMesh,-255,-255,-255)
  267.   LightMesh(WallLeftMesh,015,015,015) ;ambientlight color
  268.   TFormPoint(0,0,0,LightSource,WallLeftMesh)
  269.   LightMesh(WallLeftMesh,LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  270.  
  271.   LightMesh(WallRightMesh,-255,-255,-255)
  272.   LightMesh(WallRightMesh,015,015,015) ;ambientlight color
  273.   TFormPoint(0,0,0,LightSource,WallRightMesh)
  274.   LightMesh(WallRightMesh,LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  275.  
  276.   LightMesh(CeilingMesh,-255,-255,-255)
  277.   LightMesh(CeilingMesh,015,015,015) ;ambientlight color
  278.   TFormPoint(0,0,0,LightSource,CeilingMesh)
  279.   LightMesh(CeilingMesh,LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  280.  
  281.   For Id% = 1 To ThingsCount Step 1
  282.    LightMesh(ThingMesh(Id),-255,-255,-255)
  283.    LightMesh(ThingMesh(Id),015,015,015) ;ambientlight color
  284.    TFormPoint(0,0,0,LightSource,ThingMesh(Id))
  285.    LightMesh(ThingMesh(Id),LightR,LightG,LightB,LightIntensity,TFormedX(),TFormedY(),TFormedZ()) ;olight color
  286.   Next
  287.  
  288.   LMmsTime% = MilliSecs() - LMmsStart
  289.  
  290.  EndIf
  291.  
  292.  If( KeyDown(2)=1 )
  293.   WireFrame(True)
  294.  Else
  295.   WireFrame(False)
  296.  EndIf
  297.  
  298.  SetBuffer(BackBuffer())
  299.  RenderWorld()
  300.  
  301.  SetFont(Arial14Font)
  302.  Color(255,255,255)
  303.  Text(0,0,"Hold mouse1 to light the scene with DirectX7 vertex lighting, hold mouse2 to light the scene with LightMesh vertex lighting")
  304.  Text(0,14,"LightingMode = "+LightingModeStr(LightingMode))
  305.  If( LightingMode = LMVL )
  306.   Text(0,28,"LMmsTime = "+LMmsTime)
  307.  EndIf
  308.  
  309.  Flip(1)
  310.  
  311. Wend
  312.  
  313. ClearWorld()
  314.  
  315. End()
  316.  
  317. Function LightingModeStr$(LightingMode%)
  318.  If( LightingMode = DX7VL )
  319.   Return "DirectX7 vertex lighting"
  320.  Else If( LightingMode = LMVL )
  321.   Return "LightMesh vertex lighting"
  322.  EndIf
  323. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal