Jump to content
  • Advertisement
Sign in to follow this  
pyroteamkill

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
//-----------------------------------------------------------------------------
TerrainGrid::TerrainGrid():PhysicalGameObject()
{
//PhysicalGameObject::PhysicalGameObject();

m_buffer = LPDIRECT3DVERTEXBUFFER9();
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
g_engine->GetD3DDevice()->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&m_buffer,
NULL);

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_buffer->Unlock();

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),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&m_indexBuffer,
NULL);

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

//-----------------------------------------------------------------------------
// Render
//-----------------------------------------------------------------------------
void TerrainGrid::Render()
{
PhysicalGameObject::Render();

// select which vertex format we are using
g_engine->GetD3DDevice()->SetFVF(CUSTOMFVF);

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

// Set the index buffer
g_engine->GetD3DDevice()->SetIndices(m_indexBuffer);

g_engine->GetD3DDevice()->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2);
}


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
Advertisement
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!