为啥在D3D9里面调用SetTransform时就没法显示Texture?

tsingmei 2009-12-19 12:01:50
大家好!
我在开发过程中,遇到这样一个问题,如果我用#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1),则Texture完全可以显示,一点问题都没有。

不过当我想实现用键盘左右键来控制Texture可以向左移动/向右移动时,却发现Texture没有在D3D9的程序中显示。代码如下,这是为啥?还请赐教。

struct CUSTOMVERTEX
{
FLOAT x, y, z;
D3DCOLOR color;
FLOAT tu, tv;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)

HRESULT CGraphicAPIDX9::CreateTexture()
{
assert(m_pTexture == NULL && m_pVB == NULL && m_pIndexVB == NULL);

// 创建纹理
if(FAILED(m_pDevice->CreateTexture(uiDefaultWidgetContentWidth, uiDefaultWidgetContentHeight, 1, D3DUSAGE_DYNAMIC,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pTexture, NULL)))
{
MessageBox(NULL, L"DX9 创建动态纹理失败!", L"XXX", MB_OK);
SAFE_RELEASE(m_pTexture);
return E_FAIL;
}

// 顶点索引
unsigned short MyRectTriList[] =
{
0, 1, 2,
2, 1, 3
};

// 顶点位置
float fStartX,fStartY;
fStartX = 100;
fStartY = 100;
float fWidth, fHeight;
fWidth = uiDefaultWidgetContentWidth - 1.0;
fHeight = uiDefaultWidgetContentHeight - 1.0;

// 顶点位置
CUSTOMVERTEX MyRectVertex[4] =
{
{ 0.0f, 0.0f, 0.0f, 0xffffffff, 0.0f, 0.0f }, // x, y, z, rhw, color, u, v
{ fWidth, 0.0f, 0.0f, 0xffffffff, 1.0f, 0.0f },
{ 0.0f, fHeight, 0.0f, 0xffffffff, 0.0f, 1.0f },
{ fWidth, fHeight, 0.0f, 0xffffffff, 1.0f, 1.0f }
};

if( FAILED(m_pDevice->CreateIndexBuffer(sizeof(MyRectTriList), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIndexVB, NULL)))
{
SAFE_RELEASE(m_pIndexVB);
return E_FAIL;
}

if(FAILED(m_pDevice->CreateVertexBuffer(sizeof(MyRectVertex), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
{
SAFE_RELEASE(m_pVB);
return E_FAIL;
}

VOID* pVertices;
if(FAILED(m_pVB->Lock( 0, sizeof(MyRectVertex), (void**)&pVertices, 0)))
{
SAFE_RELEASE(m_pVB);
return E_FAIL;
}

memcpy( pVertices, MyRectVertex, sizeof(MyRectVertex) );
m_pVB->Unlock();

VOID* pIndexVertices;
if(FAILED(m_pIndexVB->Lock( 0, sizeof(MyRectTriList), (void**)&pIndexVertices, 0)))
{
SAFE_RELEASE(m_pIndexVB);
return E_FAIL;
}

memcpy( pIndexVertices, MyRectTriList, sizeof(MyRectTriList) );
m_pIndexVB->Unlock();

return S_OK;
}

void CGraphicAPIDX9::Render()
{
m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0);

// Render the scene
if( SUCCEEDED(m_pDevice->BeginScene() ))
{
if((m_pVB != NULL) && (m_pIndexVB != NULL) && (m_pTexture != NULL))
{
//World Transformation
D3DXMATRIXA16 mat;
mat._11 = 1.00f; mat._12 = 0.00f; mat._13 = 0.00f; mat._14 = 0.00f;
mat._21 = 0.00f; mat._22 = 1.00f; mat._23 = 0.00f; mat._24 = 0.00f;
mat._31 = 0.00f; mat._32 = 0.00f; mat._33 = 1.00f; mat._34 = 0.00f;
mat._41 = 0.00f; mat._42 = 0.00f; mat._43 = 0.00f; mat._44 = 1.00f;
m_pDevice->SetTransform(D3DTS_WORLD, &mat);

D3DXVECTOR3 vEyePt( 0.0f, 0.0f, 5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
m_pDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
m_pDevice->SetTransform( D3DTS_PROJECTION, &matProj );

m_pDevice->SetTexture(0, m_pTexture);
m_pDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);

m_pDevice->SetStreamSource(0, m_pVB, 0, sizeof(CUSTOMVERTEX));
//Sets the current vertex stream declaration. Flexible Vertex Format. Called SetVertexShader() in DX8
m_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
m_pDevice->SetIndices(m_pIndexVB);
m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
}
m_pDevice->EndScene();
}
}
...全文
198 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ricmaomao 2009-12-19
WINDOWS API 创建的窗口大小不对,因为标题栏有高度,边框有宽度,不是你创建的024 * 768的窗口就是实际显示大小,这个SIZE包含了边框和标题的SIZE,所以你显示出来的区域实际不是1024 * 768。
解决办法是创建窗口的时候宽高要包含标题栏和边框。
  • 打赏
  • 举报
回复
tsingmei 2009-12-19
现在可以显示了,如果让Texture可以1:1的显示在程序里面啊?
// 顶点位置
CUSTOMVERTEX MyRectVertex[4] =
{
{ -1.0f, -1.0f, 0.0f, 0xffffffff, 1.0f, 1.0f }, // x, y, z, color, u, v
{ 1.0f, -1.0f, 0.0f, 0xffffffff, 0.0f, 1.0f },
{ -1.0f, 1.0f, 0.0f, 0xffffffff, 1.0f, 0.0f },
{ 1.0f, 1.0f, 0.0f, 0xffffffff, 0.0f, 0.0f }
};

if( SUCCEEDED(m_pDevice->BeginScene() ))
{
DWORD LIGHTING = TRUE;
m_pDevice->GetRenderState(D3DRS_LIGHTING, &LIGHTING);
if((m_pVB != NULL) && (m_pIndexVB != NULL) && (m_pTexture != NULL))
{
//World Transformation 让Texture不会跟着旋转物体一起旋转
D3DXMATRIXA16 mat;
mat._11 = 1.00f; mat._12 = 0.00f; mat._13 = 0.00f; mat._14 = 0.00f;
mat._21 = 0.00f; mat._22 = 1.00f; mat._23 = 0.00f; mat._24 = 0.00f;
mat._31 = 0.00f; mat._32 = 0.00f; mat._33 = 1.00f; mat._34 = 0.00f;
mat._41 = 0.00f; mat._42 = 0.00f; mat._43 = 0.00f; mat._44 = 1.00f;
m_pDevice->SetTransform(D3DTS_WORLD, &mat);

D3DXVECTOR3 vEyePt( 0.0f, 0.0f, 5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
m_pDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
m_pDevice->SetTransform( D3DTS_PROJECTION, &matProj );

m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);// Turn off D3D lighting
m_pDevice->SetTexture(0, m_pTexture);

m_pDevice->SetStreamSource(0, m_pVB, 0, sizeof(CUSTOMVERTEX));
//Sets the current vertex stream declaration. Flexible Vertex Format. Called SetVertexShader() in DX8
m_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
m_pDevice->SetIndices(m_pIndexVB);
m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
}
m_pDevice->EndScene();
m_pDevice->SetRenderState(D3DRS_LIGHTING, LIGHTING);
}

这里坐标和变换矩阵怎么改才能让Texture 1:1显示?
  • 打赏
  • 举报
回复
znanznan 2009-12-19
没关LIGHTING,或者和你的纹理坐标变换有关
  • 打赏
  • 举报
回复
znanznan 2009-12-19
是不是摄像机跑到texture应用矩形的后面去了
  • 打赏
  • 举报
回复
发帖
游戏开发

8161

社区成员

游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
帖子事件
创建了帖子
2009-12-19 12:01
社区公告
暂无公告