I know there is a function in D3D that transforms the texture coordinate:
d3dDevice->SetTransform( D3DTS_TEXTURE0, &matrix );
The problem is how I could get the matrix. For example, I now have texture offset, scale, rotation, and brightness.开发者_JAVA技巧 How should I set that matrix?
Transforms. There are links to the D3DX Utility Library methods on that page.
Let us consider you have collection of images of same width(txTxWidth) and height(txTxHight) in a texture atlas of width (txFullWidth) and height(txFullHeight).
step 1: Build a 4x4 scale matrix of size equal to an image cell in a texture atlas.
D3DXMatrixScaling(&matScale, txTxWidth/txFullWidth, txTxHeight/txFullHeight, 1.0f);
step 2: Build a 4x4 translation matrix using the (x,y) offset and the image cell width and height
D3DXMATRIX matTrans;
matTrans._13 = x / txFullWidth; // X origin
matTrans._23 = y / txFullHeight; // Y origin
matTrans._31 = txTxWidth / txFullWidth; // Width;
matTrans._32 = txTxHeight / txFullHeight; // Height
step: 4: Don't forgot to transpose the translation matrix
D3DXMATRIX trpos_matTrans;
D3DXMatrixTranspose(&trpos_matTrans,&matTrans);
step 3: Build a 4x4 rotation matrix according to your requirement.
D3DXMatrixRotationX(&matRot, D3DXToRadian(0.0f));
step 4: Multiplication
matix = matScale * matRot * trpos_matTrans ; // SRT
step 5: Set the Texture Transform
device->SetTransform(D3DTS_TEXTURE0, &matrix);
step 6: Set the Texture Stage 0
device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
Hope this will solve your propblem. Enjoy !!!
精彩评论