📄 ch17p1_alphablendfade.cpp
字号:
/*
#############################################################################
Ch17p1_AlphaBlendFade.cpp: a program that demonstrates the fire algorithm,
without any annoying bells and/or whistles.
#############################################################################
*/
// include files ////////////////////////////////////////////////////////////
#define STRICT
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include "D3DHelperFuncs.h"
#include "Ch17p1_resource.h"
// A structure for our custom vertex type.
struct CUSTOMVERTEX
{
D3DXVECTOR3 position; // The position
D3DCOLOR color; // The color
FLOAT tu, tv; // The texture coordinates
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the
// generic functionality needed in all Direct3D samples. CMyD3DApplication
// adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
// Font for drawing text
CD3DFont* m_pFont;
CD3DFont* m_pFontSmall;
// Scene
LPDIRECT3DVERTEXBUFFER8 m_pVB;
DWORD m_dwNumVertices;
// Transforms
D3DXMATRIX m_matPosition;
D3DXMATRIX m_matView;
D3DXMATRIX m_matProj;
// original and scratch textures
LPDIRECT3DTEXTURE8 m_pOrigTexture;
// width and height of texture image
int m_iTextureWidth, m_iTextureHeight;
float m_fFadeTime;
D3DXCOLOR m_FadeToColor;
protected:
void ProcessFeedback();
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
public:
CMyD3DApplication();
};
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("Ch17p1_AlphaBlendFade");
m_bUseDepthBuffer = TRUE;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
m_pFontSmall = new CD3DFont( _T("Arial"), 9, D3DFONT_BOLD );
m_pVB = NULL;
m_dwNumVertices = 6;
m_pOrigTexture = NULL;
m_dwCreationWidth = 512;
m_dwCreationHeight = 384;
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
FLOAT fSecsPerFrame = m_fElapsedTime;
if (m_fFadeTime > 0.0f) {
m_fFadeTime += m_fElapsedTime;
if (m_fFadeTime > 7.0f) m_fFadeTime = 0.0f;
}
return S_OK;
}
void FadeQuadVerts(D3DXCOLOR FadeToColor, float fFadeTime,
float fEffectTime, LPDIRECT3DVERTEXBUFFER8 pVB)
{
HRESULT hr;
CUSTOMVERTEX* pVertices;
// calculate the new color value
FadeToColor.a = FadeToColor.a * (fFadeTime / fEffectTime);
if (FadeToColor.a > 1.0f) FadeToColor.a = 1.0f;
if( FAILED( hr = pVB->Lock( 0, 6*sizeof(CUSTOMVERTEX), (BYTE**)&pVertices, 0 ) ) )
return;
pVertices[0].color = (DWORD)FadeToColor;
pVertices[1].color = (DWORD)FadeToColor;
pVertices[2].color = (DWORD)FadeToColor;
pVertices[3].color = (DWORD)FadeToColor;
pVertices[4].color = (DWORD)FadeToColor;
pVertices[5].color = (DWORD)FadeToColor;
if( FAILED( hr = pVB->Unlock() ) ) return;
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
// Set up an orthagonal projection matrix, so we can render the entire
// texture.
D3DXMATRIX mat;
D3DXMatrixOrthoLH(&mat, (float)m_iTextureWidth, (float)m_iTextureHeight,
0.0, 100.0);
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &mat );
// this world matrix, combined with orthogonal projection, causes the
// texture to completely and exactly fill the rendering surface.
D3DXMATRIX matWorld,matTrans,matScale;
D3DXMatrixScaling(&matScale, (float)m_iTextureWidth/2.0f, (float)m_iTextureHeight/2.0f, 1.0);
// move the quad left and up 0.5 units, so that the texels are perfectly
// centered on the screen pixels.
D3DXMatrixMultiply(&matWorld, &matScale, D3DXMatrixTranslation(&matTrans, -0.5f, -0.5f, 0));
// our matrix is now finished. Tell D3D to use it!
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Clear the backbuffer
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x000000, 1.0f, 0L );
// begin rendering the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
// set our texture active...
m_pd3dDevice->SetTexture( 0, m_pOrigTexture );
// 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
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
if (m_fFadeTime > 0.0f) {
// set up texture stage states for blending the 2nd quad on top
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
// put the correct colors into the quad's verts
FadeQuadVerts(m_FadeToColor, m_fFadeTime, 5.0f, m_pVB);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -