Should I bother using an index buffer for my landscape?

Started by
5 comments, last by johnnyBravo 18 years, 7 months ago
Hi, I'm just developing a grid based landscape, and I was wondering if I should bother using index buffers. As the problem with them for me is texture coordinates. As I would use a vertex per position in the grid, I would have to make the texture coordinates go like: 01, 11, 01, 11 00, 10, 00, 10 01, 11, 01, 11 00, 10, 00, 10 So the texture would be flipped up down, left right depending on the tile. So I thought maybe I should only use a vertex buffer, unless there is a solution to this problem? Thanks
Advertisement
Hi there johnnyBravo, how are you doing buddy?


The Problem
Using index buffers for terrain generation.

The Solution
I would say that there shouldn't even be a choice. Using index buffers improves performance and reduces the vertex count. Since you will not have duplicate vertices in your terrain.

For your problem, The texture coordinates will be easy to calculate.
The Vertex generation
[source lang = "cpp"]DWORD pos = 0;for (int y = 0; y < terrainHeight; y++) {   for (int x = 0; x < terrainWidth; x++) {       pVertexData[pos].x = (float)x * cellScale;       pVertexData[pos].y = getScaledHeight(aHeightMap[pos]);       pVertexData[pos].z = (float)y * cellScale;       pVertexData[pos].u1 = (float)((terrainWidth - 1) - y) / (terrainWidth - 1);       pVertexData[pos].v1 = (float)(x) / (terrainHeight - 1);    }}

The Index generation
[source lang = "cpp"]for (int y = 0; y < (terrainHeight - 1); y++) {    for (int x = 0; x < (terrainWidth - 1); x++) {        *pIndexData++ = y * terrainWidth + x; //v1        *pIndexData++ = y * terrainWidth + x + 1; //v2        *pIndexData++ = (y + 1) * terrainWidth + x + 1; //v4        *pIndexData++ = y * terrainWidth + x; //v1        *pIndexData++ = (y + 1) * terrainWidth + x + 1; //v4        *pIndexData++ = (y + 1) * terrainWidth + x; //v3    }}


I hope this helps a bit buddy, if you have any further questions please do not hesitate to ask.
If you are using one texture for your whole landscape, or large chunks of your landscape, armadon's method is best.

It looks like you might be using a texture for each quad, or have a "tileset"-like texture to use, in which case using index buffers would not be so easy.

I haven't done any performance tests, nor am I an expert, but if the latter case is true, I wouldn't use an index buffer.
Definitely use indexes. As Armadon points out, it eliminates duplicate vertexes. It halves the number of vertexes in a grid. Another benefit of using indexes is that it allows you to use LOD without modifying the vertex buffer.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
It will not halve the number of vertices in a grid unless he is using one texture mapped onto the grid.
Also, you don't have to mirror the UVs (though mirroring makes tiling a single texture a lot easier). You can also let them go past 1 like this:

0,0 1,0 2,0 3,0 ...
0,1 1,1 2,0 3,1 ...
0,2 1,2 2,1 3,2 ...
0,3 1,3 2,0 3,3 ...
... ... ... ...
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
Also, you don't have to mirror the UVs (though mirroring makes tiling a single texture a lot easier). You can also let them go past 1 like this:

0,0 1,0 2,0 3,0 ...
0,1 1,1 2,0 3,1 ...
0,2 1,2 2,1 3,2 ...
0,3 1,3 2,0 3,3 ...
... ... ... ...


Ah yeah, i can't believe i forgot about that.

thx for the code Armadon, though i think i might have some old code that does that. Ill compare the code see if i can make any improvements, I think i remember it took a bit of effort for me to get those algorithms working.

Also when drawing a part of the landscape, eg only the viewable part of the landscape, should I store the entire lot of vertices into the vb, and store the generated indices into a two dimensional array that corresponds with the tiles?, so I can just add the indices into the dynamic ib when required?

thanks

This topic is closed to new replies.

Advertisement