为什么我的DirectX纹理坐标变换的“平移”会失效?
想做一个BillBoard的纹理坐标变换,出现了奇怪的结果,那就是缩放,旋转变换都正常,就是平移没有效,请高人们帮帮看看,谢谢!
这是我用来测试纹理坐标变换的代码:
(红色部分为关键代码)
#include "d3dUtility.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
IDirect3DVertexBuffer9* Quad = 0;
IDirect3DTexture9* Tex = 0;
//
// Classes and Structures
//
struct Vertex
{
Vertex(){}
Vertex(
float x, float y, float z,
/*float nx, float ny, float nz,*/
float u, float v)
{
_x = x; _y = y; _z = z;
/*_nx = nx; _ny = ny; _nz = nz;*/
_u = u; _v = v;
}
float _x, _y, _z;
/*float _nx, _ny, _nz;*/
float _u, _v; // texture coordinates
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_TEX1;
//
// Framework Functions
//
bool Setup()
{
//
// Create the quad vertex buffer and fill it with the
// quad geoemtry.
//
Device->CreateVertexBuffer(
6 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&Quad,
0);
Vertex* v;
Quad->Lock(0, 0, (void**)&v, 0);
// quad built from two triangles, note texture coordinates:
// v[0] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -2.0f, 0.0f, 2.0f);
// v[1] = Vertex(-1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -2.0f, 0.0f, 0.0f);
// v[2] = Vertex( 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -2.0f, 2.0f, 0.0f);
//
// v[3] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -2.0f, 0.0f, 2.0f);
// v[4] = Vertex( 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -2.0f, 2.0f, 0.0f);
// v[5] = Vertex( 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -2.0f, 2.0f, 2.0f);
v[0] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 2.0f);
v[1] = Vertex(-1.0f, 1.0f, 1.25f, 0.0f, 0.0f);
v[2] = Vertex( 1.0f, 1.0f, 1.25f, 2.0f, 0.0f);
v[3] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 2.0f);
v[4] = Vertex( 1.0f, 1.0f, 1.25f, 2.0f, 0.0f);
v[5] = Vertex( 1.0f, -1.0f, 1.25f, 2.0f, 2.0f);
Quad->Unlock();
//
// Create the texture and set filters.
//
D3DXCreateTextureFromFile(
Device,
"dx5_logo.bmp",
&Tex);
Device->SetTexture(0, Tex);
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
//
// Don't use lighting for this sample.
//
Device->SetRenderState(D3DRS_LIGHTING, false);
//
// Set the projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(Quad);
d3d::Release<IDirect3DTexture9*>(Tex);
}
bool Display(float timeDelta)
{
if( Device )
{
if( ::GetAsyncKeyState('W') & 0x8000f )
{
Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
}
// set border color address mode
if( ::GetAsyncKeyState('B') & 0x8000f )
{
Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
Device->SetSamplerState(0, D3DSAMP_BORDERCOLOR, 0x000000ff);
}
// set clamp address mode
if( ::GetAsyncKeyState('C') & 0x8000f )
{
Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
}
// set mirror address mode
if( ::GetAsyncKeyState('M') & 0x8000f )
{
Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR);
Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////用来测试纹理变换的代码 ///////////////////////////////////////////////////
static float clock = 0;
clock += timeDelta;
D3DXMATRIX MatTexture, MatScale, MatTrans, MatRotate;
D3DXMatrixIdentity(&MatTexture);
D3DXMatrixTranslation(&MatTrans, clock, 0, 1.0f);
D3DXMatrixScaling(&MatScale, 5.0f, 1.0f, 1.0f);
D3DXMatrixRotationZ(&MatRotate, D3DX_PI * 0.3f);
MatTexture = MatScale * MatTrans;
Device->SetTransform(D3DTS_TEXTURE0, &MatTrans);// 无效
//Device->SetTransform(D3DTS_TEXTURE0, &MatScale);// 有效
//Device->SetTransform(D3DTS_TEXTURE0, &MatRotate);// 有效
Device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
Device->SetStreamSource(0, Quad, 0, sizeof(Vertex));
Device->SetFVF(Vertex::FVF);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}
[解决办法]
这是因为你用3维平移矩阵变化2维坐标,而二维坐标的第4个分量是0,所以平移的项总是不起作用,像我这样构造2维的平移矩阵就可以了
static float clock = 0; clock += 0.05; D3DXMATRIX MatTexture, MatScale, MatTrans, MatRotate; D3DXMatrixIdentity(&MatTexture); D3DXMatrixIdentity(&MatTrans); MatTrans._31 = clock; //u offset MatTrans._32 = 0.5; //v offset //D3DXMatrixTranslation(&MatTrans, clock, 0.5f, 1.0f); D3DXMatrixScaling(&MatScale, 5.0f, 1.0f, 1.0f); D3DXMatrixRotationZ(&MatRotate, D3DX_PI * 0.3f * clock); MatTexture = MatScale * MatTrans; g_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &MatTrans); // 无效 //g_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &MatScale); // 有效 //g_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &MatRotate); // 有效 g_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);