#### Archived

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

# filling in the indexbuffer for rendering terrain with triangle strips

## Recommended Posts

Hello, I am currently trying to generate an IndexBuffer from a heightmap. First I just read in the heightmap from which every pixel identifies one vertex. All these verts are then stored into a VertexBuffer. Next, I try to fill in an IndexBuffer to render the terrain with triangle strips. But it seems that there is something wrong, because when I finally render it, it looks like this: wrong terrain Rendering one row works ok (at least it looks ok). But from the moment I try to render two rows, it seems to miss one squad. Well, I guess the reason is it probably draws two squads at the same position. Finally, I show you my code for filling in the index buffer down here. I really don''t know what is wrong. I tried to do several things. Like having multiple index buffers and such, but they all give some bizar problems.

// before I read in the heightmap and generate

// the vertex buffer

...
// when mapWidth is eg 4, the terrain is three squads wide

// when mapHeight is eg 4, the terrain is three squads tall

unsigned short indexCount = mapWidth * 2 * (mapHeight - 1);
WORD* inds = new WORD[indexCount];

int count = 0;
for (row = 0; row < mapHeight - 1; ++row)
{
// I go over the rows like when lawning the grass.

// On even rows I go from left to right and on odd rows

// I go from the right to the left. This is done to make

// all the strips being attached to each other

if (row % 2 == 0)
{
for (int col = 0; col < mapWidth; ++col)
{
inds[count++] = row * mapWidth + col;
inds[count++] = (row + 1) * mapWidth + col;
}
}
else
{
for (int col = mapWidth - 1; col >= 0; --col)
{
inds[count++] = (row + 1) * mapWidth + col;
inds[count++] = row * mapWidth + col;
}
}
}

// and here we create the index buffer

hRet = device->CreateIndexBuffer(sizeof(WORD) * indexCount, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ib);
if (FAILED(hRet)) return hRet;

// lock the buffer and copy the data

void *idata;
hRet = ib->Lock(0, 0, (BYTE**)&idata, D3DLOCK_NOSYSLOCK);
if (FAILED(hRet)) return hRet;
memcpy(idata, inds, sizeof(WORD) * indexCount);
ib->Unlock();

delete [] inds;
...

Anyway, any help would be appreciated.

#### Share this post

##### Share on other sites
well,

I finally found my problem. It was in the rendering part where I did a call to DrawIndexedPrimitive. I specified the index count in the third parameter, which should of course be the vertex count. So, now it works .

• ### Forum Statistics

• Total Topics
628327
• Total Posts
2982091

• 22
• 9
• 9
• 13
• 11