Archived

This topic is now archived and is closed to further replies.

D3DXCleanMesh

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

Hello, I can''t for the life on me get D3DXCleanMesh to work. Here is the code:
  
LPD3DXMESH tmpMesh = NULL;
DWORD *Adj; //Adjanceny information

HRESULT hr;

//Create a mew mesh 

if ( FAILED ( D3DXCreateMeshFVF( 
	Indices.size(), utVerts.size(), 
	D3DXMESH_DYNAMIC, D3DFVF_CUSTOMVERTEX,
	ft_App()->g_pd3dDevice, &tmpMesh) ) )
{
	MessageBox(ft_App()->hWnd, "Failed to create mesh FVF", name, MB_OK);
	return;

}

//Input the vertices 

CUSTOMVERTEX *pVert;
if ( SUCCEEDED ( tmpMesh->LockVertexBuffer( D3DLOCK_NOOVERWRITE,(BYTE **) &pVert) ) ) {

	// now copy our vertex information into the vertex buffer 

	for (i = 0; i < utVerts.size(); i++)
	{
		pVert[i] = utVerts[i];
	}

	tmpMesh->UnlockVertexBuffer();
	//tmpMesh->GetVertexBuffer(&vb);

}
else
{
	MessageBox(ft_App()->hWnd, "Failed to lock vertex buffer", name, MB_OK);
	return;
}

//Input the indices 

WORD *pInd;
if ( SUCCEEDED ( tmpMesh->LockIndexBuffer( D3DLOCK_NOOVERWRITE,(BYTE **) &pInd) ) ) {

	// copy over our index information into our index buffer 

	for (i = 0; i < Indices.size(); i++)
	{
		pInd[i] = Indices[i];
	}

	tmpMesh->UnlockIndexBuffer();
	//tmpMesh->GetIndexBuffer(&ib);

}
else
{
	MessageBox(ft_App()->hWnd, "Failed to lock index buffer", name, MB_OK);
	return;
}


//Create adjancency buffer 

DWORD *Adj = new DWORD[3 * tmpMesh->GetNumFaces() * sizeof(DWORD)];	//Do we really need sizeof(DWORD)?

//Get adjacency information

if ( tmpMesh->GenerateAdjacency(0.0, Adj) == D3D_OK ) {
	//SUCCESS

	//MessageBox(ft_App()->hWnd, "Able to create adjacency information", "YO", MB_OK);	

}
else {
	MessageBox(ft_App()->hWnd, "Failed on GenerateAdjacency()", name, MB_OK);
}

//Perform simple cleansing operation on mesh 

LPD3DXMESH tmptmpMesh = NULL; 
hr = D3DXCleanMesh( tmpMesh, Adj, &tmptmpMesh, Adj, NULL );
if (hr == D3D_OK)
{
	MessageBox(ft_App()->hWnd, "Success", "YO", MB_OK);
}
else if (hr == D3DERR_INVALIDCALL)
{
	MessageBox(ft_App()->hWnd, "D3DXCleanMesh invalid call", name, MB_OK);
}
else if (hr == E_OUTOFMEMORY)
{
	MessageBox(ft_App()->hWnd, "Out of memory for D3DXCleanMesh", name, MB_OK);
}
else
{
	MessageBox(ft_App()->hWnd, "D3DXCleanMesh unknown error", name, MB_OK);
}
  
