Sign in to follow this  
flery

Problem with LPD3DXMESH receiving correct VertexCount [solved]

Recommended Posts

Hi, i'm currently trying to render a boundingbox for an *.x file. When accessing the vertexbuffer i read the vertex positions and use D3DXComputeBoundingBox to calculate the boundingbox. In my tests i used a cube and as the function requires the number of vertices i use g_pMesh->GetNumVertices() to receive it. Only problem is the Number is not correct and i dont know which factors to include to correct the value.
void Object::Load(IDirect3DDevice9 *pd3dDevice, char* filename)
{
	LPDIRECT3DVERTEXBUFFER9 pVB;
	LPD3DXBUFFER pD3DXMtrlBuffer;
	D3DXVECTOR3 min, max;
	
	D3DXVECTOR3*  pVertices;
	short*      pIndices;
	

	if( FAILED( D3DXLoadMeshFromX( filename, D3DXMESH_SYSTEMMEM, pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) )
    {
    // If model is not in current folder, try parent folder
    if( FAILED( D3DXLoadMeshFromX( filename, D3DXMESH_SYSTEMMEM, pd3dDevice, NULL, &pD3DXMtrlBuffer, NULL, &g_dwNumMaterials, &g_pMesh ) ) )
        {
            MessageBox(NULL, "Error Loading X File", "Meshes.exe", MB_OK);
            //return E_FAIL;
        }
    }
	D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
	g_pMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];
	g_pMeshTextures  = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];

	for (int i = 0; i < g_dwNumMaterials; i++)
	{
		g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

		g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

		// Create the texture.
		if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, d3dxMaterials[i].pTextureFilename, &g_pMeshTextures[i] ) ) ) {
			g_pMeshTextures[i] = NULL;	
		}
	}
	
	g_pMesh->GetVertexBuffer(&pVB);
	
	pVB->Lock( 0, 0, (void**)&pVertices, 0 );
	
	D3DXComputeBoundingBox((D3DXVECTOR3*) pVertices, g_pMesh->GetNumVertices(), g_pMesh->GetNumBytesPerVertex()-sizeof(D3DXVECTOR3), &min, &max);
		
	bBox.Init(min, max, pd3dDevice);
	char test[1000];
	::OutputDebugString("Min Max Vektoren:\n");
	sprintf(test,"%f %f %f\n", min.x, min.y, min.z);
	::OutputDebugString(test);
	sprintf(test,"%f %f %f\nBytes per Vertex: %i sizeof(float): %i\n\n", max.x, max.y, max.z, g_pMesh->GetNumBytesPerVertex()/4, sizeof(float));
	::OutputDebugString(test);
	for (int i =0; i < g_pMesh->GetNumVertices(); i++)
	{
		sprintf(test, "%i/%i x:%f y:%f z:%f\n", i, g_pMesh->GetNumVertices(), pVertices[0].x, pVertices[0].y, pVertices[0].z);
		pVertices += g_pMesh->GetNumBytesPerVertex()/4;
		::OutputDebugString(test);
	}

	pVB->Unlock();

	pD3DXMtrlBuffer->Release();
}




