Sign in to follow this  

DirectX index buffer drawing issues

Recommended Posts

Hay guys.
I have been building a terrain segment on my game engine and I have run into some issues with using the index buffer.
I have followed a few tutorials but am still having problems getting it to draw.
It will draw if I use raw vertices though.

I have looked all round the location is should render just to check if the normals had been flipped and stuff.

Here is the relevent code:

// Default Constructor

m_indicies = new short[6];

// Create the vertices using the CUSTOMVERTEX struct
m_vert[0].COLOR = D3DCOLOR_XRGB(255, 0, 0);
m_vert[0].X = GetPosition().x + 0.0f;
m_vert[0].Y = GetPosition().y + 10.0f;
m_vert[0].Z = GetPosition().z + 0.0f;

m_vert[1].COLOR = D3DCOLOR_XRGB(0, 255, 255);
m_vert[1].X = GetPosition().x + 20.0f;
m_vert[1].Y = GetPosition().y + 10.0f;
m_vert[1].Z = GetPosition().z + 0.0f;

m_vert[2].COLOR = D3DCOLOR_XRGB(0, 0, 255);
m_vert[2].X = GetPosition().x + 0.0f;
m_vert[2].Y = GetPosition().y + 10.0f;
m_vert[2].Z = GetPosition().z + 20.0f;

m_vert[3].COLOR = D3DCOLOR_XRGB(0, 255, 0);
m_vert[3].X = GetPosition().x + 20.0f;
m_vert[3].Y = GetPosition().y + 10.0f;
m_vert[3].Z = GetPosition().z + 20.0f;

// create a vertex buffer interface called m_buffer

VOID* pVoid; // a void pointer

// lock t_buffer and load the vertices into it
m_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, m_vert, sizeof(m_vert));

m_indicies[0] = 0;
m_indicies[1] = 2;
m_indicies[2] = 3;
m_indicies[3] = 0;
m_indicies[4] = 3;
m_indicies[5] = 1;

// Create index buffer
g_engine->GetD3DDevice()->CreateIndexBuffer(6 * sizeof(short),

// Lock index buffer and load the vertices into it
m_indexBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, m_indicies, sizeof(m_indicies));

// Render
void TerrainGrid::Render()

// select which vertex format we are using

// select the vertex buffer to display
g_engine->GetD3DDevice()->SetStreamSource(0, m_buffer, 0, sizeof(CUSTOMVERTEX));

// Set the index buffer


Any help you can provide would be excelent. I have been stuck on this for days now. :(

Share this post

Link to post
Share on other sites
What happens? Does nothing at all draw?

Does it work if you disable culling (SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE))? If so, then the order of your indices is wrong.

Share this post

Link to post
Share on other sites
This line will only copy 2 indices:
memcpy(pVoid, m_indicies, sizeof(m_indicies));
Because m_indicies is a pointer, which is 4 bytes, so sizeof(m_indicies) == 4.

You want:
memcpy(pVoid, m_indicies, sizeof(short)*6);

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