1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
{
HRESULT hr = E_FAIL;
WCHAR sz[MAX_PATH];
// créé le layout du clone mesh
D3D10_INPUT_ELEMENT_DESC CloneMeshDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 36, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 48, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
ID3DX10Mesh* pSourceMesh = g_pCube->m_pMeshDX10;
ID3DX10Mesh* pCloneMesh;
// acquiert description du vb
D3D10_PASS_DESC PassDesc;
V( g_pMeshTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ) );
UINT uiNBElements = sizeof( CloneMeshDesc ) / sizeof( CloneMeshDesc[0] );
DWORD dwNumVertices = g_pCube->m_pMeshDX10->GetVertexCount();
DWORD dwNumIndices = g_pCube->m_pMeshDX10->GetFaceCount()*3;
// créé le input layout pour le mesh cloné
ID3D10InputLayout* CloneMeshlayout;
V( pd3dDevice->CreateInputLayout(
CloneMeshDesc,
uiNBElements,
PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize,
&CloneMeshlayout
) );
// créé un mesh dupliqué avec nouveau layout
V( D3DX10CreateMesh(
pd3dDevice, // device
CloneMeshDesc, // déclaration de format de vertex
uiNBElements, // nombre d'éléments de la déclaration
"POSITION", // sémantique déclarant la position
dwNumVertices, // nombre de vertices
dwNumIndices/3, // nombre de faces
D3DX10_MESH_32_BIT, // options
&pCloneMesh // mesh de destination créé
) );
// acquiert les VB
ID3DX10MeshBuffer* pSrcVB;
V( pSourceMesh->GetVertexBuffer( 0, &pSrcVB ) );
ID3DX10MeshBuffer* pDestVB;
V( pCloneMesh->GetVertexBuffer( 0, &pDestVB ) );
// verrouille les VB src et dest
MESH_VERTEX* pSrcData;
V( pSrcVB->Map( (void**)&pSrcData, &dwNumVertices ) );
BNRMTGT_MESH_VERTEX* pDestData;
V( pDestVB->Map( (void**)&pDestData, &dwNumVertices ) );
// copie les données vertices du mesh source vers mesh destination
for ( int i = 0; i < (int)dwNumVertices; i++ )
{
pDestData[i].position = pSrcData[i].position;
pDestData[i].normal = pSrcData[i].normal;
pDestData[i].texuv = pSrcData[i].texuv;
}
// clone le mesh en calculant tangentes et binormales associées
CloneBnrmTgtMesh( pSourceMesh, pCloneMesh );
// DEBUG output
for ( int i = 0; i < (int)dwNumVertices; i++ )
{
StringCchPrintf( sz, 256, L"pos x:%0.2f, y:%0.2f, z:%0.2f, tex u:%0.2f, v:%0.2f\n", pDestData[i].position.x, pDestData[i].position.y, pDestData[i].position.z,
pDestData[i].texuv.x, pDestData[i].texuv.y );
OutputDebugString( sz );
StringCchPrintf( sz, 256, L"tgt x:%0.2f, y:%0.2f, z:%0.2f, bnrml x:%0.2f, y:%0.2f, z:%0.2f ", pDestData[i].tangent.x, pDestData[i].tangent.y, pDestData[i].tangent.z,
pDestData[i].binormal.x, pDestData[i].binormal.y, pDestData[i].binormal.z );
OutputDebugString( sz );
StringCchPrintf( sz, 256, L" normal x:%0.2f, y:%0.2f, z:%0.2f\n", pDestData[i].normal.x, pDestData[i].normal.y, pDestData[i].normal.z );
OutputDebugString( sz );
}
// libère les VB aprés copie
V( pSrcVB->Unmap() );
V( pDestVB->Unmap() );
// copie l'index buffer du mesh source
ID3DX10MeshBuffer* pSrcIB;
V( pSourceMesh->GetIndexBuffer( &pSrcIB ) );
ID3DX10MeshBuffer* pDestIB;
V( pSourceMesh->GetIndexBuffer( &pDestIB ) );
void* pSrcIBData;
V( pSrcIB->Map( &pSrcIBData, &dwNumIndices ) );
void* pDstIBData;
V( pDestIB->Map( &pDstIBData, &dwNumIndices ) );
memcpy( pDstIBData, pSrcIBData, dwNumIndices );
V( pSrcIB->Unmap() );
V( pDestIB->Unmap() );
return;
}
//--------------------------------------------------------------------------------------
// Nom: CloneBnrmTgtMesh(..)
// Desc: clone un mesh en calculant tangentes et binormales
//--------------------------------------------------------------------------------------
void CloneBnrmTgtMesh( ID3DX10Mesh* pSrcMesh, ID3DX10Mesh* pDestMesh )
{
HRESULT hr = E_FAIL;
int faceCount, i, index;
MESH_VERTEX vertex1, vertex2, vertex3;
D3DXVECTOR3 tangent, binormal, normal;
// nombre de faces du mesh source
DWORD dwNumVertices = pSrcMesh->GetVertexCount();
faceCount = dwNumVertices / 3;
// initialise l'index pour parcourir les vertices du mesh source
index = 0;
ID3DX10MeshBuffer* pSrcVB;
hr = pSrcMesh->GetVertexBuffer( 0, &pSrcVB );
MESH_VERTEX* pSrcData;
hr = pSrcVB->Map( (void**)&pSrcData, &dwNumVertices );
ID3DX10MeshBuffer* pDestVB;
hr = pDestMesh->GetVertexBuffer( 0, &pDestVB );
BNRMTGT_MESH_VERTEX* pDestData;
hr = pDestVB->Map( (void**)&pDestData, &dwNumVertices );
// parcoure les façes et calcule tangente, binormal, et normales
for(i=0; i<faceCount; i++)
{
// acquiert les 3 vertices pour cette face depuis le mesh source
vertex1.position.x = pSrcData[index].position.x;
vertex1.position.y = pSrcData[index].position.y;
vertex1.position.z = pSrcData[index].position.z;
vertex1.texuv.x = pSrcData[index].texuv.x;
vertex1.texuv.y = pSrcData[index].texuv.y;
vertex1.normal.x = pSrcData[index].normal.x;
vertex1.normal.y = pSrcData[index].normal.y;
vertex1.normal.z = pSrcData[index].normal.z;
index++; // vertex suivant
vertex2.position.x = pSrcData[index].position.x;
vertex2.position.y = pSrcData[index].position.y;
vertex2.position.z = pSrcData[index].position.z;
vertex2.texuv.x = pSrcData[index].texuv.x;
vertex2.texuv.y = pSrcData[index].texuv.y;
vertex2.normal.x = pSrcData[index].normal.x;
vertex2.normal.y = pSrcData[index].normal.y;
vertex2.normal.z = pSrcData[index].normal.z;
index++;
vertex3.position.x = pSrcData[index].position.x;
vertex3.position.y = pSrcData[index].position.y;
vertex3.position.z = pSrcData[index].position.z;
vertex3.texuv.x = pSrcData[index].texuv.x;
vertex3.texuv.y = pSrcData[index].texuv.y;
vertex3.normal.x = pSrcData[index].normal.x;
vertex3.normal.y = pSrcData[index].normal.y;
vertex3.normal.z = pSrcData[index].normal.z;
index++;
// calcul tangente et binormale de cette face
ComputeTangentBinormal( vertex1, vertex2, vertex3, tangent, binormal );
// calcul la nouvelle normale en utilisant tangente et binormale
ComputeNormal( tangent, binormal, normal );
// stocke les vecteurs calculés dans le nouveau mesh
pDestData[index-1].normal.x = normal.x;
pDestData[index-1].normal.y = normal.y;
pDestData[index-1].normal.z = normal.z;
pDestData[index-1].tangent.x = tangent.x;
pDestData[index-1].tangent.y = tangent.y;
pDestData[index-1].tangent.z = tangent.z;
pDestData[index-1].binormal.x = binormal.x;
pDestData[index-1].binormal.y = binormal.y;
pDestData[index-1].binormal.z = binormal.z;
pDestData[index-2].normal.x = normal.x;
pDestData[index-2].normal.y = normal.y;
pDestData[index-2].normal.z = normal.z;
pDestData[index-2].tangent.x = tangent.x;
pDestData[index-2].tangent.y = tangent.y;
pDestData[index-2].tangent.z = tangent.z;
pDestData[index-2].binormal.x = binormal.x;
pDestData[index-2].binormal.y = binormal.y;
pDestData[index-2].binormal.z = binormal.z;
pDestData[index-3].normal.x = normal.x;
pDestData[index-3].normal.y = normal.y;
pDestData[index-3].normal.z = normal.z;
pDestData[index-3].tangent.x = tangent.x;
pDestData[index-3].tangent.y = tangent.y;
pDestData[index-3].tangent.z = tangent.z;
pDestData[index-3].binormal.x = binormal.x;
pDestData[index-3].binormal.y = binormal.y;
pDestData[index-3].binormal.z = binormal.z;
}
// libère les VB
V( pSrcVB->Unmap() );
V( pDestVB->Unmap() );
return;
} |
Partager