with the following debug output: Min Max Vektoren: -5.000000 -5.000000 -5.000000 15997507938632555000000000000000000.000000 76830814256935743000000000000000.000000 76818434856542890000000000000000.000000 Bytes per Vertex: 17 sizeof(float): 4 0/34 x:-5.000000 y:10.000000 z:5.000000 1/34 x:5.000000 y:0.000000 z:5.000000 2/34 x:5.000000 y:10.000000 z:5.000000 3/34 x:5.000000 y:0.000000 z:-5.000000 4/34 x:5.000000 y:10.000000 z:-5.000000 5/34 x:-5.000000 y:0.000000 z:-5.000000 6/34 x:5.000000 y:10.000000 z:5.000000 7/34 x:-5.000000 y:10.000000 z:-5.000000 8/34 x:5.000000 y:0.000000 z:5.000000 9/34 x:-5.000000 y:0.000000 z:-5.000000 10/34 x:-5.000000 y:10.000000 z:-5.000000 11/34 x:-5.000000 y:0.000000 z:-5.000000 12/34 x:0.000000 y:0.000000 z:0.000000 13/34 x:0.000000 y:0.000000 z:0.000000 14/34 x:0.000000 y:0.000000 z:0.000000 15/34 x:-0.000000 y:-0.000000 z:-158839966806181750000000000000000000000.000000 16/34 x:7153659904.000000 y:0.000000 z:0.000000 17/34 x:-158839966806181750000000000000000000000.000000 y:0.000000 z:0.000000 18/34 x:0.000000 y:0.000000 z:0.000000 19/34 x:0.000000 y:0.000000 z:0.000000 20/34 x:0.000000 y:0.000000 z:-0.000000 21/34 x:0.000000 y:0.000000 z:0.000000 22/34 x:0.000000 y:0.000000 z:0.000000 23/34 x:0.002930 y:0.000000 z:0.000000 24/34 x:0.000000 y:12583071.000000 z:0.000000 25/34 x:0.000000 y:0.000000 z:3145768.250000 26/34 x:0.000000 y:0.000000 z:0.000000 27/34 x:0.000000 y:0.000000 z:0.000000 28/34 x:0.000000 y:0.000000 z:0.000000 29/34 x:-158839966806181750000000000000000000000.000000 y:-158839966806181750000000000000000000000.000000 z:-158839966806181750000000000000000000000.000000 30/34 x:-158839966806181750000000000000000000000.000000 y:-158839966806181750000000000000000000000.000000 z:-158839966806181750000000000000000000000.000000 31/34 x:-158839966806181750000000000000000000000.000000 y:-158839966806181750000000000000000000000.000000 z:-158839966806181750000000000000000000000.000000 32/34 x:-158839966806181750000000000000000000000.000000 y:-158839966806181750000000000000000000000.000000 z:-158839966806181750000000000000000000000.000000 33/34 x:-158839966806181750000000000000000000000.000000 y:-158839966806181750000000000000000000000.000000 z:-158839966806181750000000000000000000000.000000 the problem is as you can see there should only be a vertex count of 8 as my cube consists only of 8 vertices. (the data for the first 8 vertices is correct) any hints on how to calculate the negative offset to make in that case an 8 out of the 34 (which is the vertexcount received by g_pMesh->GetNumVertices()). thanks flery [Edited by - flery on May 3, 2006 8:46:52 AM]

Share this post


Link to post
Share on other sites
You're assuming that your incoming mesh is of a D3DFVF_XYZ vertex format. Generally speaking, assuming anything about external data is at best risky [wink]

Quote:
Bytes per Vertex: 17
Now 17 bytes per vertex is not the same as the 12 bytes/vertex you'd expect from a standard float3/D3DXVECTOR3...

Thus you're probably just stepping through a binary chunk of data and reading in effectively random position data...

Either decode the vertex declaration (easier and more reliable than an FVF) and/or use ::CloneMesh() or CloneMeshFVF() to get the data into a format that suits your algorithms.

hth
Jack

Share this post


Link to post
Share on other sites
For me i didn't minus the sizeof(D3DXVECTOR3) in the stride input of D3DXComputeBoundingBox, and it worked ok for me.

Share this post


Link to post
Share on other sites
Quote:
You're assuming that your incoming mesh is of a D3DFVF_XYZ vertex format.

actually i'm not. i know that the first 12 bytes are XYZ and everything that comes after that is unknown but i know that every vertex is 17 bytes "width". there is nothing wrong with reading the vertex data the problem is the count which without any of my code implementations returns something i dont understand. 17 (bytes per vertex) x 8 (actual vertices) < is what i'd expect. insted i get 17 (bytes per vertex) x 34 (g_pMesh->GetNumVertices()). without considering my code you should get 8 out of g_pMesh->GetNumVertices() not 34 or am i wrong ?

thanks so far

flery

Share this post


Link to post
Share on other sites
hi,
it seems to work now dont know why, didnt change anything relevant (at least i thought so).

thanks anyway
flery

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