Sign in to follow this  
Hanchy

Get Vertex's Normal

Recommended Posts

I have load a .x file, and I have also generated the normals of vertices. My problem is that I don't know how to get them. Can you help me?

Share this post


Link to post
Share on other sites
A dead simple way is thus:

1. Create a temporary ID3DXMesh
2. Use ID3DXMesh::CloneFVF( D3DFVF_NORMAL ) to clone your .X file based mesh to the temporary one.
3. Use ID3DXMesh::LocVertexBuffer() to gain access to the temporary mesh's data
4. read out all the data, you will have a stream of D3DXVECTOR3's - each of which is the normal for the corresponding vertex.

Because of the cloning/locking it's not the most efficient method, but it'll be plenty good enough for load-time usage.

hth
Jack

Share this post


Link to post
Share on other sites
Just because it's more interesting than my day-job, here's some code off the top of my head. I don't have a compiler to run it through, so no guarantees that it's perfect [smile]


//we'll assume 'mesh' is your original, fully loaded, LPD3DXMESH object
//and that 'device' is your active LPDIRECT3DDEVICE9 object
ID3DXMesh *temporary_mesh;
if( FAILED( mesh->CloneMeshFVF( mesh->GetOptions(), D3DFVF_NORMAL, device, &temporary_mesh ) ) )
{
//hmm, something not right here - possibly your source mesh doesn't
//have the correct information!
}

D3DXVECTOR3 *pNormals = NULL;

if( FAILED( temporary_mesh->LockVertexBuffer( 0, reinterpret_cast< void* >( &pNormals ) ) ) )
{
//couldn't lock the vertex buffer...?!
}

//From here, you should have 'pNormals' containing 'temporary_mesh->GetNumVertices()' elements.




hth
Jack

Share this post


Link to post
Share on other sites
Thanks!
I've already do it. my code is this:

DWORD FVF = g_pWireMesh->GetFVF();
DWORD dwVertSize = D3DXGetFVFVertexSize( FVF );
VOID* pVB = NULL;
if ( SUCCEEDED( g_pWireMesh->LockVertexBuffer( D3DLOCK_DISCARD, &pVB ) ) ) {
DWORD dwNumVertice = g_pWireMesh->GetNumVertices();
for ( DWORD i=0; i < dwNumVertice; ++i ) {
D3DXVECTOR3 *pvPtr = (D3DXVECTOR3 *) pVB;
pvPtr->
}
}
g_pWireMesh->UnlockVertexBuffer();


Can you give me more details??
Thanks a lot.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hanchy
I've already do it. my code is this:
<--snip-->
Can you give me more details??

Details on what exactly?? [smile] does that code you've written not work then?

Cheers,
Jack

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