📄 ch17p2_wipetransition.cpp
字号:
#include "Ch17p2_WipeTransition.h"
CWipeTransition::CWipeTransition(LPDIRECT3DDEVICE8 pDev, LPDIRECT3DTEXTURE8 pOrigTexture,
LPDIRECT3DTEXTURE8 pSecondImage, WipeDir direction)
{
m_pSecondImage = pSecondImage;
m_pd3dDevice = pDev;
m_fFadeTime = 0;
m_pOrigImage = pOrigTexture;
m_Direction = direction;
if (direction == Random) {
switch (RandomNumber(0, 3)) {
case 0: m_Direction = Left; break;
case 1: m_Direction = Right; break;
case 2: m_Direction = Up; break;
case 3: m_Direction = Down; break;
}
}
// create vertex buffer for one quad
if (FAILED(CreateQuad(&m_pVB, D3DPOOL_MANAGED, 2, 0, pDev))) {
throw("Error creating vertex buffer!");
}
}
CWipeTransition::~CWipeTransition()
{
SAFE_RELEASE(m_pVB);
}
void CWipeTransition::DoTransition(float fTransTime)
{
m_fFadeTime += fTransTime;
}
void CWipeTransition::CalcQuadVerts()
{
HRESULT hr;
VERTEX_XYZ_DIFFUSE_TEX1* pVertices;
float fRatio = m_fFadeTime / m_fDuration;
float fNewVertX1, fNewVertY1, fNewVertX2, fNewVertY2;
float fNewU1, fNewV1, fNewU2, fNewV2;
switch(m_Direction) {
case Up:
fNewVertX1 = -1.0f; fNewVertX2 = 1.0f; fNewVertY2 = -1.0f;
fNewVertY1 = -1.0f + (2.0f * fRatio);
fNewU1 = 0.0f; fNewU2 = 1.0f; fNewV1 = 1.0f - (1.0f * fRatio); fNewV2 = 1.0f;
break;
case Down:
fNewVertX1 = -1.0f; fNewVertX2 = 1.0f; fNewVertY1 = 1.0f;
fNewVertY2 = 1.0f - (2.0f * fRatio);
fNewU1 = 0.0f; fNewU2 = 1.0f; fNewV1 = 0.0f; fNewV2 = 1.0f * fRatio;
break;
case Left:
fNewVertY1 = 1.0f; fNewVertY2 = -1.0f; fNewVertX1 = -1.0f;
fNewVertX2 = -1.0f + (2.0f * fRatio);
fNewV1 = 0.0f; fNewV2 = 1.0f; fNewU1 = 0.0f; fNewU2 = 1.0f * fRatio;
break;
case Right:
fNewVertY1 = 1.0f; fNewVertY2 = -1.0f; fNewVertX2 = 1.0f;
fNewVertX1 = 1.0f - (2.0f * fRatio);
fNewV1 = 0.0f; fNewV2 = 1.0f; fNewU1 = 1.0f - (1.0f * fRatio); fNewU2 = 1.0f;
break;
};
if( FAILED( hr = m_pVB->Lock( 0, 6*sizeof(VERTEX_XYZ_DIFFUSE_TEX1), (BYTE**)&pVertices, 0 ) ) )
return;
pVertices[0].position = D3DXVECTOR3(fNewVertX1, fNewVertY1, 0.0f);
pVertices[0].tu = fNewU1; pVertices[0].tv = fNewV1;
pVertices[1].position = D3DXVECTOR3(fNewVertX2, fNewVertY1, 0.0f);
pVertices[1].tu = fNewU2; pVertices[1].tv = fNewV1;
pVertices[2].position = D3DXVECTOR3(fNewVertX2, fNewVertY2, 0.0f);
pVertices[2].tu = fNewU2; pVertices[2].tv = fNewV2;
pVertices[3].position = D3DXVECTOR3(fNewVertX1, fNewVertY1, 0.0f);
pVertices[3].tu = fNewU1; pVertices[3].tv = fNewV1;
pVertices[4].position = D3DXVECTOR3(fNewVertX2, fNewVertY2, 0.0f);
pVertices[4].tu = fNewU2; pVertices[4].tv = fNewV2;
pVertices[5].position = D3DXVECTOR3(fNewVertX1, fNewVertY2, 0.0f);
pVertices[5].tu = fNewU1; pVertices[5].tv = fNewV2;
if( FAILED( hr = m_pVB->Unlock() ) ) return;
}
void CWipeTransition::ResetQuadVerts()
{
HRESULT hr;
VERTEX_XYZ_DIFFUSE_TEX1* pVertices;
if( FAILED( hr = m_pVB->Lock( 0, 6*sizeof(VERTEX_XYZ_DIFFUSE_TEX1), (BYTE**)&pVertices, 0 ) ) )
return;
// first triangle
pVertices[0].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
pVertices[0].color = 0;
pVertices[0].tu = 0.0f;
pVertices[0].tv = 0.0f;
pVertices[1].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
pVertices[1].color = 0;
pVertices[1].tu = 1.0f;
pVertices[1].tv = 0.0f;
pVertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
pVertices[2].color = 0;
pVertices[2].tu = 1.0f;
pVertices[2].tv = 1.0f;
// second triangle
pVertices[3].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
pVertices[3].color = 0;
pVertices[3].tu = 0.0f;
pVertices[3].tv = 0.0f;
pVertices[4].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
pVertices[4].color = 0;
pVertices[4].tu = 1.0f;
pVertices[4].tv = 1.0f;
pVertices[5].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
pVertices[5].color = 0;
pVertices[5].tu = 0.0f;
pVertices[5].tv = 1.0f;
if( FAILED( hr = m_pVB->Unlock() ) ) return;
}
void CWipeTransition::DoRender(D3DXMATRIX mFinalScale)
{
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
// set our texture active...
m_pd3dDevice->SetTexture( 0, m_pOrigImage );
// set up our texture stages for a simple texture copy...
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
// draw our quad
ResetQuadVerts();
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX_XYZ_DIFFUSE_TEX1) );
m_pd3dDevice->SetVertexShader( D3DFVF_XYZ_DIFFUSE_TEX1 );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
if (m_fFadeTime > 0.0f) {
// put the correct colors into the quad's verts
CalcQuadVerts();
m_pd3dDevice->SetTexture( 0, m_pSecondImage );
m_pd3dDevice->SetStreamSource(0, m_pVB, sizeof(VERTEX_XYZ_DIFFUSE_TEX1));
m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -