# [Direct3D 9] Rendering issues using vertex and index buffers

This topic is 2598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to create a terrain grid by generating a series of vertices to make up a square of triangles and creating a list of indices to refer to the verts, adding them to vertex and index buffers respectively, then drawing it using the standard IDirect3DDevice9 method DrawIndexedPrimitive. However, I am having issues which seem to be linked to the buffers, but I cannot seem to find the specific issue and as such was wondering if I could request some assistance with the issue.

I am confident that I am generating the vertices and indices correctly as I have run through it on paper numerous times and stepped through the program to make sure they are being calculated and stored correctly in a vector list. My vertices are stored in a custom vertex struct defined as follows:

struct FSVERTEX { D3DXVECTOR3 position; // x/y/z co-ordinates D3DXVECTOR3 normal; // vertex normal DWORD colour; // vertex colour FLOAT u, v; // texture co-ordinates }; #define D3DFVF_FSVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_TEX1)

For my indices, I am storing them using the WORD data type in a vector list, same as the vertices. Once I have created them, I am adding them to a vertex and index buffer declared using LPDIRECT3DVERTEXBUFFER9 and LPDIRECT3DINDEXBUFFER9 respectively. The code I have for adding them to the buffers is as follows:

void FSTerrainNode::GenerateBuffers(LPDIRECT3DDEVICE9 gfx3DDevice) { FSVERTEX temp; DWORD tempIndex; // Creates the vertex buffer gfx3DDevice->CreateVertexBuffer( _numVertices * sizeof( D3DFVF_FSVERTEX ), 0, D3DFVF_FSVERTEX, D3DPOOL_DEFAULT, &_gfxTerrainBuffer, NULL ); // Loads the vertices into the vertex buffer FSVERTEX* pVertices; _gfxTerrainBuffer->Lock( 0, _numVertices * sizeof( D3DFVF_FSVERTEX ), ( void** )&pVertices, 0 ); for (int i = 0; i < (int)_vertices.size(); i++) { pVertices = _vertices; temp = pVertices; } _gfxTerrainBuffer->Unlock(); // Create the index buffer HResult( gfx3DDevice->CreateIndexBuffer(_numIndices * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &_gfxIndexBuffer, NULL) ); // Loads the indices into the index buffer WORD* pIndices; _gfxIndexBuffer->Lock( 0, _numIndices * sizeof(WORD), ( void** )&pIndices, 0 ); for (WORD i = 0; i < (WORD)_indices.size(); i++) { pIndices = _indices; tempIndex = pIndices; } _gfxIndexBuffer->Unlock(); }

Once I have done that, I am then attempting to render the vertices using the following function calls:

gfx3DDevice->SetFVF( D3DFVF_FSVERTEX ); gfx3DDevice->SetStreamSource( 0, _gfxTerrainBuffer, 0, sizeof( FSVERTEX ) ); gfx3DDevice->SetIndices( _gfxIndexBuffer ); gfx3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, _numVertices, 0, _numTriangles ); // _numVertices, _numTriangles

When it starts rendering I am having issues with the display. At attempting to display a 256x256 grid, no grid is displayed on-screen. 255x255 however yields the following result:

Attempting 1x1 to 9x9 displays no apparent errors in the grid, but upon reaching 10x10, the bug becomes apparent as the grid is displayed as follows:

I've tried looking into the issue but I can't seem to find what's specifically causing the issue, but I have a gut feeling it's to do with the vertex and/or index buffers. If anyone could give me assistance on this issue it would be greatly appreciated. Thanks a lot!

##### Share on other sites
I have managed to fix this problem, realised I needed to increase the allocated size of indices to DWORD and corrected the memory locking for the buffers, as well as changed the index format to INDEX32. Cheers anyway!

##### Share on other sites

I have managed to fix this problem, realised I needed to increase the allocated size of indices to DWORD and corrected the memory locking for the buffers, as well as changed the index format to INDEX32. Cheers anyway!

Yep, the max. index with WORD indices is 65535, which obviously isn't enought for a 256x256 grid

But I guess most of us have been through this error sometimes in the past , wondering what the hell is wrong (I personaly at least 3 times, now I'm always aware of this potential problem.)

• 9
• 9
• 13
• 41
• 15