Can CloneMesh() convert one vertex format to another?

Started by
4 comments, last by xujiezhige 12 years, 2 months ago
I load a teapot mesh from .X file. Then I want to add normal data member to vertex format. So I clone a new mesh with new vertex format.
See the following code:

[color=#ff0000]if( FAILED( D3DXLoadMeshFromX( pszFileName, D3DXMESH_SYSTEMMEM, m_pd3dDevice, NULL,
NULL, NULL, NULL, &pMeshTemp ) ) )
return E_FAIL;

[color=#ff0000] D3DVERTEXELEMENT9 decl[]=
{
0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0,
0, 16, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0,
D3DDECL_END()
};

[color=#ff0000] if( FAILED( pMeshTemp->CloneMesh( D3DXMESH_MANAGED, decl, m_pd3dDevice, ppMesh ) ) )
return E_FAIL;

[color=#ff0000]if( FAILED( D3DXComputeNormals( *ppMesh, NULL ) ) )
return E_FAIL;

[color=#000000]The codes provided are problemtic, the program crashes when running.

[color=#000000]But it works when I change [color="#ff0000"]D3DDECLTYPE_FLOAT4 to [color="#ff0000"]D3DDECLTYPE_FLOAT3 [color=#000000]and [color=#ff0000]16 [color=#000000]to [color=#ff0000]12[color=#000000].

[color=#000000]Is the problem related to the x file?
Advertisement
Normals only have 3 floats. So try changing second D3DDECLTYPE_FLOAT4 to FLOAT3 and it should also work. Thats not a problem, because you made a mistake for making Normals float4 in the first place.
Positions also typically have 3 positions, not 4.

Positions also typically have 3 positions, not 4.


Normals only have 3 floats. So try changing second D3DDECLTYPE_FLOAT4 to FLOAT3 and it should also work. Thats not a problem, because you made a mistake for making Normals float4 in the first place.


Thank you!

But we often use 4 float to represent position, just making the last element to 1. So I could use 4 float to contain position ( or normal ).
Can you tell why?
I all depens on your Vertex-Struct and/or FVF. If you use 4 floats there, you can use 4 floats here. Internally DirectX does nothing else than float4(pos.x,pos.y,pos.z,1.0f); so thats why you can theoretically do this. Obivously you defined Position and/or Normal to float3 somewhere else in your code.
Thank you, The king2.

Through my several test, I find CloneMesh() can convert any vertex format to what you want. The reason of program crashing after my first modification is that D3DXComputeNormal() will fail if the normal is float4.

I find some information in Doc.gallery_193491_378_4021.jpg

This topic is closed to new replies.

Advertisement