Everything works find upto the point where I try to clean the mesh (I''m trying to get a progressive mesh eventually), but when I call D3DXCleanMesh, I get an "unknown error". I have installed the debug version of directx8.1, but that doesn''t say anything about it. If anyone could supply code that actually generated a progressive mesh, I would be most gratefull. i''ve been stuck on this problem for weeks, and I;ve searches the internet but to no avail (there was one posting on GameDev.net, which gave the following advice: 1. Make sure to have you mesh with indexed vertex buffers. 2. Create a D3DXMesh with your data a. Create a d3dxmesh b. Lock the D3DXmesh vertex data, copy all you vertex into it c. Make the same with the index buffer d. Maker a huge table of DWORDS e. Use this table to generate the adjacency buffer of your d3dxmesh d. Unlock the vetex and index buffers 3. Creata de PMesh a. D3DXGeneratePMesh with the d3dxmesh, and the adjayency buffer that we creted, use parameter D3DXMESHSIMP_VERTEX . b. PMesh->SetNumVertices( 0xffffffff ); PMesh->SetNumFaces( 0xffffffff ); But I can''t get it to work. Thanks for any help James

Share this post


Link to post
Share on other sites
If you are using DX8.0 update to 8.1 and watch the debug output. CleanMesh is very picky. All the way to progressive mesh.


//--------------
// Code from my engine
// For educational purposes
// Wreakon
//--------------

//BYTE *pVertices;
pMesh = NULL;
ID3DXMesh* polMesh = NULL;
ID3DXSPMesh* pPMesh = NULL;

LPD3DXBUFFER poAdj = NULL;
LPD3DXBUFFER m_pAdjacency = NULL;
LPDIRECT3DVERTEXBUFFER8 pVertexBuffer = NULL;
DWORD* pdwFVFDec = new DWORD[MAX_FVF_DECL_SIZE];
sffVertex* pVB = NULL;
BYTE* pIB = NULL;
LPDIRECT3DINDEXBUFFER8 pIBInst = NULL;

/* Stripifying stuff */
ID3DXMesh* tempMeshFVF = NULL;
int vertCtr = 0;
m_bStrip = false;
/* Simplification declarations */
D3DXATTRIBUTEWEIGHTS AttributeWeights;


LPD3DXBUFFER* ErrorMessage = NULL;
/* get the FVF declaration */
D3DXDeclaratorFromFVF(D3DFVF_SFFVERTEX, pdwFVFDec);

/* settings, etc */
m_bRender = false;
mb_ObjChanged = false;
int x;

if (FAILED(D3DXCreateTextureFromFile(m_d3d_Device, "terrain.bmp", &pTexture)))
Exit("Error #1");

/* test code */
//Load3DS("sphere.3ds", 0);
LoadBMPHeightMap(Bitmap, 20.0f, 0.25f);


//------------------
// Overview
// Create a ID3DXMesh Object (D3DXCreateMesh())
// Generate Adjacency (GenerateAdjacency())
// Optimize (OptimizeInPlace())
// D3DXCleanMesh() & D3DValidMesh()
// D3DXComputeNormals()
// Stripify Object (Optionally)
// Load into ID3DXSPMesh (D3DXCreateSPMesh())
// Reduce Polys (ReduceVertices() && ReduceFaces()
// Load into ID3DXPMesh (ClonePMesh())
//------------------


/* load the mesh into ID3DX D3DXMESH_DYNAMIC D3DXMESH_WRITEONLY|-1 -186*/
if (FAILED(D3DXCreateMesh(mi_NumIndices/3, mi_NumVertices, D3DXMESH_MANAGED, pdwFVFDec, m_d3d_Device, &polMesh)))
goto CLEANUP;

/* Create pMesh ID3DXMesh */
if (FAILED(polMesh->LockVertexBuffer(0, (BYTE**)&pVB)) || FAILED(polMesh->LockIndexBuffer(0, &pIB)))
goto CLEANUP;

memcpy(pVB, pData, sizeof(sffVertex)*(polMesh->GetNumVertices()));
memcpy(pIB, m_Idxlst, sizeof(WORD)*(polMesh->GetNumFaces()*3));

/* Unlock the Vertices */
if (FAILED(polMesh->UnlockIndexBuffer()) || FAILED(polMesh->UnlockVertexBuffer()))
goto CLEANUP;

/* Allocate Memory for the m_pAdjacency Buffer */
if (FAILED(D3DXCreateBuffer((polMesh->GetNumFaces())*sizeof(DWORD)*3, &m_pAdjacency)))
goto CLEANUP;

/* generate Adjacency information */
if (FAILED(polMesh->GenerateAdjacency(0.0f, (DWORD*)m_pAdjacency->GetBufferPointer())))
goto CLEANUP;

/* Allocate Memory for the poAdj Buffer */
if (FAILED(D3DXCreateBuffer((polMesh->GetNumFaces())*sizeof(DWORD)*3, &poAdj)))
goto CLEANUP;

/* generate Adjacency information */
if (FAILED(D3DXWeldVertices(polMesh, NULL, (DWORD*)m_pAdjacency->GetBufferPointer(), (DWORD*)poAdj->GetBufferPointer(), NULL, NULL)))
Exit("Failed Welding Vertices.");

/* Re-Allocate Memory for the m_pAdjacency Buffer */
m_pAdjacency->Release();
if (FAILED(D3DXCreateBuffer((polMesh->GetNumFaces())*sizeof(DWORD)*3, &m_pAdjacency)))
goto CLEANUP;

/* Optimize the mesh */
if (FAILED(polMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_VERTEXCACHE, (DWORD*)poAdj->GetBufferPointer(), (DWORD*)m_pAdjacency->GetBufferPointer(), NULL, NULL)))
goto CLEANUP;

/* Re-Allocate Memory for the m_pAdjacency Buffer */
poAdj->Release();
if (FAILED(D3DXCreateBuffer((polMesh->GetNumFaces())*sizeof(DWORD)*3, &poAdj)))
goto CLEANUP;

/* generate Adjacency information */
if (FAILED(D3DXWeldVertices(polMesh, NULL, (DWORD*)m_pAdjacency->GetBufferPointer(), (DWORD*)poAdj->GetBufferPointer(), NULL, NULL)))
Exit("Failed Welding Vertices.");


