8,305
社区成员
发帖
与我相关
我的任务
分享
// Get the current world-view matrix
D3DMATRIX matWorld, matView, matWV;
m_pd3dDevice->GetTransform( D3DTRANSFORMSTATE_VIEW, &matView );
m_pd3dDevice->GetTransform( D3DTRANSFORMSTATE_WORLD, &matWorld );
D3DMath_MatrixMultiply( matWV, matWorld, matView );
// Extract world-view matrix elements for speed
FLOAT m11 = matWV._11, m21 = matWV._21, m31 = matWV._31;
FLOAT m12 = matWV._12, m22 = matWV._22, m32 = matWV._32;
FLOAT m13 = matWV._13, m23 = matWV._23, m33 = matWV._33;
// Loop through the vertices, transforming each one and calculating
// the correct texture coordinates.
for( WORD i = 0; i < dwNumVertices; i++ )
{
FLOAT nx = pvVertices[i].nx;
FLOAT ny = pvVertices[i].ny;
FLOAT nz = pvVertices[i].nz;
// Check the z-component, to skip any vertices that face backwards
if( nx*m13 + ny*m23 + nz*m33 > 0.0f )
continue;
// Assign the spheremap's texture coordinates
pvVertices[i].tu = 0.5f * ( 1.0f + ( nx*m11 + ny*m21 + nz*m31 ) );
pvVertices[i].tv = 0.5f * ( 1.0f - ( nx*m12 + ny*m22 + nz*m32 ) );
}
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS,
D3DTTFF_COUNT2 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX,
D3DTSS_TCI_SPHEREMAP );