Sign in to follow this  

Rendering Bounding boxes [Direct3D]

This topic is 2851 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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

This topic is 2851 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.

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