Sign in to follow this  
PeterInnes

[Direct3D 9] Rendering issues using vertex and index buffers

Recommended Posts

PeterInnes    102
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:

[code]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)[/code]

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:

[code]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[i] = _vertices[i];
temp = pVertices[i];
}
_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[i] = _indices[i];
tempIndex = pIndices[i];
}
_gfxIndexBuffer->Unlock();
}[/code]

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

[code]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[/code]

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:

[img]http://a6.sphotos.ak.fbcdn.net/hphotos-ak-snc6/207232_10150162383099341_705374340_6466077_6710620_n.jpg[/img]

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:

[img]http://img690.imageshack.us/img690/9889/fsengine2.png[/img]

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 this post


Link to post
Share on other sites
PeterInnes    102
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 this post


Link to post
Share on other sites
Tom KQT    1704
[quote name='HungryHippo5' timestamp='1302686348' post='4797867']
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!
[/quote]

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.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this