Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tevong

Creating a terrain in DirectX8

This topic is 6056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to write a function that''ll draw a terrain matrix with random heights like in DarkBasic and although i haven''t put in random heights and textures in yet, what I''ve done so far refuses to work: Option Explicit ''vertex buffer to hold vertices Public TerrainBuffer As Direct3DVertexBuffer8 Public VertexSize As Long Public numPrimitives As Long ''vertices type Public Type CUSTOMVERTEX x As Single y As Single z As Single normal As D3DVECTOR tu As Single tv As Single End Type ''FVF Public Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1) ''--------------------------------------------------------------------------------- ''Name: CreateTerrain ''Type: function, returns a boolean value ''Desc: Creates a random terrain ''--------------------------------------------------------------------------------- Public Function CreateTerrain(dimX As Single, dimY As Single, _ tileSize As Single, texture As Direct3DTexture8, _ MaxHeight As Single) As Boolean Dim vertices() As CUSTOMVERTEX ''info variables Dim numVertices As Long Dim counterX As Single Dim counterZ As Single Dim counter As Long ''calculate number of primitives needed for matrix numPrimitives = ((dimX / tileSize) * (dimY / tileSize)) * 2 For counterX = 0 To dimX Step tileSize For counterZ = 0 To dimY Step tileSize numVertices = numVertices + 1 Next counterZ Next counterX VertexSize = Len(vertices(0)) ReDim vertices(numVertices) ''create vertices For counterX = 0 To dimX Step tileSize For counterZ = 0 To dimY Step tileSize With vertices(counter) .x = counterX .y = 1 .z = counterZ .normal = vec(counterX, 1, counterZ) .tu = 0 .tv = 0 End With counter = counter + 1 Next counterZ Next counterX ''put vertices into buffer Set TerrainBuffer = objD3DDevice.CreateVertexBuffer(VertexSize, _ 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED) If TerrainBuffer Is Nothing Then CreateTerrain = False Exit Function End If D3DVertexBuffer8SetData TerrainBuffer, 0, numVertices, 0, vertices(0) CreateTerrain = True End Function Public Sub DrawTerrain() ''set stream objD3DDevice.SetStreamSource 0, TerrainBuffer, VertexSize ''set vertex shader objD3DDevice.SetVertexShader D3DFVF_CUSTOMVERTEX ''draw primitives objD3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, numPrimitives End Sub

Share this post


Link to post
Share on other sites
Advertisement
What is it exactly that''s not working? Here are some quick changes that I would make anyway...

* When you create your vertex buffer, set the D3DPOOL value to D3DPOOL_DEFAULT instead....

* You haven''t set the normals properly for what appears to be a flat plane along the x-z axis - try turning the lighting off and/or change the background colour to something other that black or white...for a plane of this type, simply set the x and z components of the normal to 0 and the y to 1 - when the terrain heights are created the normals will need to be recalculated for the lighting to work..

* Also, when running through the loop you are only creating the vertex/grid points for each tile, yet you draw the terrain using D3DPT_TRIANGLELIST...(try changing to D3DPT_POINTLIST) you will find that when you have the triangles correct and put the heights in, the triangles will not join. As for rendering, it would be much more efficient to use a vertex buffer for the grid points, then use an index buffer to create the actual triangles...

* Check how many vertices you are actually asking the vertex buffer to create - depending on what GFX card you are using, ther is a limit. As far as I know, GeForce2 GTS can handle upto 65535 at a time, and the same for indices. More recent cards can handle much more for instance. Anyway, that is a lot of vertices for a terrain grid, so don''t worry about it to much...

That''s all I can think of, but let us know what exactly is going wrong, and try the above anyway...




Share this post


Link to post
Share on other sites
Guess I''ll have to go and learn about index buffers now.. thnx
what I mean by it doesn''t work is it''s not displaying the flat plane, and if i put a value of 100 for dimX and 100 for dimY and a tilesize of 20 I get a memory address error and the whole thing crashes =(

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!