Ooops
May 26, 2020, 05:19:42 PM

Author Topic: [bb] Furys Alpha Terrain example. by Wings [ 1+ years ago ]  (Read 1388 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Furys Alpha Terrain example.
Author : Wings
Posted : 1+ years ago

Description : This is a 10 hours coding example of how to do vertex alpha terrain in blitz3d. The difirent from moste other sources is that this source has no Zbuffer fights. this makes terrain look good even at long distans. You can have as many layers as you want. just make sure that the mud layer is the highest layer. Contolls are WASD E + Rightmousebutton and mouse to rotate camera.
There is 2 examples. an not optimized version and an optimized that do not create unusfully vertex to speed up engine.

sourcefile included 2exe. 10mbit host.
<a href="http://www.tiberion.eu/QueRenderEngine.zip" target="_blank">http://www.tiberion.eu/QueRenderEngine.zip[/url]
i reject all my claims to this file. i take no responibility use at your own risk and responsbility. if you find anything wrong about it let me know so i can fix the wronges..


Code :
Code: BlitzBasic
  1. ;Images and compiled examples.
  2. ;http://www.tiberion.eu/furysalphademo.zip
  3.  
  4. ;Furys Terrain Alpha Demo
  5. ;
  6. ;This is a alpha demo that show how to use alpha maps on a surface.
  7. ;
  8.  
  9. ;Written by Daniel Eriksson (Tiberion Crew) in 2008-04-13
  10.  
  11. Global camera
  12.  
  13. Dim verts(65,65,8) ;8 layers of vertices
  14. Dim surfs(8)
  15.  
  16. Dim hmap#(1024,1024)
  17. Dim TEXTMAP(1024,1024,8) ; this is the textmap buffer
  18.  
  19.  
  20. Dim Face_NX#(32768)
  21. Dim Face_NY#(32768)
  22. Dim Face_NZ#(32768)
  23.                
  24. Dim Vertex_ConnectedTris(32768)
  25. Dim Vertex_TriList(32768, 32)
  26.  
  27. Global brush_grass,brush_rock,brush_rock2,brush_mud,trisr
  28.  
  29.  
  30.  
  31. Global player,playeravtar,camtarketroot,camchasetarget,swimmode
  32. Global WX=1024
  33. Global WZ=1024
  34.  
  35.  
  36.  
  37.  
  38.  
  39. Graphics3D 1280,800,32,2
  40. SetBuffer BackBuffer()
  41. Cls
  42.  
  43. camera=CreateCamera()
  44. PositionEntity camera,0,150,0
  45. CameraViewport camera,0,0,1280,800
  46. PositionEntity camera,1,50,0
  47. CameraRange camera,0.1,64
  48. CameraClsColor camera,140,128,255
  49. CameraFogColor camera,138,125,253
  50. CameraFogMode camera,1
  51. CameraFogRange camera,1,64
  52.  
  53.  
  54. light=CreateLight()
  55.  
  56.  
  57. ;Load terrain brushes
  58. brush_grass=LoadBrush ("imggrass3.jpg",1,4,4)
  59. If brush_grass=0 Then RuntimeError "grass01.jpg not found."
  60. BrushFX brush_grass,2+32
  61.  
  62. brush_rock=LoadBrush ("img
  63. ock3.jpg",1,2,2)
  64. If Not brush_rock Then RuntimeError "rock01.jpg not found."
  65. BrushFX brush_rock,2+32
  66.  
  67.  
  68. brush_rock2=LoadBrush ("imgcliff.jpg",1,8,8)
  69. If Not brush_rock2 Then RuntimeError "brush_rock2.bmp not found."
  70. BrushFX brush_rock2,2+32
  71.  
  72.  
  73. brush_mud=LoadBrush ("imgmudd.jpg",1,8,8)  ; (this is base texture no alpha enabled)
  74. ;BrushFX brush_mud,2+32
  75.  
  76.  
  77. sandplane=CreatePlane()
  78. PaintEntity sandplane,brush_mud
  79. PositionEntity sandplane,0,-0.5,0
  80.  
  81. brush_water=LoadBrush("imgwater512.jpg",1,8,8)
  82. plane=CreatePlane()
  83.  
  84.  
  85.  
  86. PaintEntity plane,brush_water
  87.  
  88. PositionEntity plane,0,4.5,0
  89. EntityFX plane,32
  90. EntityAlpha plane,0.66
  91.  
  92.  
  93.  
  94. ;Create a smal whaterlake on the big island
  95. lake=CreateCube()
  96. laketext=LoadTexture("imgwater512.jpg")
  97. ScaleTexture laketext,0.2,0.2
  98. ScaleEntity lake,20,0.1,21
  99. EntityTexture lake,laketext
  100. EntityFX lake,32
  101. EntityAlpha lake,0.66
  102. PositionEntity lake,134.15,9.335,114.701
  103.  
  104.  
  105.  
  106. ;LoadHeight map
  107. image=LoadImage("imghmap.bmp")
  108. If Not image Then RuntimeError "imghmap.bmp not found."
  109.  
  110. For x=0 To ImageWidth(image)
  111.         For z=0 To ImageHeight(image)
  112.  
  113.                 h#=(ReadPixel(x,z,ImageBuffer(image))And 255)/10.0
  114.  
  115.                 hmap#(x,z)=h#
  116.  
  117.         Next ;z
  118. Next ;x
  119.  
  120.  
  121. ;LoadSandMap
  122. smap=LoadImage("imgsandmap.bmp")
  123. If Not smap Then RuntimeError "cant find imgsandmap.bmp"
  124.  
  125.  
  126.  
  127. ;This init the alpha values in varius planes
  128. For x=0 To ImageWidth(image)
  129.         For z=0 To ImageHeight(image)
  130.                 TEXTMAP(x,z,0)=0 ; Alpha value 0
  131.                 TEXTMAP(x,z,1)=0 ; Alpha value 0
  132.                 TEXTMAP(x,z,2)=0 ; Alpha value 0
  133.                 TEXTMAP(x,z,3)=1 ; Alpha value 1
  134.         Next
  135. Next
  136.  
  137.  
  138.  
  139. ;Montain emulation texturing.. Paint montains
  140. For x=1 To ImageWidth(image)
  141.         For z=1 To ImageHeight(image)
  142.        
  143.         sand=ReadPixel(x,z,ImageBuffer(smap)) And $FF
  144.        
  145.         testy#=HMAP#(x,z)
  146.        
  147.        
  148.        
  149.         If testy#<5
  150.        
  151.                 ;TEXTMAP(X,Z,2)=1 ;Remed this line out to test if whater works good then normal mapping is enabled
  152.        
  153.         Else
  154.        
  155.         TEXTMAP(X,Z,2)=1
  156.        
  157.                 For testx=-1 To 1
  158.                 For testz=-1 To 1
  159.        
  160.        
  161.                         If hmap#(testx+x,testz+z)>testy#+0.5
  162.                                 TEXTMAP(x,z,0)=0
  163.                                 TEXTMAP(x,z,1)=1
  164.                        
  165.                                 ;TEXTMAP(testx+x,testz+z,0)=0
  166.                                 ;TEXTMAP(testx+x,testz+z,1)=1
  167.                         End If
  168.        
  169.        
  170.                         ;Verry steep hill
  171.                         If hmap#(testx+x,testz+z)>testy#+1
  172.  
  173.                                 TEXTMAP(x,z,0)=1
  174.                                 TEXTMAP(x,z,1)=1
  175.                                 TEXTMAP(x,z,3)=1
  176.                         End If
  177.        
  178.                 Next
  179.                 Next
  180.        
  181.         End If
  182.        
  183.         If sand=255 ; ops this is sand so all else is invalid.
  184.        
  185.                                 TEXTMAP(x,z,0)=0 ; Alpha value 0
  186.                 TEXTMAP(x,z,1)=0 ; Alpha value 0
  187.                 TEXTMAP(x,z,2)=0 ; Alpha value 0
  188.                 TEXTMAP(x,z,3)=1 ; Alpha value 1
  189.        
  190.         End If
  191.        
  192.        
  193.        
  194.         Next
  195.         Print "Building up montain emulation..."+x
  196. Next
  197.  
  198.  
  199.  
  200.  
  201. ;Creates the world meshs
  202. For x=0 To ImageWidth(image) Step 63
  203.         For z=0 To ImageHeight(image) Step 63
  204.         mesh=create_cell(x,z)
  205.         Next
  206. Next
  207.  
  208.  
  209.  
  210. While Not KeyHit(1)
  211.  
  212.  
  213. camera_handling()
  214.  
  215.  
  216.  
  217.  
  218. UpdateWorld
  219. RenderWorld
  220.  
  221.  
  222. Text 0,0,"Tris total "+trisr
  223. Text 0,10,"Tris rendered "+TrisRendered()
  224. Text 0,20,"FPS: "+fps
  225. Text 0,30,"Camera X: "+EntityX(camera)
  226. Text 0,40,"Camera Y: "+EntityY(camera)
  227. Text 0,50,"Camera Z: "+EntityZ(camera)
  228.  
  229.  
  230. Flip
  231. ct=ct+1
  232.  
  233. If MilliSecs()-oldms>1000
  234.  
  235.         oldms=MilliSecs()
  236.         fps=ct
  237.         ct=0
  238.  
  239.  
  240. End If
  241.  
  242.  
  243. Wend
  244.  
  245.  
  246.  
  247. Function create_cell(xoffs,zoffs)
  248.  
  249.  
  250.         ;empty verts
  251.         For layer=0 To 3
  252.         For x=0 To 63
  253.         For z=0 To 63
  254.                 verts(x,z,layer)=-1
  255.         Next
  256.         Next
  257.         Next
  258.        
  259.  
  260.         ;Create the grass mesh
  261.         mesh=CreateMesh()
  262.  
  263.  
  264.         For layer=0 To 3
  265.  
  266.                 If layer=0 Then surf=CreateSurface(mesh,brush_rock2)
  267.                 If layer=1 Then surf=CreateSurface(mesh,brush_rock)
  268.                 If layer=2 Then surf=CreateSurface(mesh,brush_grass)
  269.                 If layer=3 Then surf=CreateSurface(mesh,brush_mud)
  270.  
  271.  
  272.  
  273.  
  274.                 For z=0 To 63
  275.                         For x=0 To 63
  276.                        
  277.                                 ;Check if to skipp creation of this vertex
  278.                                 hitt=0
  279.                                
  280.                                 If textmap(x+xoffs,z+zoffs,layer)=1 Then hitt=hitt+1
  281.                                 If textmap(x+xoffs+1,z+zoffs,layer)=1 Then hitt=hitt+1
  282.                                 If textmap(x+xoffs,z+zoffs+1,layer)=1 Then hitt=hitt+1
  283.                                 If textmap(x+xoffs+1,z+zoffs+1,layer)=1 Then hitt=hitt+1
  284.  
  285.                                 If z>0 And x>0 Then
  286.                                         If textmap(x+xoffs-1,z+zoffs-1,layer)=1 Then hitt=hitt+1
  287.                                         If textmap(x+xoffs-1,z+zoffs,layer)=1 Then hitt=hitt+1
  288.                                         If textmap(x+xoffs,z+zoffs-1,layer)=1 Then hitt=hitt+1
  289.                                 End If
  290.  
  291.  
  292.  
  293.                        
  294.                                 If layer=3 Then hitt=1 ;Always create if its layer mud
  295.                        
  296.                                 If hitt Then verts(X,Z,layer) =AddVertex (surf,X,hmap#(x+xoffs,z+zoffs),Z,X,Z)
  297.                        
  298.                         Next ;X
  299.                 Next ;z
  300.  
  301.  
  302.                 ;Add triangles to mesh
  303.                 For z=0 To 62
  304.                         For x=0 To 62
  305.                        
  306.                                 ;Create this quad if there is vertices available.
  307.                                 hitt=0
  308.                                 If verts(x,z,layer)<>-1 Then hitt=hitt+1
  309.                                 ;If verts(x+1,z,layer)<>-1 Then hitt=hitt+1
  310.                                 If verts(x,z+1,layer)<>-1 Then hitt=hitt+1
  311.                                 If verts(x+1,z+1,layer)<>-1 Then hitt=hitt+1
  312.                                
  313.  
  314.                                 If hitt=3
  315.  
  316.                                
  317.                                         AddTriangle surf,verts(x,z,layer),verts(x,z+1,layer),verts(x+1,z+1,layer)
  318.                                
  319.                                 End If
  320.                                
  321.                                 hitt=0
  322.                                 If verts(x,z,layer)<>-1 Then hitt=hitt+1
  323.                                 If verts(x+1,z,layer)<>-1 Then hitt=hitt+1
  324.                                 ;If verts(x,z+1,layer)<>-1 Then hitt=hitt+1
  325.                                 If verts(x+1,z+1,layer)<>-1 Then hitt=hitt+1
  326.                                        
  327.                                 If hitt=3
  328.                                         AddTriangle surf,verts(x,z,layer),verts(x+1,z+1,layer),verts(x+1,z,layer)
  329.                                 trisr=trisr+1  
  330.                                 End If 
  331.                         Next ; X
  332.                 Next ;Z
  333.  
  334.  
  335.                 ;Montain emulation
  336.                 For x=0 To 63
  337.                         For z=0 To 63
  338.         If verts(x,z,layer)<>-1
  339.                                 VertexColor surf,verts(x,z,layer),255,255,255,0
  340.                                 If TEXTMAP(x+xoffs,z+zoffs,layer)=1 Then VertexColor surf,verts(x,z,layer),255,255,255,255  
  341.         End If
  342.                         Next ;Z
  343.                 Next ;X
  344.  
  345.  
  346.         Next ; Layer
  347.  
  348.  
  349.         PositionEntity mesh,xoffs,0,zoffs
  350.  
  351.         ;UpdateNormals mesh so it have shade. and dont look flat.
  352.         Calculate_Normals(mesh)
  353.  
  354.  
  355.         Return mesh
  356.  
  357. End Function
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364. Function camera_handling()
  365.  
  366.  
  367.         If KeyDown(17) Then MoveEntity camera,0,0,0.02  ;W     
  368.         If KeyDown(18) Then MoveEntity camera,0,0,1 ; E
  369.         If KeyDown(31) Then     MoveEntity camera,0,0,-1 ;S
  370.         If KeyDown(30) Then     MoveEntity camera,-0.02,0,0 ;A
  371.         If KeyDown(32) Then MoveEntity camera,0.02,0,0  ;D
  372. If MouseDown(2)
  373.         smx#=MouseXSpeed()/10.0
  374.         smy#=MouseYSpeed()/10.0
  375.        
  376.  
  377.         TurnEntity camera,smy#,-smx#,0
  378.         RotateEntity camera,EntityPitch#(camera),EntityYaw#(camera),0
  379.         ;TranslateEntity camera,0,-0.1,0
  380.         MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  381. End If
  382.  
  383.  
  384. ;       y#=BilinearInterpValue# ( Float(EntityX(camera))/2 ,Float(EntityZ(camera))/2 )
  385.  
  386. ;       If y#+0.5>EntityY#(camera)
  387.  
  388. ;               PositionEntity camera,EntityX(camera),y#+0.5,EntityZ(camera)
  389.  
  390. ;       End If
  391.  
  392. End Function
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405. Function Calculate_Normals(ThisMesh)
  406.        
  407.         ; Loop through all surfaces of the mesh.
  408.         Surfaces = CountSurfaces(ThisMesh)
  409.         For LOOP_Surface = 1 To Surfaces
  410.  
  411.                 Surface_Handle = GetSurface(ThisMesh, LOOP_Surface)
  412.  
  413.                 ; Reset the number of connected polygons for each vertex.
  414.                 For LoopV = 0 To 32767 
  415.                         Vertex_ConnectedTris(LoopV) = 0
  416.                 Next   
  417.        
  418.                 ; Loop through all triangles in this surface of the mesh.
  419.                 Tris = CountTriangles(Surface_Handle)
  420.                 For LOOP_Tris = 0 To Tris-1
  421.  
  422.                         ; Get the vertices that make up this triangle.
  423.                                 Vertex_0 = TriangleVertex(Surface_Handle, LOOP_Tris, 0)
  424.                                 Vertex_1 = TriangleVertex(Surface_Handle, LOOP_Tris, 1)
  425.                                 Vertex_2 = TriangleVertex(Surface_Handle, LOOP_Tris, 2)
  426.        
  427.                         ; Adjust the number of triangles each vertex is connected to and
  428.                         ; store this triangle in each vertex's list of triangles it is connected to.
  429.                                 ConnectedTris = Vertex_ConnectedTris(Vertex_0)
  430.                                 Vertex_TriList(Vertex_0, ConnectedTris) = LOOP_Tris
  431.                                 Vertex_ConnectedTris(Vertex_0) = ConnectedTris + 1
  432.  
  433.                                 ConnectedTris = Vertex_ConnectedTris(Vertex_1)
  434.                                 Vertex_TriList(Vertex_1, ConnectedTris) = LOOP_Tris
  435.                                 Vertex_ConnectedTris(Vertex_1) = ConnectedTris + 1
  436.  
  437.                                 ConnectedTris = Vertex_ConnectedTris(Vertex_2)
  438.                                 Vertex_TriList(Vertex_2, ConnectedTris) = LOOP_Tris
  439.                                 Vertex_ConnectedTris(Vertex_2) = ConnectedTris + 1
  440.  
  441.                         ; Calculate the normal for this face.
  442.  
  443.                                 ; Get the corners of this face:
  444.                                 Ax# = VertexX#(Surface_Handle, Vertex_0)
  445.                                 Ay# = VertexY#(Surface_Handle, Vertex_0)
  446.                                 Az# = VertexZ#(Surface_Handle, Vertex_0)
  447.  
  448.                                 Bx# = VertexX#(Surface_Handle, Vertex_1)
  449.                                 By# = VertexY#(Surface_Handle, Vertex_1)
  450.                                 Bz# = VertexZ#(Surface_Handle, Vertex_1)
  451.  
  452.                                 Cx# = VertexX#(Surface_Handle, Vertex_2)
  453.                                 Cy# = VertexY#(Surface_Handle, Vertex_2)
  454.                                 Cz# = VertexZ#(Surface_Handle, Vertex_2)
  455.  
  456.                                 ; Triangle 1
  457.                                 ; Get the vectors for two edges of the triangle.
  458.                                 Px# = Ax#-Bx#
  459.                                 Py# = Ay#-By#
  460.                                 Pz# = Az#-Bz#
  461.  
  462.                                 Qx# = Bx#-Cx#
  463.                                 Qy# = By#-Cy#
  464.                                 Qz# = Bz#-Cz#
  465.  
  466.                                 ; Compute their cross product.
  467.                                 Nx# = Py#*Qz# - Pz#*Qy#
  468.                                 Ny# = Pz#*Qx# - Px#*Qz#
  469.                                 Nz# = Px#*Qy# - Py#*Qx#
  470.  
  471.                                 ; Store the face normal.
  472.                                 Face_NX#(LOOP_Tris) = Nx#
  473.                                 Face_NY#(LOOP_Tris) = Ny#
  474.                                 Face_NZ#(LOOP_Tris) = Nz#
  475.  
  476.                 Next
  477.  
  478.                 ; Now that all the face normals for this surface have been calculated, calculate the vertex normals.
  479.                 Vertices = CountVertices(Surface_Handle)
  480.                 For LOOP_Vertices = 0 To Vertices-1
  481.  
  482.                         ; Reset this normal.
  483.                         Nx# = 0
  484.                         Ny# = 0
  485.                         Nz# = 0
  486.  
  487.                         ; Add the normals of all polygons which are connected to this vertex.
  488.                         Polys = Vertex_ConnectedTris(LOOP_Vertices)
  489.                                
  490.                         For LOOP_Polys = 0 To Polys-1
  491.  
  492.                                 ThisPoly = Vertex_TriList(LOOP_Vertices, LOOP_Polys)
  493.  
  494.                                 Nx# = Nx# + Face_NX#(ThisPoly)
  495.                                 Ny# = Ny# + Face_NY#(ThisPoly)
  496.                                 Nz# = Nz# + Face_NZ#(ThisPoly)                 
  497.                                
  498.                         Next   
  499.                                
  500.                         ; Normalize the new vertex normal.
  501.                         ; (Normalizing is scaling the vertex normal down so that it's length = 1)
  502.  
  503.                                 Nl# = Sqr(Nx#^2 + Ny#^2 + Nz#^2)
  504.  
  505.                                 ; Avoid a divide by zero error if by some freak accident, the vectors add up to 0.
  506.                                 ; If Nl# = 0 Then Nl# = 0.1
  507.  
  508.                                 Nx# = Nx# / Nl#
  509.                                 Ny# = Ny# / Nl#
  510.                                 Nz# = Nz# / Nl#
  511.  
  512.                         ; Set the vertex normal.
  513.                        
  514.                                 VertexNormal Surface_Handle, LOOP_Vertices, Nx#, Ny#, Nz#
  515.                                 ;VertexColor Surface_Handle, LOOP_Vertices, polys*127, polys*127, polys*127
  516.                
  517.                 Next
  518.  
  519.         Next
  520.  
  521. End Function


Comments :


Wings(Posted 1+ years ago)

 Here is the Optimized version. it is good for game play.as it dose not creat unused vertexs. so its less GPU intensive.  Another way to optimize speed is to limit camera range this way GPU will not render cells that is not on screen.
Code: [Select]
;Images and compiled examples.
;http://www.tiberion.eu/furysalphademo.zip


;Furys Terrain Alpha Demo
;
;This is a alpha demo that show how to use alpha maps on a surface.
;

;WARNING this is the optimized version. it only create vertex where its needed. its not sutable for a editor. use the other version for edits.

;Written by Daniel Eriksson (Tiberion Crew) in 2008-04-13

Global camera

Dim verts(65,65,8) ;8 layers of vertices
Dim surfs(8)

Dim hmap#(1024,1024)
Dim TEXTMAP(1024,1024,8) ; this is the textmap buffer


Dim Face_NX#(32768)
Dim Face_NY#(32768)
Dim Face_NZ#(32768)

Dim Vertex_ConnectedTris(32768)
Dim Vertex_TriList(32768, 32)

Global brush_grass,brush_rock,brush_rock2,brush_mud,trisr



Global player,playeravtar,camtarketroot,camchasetarget,swimmode
Global WX=1024
Global WZ=1024





Graphics3D 1280,800,32,1
SetBuffer BackBuffer()
Cls

camera=CreateCamera()
PositionEntity camera,0,150,0
CameraViewport camera,0,0,1280,800
PositionEntity camera,1,50,0
CameraRange camera,0.1,1000
CameraClsColor camera,140,128,255
CameraFogColor camera,138,125,253
CameraFogMode camera,1
CameraFogRange camera,1,1000


light=CreateLight()


;Load terrain brushes
brush_grass=LoadBrush ("imggrass3.jpg",1,4,4)
If brush_grass=0 Then RuntimeError "grass01.jpg not found."
BrushFX brush_grass,2+32

brush_rock=LoadBrush ("imgock3.jpg",1,2,2)
If Not brush_rock Then RuntimeError "rock01.jpg not found."
BrushFX brush_rock,2+32


brush_rock2=LoadBrush ("imgcliff.jpg",1,8,8)
If Not brush_rock2 Then RuntimeError "brush_rock2.bmp not found."
BrushFX brush_rock2,2+32


brush_mud=LoadBrush ("imgmudd.jpg",1,8,8)  ; (this is base texture no alpha enabled)
;BrushFX brush_mud,2+32


sandplane=CreatePlane()
PaintEntity sandplane,brush_mud
PositionEntity sandplane,0,-0.5,0

brush_water=LoadBrush("imgwater512.jpg",1,8,8)
plane=CreatePlane()



PaintEntity plane,brush_water

PositionEntity plane,0,4.5,0
EntityFX plane,32
EntityAlpha plane,0.66



;Create a smal whaterlake on the big island
lake=CreateCube()
laketext=LoadTexture("imgwater512.jpg")
ScaleTexture laketext,0.2,0.2
ScaleEntity lake,20,0.1,21
EntityTexture lake,laketext
EntityFX lake,32
EntityAlpha lake,0.66
PositionEntity lake,134.15,9.235,114.701



;LoadHeight map
image=LoadImage("imghmap.bmp")
If Not image Then RuntimeError "imghmap.bmp not found."

For x=0 To ImageWidth(image)
For z=0 To ImageHeight(image)

h#=(ReadPixel(x,z,ImageBuffer(image))And 255)/10.0

hmap#(x,z)=h#

Next ;z
Next ;x


;LoadSandMap
smap=LoadImage("imgsandmap.bmp")
If Not smap Then RuntimeError "cant find imgsandmap.bmp"



;This init the alpha values in varius planes
For x=0 To ImageWidth(image)
For z=0 To ImageHeight(image)
TEXTMAP(x,z,0)=0 ; Alpha value 0
TEXTMAP(x,z,1)=0 ; Alpha value 0
TEXTMAP(x,z,2)=0 ; Alpha value 0
TEXTMAP(x,z,3)=1 ; Alpha value 1
Next
Next



;Montain emulation texturing.. Paint montains
For x=1 To ImageWidth(image)
For z=1 To ImageHeight(image)

sand=ReadPixel(x,z,ImageBuffer(smap)) And $FF

testy#=HMAP#(x,z)



If testy#<5

;TEXTMAP(X,Z,2)=1 ;Remed this line out to test if whater works good then normal mapping is enabled

Else

TEXTMAP(X,Z,2)=1

For testx=-1 To 1
For testz=-1 To 1


If hmap#(testx+x,testz+z)>testy#+0.5
TEXTMAP(x,z,0)=0
TEXTMAP(x,z,1)=1

;TEXTMAP(testx+x,testz+z,0)=0
;TEXTMAP(testx+x,testz+z,1)=1
End If


;Verry steep hill
If hmap#(testx+x,testz+z)>testy#+1

TEXTMAP(x,z,0)=1
TEXTMAP(x,z,1)=1
TEXTMAP(x,z,3)=1
End If

Next
Next

End If

If sand=255 ; ops this is sand so all else is invalid.

TEXTMAP(x,z,0)=0 ; Alpha value 0
TEXTMAP(x,z,1)=0 ; Alpha value 0
TEXTMAP(x,z,2)=0 ; Alpha value 0
TEXTMAP(x,z,3)=1 ; Alpha value 1

End If



Next
Print "Building up montain emulation..."+x
Next




;Creates the world meshs
For x=0 To ImageWidth(image) Step 63
For z=0 To ImageHeight(image) Step 63
mesh=create_cell(x,z)
Next
Next



While Not KeyHit(1)


camera_handling()




UpdateWorld
RenderWorld


Text 0,0,"Tris total "+trisr
Text 0,10,"Tris rendered "+TrisRendered()
Text 0,20,"FPS: "+fps
Text 0,30,"Camera X: "+EntityX(camera)
Text 0,40,"Camera Y: "+EntityY(camera)
Text 0,50,"Camera Z: "+EntityZ(camera)


Flip
ct=ct+1

If MilliSecs()-oldms>1000

oldms=MilliSecs()
fps=ct
ct=0


End If


Wend



Function create_cell(xoffs,zoffs)


;empty verts
For layer=0 To 3
For x=0 To 63
For z=0 To 63
verts(x,z,layer)=-1
Next
Next
Next


;Create the grass mesh
mesh=CreateMesh()


For layer=0 To 3

If layer=0 Then surf=CreateSurface(mesh,brush_rock2)
If layer=1 Then surf=CreateSurface(mesh,brush_rock)
If layer=2 Then surf=CreateSurface(mesh,brush_grass)
If layer=3 Then surf=CreateSurface(mesh,brush_mud)




For z=0 To 63
For x=0 To 63

;Check if to skipp creation of this vertex
hitt=0

If textmap(x+xoffs,z+zoffs,layer)=1 Then hitt=hitt+1
If textmap(x+xoffs+1,z+zoffs,layer)=1 Then hitt=hitt+1
If textmap(x+xoffs,z+zoffs+1,layer)=1 Then hitt=hitt+1
If textmap(x+xoffs+1,z+zoffs+1,layer)=1 Then hitt=hitt+1

If z>0 And x>0 Then
If textmap(x+xoffs-1,z+zoffs-1,layer)=1 Then hitt=hitt+1
If textmap(x+xoffs-1,z+zoffs,layer)=1 Then hitt=hitt+1
If textmap(x+xoffs,z+zoffs-1,layer)=1 Then hitt=hitt+1
End If




If layer=3 Then hitt=1 ;Always create if its layer mud

If hitt Then verts(X,Z,layer) =AddVertex (surf,X,hmap#(x+xoffs,z+zoffs),Z,X,Z)

Next ;X
Next ;z


;Add triangles to mesh
For z=0 To 62
For x=0 To 62

;Create this quad if there is vertices available.
hitt=0
If verts(x,z,layer)<>-1 Then hitt=hitt+1
;If verts(x+1,z,layer)<>-1 Then hitt=hitt+1
If verts(x,z+1,layer)<>-1 Then hitt=hitt+1
If verts(x+1,z+1,layer)<>-1 Then hitt=hitt+1


If hitt=3


AddTriangle surf,verts(x,z,layer),verts(x,z+1,layer),verts(x+1,z+1,layer)

End If

hitt=0
If verts(x,z,layer)<>-1 Then hitt=hitt+1
If verts(x+1,z,layer)<>-1 Then hitt=hitt+1
;If verts(x,z+1,layer)<>-1 Then hitt=hitt+1
If verts(x+1,z+1,layer)<>-1 Then hitt=hitt+1

If hitt=3
AddTriangle surf,verts(x,z,layer),verts(x+1,z+1,layer),verts(x+1,z,layer)
trisr=trisr+1
End If
Next ; X
Next ;Z


;Montain emulation
For x=0 To 63
For z=0 To 63
If verts(x,z,layer)<>-1
VertexColor surf,verts(x,z,layer),255,255,255,0
If TEXTMAP(x+xoffs,z+zoffs,layer)=1 Then VertexColor surf,verts(x,z,layer),255,255,255,255  
End If
Next ;Z
Next ;X


Next ; Layer


PositionEntity mesh,xoffs,0,zoffs

;UpdateNormals mesh so it have shade. and dont look flat.
Calculate_Normals(mesh)


Return mesh

End Function






Function camera_handling()


If KeyDown(17) Then MoveEntity camera,0,0,0.02  ;W
If KeyDown(18) Then MoveEntity camera,0,0,1 ; E
If KeyDown(31) Then MoveEntity camera,0,0,-1 ;S
If KeyDown(30) Then MoveEntity camera,-0.02,0,0 ;A
If KeyDown(32) Then MoveEntity camera,0.02,0,0  ;D
If MouseDown(2)
smx#=MouseXSpeed()/10.0
smy#=MouseYSpeed()/10.0


TurnEntity camera,smy#,-smx#,0
RotateEntity camera,EntityPitch#(camera),EntityYaw#(camera),0
;TranslateEntity camera,0,-0.1,0
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
End If


; y#=BilinearInterpValue# ( Float(EntityX(camera))/2 ,Float(EntityZ(camera))/2 )

; If y#+0.5>EntityY#(camera)

; PositionEntity camera,EntityX(camera),y#+0.5,EntityZ(camera)

; End If

End Function












Function Calculate_Normals(ThisMesh)

; Loop through all surfaces of the mesh.
Surfaces = CountSurfaces(ThisMesh)
For LOOP_Surface = 1 To Surfaces

Surface_Handle = GetSurface(ThisMesh, LOOP_Surface)

; Reset the number of connected polygons for each vertex.
For LoopV = 0 To 32767
Vertex_ConnectedTris(LoopV) = 0
Next

; Loop through all triangles in this surface of the mesh.
Tris = CountTriangles(Surface_Handle)
For LOOP_Tris = 0 To Tris-1

; Get the vertices that make up this triangle.
Vertex_0 = TriangleVertex(Surface_Handle, LOOP_Tris, 0)
Vertex_1 = TriangleVertex(Surface_Handle, LOOP_Tris, 1)
Vertex_2 = TriangleVertex(Surface_Handle, LOOP_Tris, 2)

; Adjust the number of triangles each vertex is connected to and
; store this triangle in each vertex's list of triangles it is connected to.
ConnectedTris = Vertex_ConnectedTris(Vertex_0)
Vertex_TriList(Vertex_0, ConnectedTris) = LOOP_Tris
Vertex_ConnectedTris(Vertex_0) = ConnectedTris + 1

ConnectedTris = Vertex_ConnectedTris(Vertex_1)
Vertex_TriList(Vertex_1, ConnectedTris) = LOOP_Tris
Vertex_ConnectedTris(Vertex_1) = ConnectedTris + 1

ConnectedTris = Vertex_ConnectedTris(Vertex_2)
Vertex_TriList(Vertex_2, ConnectedTris) = LOOP_Tris
Vertex_ConnectedTris(Vertex_2) = ConnectedTris + 1

; Calculate the normal for this face.

; Get the corners of this face:
Ax# = VertexX#(Surface_Handle, Vertex_0)
Ay# = VertexY#(Surface_Handle, Vertex_0)
Az# = VertexZ#(Surface_Handle, Vertex_0)

Bx# = VertexX#(Surface_Handle, Vertex_1)
By# = VertexY#(Surface_Handle, Vertex_1)
Bz# = VertexZ#(Surface_Handle, Vertex_1)

Cx# = VertexX#(Surface_Handle, Vertex_2)
Cy# = VertexY#(Surface_Handle, Vertex_2)
Cz# = VertexZ#(Surface_Handle, Vertex_2)

; Triangle 1
; Get the vectors for two edges of the triangle.
Px# = Ax#-Bx#
Py# = Ay#-By#
Pz# = Az#-Bz#

Qx# = Bx#-Cx#
Qy# = By#-Cy#
Qz# = Bz#-Cz#

; Compute their cross product.
Nx# = Py#*Qz# - Pz#*Qy#
Ny# = Pz#*Qx# - Px#*Qz#
Nz# = Px#*Qy# - Py#*Qx#

; Store the face normal.
Face_NX#(LOOP_Tris) = Nx#
Face_NY#(LOOP_Tris) = Ny#
Face_NZ#(LOOP_Tris) = Nz#

Next

; Now that all the face normals for this surface have been calculated, calculate the vertex normals.
Vertices = CountVertices(Surface_Handle)
For LOOP_Vertices = 0 To Vertices-1

; Reset this normal.
Nx# = 0
Ny# = 0
Nz# = 0

; Add the normals of all polygons which are connected to this vertex.
Polys = Vertex_ConnectedTris(LOOP_Vertices)

For LOOP_Polys = 0 To Polys-1

ThisPoly = Vertex_TriList(LOOP_Vertices, LOOP_Polys)

Nx# = Nx# + Face_NX#(ThisPoly)
Ny# = Ny# + Face_NY#(ThisPoly)
Nz# = Nz# + Face_NZ#(ThisPoly)

Next

; Normalize the new vertex normal.
; (Normalizing is scaling the vertex normal down so that it's length = 1)

Nl# = Sqr(Nx#^2 + Ny#^2 + Nz#^2)

; Avoid a divide by zero error if by some freak accident, the vectors add up to 0.
; If Nl# = 0 Then Nl# = 0.1

Nx# = Nx# / Nl#
Ny# = Ny# / Nl#
Nz# = Nz# / Nl#

; Set the vertex normal.

VertexNormal Surface_Handle, LOOP_Vertices, Nx#, Ny#, Nz#
;VertexColor Surface_Handle, LOOP_Vertices, polys*127, polys*127, polys*127

Next

Next

End Function



_33(Posted 1+ years ago)

 Where can we get the media?


JA2(Posted 1+ years ago)

 <div class="quote"> [Where can we get the media?  </div>;Images and compiled examples.;http://www.tiberion.com/furysalphademo.zip


Wings(Posted 1+ years ago)

 For you informationFunction Calculate_Normals(ThisMesh)It is the best function of all.. thanks for the one who created it.(i discovered that build in vertex normal has serius bugg..)


Wings(Posted 1+ years ago)

 just a warning note ..readpixelfast is 10 times slower in version 1.99 than in v1.98


Wings(Posted 1+ years ago)

 Heres a link to a compiled example. and newest version<a href="http://www.tiberion.eu/QueRenderEngine.zip" target="_blank">http://www.tiberion.eu/QueRenderEngine.zip[/url]NewFeatures:5000x5000 heightmapQueBased render. makes it stable at 60 fps.it means i made render a little complicated. but to explain it easy it renders a little now and then based on a render que i made up.. using types.CopyFreeNotice:The file aboove is publicdomain and you may sell it and profit for it.  you dont have to mention me in creddits and you may copy and spread it on internet and its ok to use this file in public television and sattelites and radio bradcast it is also ok. its ok to have this file in the car but i dont recomend it as i take no responsibillity if you or anyone gets hurts. i take no responibility for anything this file can do to you computer, or if your fuse went out or house burnsout. in facr i have no responybility att all to this file. You may not however place this file under copyright by the way it its you must change somthing first on order to do that.


plash(Posted 1+ years ago)

 <div class="quote"> its ok to use this file in public television and sattelites and radio bradcast </div>How do you use a terrain renderer in a radio broadcast?P.S. That's one of the funniest "CopyFreeNotice"'s I've ever read.


Doggie(Posted 1+ years ago)

 You beam it to your satellite first. I thought that was obvious.


Wings(Posted 1+ years ago)

 Now to the most funny part..all these terrain coding and looking nice.. but sens dx7 is as it is.. i have no use for the above code ?Sens i want water fire and magic all uses alpha meshes so makes the above useless. I made fast decicion this summer to use vertex color.. for terrain. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal