Generating an index buffer from a vertex buffer

Started by
6 comments, last by VanillaSnake21 5 years, 4 months ago

I'm working on creating a simple terrain for my game. It starts off as a flat, square grid mesh with vertices at even intervals which then gets transformed by the height map and textured. I'm having some issues with creating the square grid. I've just iteratively plopped vertices down, however I'm not seeing how I can now generate an index buffer from that. I've sat down with pen and paper and tried to find a pattern formulae to the indices, but it's not as easy as I expected. I've attached an image of what I came up with. It seems like there is some pattern there but it's not immediately clear. Is there any algorithm for this? I assume this is some form of tessellation, but I have not been able to find anything online. Thanks.

 

draw.jpg

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement

I'm not certain this is what you're looking for, but there is a pattern for building an index set for a regular grid of this sort. (This will be off the top of my head, so there may be errors in my description.)

I'll assume the first index is 0 rather than 1, as that's more typical and makes the math easier.

The grid has a size in cells, and a size in vertices. For each dimension the size in vertices is one greater than the size in cells. In your example the size in cells for each dimension is 5, and the size in vertices is 6.

Each cell in the grid has xy coordinates, starting with 0, 0 in the lower left (with respect to your drawing).

For any cell y coordinate, there are two rows of vertices of interest, starting at 'cellY * vertsPerRow' and '(cellY + 1) * vertsPerRow'. Finding the 4 indices for a cell might look something like this:


index0 = cellY * vertsPerRow + cellX;
index1 = index0 + 1;
index2 = (cellY + 1) * vertsPerRow + cellX;
index3 = index2 + 1;

The index arrangement is for each cell is:


2----3
|    |
|    |
0----1

You can then triangulate that however you want.

24 minutes ago, Zakwayda said:

Finding the 4 indices for a cell might look something like this:

But don't I need 6 indices per 4 vertex square? Three for each triangle? 

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

3 minutes ago, VanillaSnake21 said:

But don't I need 6 indices per 4 vertex square? Three for each triangle? 

6 indices total, but only 4 unique indices. You can see that in the first example for your diagram:

1 2 8 8 7 1

There are 6 indices there, but only 4 unique indices, that is:

1 2 7 8

It'll be the same for every cell (just with different specific indices).

If that's still not clear, I can probably provide a more concrete example.

@Zakwayda, Ok, I see what you mean. 

So if I can just do this:


index0 = cellY * vertsPerRow + cellX;
index1 = index0 + 1;
index2 = (cellY + 1) * vertsPerRow + cellX;
index3 = index2 + 1;

index4 = index2

index5 = index1

 

And then repeat for next cell


index6 = cellY * vertsPerRow + cellX;
index7 = index6 + 1;
index8 = (cellY + 1) * vertsPerRow + cellX;
index9 = index8 + 1;

index10 = index8

index11 = index7

 

And so on...

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

You could do something like that, provided the indices are added to the index array in the right order. Or you could just add them to the array directly without creating duplicates, e.g.:


index0 = cellY * vertsPerRow + cellX;
index1 = index0 + 1;
index2 = (cellY + 1) * vertsPerRow + cellX;
index3 = index2 + 1;

indices.push_back(index0);
indices.push_back(index1);
indices.push_back(index3);
indices.push_back(index0);
indices.push_back(index3);
indices.push_back(index2);

Also, just for clarity, the above (or similar) should only appear in your code once. To generate the indices for all the cells in the grid, a nested loop should be used. (That may be obvious, but since your last example appears to 'hard code' the indices for the first and second cells, I thought it might be worth mentioning that a loop should be used instead.)

@Zakwayda Thank you, I finally got it working, thanks for taking the time to explain it. 

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

This topic is closed to new replies.

Advertisement