Archived

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

jemiller100

brute force terrain using managed DirectX???

Recommended Posts

jemiller100    122
Can anyone provide example code for loading a vertex buffer and an index buffer of a simple terrain using MANAGED DirectX? Can you point me to some samples or examples that could help me? Could you explain the difference of vertex buffers rom index buffers? As far as I can tell the vertex buffer holds all unique vertex points and the index point to each duplicating the ones shared by multiple polygons. Is this correct? I am looking for something very simple so I can better understand using vertex and index buffers. I am not worried about performance at this point. Thanks!

Share this post


Link to post
Share on other sites
Raloth    379
Your vertex buffer contains all of the unique vertices. When DirectX goes to render them, it checks the index buffer and renders the vertex indexed by that number.

Suppose you have vertices numbered like so:

1 2 3
4 5 6
7 8 9
Assuming you want to make that into a patch of terrain, your index buffer might look something like this for a triangle list: (1,2,4,2,5,4,2,3,5,3,6,5,4,5,7,5,8,7,5,6,8,6,9,8)

A triangle strip is more efficient. The first three indices/vertices supplied tell DirectX the first triangle and the front/back of your triangles. It then takes the next point and builds a triangle from it with the two last points used. An index strip might look something like this: (1,4,2,5,3,6,6,4,7,5,8,6,9) [edit] I should note that I changed the way the triangles face to get the strip to work right.

You may wonder why the sixes were repeated. This makes two "degenerate triangles", triangles that share a vertex so there is nothing rendered. This lets you render the terrain in one DrawIndexedPrimitive call instead of many like you would have to do with triangle strips otherwise.

Yes it's a lot of work but after figuring out the pattern a for loop makes things easy.

[edited by - Raloth on January 19, 2004 8:08:27 PM]

Share this post


Link to post
Share on other sites
jemiller100    122
Thanks, Raloth!

Ok, that makes sense. In your example by having the 2 "degenerate triangles" I can render the strip in one call as opposed to 2? So a triangle strip is a band of polygons one quad tall?

Can anyone provide me the managed Direct3D syntax for creating an indexbuffer? What type does the indexbuffer array need to be?

Share this post


Link to post
Share on other sites
Raloth    379
Yes, the degenerate triangles let you do it in one call vs two. The driver is very good at throwing them out and there is almost no speed penalty. I wouldn''t think of them as a "band of polygons one quad tall". This is in effect what triangle strips are, but they can be twisted around and such so they are not necessarily "tall" or even quads.

Share this post


Link to post
Share on other sites
jemiller100    122
Does anyone have any code for looping to fill a vertex buffer and index buffer to form a grid of quads? I know each polygon needs to be clockwise in the index buffer. The bottom left corner of my grid is vertex 0,0,0. I want to build it along positive x and positive z. I am open to other ways of doing it though. I am having trouble figuring out a good way to loop for the index buffer and vertex buffer.

Share this post


Link to post
Share on other sites
Raloth    379
Right from my code:
	TerrainVertex* pvertices;
vertexBuffer->Lock(0,0,(void**)&pvertices,0);
int vert = 0;
for(int i=0;i<129;i++) {
for(int j=0;j<129;j++) {
pvertices[vert] = TerrainVertex(i,0,j,(float)i/4.0f,(float)(128-j)/4.0f);
++vert;
}
}
vertexBuffer->Unlock();

WORD* pindices;
indexBuffer->Lock(0,0,(void**)&pindices,0);
vert = 0;
for(int i=0;i<128;i++) {
for(int j=0;j<128;j++) {
pindices[vert + 0] = i*129 + j;
pindices[vert + 1] = (i+1)*129 + j;
pindices[vert + 2] = i*129 + (j+1);
pindices[vert + 3] = (i+1)*129 + j;
pindices[vert + 4] = (i+1)*129 + (j+1);
pindices[vert + 5] = i*129 + (j+1);
vert+=6;
}
}
indexBuffer->Unlock();
129 is the number of vertices long of each terrain patch. I don't remember which way my triangles face and I'm too tired right now to look at it, but if it's wrong it shouldn't be hard to change.

[edit] I'm only using triangle lists right now.

[edited by - Raloth on January 22, 2004 9:53:17 PM]

Share this post


Link to post
Share on other sites
Nypyren    12072
For variety, also try out the GraphicsStream version of the lock functions. also, remember that if you specify read-only or write-only, not as much array copy overhead will take place.

Share this post


Link to post
Share on other sites
jemiller100    122
Thanks, Raloth.

I am having a problem understanding your index buffer loop. On the first iteration with i and j being 0 your indexes are 0,129,1,129,130,1. I must be missing something or overlooked something but this doesn''t seem right. Could you explain or point out where I am getting this wrong?

Share this post


Link to post
Share on other sites
Raloth    379
My vertices look like this (shorter version):
0 3 6
1 4 7
2 5 8
The loop to create this indices from this would look like:
	for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
pindices[vert + 0] = i*3+ j;
pindices[vert + 1] = (i+1)*3+ j;
pindices[vert + 2] = i*3+ (j+1);
pindices[vert + 3] = (i+1)*3+ j;
pindices[vert + 4] = (i+1)*3+ (j+1);
pindices[vert + 5] = i*3+ (j+1);
vert+=6;
}
}
In order to hop across to the next column to make a triangle you must multiply the x coordinate by the number of vertices in a column.

[edit] i is supposed to be the x coordinate and j is supposed to be the y coordinate with the way I do it, but I'm not actually sure if it ends up working that way . It hurts my head to think about the arrays too much. Just keep with the same pattern every time and it will work out in the end.

[edited by - Raloth on January 23, 2004 8:18:16 PM]

Share this post


Link to post
Share on other sites