Skip to main content
gamedev.net
🔒 Locked

Problem rendering terrain

Started by dev578 2 replies 250+ views
Original Post
dev578
dev578
Alright, here is what I have so far: int Tracker = 0; for( int x = 0; x < terrain.GetSize(); x++ ) { for( int y = 0; y < terrain.GetSize(); y++ ) { float z = terrain.GetCell( x, y ); pVertices[Tracker].x = (float)x; pVertices[Tracker].y = z; pVertices[Tracker].z = (float)y; pVertices[Tracker].color= 0xffffffff; Tracker+=1; } } First, it will take care of all the z''s in the x=0 row (variable y in the terrain generator), then it will shift x over one and take care of all the z''s in that row...etc. It works completely fine, I am just having a problem rendering it. If I do DeviceD3D->DrawPrimitive( D3DPT_POINTLIST,...); It will come out just fine, as points (it actually looks pretty cool I wan''t to draw the primitive with triangles, but it comes out really screwy. I need to switch the order in which pVertices gets filled, but I have no idea how. If anyone could help me out, that would be great. -Dev578
Trip99
Trip99
It depends if you are wanting to render a strip or fan or indexed triangles etc. Normally I wouild expect to render terrain using indexed so you need an index buffer and you need to fill it with the indices of each vertex for each triangle,
------------------------See my games programming site at: www.toymaker.info
Etnu
Etnu
You''re either going to need to index into that list, or duplicate your vertices.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.
Karl G
Karl G
What these guys are trying to tell you is that right now you have this (vertices are numbered):

0    1    2    3

4 5 6 7

8 9 10 11

12 13 14 15


And what you need is this:

0     1,4,6    7,10,12    13,16


2,3 5,8,9 11,14,15 17

(etc)


Then render using triangle list. Basically, you have to duplicate vertices because it''s not smart enough to determine "oh, this is a height field and I want to make triangles between all of the points"--you have to tell it what to make. The other option, which is a lot easier to read and think about but is a bit harder to implement is to use indices. Indices are stored using the IDirect3DIndexBuffer9 interface, and act like a vertex "table of contents" if you will.

For your vertices:

0      1


2 3


You want to have two triangles. To do this, add these indices to your index buffer (note that we are rendering using triangle lists, not strips or fans, and that the function called must be "DrawIndexedPrimitive"):

0, 1, 2,
2, 1, 3


"0,1,2" represents your first triangle. "2,1,3" is the second. This should be repeated throughout your terrain so that for each quad (set of 4 vertices) the two triangles created have the indices "n, n+1, n+width" and "n+width, n+1, n+width+1" where ''n'' is the top left-hand vertex and width is the number of *vertices* wide the terrain is.

This may be a bit hard to comprehend at first but I hope you get this gist of it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.