/* Re-Allocate Memory for the m_pAdjacency Buffer */
m_pAdjacency->Release();
if (FAILED(D3DXCreateBuffer((polMesh->GetNumFaces())*sizeof(DWORD)*3, &m_pAdjacency)))
goto CLEANUP;

/* Clean Mesh */
if FAILED(D3DXCleanMesh(polMesh, (DWORD*)poAdj->GetBufferPointer(), &pMesh, (DWORD*)m_pAdjacency->GetBufferPointer(), ErrorMessage))
goto CLEANUP;

/* generate Adjacency information */
if (FAILED(D3DXWeldVertices(pMesh, NULL, (DWORD*)m_pAdjacency->GetBufferPointer(), (DWORD*)poAdj->GetBufferPointer(), NULL, NULL)))
Exit("Failed Welding Vertices.");

/* validate the mesh */
if(FAILED(D3DXValidMesh(pMesh, (DWORD*)poAdj->GetBufferPointer(), NULL)))
goto CLEANUP;

/* Re-Allocate Memory for the m_pAdjacency Buffer */
poAdj->Release();
if (FAILED(D3DXCreateBuffer((pMesh->GetNumFaces())*sizeof(DWORD)*3, &poAdj)))
goto CLEANUP;

/* Re-Generate Adjacency */
if (FAILED(pMesh->GenerateAdjacency(0.0f, (DWORD*)poAdj->GetBufferPointer())))
goto CLEANUP;

/* generate Adjacency information */
if (FAILED(D3DXWeldVertices(pMesh, NULL, (DWORD*)poAdj->GetBufferPointer(), (DWORD*)m_pAdjacency->GetBufferPointer(), NULL, NULL)))
Exit("Failed Welding Vertices.");

/* Re-Allocate Memory for the m_pAdjacency Buffer - no need */
poAdj->Release();
if (FAILED(D3DXCreateBuffer((pMesh->GetNumFaces())*sizeof(DWORD)*3, &poAdj)))
goto CLEANUP;

/* Compute Normals */
if (FAILED(D3DXComputeNormals(pMesh, (DWORD*)m_pAdjacency->GetBufferPointer())))
{
m_bnormals = true;
goto CLEANUP;
}

AttributeWeights.Position = 1.0;
AttributeWeights.Boundary = 0.0;
AttributeWeights.Normal = 1.0;
AttributeWeights.Diffuse = 0.0;
AttributeWeights.Specular = 0.0;

for (x = 0; x != 8; x++)
AttributeWeights.Tex[x] = 0.0f;

/* Re-Create polMesh */
//polMesh->Release();
//if (FAILED(D3DXCreateMesh(pMesh->GetNumFaces(), pMesh->GetNumVertices(), D3DXMESH_MANAGED, pdwFVFDec, m_d3d_Device, &polMesh)))
//goto CLEANUP;

/* Simplify Mesh */
//if (FAILED(D3DXSimplifyMesh(pMesh, (DWORD*)m_pAdjacency->GetBufferPointer(), AttributeWeights, NULL, 369, D3DXMESHSIMP_VERTEX, &polMesh)))
//goto CLEANUP;

/* Create the ID3DXSPMesh pPMesh */
//if (FAILED(D3DXCreateSPMesh(polMesh, (DWORD*)m_pAdjacency->GetBufferPointer(), AttributeWeights, NULL, &pPMesh)))
//goto CLEANUP;

/* Reduce Poly Count of ID3DSPMesh */
//x = pPMesh->GetNumFaces();
//for ( int i = 0; i <= x; i++)
//if (FAILED(pPMesh->ReduceVertices(i)))
// goto CLEANUP;

/* Reduce Poly Count of ID3DSPMesh */
//for ( int i = 0; i != x; i++)
//if (FAILED(pPMesh->ReduceFaces(i)))
//goto CLEANUP;

//x = pPMesh->GetNumFaces();
//x = pPMesh->GetMaxFaces();

/* Copy into Terrain Cell, ID3DXPMesh for rendering */
//if (FAILED(pPMesh->ClonePMesh(D3DXMESH_MANAGED, pdwFVFDec, m_d3d_Device, NULL, &TerrainCell)))
//goto CLEANUP;

if (FAILED(D3DXGeneratePMesh(polMesh,(DWORD*)m_pAdjacency->GetBufferPointer(),NULL,
NULL,
1,
D3DXMESHSIMP_FACE,
&TerrainCell)))
goto CLEANUP;

-------
"Programming is like sex make one mistake, and you have to support it forever."
Homepage: www.ussshrike.com/pcwebvia/

Share this post


Link to post
Share on other sites