Sign in to follow this  
pyroteamkill

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
//-----------------------------------------------------------------------------
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
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