January 15, 2021, 05:28:07 PM

Author Topic: [bb] rainbow generator by RemiD [ 8 months ago ]  (Read 648 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] rainbow generator by RemiD [ 8 months ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : rainbow generator
Author : RemiD
Posted : 8 months ago

Description : generates a rainbow using a mesh (several arcs) and vertices colors

Code :
Code: BlitzBasic
  1. Graphics3D(1000,625,32,2)
  2.  
  3. SeedRnd(MilliSecs())
  4.  
  5. ;Input
  6. Global MX%
  7. Global MY%
  8. Global MXDiff%
  9. Global MYDiff%
  10.  
  11. ;Camera
  12. Global Camera = CreateCamera()
  13. CameraViewport(Camera,0,0,GraphicsWidth(),GraphicsHeight())
  14. CameraRange(Camera,0.2,200)
  15. CameraClsColor(Camera,000,000,000)
  16.  
  17. ;Origine
  18. Origine = CreateCube()
  19. ScaleMesh(Origine,0.01,0.01,0.01)
  20. EntityColor(Origine,255,000,255)
  21. EntityFX(Origine,1)
  22.  
  23. ;DLight
  24. Global DLight = CreateLight(1)
  25. LightColor(DLight,192,192,192)
  26. PositionEntity(DLight,50,1000,-1000,True)
  27. RotateEntity(DLight,45,0,0,True)
  28.  
  29. AmbientLight(032,032,032)
  30.  
  31. ;InteractionMode
  32. Global InteractionMode%
  33. Const C2D% = 1
  34. Const C3D% = 2
  35. InitializeInteractionMode()
  36.  
  37. ;Ghost
  38. Global GhostRoot
  39. Global GhostRootYaw#
  40. Global GhostEyes
  41. Global GhostEyesPitch#
  42. BuildGhost()
  43. InitializeGhost()
  44.  
  45. Terrain_ROAM = CreateTerrain(128)
  46. ScaleEntity(Terrain_ROAM,1,25.5,1)
  47. TerrainDetail(Terrain_ROAM,6553,True)
  48. TerrainShading(Terrain_ROAM,True)
  49. For VX% = 0 To 128-1
  50.  For VZ% = 0 To 128-1
  51.   HeightRGB% = Rand(030,033)
  52.   Height# = 1.0/255*HeightRGB
  53.   ModifyTerrain(Terrain_ROAM,VX,VZ,Height,False)
  54.  Next
  55. Next
  56. EntityColor(Terrain_ROAM,000,128,000)
  57.  
  58. Sky_Mesh = CreateSphere(8)
  59. FlipMesh(Sky_Mesh)
  60. ScaleMesh(Sky_Mesh,128.0,128.0,128.0)
  61. ;EntityColor(Sky_Mesh,092,092,255) ; blue sky
  62. EntityColor(Sky_Mesh,128,128,128) ;grey sky
  63. EntityFX(Sky_Mesh,1)
  64. PositionEntity(Sky_Mesh,64,0,64)
  65.  
  66. ;each section is made of 4 vertices 2 triangles
  67. ;each section is separated by 2 edges
  68. ;triangle A
  69. ;V0I = thisvertextop | V1I = nextvertextop | V2I = thisvertexbottom
  70. ;triangle B
  71. ;V0I = thisvertexbottom | V1I = nextvertextop | V2I = nextvertexbottom
  72.  
  73. ;notes :
  74. ;degrees = radians * 180 / PI
  75. ;radians = degrees * Pi / 180
  76. ;increment of 10degrees = 10.0*3.142/180.0 = 0.174radians
  77. ;increment of 0.1radians = 0.1*180.0/3.142 = 5.728degrees
  78.  
  79. Global ColorsCount%
  80. Dim Color_R%(6)
  81. Dim Color_G%(6)
  82. Dim Color_B%(6)
  83.  
  84. ;colors of a rainbow :
  85. ;magenta->red->yellow->green->cyan->blue->magenta
  86. Color_R(1) = 000 : Color_G(1) = 000 : Color_B(1) = 255
  87. Color_R(2) = 000 : Color_G(2) = 255 : Color_B(2) = 255
  88. Color_R(3) = 000 : Color_G(3) = 255 : Color_B(3) = 000
  89. Color_R(4) = 255 : Color_G(4) = 255 : Color_B(4) = 000
  90. Color_R(5) = 255 : Color_G(5) = 000 : Color_B(5) = 000
  91. Color_R(6) = 255 : Color_G(6) = 000 : Color_B(6) = 255
  92.  
  93. Global EdgesCCount%
  94. Dim Edge_VertTopI%(19)
  95. Dim Edge_VertBottomI%(19)
  96.  
  97. Rainbow_Mesh = CreateMesh()
  98. CreateSurface(Rainbow_Mesh)
  99.  
  100. BottomR% = Color_R(6) : BottomG% = Color_G(6) : BottomB% = Color_B(6)
  101. For n% = 1 To 6 Step 1
  102.  
  103.  TPart = CreateMesh()
  104.  Surface = CreateSurface(TPart)
  105.  
  106.  For Angle# = -90.0 To +90.0 Step +10.0
  107.  
  108.   VertUpMarker = CreateCube()
  109.   ScaleMesh(VertUpMarker,0.02/2,0.02/2,0.02/2)
  110.   EntityColor(VertUpMarker,Rand(025,255),Rand(025,255),Rand(025,255))
  111.   RotateEntity(VertUpMarker,0,0,Angle,True)
  112.   MoveEntity(VertUpMarker,0,+30+n+0.5,0)
  113.   AddVertex(Surface,EntityX(VertUpMarker,True),EntityY(VertUpMarker,True),EntityZ(VertUpMarker,True))
  114.   FreeEntity(VertUpMarker)
  115.  
  116.   VertDownMarker = CreateCube()
  117.   ScaleMesh(VertDownMarker,0.02/2,0.02/2,0.02/2)
  118.   EntityColor(VertDownMarker,Rand(025,255),Rand(025,255),Rand(025,255))
  119.   RotateEntity(VertDownMarker,0,0,Angle,True)
  120.   MoveEntity(VertDownMarker,0,+30+n-0.5,0)
  121.   AddVertex(Surface,EntityX(VertDownMarker,True),EntityY(VertDownMarker,True),EntityZ(VertDownMarker,True))
  122.   FreeEntity(VertDownMarker)
  123.  
  124.  Next
  125.  
  126.  VI% = -1
  127.  For EI% = 1 To 19 Step 1
  128.   VI = VI + 1
  129.   Edge_VertTopI(EI) = VI
  130.   VI = VI + 1
  131.   Edge_VertBottomI(EI) = VI
  132.  Next
  133.  
  134.  For SI% = 1 To 18 Step 1
  135.   VAI% = Edge_VertTopI(SI)
  136.   VBI% = Edge_VertTopI(SI+1)
  137.   VCI% = Edge_VertBottomI(SI)
  138.   VDI% = Edge_VertBottomI(SI+1)
  139.   AddTriangle(Surface,VAI,VBI,VCI)
  140.   AddTriangle(Surface,VCI,VBI,VDI)
  141.  Next
  142.  
  143.  TopR% = Color_R(n) : TopG% = Color_G(n) : TopB% = Color_B(n)
  144.  If( n = 1 )
  145.   For EI% = 1 To 19 Step 1
  146.    VertexColor(Surface,Edge_VertTopI(EI),TopR,TopG,TopB,0.2)
  147.    VertexColor(Surface,Edge_VertBottomI(EI),BottomR,BottomG,BottomB,0.0)
  148.   Next
  149.  Else If( n > 1 And n < 6 )
  150.   For EI% = 1 To 19 Step 1
  151.    VertexColor(Surface,Edge_VertTopI(EI),TopR,TopG,TopB,0.2)
  152.    VertexColor(Surface,Edge_VertBottomI(EI),BottomR,BottomG,BottomB,0.2)
  153.   Next
  154.  Else If( n = 6 )
  155.   For EI% = 1 To 19 Step 1
  156.    VertexColor(Surface,Edge_VertTopI(EI),TopR,TopG,TopB,0.0)
  157.    VertexColor(Surface,Edge_VertBottomI(EI),BottomR,BottomG,BottomB,0.2)
  158.   Next
  159.  EndIf
  160.  BottomR% = TopR : BottomG% = TopG : BottomB% = TopB
  161.  
  162.  AddMesh(TPart,Rainbow_Mesh)
  163.  FreeEntity(TPart)
  164.  
  165. Next
  166.  
  167. UpdateNormals(Rainbow_Mesh)
  168.  
  169. EntityFX(Rainbow_Mesh,1+2+16+32)
  170. EntityBlend(Rainbow_Mesh,3) ;1 or 3
  171.  
  172. PositionEntity(Rainbow_Mesh,64,3.0,64,True)
  173. RotateEntity(Rainbow_Mesh,0,180,0,True)
  174.  
  175. PositionEntity(GhostRoot,64,3.0+1.65,10,True)
  176. GhostRootYaw = 0
  177.  
  178. Global MainLoopTimer = CreateTimer(30)
  179.  
  180. Main()
  181.  
  182. End()
  183.  
  184. Function Main()
  185.  
  186.  Repeat
  187.  
  188.   GetInput()
  189.  
  190.   UpdateInteractionMode()
  191.   If( InteractionMode = C2D )
  192.    ;
  193.   Else If( InteractionMode = C3D )
  194.    UpdateGhost()          
  195.   EndIf
  196.  
  197.   PositionRotateEntityLikeOtherEntity(Camera,GhostEyes)
  198.  
  199.   WireFrame(False)
  200.   If( KeyDown(2)=1 )
  201.    WireFrame(True)
  202.   EndIf
  203.  
  204.   SetBuffer(BackBuffer())
  205.   RenderWorld()
  206.  
  207.   ;Flip(1)
  208.   WaitTimer(MainLoopTimer)
  209.   VWait():Flip(False)
  210.  
  211.  Until( KeyDown(1)=1 )
  212.  
  213. End Function
  214.  
  215. Function CText(TextStr$,PX%,PY%)
  216.  Text(PX,PY,TextStr,False,False)
  217. End Function
  218.  
  219. Function PositionEntityLikeOtherEntity(Entity,OEntity)
  220.  PositionEntity(Entity,EntityX(OEntity,True),EntityY(OEntity,True),EntityZ(OEntity,True),True)
  221. End Function
  222.  
  223. Function RotateEntityLikeOtherEntity(Entity,OEntity)
  224.  RotateEntity(Entity,EntityPitch(OEntity,True),EntityYaw(OEntity,True),EntityRoll(OEntity,True),True)
  225. End Function
  226.  
  227. Function PositionRotateEntityLikeOtherEntity(Entity,OEntity)
  228.  PositionEntity(Entity,EntityX(OEntity,True),EntityY(OEntity,True),EntityZ(OEntity,True),True)
  229.  RotateEntity(Entity,EntityPitch(OEntity,True),EntityYaw(OEntity,True),EntityRoll(OEntity,True),True)
  230. End Function
  231.  
  232. Function InitializeInteractionMode()
  233.  InteractionMode = C3D
  234.  HidePointer()
  235. End Function
  236.  
  237. Function UpdateInteractionMode()
  238.  
  239.  If( KeyHit(15)=1 )
  240.   If( InteractionMode = C2D )
  241.    InteractionMode = C3D
  242.    HidePointer()
  243.   Else If( InteractionMode = C3D )
  244.    InteractionMode = C2D
  245.    ShowPointer()
  246.   EndIf
  247.  EndIf
  248.  
  249. End Function
  250.  
  251. Function BuildGhost()
  252.  
  253.  GhostRoot = CreatePivot()
  254.  
  255.  GhostEyes = CreatePivot()
  256.  PositionRotateEntityLikeOtherEntity(GhostEyes,GhostRoot)
  257.  EntityParent(GhostEyes,GhostRoot,True)
  258.  
  259. End Function
  260.  
  261. Function InitializeGhost()
  262.  PositionEntity(GhostRoot,0,0,-3,True)
  263. End Function
  264.  
  265. Function UpdateGhost()
  266.  
  267.  MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
  268.  GhostRootYaw = GhostRootYaw - Float(MXDiff)/10
  269.  RotateEntity(GhostRoot,0,GhostRootYaw,0,False)
  270.  GhostEyesPitch = GhostEyesPitch + Float(MYDiff)/10
  271.  If( GhostEyesPitch < -89 )
  272.   GhostEyesPitch = -89
  273.  Else If( GhostEyesPitch > 89 )
  274.   GhostEyesPitch = 89
  275.  EndIf
  276.  RotateEntity(GhostEyes,GhostEyesPitch,0,0,False)
  277.  
  278.  If( KeyDown(42) = 0 And KeyDown(29) = 0 )
  279.   Speed# = 0.1
  280.  Else If( KeyDown(42) = 1 And KeyDown(29) = 0 )
  281.   Speed# = 1
  282.  Else If( KeyDown(42) = 0 And KeyDown(29) = 1 )
  283.   Speed# = 0.01
  284.  EndIf
  285.  
  286.  If( KeyDown(17)=1 Or MouseDown(1)=1 )
  287.   MoveEntity(GhostRoot,0,0,Speed)
  288.  Else If( KeyDown(31)=1 Or MouseDown(2)=1 )
  289.   MoveEntity(GhostRoot,0,0,-Speed)
  290.  EndIf
  291.  If( KeyDown(30)=1 )
  292.   MoveEntity(GhostRoot,-Speed,0,0)
  293.  Else If( KeyDown(32)=1 )
  294.   MoveEntity(GhostRoot,Speed,0,0)
  295.  EndIf
  296.  If( KeyDown(16)=1 )
  297.   MoveEntity(GhostRoot,0,-Speed,0)
  298.  Else If( KeyDown(18)=1 )
  299.   MoveEntity(GhostRoot,0,Speed,0)
  300.  EndIf
  301.  
  302. End Function
  303.  
  304. Function GetInput()
  305.  
  306.  MX = MouseX()
  307.  MY = MouseY()
  308.  
  309.  MXDiff = MouseXSpeed()
  310.  MYDiff = MouseYSpeed()
  311.  
  312. End Function


Comments :


RemiD(Posted 8 months ago)

 the colors look more distinct with a grey sky (or maybe white/grey clouds) than with a blue sky...with a grey sky :with a blue sky :


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal