Jump to content
  • Advertisement
Sign in to follow this  
xujiezhige

CloneMesh()

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


HRESULT CMyD3DApplication::LoadXFile(TCHAR* name)
{
HRESULT hr;
LPD3DXMESH pMeshSysMem = NULL, pMeshSysMem2 = NULL;
if (FAILED (D3DXLoadMeshFromX(name, D3DXMESH_SYSTEMMEM,
m_pd3dDevice, NULL, NULL, NULL, NULL, &pMeshSysMem)))
return E_FAIL;
D3DVERTEXELEMENT9 decl[]=
{
// stream, offset, type, method, semantic type (for example normal), ?
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
D3DDECL_END()
};
hr = pMeshSysMem->CloneMesh(D3DXMESH_MANAGED, decl, m_pd3dDevice, &pMeshSysMem2);
// compute the normals
hr = D3DXComputeNormals(pMeshSysMem2,NULL);
if(FAILED(hr))
return E_FAIL;
// compute U (tangent)
hr = D3DXComputeTangent(pMeshSysMem2,0,0,0,TRUE,NULL);
if(FAILED(hr))
return E_FAIL;
//---------------------------------------------------------------------------
// the HLSL compiler awaits as an input a float4 position
// therefore we first calculate the normals and tangents with a float3 and
// then clone the mesh with a float4 position
//---------------------------------------------------------------------------
D3DVERTEXELEMENT9 decl2[]=
{
// stream, offset, type, method, semantic type (for example normal), ?
{0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
{0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
D3DDECL_END()
};
hr = pMeshSysMem2->CloneMesh(D3DXMESH_MANAGED, decl2, m_pd3dDevice, &m_pD3DXMesh);

// cleanup
SAFE_RELEASE(pMeshSysMem);
SAFE_RELEASE(pMeshSysMem2);
return S_OK;
}


Who can tell me why the writter has to convert the vertex format two times? Why can't i delete the first convertion, just leaving the last one?

Share this post


Link to post
Share on other sites
Advertisement
Obviously, for calculating the normals and tangents, the mesh only supporte float3 as position. However, as the comments suppose, whatever shader the writer used, need float4 as input. I always used float3 as input for both normal-calculation and rendering, so if you configure the shaders/effect right, you could skip the second clone().

Share this post


Link to post
Share on other sites
Thank you, The King2.

Is your meaning the position format for mesh must be float3? Or, the position format has to identical to normal and tangent?

And another question, If the vertex shader needs position with float4, i have to make the position of mesh float4, can't be float3?
I just have written some codes, the position is float3, but the vertex shader input position is float4. The program runs well.

Share this post


Link to post
Share on other sites
I don't know how the ID3DXMesh-Interface really works. I always make position float3, as havin a 4th parameter is useless anyway.

Having a float4 in the shader isn't a problem, even if the input position really is just float3. Anyway, the 4th float will be 1.0f. Having explicitely float3 in the shader just forces you to write float4(in.vPos.xyz,1.0f), otherwise this is done for you.

Regarding the two times cloning in the previous version, I now quess the author didn't really know how to work with the shaders exactly.

Share this post


Link to post
Share on other sites
Thank you, The King2.

You are right. The first time I change the codes without some details modification, so the program crash. Now i skip the second CloneMesh() with relative modification, the program runs well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!