Jump to content
  • Advertisement
Sign in to follow this  

DirectX index buffer drawing issues

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

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!