|
|
TCI里边要求程序员自己创建转换矩阵
这个转换矩阵是否也根据仿射变换的原理得出?
给个实例,请解释一下,谢谢
// If D3DPTEXTURECAPS_PROJECTED is set, projected textures are computed
// per pixel, so this sample will work fine with just a quad for the water
// model. If it's not set, textures are projected per vertex rather than
// per pixel, so distortion will be visible unless we use more vertices.
if( m_d3dCaps.TextureCaps & D3DPTEXTURECAPS_PROJECTED && !m_bUseVertexShader)
{
m_n = 2; // Number of vertices in the ground grid along X
m_m = 2; // Number of vertices in the ground grid along Z
}
else
{
m_n = 8; // Number of vertices in the ground grid along X
m_m = 8; // Number of vertices in the ground grid along Z
}
m_nTriangles = (m_n-1)*(m_m-1)*2; // Number of triangles in the ground
// Create a square grid m_n*m_m for rendering the water
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_nTriangles*3*sizeof(VERTEX),
D3DUSAGE_WRITEONLY, VERTEX::FVF,
D3DPOOL_MANAGED, &m_pWaterVB, NULL ) ) )
return E_FAIL;
m_pWaterVB->Lock( 0, 0, (void**)&v, 0 );
float dX = 2000.0f/(m_n-1);
float dZ = 1250.0f/(m_m-1);
float x0 = -1000;
float z0 = -1250;
float dU = 1.0f/(m_n-1);
float dV = 0.7f/(m_m-1);
UINT k = 0;
for (UINT z=0; z < (m_m-1); z++)
{
for (UINT x=0; x < (m_n-1); x++)
{
v[k].p = D3DXVECTOR3(x0 + x*dX, 0.0f, z0 + z*dZ );
v[k].tu = x*dU;
v[k].tv = z*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + x*dX, 0.0f, z0 + (z+1)*dZ );
v[k].tu = x*dU;
v[k].tv = (z+1)*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + (x+1)*dX, 0.0f, z0 + (z+1)*dZ );
v[k].tu = (x+1)*dU;
v[k].tv = (z+1)*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + x*dX, 0.0f, z0 + z*dZ );
v[k].tu = x*dU;
v[k].tv = z*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + (x+1)*dX, 0.0f, z0 + (z+1)*dZ );
v[k].tu = (x+1)*dU;
v[k].tv = (z+1)*dV;
k++;
v[k].p = D3DXVECTOR3(x0 + (x+1)*dX, 0.0f, z0 + z*dZ );
v[k].tu = (x+1)*dU;
v[k].tv = z*dV;
k++;
}
}
m_pWaterVB->Unlock();
////////////////////////////////////////////////////////////////////////////////////////////
关键就是这个注视不太理解(数学意义)
// Set up projected texture coordinates
// tu = (0.8x + 0.5z) / z
// tv = (0.8y - 0.5z) / z
////////////////////////////////////////////////////////////////////////////////////////////
D3DXMATRIXA16 mat;
mat._11 = 0.8f; mat._12 = 0.0f; mat._13 = 0.0f;
mat._21 = 0.0f; mat._22 = 0.8f; mat._23 = 0.0f;
mat._31 = 0.5f; mat._32 =-0.5f; mat._33 = 1.0f;
mat._41 = 0.0f; mat._42 = 0.0f; mat._43 = 0.0f;
m_pd3dDevice->SetTransform( D3DTS_TEXTURE1, &mat );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3|D3DTTFF_PROJECTED );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION | 1 );
|
|