October 28, 2021, 12:56:15

Author Topic: [bb] Mesh Terrain Generator by Sarakan [ 1+ years ago ]  (Read 662 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Mesh Terrain Generator
Author : Sarakan
Posted : 1+ years ago

Description : I hated Createterrain() due to not being able to change,move,add, and color each vertex as I'd wanted. So I made the function CreateTerrainMesh(x,y)! Easy enough to use too.

Flatterrain = CreateTerrainMesh(20,7)

This will create a flat mesh with 20 x 7 vertice that you can adjust as you want. I'm thinking 3d Scorched Earth, I know there already is one...but I'm wanting to expand my programming.


Code :
Code: BlitzBasic
  1. Function CreateTerrainMesh(x,y)
  2.         ;x = how many horizontal vertices
  3.         ;y = how many vertical vertices
  4.  
  5.         mesh = CreateMesh()
  6.         surf = CreateSurface(mesh)     
  7.         If x < 2 Or y < 2 Then Return mesh              ;in case user gives a bad number simply return the mesh
  8.         For tempx = 1 To x
  9.                 For tempy = 1 To y
  10.                         AddVertex(surf,tempx*4,0,tempy*4)
  11.                 Next   
  12.         Next   
  13.  
  14.         For v0 = 0 To (x*y)-y-2
  15.                 For Z = 1 To x*y
  16.                         If v0 = z*y-1 Then v0 = v0 + 1
  17.                 Next
  18.                 v1 = V0 + 1
  19.                 v2 = v1 + y
  20.                 v3 = v0 + y
  21.                 AddTriangle(surf,v0,v1,v2)
  22.                 AddTriangle(surf,v0,v2,v3)
  23.         Next
  24.  
  25.         Return mesh
  26. End Function
  27.  
  28.  
  29.  
  30.  
  31.  
  32. ;EXAMPLE
  33. Graphics3D 800,600,16,2
  34. SetBuffer BackBuffer()
  35. SeedRnd MilliSecs()
  36.  
  37. Global camera = CreateCamera()
  38. Global light = CreateLight(camera)
  39. MoveEntity camera,5,4,-6
  40.  
  41.  
  42.  
  43. meshy_terrain = CreateTerrainMesh(5,7) ;<======== HERE IT IS
  44.  
  45.  
  46. PlaceBallOnEachVertex(meshy_terrain)
  47.  
  48.  
  49.  
  50.  
  51. While Not KeyDown(1)
  52. Get_Input()
  53. RenderWorld
  54. Text 1,1, "Arrow keys, A key, and Z key move camera"
  55. Flip
  56. Cls
  57. Wend
  58.  
  59. End
  60.  
  61.  
  62.  
  63. Function PlaceBallOnEachVertex(mesh)
  64. surface = GetSurface(mesh,1)
  65. For temp = 0 To CountVertices(surface) - 1
  66.  
  67. tempball = CreateSphere(8)
  68. PositionEntity tempball,VertexX#(surface,temp), VertexY#(surface,temp), VertexZ#(surface,temp)
  69. EntityColor tempball,190,0,0
  70. EntityAlpha tempball,.4
  71. EntityParent tempball, mesh
  72. NameEntity tempball,"Vertex " + temp
  73.  
  74. tempimg = CreateTexture(Len(temp) * 10,15)
  75. SetBuffer TextureBuffer(tempimg)
  76. Cls
  77. Text 1,1,temp
  78. SetBuffer BackBuffer()
  79.  
  80. vert_sprite = CreateSprite()
  81. EntityTexture vert_sprite,tempimg
  82. PositionEntity vert_sprite,VertexX#(surface,temp), VertexY#(surface,temp), VertexZ#(surface,temp),1
  83. EntityParent vert_sprite, tempball
  84. Next
  85. End Function
  86.  
  87.  
  88. Function CreateTerrainMesh(x,y)
  89. ;x = how many horizontal vertices
  90. ;y = how many vertical vertices
  91.  
  92. mesh = CreateMesh()
  93. surf = CreateSurface(mesh)
  94. If x < 2 Or y < 2 Then Return mesh ;in case user gives a bad number simply return the mesh
  95. For tempx = 1 To x
  96. For tempy = 1 To y
  97. AddVertex(surf,tempx*4,0,tempy*4)
  98. Next
  99. Next
  100.  
  101. For v0 = 0 To (x*y)-y-2
  102. For Z = 1 To x*y
  103. If v0 = z*y-1 Then v0 = v0 + 1
  104. Next
  105. v1 = V0 + 1
  106. v2 = v1 + y
  107. v3 = v0 + y
  108. AddTriangle(surf,v0,v1,v2)
  109. AddTriangle(surf,v0,v2,v3)
  110. Next
  111.  
  112. Return mesh
  113. End Function
  114.  
  115.  
  116. Function Get_Input()
  117. shift = 0
  118. up = 0
  119. down = 0
  120. left_key = 0
  121. right_key = 0
  122. a = 0
  123. z = 0
  124. If KeyDown(shiftkey) = 1 Then shift = 1
  125. If KeyDown(200) = 1 Then up = 1
  126. If KeyDown(208) = 1 Then down = 1
  127. If KeyDown(203) = 1 Then Left_key = 1
  128. If KeyDown(205) = 1 Then Right_key = 1
  129. If KeyDown(30) = 1 Then a = 1
  130. If KeyDown(44) = 1 Then z = 1
  131.  
  132. If right_key = 1 Then MoveEntity camera,1,0,0
  133. If left_key = 1 Then MoveEntity camera,-1,0,0
  134. If up = 1 Then MoveEntity camera,0,0,1
  135. If down = 1 Then MoveEntity camera,0,0,-1
  136. If a = 1 Then MoveEntity camera,0,1,0
  137. If z = 1 Then MoveEntity camera,0,-1,0
  138. If KeyDown(211) Then TurnEntity camera,0,2,0
  139. If KeyDown(209) Then TurnEntity camera,0,-2,0
  140. End Function


Comments :


Sarakan(Posted 1+ years ago)

 Added example to code


Jager(Posted 1+ years ago)

 What would be really good is something similar but reads in a map file (picture of terrain) where you tell it what colour is what height, and it creates a mesh of the map! :)


Wayne(Posted 1+ years ago)

 when trying to run returns error: duplicate identifier


DJWoodgate(Posted 1+ years ago)

 Did you notice his example code also includes the function?


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal