January 15, 2021, 06:26:01 PM

Author Topic: [bb] color of shaded triangles depending on ambientlight color and thing color by RemiD [ 9 months ago ]  (Read 1367 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : color of shaded triangles depending on ambientlight color and thing color
Author : RemiD
Posted : 9 months ago

Description : this allows you to calculate the color of the shaded (not lighted) triangles depending on the color of the ambient light and the color of the thing

Code :
Code: BlitzBasic
  1. Graphics3D(1000,625,32,2)
  2.  
  3. SeedRnd(MilliSecs())
  4.  
  5. ;Camera
  6. Global Camera = CreateCamera()
  7. CameraViewport(Camera,0,0,GraphicsWidth(),GraphicsHeight())
  8. CameraRange(Camera,0.1,100)
  9. CameraClsColor(Camera,000,000,000)
  10.  
  11. ;Origine
  12. Origine = CreateCube()
  13. ScaleMesh(Origine,0.01,0.01,0.01)
  14. EntityColor(Origine,255,000,255)
  15. EntityFX(Origine,1)
  16.  
  17. ;Center
  18. Global Center = CreatePivot()
  19. PositionEntity(Center,50,0,50,True)
  20.  
  21. ;DLight
  22. Global DLight = CreateLight(1)
  23. LightColor(DLight,192,192,192)
  24. PositionEntity(DLight,50,1000,-1000,True)
  25. RotateEntity(DLight,45,0,0,True)
  26.  
  27. AmbientLight(038,038,038)
  28.  
  29. ;Cube lighted/shaded normally (with directx7 lighting/shading)
  30. Global CubeRenderer = CreateCube()
  31. ScaleMesh(CubeRenderer,1.0/2,1.0/2,1.0/2)
  32. PositionMesh(CubeRenderer,0,1.0/2,0)
  33. R% = Rand(025,255) : G% = Rand(025,255) : B% = Rand(025,255)
  34. EntityColor(CubeRenderer,R,G,B)
  35. PositionEntity(CubeRenderer,0,0,0,True)
  36.  
  37. ;Quad fullbright and has the same color than the shaded (not lighted) triangles of the cube
  38. Global QuadRenderer = CreateMesh()
  39. Surface = CreateSurface(QuadRenderer)
  40. AddVertex(Surface, 0.5, 0.5, 0.0)
  41. AddVertex(Surface, -0.5, 0.5, 0.0)
  42. AddVertex(Surface, 0.5, -0.5, 0.0)
  43. AddVertex(Surface, -0.5, -0.5, 0.0)
  44. AddTriangle(Surface,0,1,2)
  45. AddTriangle(Surface,2,1,3)
  46. UpdateNormals(QuadRenderer)
  47. EntityColor(QuadRenderer,038.0/255*R,038.0/255*G,038.0/255*B) ;this is the secret formula ! (AmbientR/255*ThingR,AmbientG/255*ThingG,AmbientB/255*ThingB)
  48. EntityFX(QuadRenderer,1)
  49. PositionEntity(QuadRenderer,0,1.5,0,True)
  50.  
  51. PositionEntity(Camera,3,1.65,3,True)
  52. RotateEntity(Camera,0,135,0,True)
  53.  
  54. Global MainLoopTimer = CreateTimer(30)
  55.  
  56. Main()
  57.  
  58. End()
  59.  
  60. Function Main()
  61.  
  62.  Repeat
  63.  
  64.   If( KeyHit(57)=1 )
  65.    R% = Rand(025,255) : G% = Rand(025,255) : B% = Rand(025,255)
  66.    EntityColor(CubeRenderer,R,G,B)
  67.    EntityColor(QuadRenderer,038.0/255*R,038.0/255*G,038.0/255*B)
  68.   EndIf
  69.  
  70.   If( KeyHit(28)=1 )
  71.    PositionEntity(DLight,Rnd(-1000,1000),Rnd(0,1000),-1000,True)
  72.    PointEntity(DLight,Center)
  73.   EndIf
  74.  
  75.   WireFrame(False)
  76.   If( KeyDown(2)=1 )
  77.    WireFrame(True)
  78.   EndIf
  79.  
  80.   SetBuffer(BackBuffer())
  81.   RenderWorld()
  82.  
  83.   Color(255,255,255)
  84.   Text(0,0,"press space to change the color of the thing")
  85.   Text(0,15,"press enter to change the light position")
  86.  
  87.   Text(0,30,"the cube is lighted/shaded normally (with directx7 lighting/shading)")
  88.   Text(0,45,"the quad is fullbright and has the same color than the shaded (not lighted) triangles of the cube")
  89.  
  90.   ;Flip(1)
  91.   WaitTimer(MainLoopTimer)
  92.   VWait():Flip(False)
  93.  
  94.  Until( KeyDown(1)=1 )
  95.  
  96. End Function


Comments :


Rick Nasher(Posted 9 months ago)

 Nice demonstration of how to do this.Dunno if intentional but code of download differs from the code displayed above.


RemiD(Posted 9 months ago)

 Maybe, i added a longer version at first, but i simplified it to keep only the essentials.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal