October 19, 2021, 11:03:36

Author Topic: [bb] Mesh terrain by BadJim [ 1+ years ago ]  (Read 824 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Mesh terrain by BadJim [ 1+ years ago ]
« on: June 29, 2017, 00:28:39 »
Title : Mesh terrain
Author : BadJim
Posted : 1+ years ago

Description : The LOD terrain provided with Blitz 3d is nice, but there are flaws. You can't get a high poly count because it must be continually updated. Also, some users have noticed problems tiling them. The edges don't fit right.

 There is more than one way to do things however, so I now present code to create your terrain from tiled meshes. The main advantage of this is that you can have a much higher polycount, at the expense of having no LOD. Therefore it will look prettier far away, but might look less pretty close up or restrict how far you can allow the player to see. On the whole though I think the tradeoff is worthwhile.

Also these terrains are designed to be tiled. You could even have a tilemap system for putting different textures on them.

Copy/Paste the code into the editor, run it and make up your own mind. The texture is generated, so it is all self contained.


Code :
Code: BlitzBasic
  1. Graphics3D 800,600
  2.  
  3. Global mills=MilliSecs()
  4. Global lod=2000
  5. Global wframe=0
  6. AmbientLight 50,50,50
  7. landscapesize=256
  8.  
  9.  
  10. cam=CreateCamera()
  11. PositionEntity cam,landscapesize/2,10,landscapesize/2
  12. CameraViewport cam,0,0,GraphicsWidth(),GraphicsHeight()
  13. CameraRange cam,0.2,80
  14. SetBuffer BackBuffer()
  15.  
  16. plane=CreatePlane()
  17. PositionEntity plane,0,-100,0
  18.  
  19. light=CreateLight(2)
  20. PositionEntity light,0,35,0
  21. LightRange light,50
  22.  
  23.  
  24. terrain=makeheightmap(landscapesize,landscapesize)
  25. maketerrainpatchmesh(terrain,landscapesize,landscapesize)
  26. Global lodterrain=makeoldterrain(terrain,landscapesize)
  27. HideEntity lodterrain
  28.  
  29. Repeat
  30.         controls(cam)
  31.         render()
  32.         Delay 1
  33. Until KeyHit(1)
  34. End
  35.  
  36. Type terrainpatch;------------------------------
  37. Field patch
  38. End Type
  39.  
  40. Function render();-----------------------------------------------------------
  41.         UpdateWorld()
  42.         RenderWorld()
  43.         WritePixel GraphicsWidth()/2,GraphicsHeight()/2,16777215
  44.         Text 0,0,"polys : "+TrisRendered()
  45.         Text 0,18,"millisec render : "+(MilliSecs()-mills)
  46.         Text 0,50,"w,a,s,d,space,c + mouse =movement"
  47.         Text 0,68,"1=lod terrain, 2=mesh terrain, +/- = adjust lod, tab=wireframe"
  48.         mills=MilliSecs()
  49.         Flip
  50. End Function
  51.  
  52.  
  53. Function controls(cam);---------------------------------------------------------
  54.         If KeyDown(17) Then MoveEntity cam,0,0,0.4;forward
  55.         If KeyDown(31) Then MoveEntity cam,0,0,-0.4;back
  56.         If KeyDown(30) Then MoveEntity cam,-0.4,0,0;left
  57.         If KeyDown(32) Then MoveEntity cam,0.4,0,0;right
  58.         If KeyDown(57) Then MoveEntity cam,0,0.4,0;up
  59.         If KeyDown(46) Then MoveEntity cam,0,-0.4,0;down
  60.  
  61.        
  62.         TurnEntity cam,MouseYSpeed(),0-MouseXSpeed(),0
  63.         RotateEntity cam,EntityPitch(cam),EntityYaw(cam),0
  64.         MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  65.        
  66.         If KeyHit(2)
  67.                 For terrainpatch.terrainpatch=Each terrainpatch
  68.                         HideEntity terrainpatchpatch
  69.                 Next
  70.                 ShowEntity lodterrain
  71.         EndIf
  72.         If KeyHit(3)
  73.                 For terrainpatch.terrainpatch=Each terrainpatch
  74.                         ShowEntity terrainpatchpatch
  75.                 Next
  76.                 HideEntity lodterrain
  77.         EndIf
  78.         If KeyHit(12) Then lod=lod/1.41
  79.         If KeyHit(13) Then lod=lod*1.41
  80.         TerrainDetail lodterrain,lod,True
  81.         If KeyHit(15) Then wframe=1-wframe
  82.         WireFrame wframe
  83. End Function
  84.  
  85.  
  86. Function maketerrainpatchmesh(heightmap,xlength,zlength);------------------------------------------------------------------
  87.  
  88.         For z=0 To zlength-32 Step 32
  89.                 For x = 0 To xlength-32 Step 32
  90.                         terrainpatch.terrainpatch = New terrainpatch
  91.                         terrainpatchpatch=maketerrainpatch(heightmap,xlength,zlength,x,z,x+32,z+32)
  92.                 Next
  93.         Next
  94.        
  95.        
  96.         Return terrainpatchpatch
  97. End Function
  98.  
  99. Function maketerrainpatch(heightmap,xlength,zlength,x1,z1,x2,z2);-----------------------------------------------
  100.         terrain=CreateMesh()
  101.         brush=CreateBrush()
  102.         BrushFX brush,0
  103.         tex=maketexture()
  104.         ScaleTexture tex,10,10
  105.         BrushTexture brush,tex
  106.         surface=CreateSurface(terrain,brush)
  107.        
  108.        
  109.         For z=z1 To z2;add vertices
  110.                 For x=x1 To x2
  111.                         AddVertex surface,x,PeekFloat(heightmap,(x+z*xlength)*4),z,x,z
  112.                 Next
  113.         Next
  114.  
  115.         For z=0 To z2-(z1+2) Step 2;add triangles
  116.                 For x=0 To x2-(x1+2) Step 2
  117.                         AddTriangle surface,    (x+0)+(z+0)*(x2-x1+1)(x+0)+(z+1)*(x2-x1+1)(x+1)+(z+0)*(x2-x1+1)
  118.                         AddTriangle surface,    (x+1)+(z+1)*(x2-x1+1)(x+1)+(z+0)*(x2-x1+1)(x+0)+(z+1)*(x2-x1+1)
  119.  
  120.                         AddTriangle surface,    (x+1)+(z+0)*(x2-x1+1)(x+1)+(z+1)*(x2-x1+1)(x+2)+(z+0)*(x2-x1+1)
  121.                         AddTriangle surface,    (x+2)+(z+1)*(x2-x1+1)(x+2)+(z+0)*(x2-x1+1)(x+1)+(z+1)*(x2-x1+1)
  122.  
  123.                         AddTriangle surface,    (x+0)+(z+1)*(x2-x1+1)(x+0)+(z+2)*(x2-x1+1)(x+1)+(z+1)*(x2-x1+1)
  124.                         AddTriangle surface,    (x+1)+(z+2)*(x2-x1+1)(x+1)+(z+1)*(x2-x1+1)(x+0)+(z+2)*(x2-x1+1)
  125.  
  126.                         AddTriangle surface,    (x+1)+(z+1)*(x2-x1+1)(x+1)+(z+2)*(x2-x1+1)(x+2)+(z+1)*(x2-x1+1)
  127.                         AddTriangle surface,    (x+2)+(z+2)*(x2-x1+1)(x+2)+(z+1)*(x2-x1+1)(x+1)+(z+2)*(x2-x1+1)
  128.  
  129.                 Next
  130.         Next
  131.  
  132.         ScaleEntity terrain,1,5,1
  133.         UpdateNormals terrain  
  134.        
  135.         Return terrain
  136. End Function
  137.  
  138.  
  139. Function makeoldterrain(heightmap,scope);---------------------------------------------------------
  140.         terrain=CreateTerrain(scope)
  141.         For z=0 To scope-1;set heightmap
  142.                 For x=0 To scope-1
  143.                         ModifyTerrain terrain,x,z,PeekFloat( heightmap,(z*scope+x)*4)
  144.                 Next
  145.         Next
  146.         tex=makeTexture()
  147.         ScaleTexture tex,10,10
  148.         EntityTexture terrain,tex
  149.         ScaleEntity terrain,1,5,1
  150.         TerrainShading terrain,True
  151.         Return terrain
  152. End Function
  153.  
  154. Function makeheightmap(xlength,zlength);----------------------------------------------------------
  155.         xlength=xlength+1
  156.         zlength=zlength+1
  157.         heightmap=CreateBank(xlength*zlength*4)
  158.        
  159.         For z=0 To zlength-1;generate random data
  160.                 For x=0 To xlength-1
  161.                         PokeFloat heightmap,(z*xlength+x)*4,Rnd(1)
  162.                 Next
  163.         Next
  164.        
  165.         For n=1 To 4;smooth terrain.................................
  166.                 If KeyHit(1) Then End;emergency get out
  167.                 For z=0 To zlength-1;x smooth
  168.                         For x=0 To xlength-1
  169.                                 height1#=PeekFloat(heightmap,(z*xlength+x)*4)
  170.                                 height2#=PeekFloat(heightmap,(z*xlength+((x+1)Mod xlength))*4)
  171.                                 PokeFloat heightmap,(z*xlength+x)*4,(height1#+height2#)/2; +Rnd(0.03)
  172.                         Next
  173.                 Next
  174.                 For z=0 To zlength-1;z smooth
  175.                         For x=0 To xlength-1
  176.                                 height1#=PeekFloat(heightmap,(z*xlength+x)*4)
  177.                                 height2#=PeekFloat(heightmap,(((z+1)Mod zlength)*xlength+x)*4)
  178.                                 PokeFloat heightmap,(z*xlength+x)*4,(height1#+height2#)/2;+Rnd(0.03)
  179.                         Next
  180.                 Next
  181.                 Cls
  182.                 Text 0,0,n
  183.                 Flip
  184.         Next
  185.  
  186.  
  187.         lowest#=100;normalise..............................
  188.         highest#=0
  189.         For z=0 To zlength-1
  190.                 For x=0 To xlength-1
  191.                         If PeekFloat(heightmap,(z*xlength+x)*4)<lowest# Then lowest#=PeekFloat(heightmap,(z*xlength+x)*4)
  192.                         If PeekFloat(heightmap,(z*xlength+x)*4)>highest# Then highest#=PeekFloat(heightmap,(z*xlength+x)*4)
  193.                 Next
  194.         Next
  195.  
  196.         lowest#=lowest#-0.01
  197.         highest#=highest#+0.01
  198.         For z=0 To zlength-1
  199.                 For x=0 To xlength-1
  200.                         PokeFloat heightmap,(z*xlength+x)*4,(PeekFloat(heightmap,(z*xlength+x)*4)-lowest#) / (highest#-lowest#)
  201.                 Next
  202.         Next
  203.  
  204.        
  205.         Return heightmap
  206. End Function
  207.  
  208. Function maketexture();-----------------------------------------------------------------
  209.         tex=CreateTexture(256,256,8)
  210.                 For y=0 To 255
  211.                         For x=0 To 255
  212.                                 WritePixel x,y,(Rand(64)+32)*66049,TextureBuffer(tex)
  213.                         Next
  214.                 Next
  215.         ScaleTexture tex,4,4
  216.         Return tex
  217. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal