# Rendering Bounding boxes [Direct3D]

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

## Recommended Posts

Hi, Im trying to right a function that will allow me to render out the lines used to create a bounding box but im having a few problems with getting it to build a box shape. For this function ive been trying to convert an a peice of XNA code over to Direct3D The original can be seen here: http://www.xnawiki.com/index.php?title=Rendering_Bounding_Boxes This is the code i have at the moment :

LPDIRECT3DVERTEXBUFFER9 m_VertexBuffer;
LPDIRECT3DINDEXBUFFER9 m_IndexBuffer;
D3DXVECTOR3* m_vertices;
int *m_indices;

void SetUp()
{
//Create the vertex buffer
m_Direct3DDevice->CreateVertexBuffer(8*sizeof(D3DXVECTOR3), D3DUSAGE_WRITEONLY, 0,
D3DPOOL_MANAGED, &m_VertexBuffer, NULL );
//Create the index buffer
m_Direct3DDevice->CreateIndexBuffer(12*3*2,D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_IndexBuffer, NULL);

m_IndexBuffer->Lock(0, 0, (void**)&m_indices, 0);
m_indices[0] = 0;		m_indices[1] = 1;		m_indices[2] = 1;		m_indices[3] = 2;
m_indices[4] = 2;		m_indices[5] = 3;		m_indices[6] = 3;		m_indices[7] = 0;
m_indices[8] = 0;		m_indices[9] = 4;		m_indices[10] = 1;		m_indices[11] = 5;
m_indices[12] = 2;		m_indices[13] = 6;		m_indices[14] = 3;		m_indices[15] = 7;
m_indices[16] = 4;		m_indices[17] = 5;		m_indices[18] = 5;		m_indices[19] = 6;
m_indices[20] = 6;		m_indices[21] = 7;		m_indices[22] = 7;		m_indices[23] = 4;
m_IndexBuffer->Unlock();
}

void RenderBoundingBox(CBoundingBox BoundingBox, D3DXMATRIX WorldMatrix)
{
m_Direct3DDevice->SetMaterial( &m_BoundingBoxMaterial );
m_Direct3DDevice->SetTexture(0,NULL);

m_VertexBuffer->Lock(0, 0, (void**)&m_vertices, 0);

m_vertices[0] = BoundingBox.GetCorner(0);
m_vertices[1] = BoundingBox.GetCorner(1);
m_vertices[2] = BoundingBox.GetCorner(2);
m_vertices[3] = BoundingBox.GetCorner(3);

m_vertices[4] = BoundingBox.GetCorner(4);
m_vertices[5] = BoundingBox.GetCorner(5);
m_vertices[6] = BoundingBox.GetCorner(6);
m_vertices[7] = BoundingBox.GetCorner(7);

m_VertexBuffer->Unlock();

m_Direct3DDevice->SetTransform( D3DTS_WORLD, &WorldMatrix );
m_Direct3DDevice->SetTransform(D3DTS_VIEW, &CAMERA.GetViewMatrix());

m_Direct3DDevice->SetStreamSource( 0, m_VertexBuffer,0, sizeof(D3DXVECTOR3) );
m_Direct3DDevice->SetFVF( D3DFVF_XYZ );
m_Direct3DDevice->SetIndices( m_IndexBuffer);

m_Direct3DDevice->DrawIndexedPrimitive(D3DPT_LINELIST,0,0,8,0,12);
}

Here is a Screenshot of the result that is being produced : http://img683.imageshack.us/img683/2509/bbrenderproblem.png Ive been going over the code for a while now and i cant work out what is going wrong, The only difference i can see from my code and the original XNA example linked above is that it uses
DrawUserIndexedPrimitives

however there is no such function in Direct3D so i am using DrawIndexedPrimitives, im not sure if there is any different with these functions but it appears that XNA has both of them. I expect the problem is something quite simple but i cant see if right now, if anyone could help me out here it would be great. Thanks, Magnumwolf

##### Share on other sites
I think there's nothing wrong with the Direct3D rendering code. I think the order of the vertices is not correct. I don't know which corner BoundingBox.GetCorner(i) returns.

##### Share on other sites
I thought that might be the problem but ive played around with giving it different corners and it will change the directions of some of the drawn lines but it still wont draw from more than 1 point

##### Share on other sites
Hi

It looks to me as if you have created a 16 bit index buffer where each index has a stride of 16 bit, obviously. Then you lock the index buffer and store the buffer's memory pointer as an int pointer. No problem though, but when you are indexing your m_indices like an array then you index it as 32 bit integer array (int has most likely 32 bit stride).

So m_index[1] is actually accessing the fourth index in the index buffer's memory because each array position really has only 16 bit. You should define m_index as short to make it do what you think you are already doing.

##### Share on other sites
Arh yes thats it! Thanks so much, always something so simple ><

##### Share on other sites
Each programmer hates errors like that one, we all have been through that and its quite easy to waste a day looking at your code without seeing that [grin]

• 9
• 11
• 17
• 11
• 13