Sign in to follow this  
thuong

How to calculate vertex normal

Recommended Posts

thuong    100
Hi All, I have problem with vertex normal calculate, anybody have exprience pls help me. Thanks very much. I load data from .X file to ID3DXMesh object each mesh have own ID3DXMesh object. I try to change vertices of ID3DXMesh object by LockVertexBuffer, following is my code: float *DestPtr; MeshPtr->MeshData.pMesh->LockVertexBuffer(0, (void**)&DestPtr); DWORD i; for(i = 0; i < MeshPtr->MeshData.pMesh->GetNumVertices(); i++) { *DestPtr = New_Vertex.x; *(DestPtr+1) = New_Vertex.y; *(DestPtr+2) = New_Vertex.z; DestPtr += MeshPtr->m_inumElementsPerVertex; // MeshPtr->m_inumElementsPerVertex = 6, mesh have not texture // *(DestPtr+3) is vertex normal x // *(DestPtr+4) is vertex normal y // *(DestPtr+5) is vertex normal z // MeshPtr->m_inumElementsPerVertex = 8, mesh have texture // *(DestPtr+3) is vertex normal x // *(DestPtr+4) is vertex normal y // *(DestPtr+5) is vertex normal z // *(DestPtr+6) is texture coord U // *(DestPtr+7) is texture coord V } MeshPtr->MeshData.pMesh->UnlockVertexBuffer(); Following is code for face rearranging of mesh: // Replace Face WORD * pInd; DWORD numFaces = MeshPtr->MeshData.pMesh->GetNumFaces(); MeshPtr->MeshData.pMesh->LockIndexBuffer(0, (void**)&pInd); for(i = 0; i < numFaces; i++) { for(int j = 0; j < 3; j++) { *pInd = New_Array_Triangles[i].Vertex[j]; pInd++; } } // Calculate Adjaceny and Vertex Normal agian DWORD* adjacency = new DWORD[numFaces*3]; memset(adjacency,0,sizeof(DWORD) * 3 * numFaces); MeshPtr->MeshData.pMesh->GenerateAdjacency(1.0, adjacency); memcpy(MeshPtr->pAdjacency, adjacency, sizeof(DWORD) * 3 * numFaces); D3DXComputeNormals(MeshPtr->MeshData.pMesh, adjacency); MeshPtr->MeshData.pMesh->UnlockIndexBuffer(); Following is capture image of renderring before replace: Image Hosted by ImageShack.us Following is capture image of renderring after replace: Image Hosted by ImageShack.us Thanks very much, David, [Edited by - thuong on July 11, 2007 2:36:07 PM]

Share this post


Link to post
Share on other sites
jollyjeffers    1570
You are, at the very least, trashing your memory.

Your first fragment of code is effectively casting vertex data to be of XYZ position information only. Your later code tries to generate normals, which would require storage for them which would imply that the vertex data contains more than just positions - which conflicts with your earlier code.

Chances are your first piece of code is flawed (to be honest, it's not good code - there are more robust ways of doing things) and you're writing position data to areas allocated for normal vectors, which is both confusing the renderer and the D3DXComputeNormals() function.

Failing that, some more details on your debugging results might be useful.

hth
Jack

Share this post


Link to post
Share on other sites
thuong    100
Hi jollyjeffers,
Thank you very much,

Sorry missing some thing in the my code:

float *DestPtr;
MeshPtr->MeshData.pMesh->LockVertexBuffer(0, (void**)&DestPtr);
DWORD i;
for(i = 0; i < MeshPtr->MeshData.pMesh->GetNumVertices(); i++)
{
*DestPtr = New_Vertex.x;
*(DestPtr+1) = New_Vertex.y;
*(DestPtr+2) = New_Vertex.z;

DestPtr += MeshPtr->m_inumElementsPerVertex;

// MeshPtr->m_inumElementsPerVertex = 6, mesh have not texture
// *(DestPtr+3) is vertex normal x
// *(DestPtr+4) is vertex normal y
// *(DestPtr+5) is vertex normal z

// MeshPtr->m_inumElementsPerVertex = 8, mesh have texture
// *(DestPtr+3) is vertex normal x
// *(DestPtr+4) is vertex normal y
// *(DestPtr+5) is vertex normal z
// *(DestPtr+6) is texture coord U
// *(DestPtr+7) is texture coord V
}
MeshPtr->MeshData.pMesh->UnlockVertexBuffer();

Above is really my code
But with this code it still do not work correctly.

David,

[Edited by - thuong on July 12, 2007 2:34:01 AM]

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