📄 ch13p1_simplefeedback.cpp
字号:
/*
#############################################################################
Ch13p1_SimpleFeedback.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 "Ch13p1_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;
LPDIRECT3DTEXTURE8 m_pScratchTexture;
LPDIRECT3DTEXTURE8 m_pImageTexture;
// width and height of texture image
int m_iTextureWidth, m_iTextureHeight;
float m_fFeedbackRotation;
float m_fFeedbackRotationAccel;
float m_fImageRotation;
float m_fImageRotationAccel;
float m_fFeedbackScale;
float m_fFeedbackScaleAccel;
// vertex buffer for combining scratch texture with original image
LPDIRECT3DVERTEXBUFFER8 m_pScratchVB;
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("Ch13p1_SimpleFeedback");
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_pScratchVB = NULL;
m_dwNumVertices = 6;
m_pOrigTexture = NULL;
m_pScratchTexture = NULL;
m_pImageTexture = NULL;
m_fFeedbackRotation = 0.0f;
m_fFeedbackRotationAccel = 10.0f;
m_fImageRotation = 0.0f;
m_fImageRotationAccel = 30.0f;
m_fFeedbackScale = 10.0f;
m_fFeedbackScaleAccel = 1.0f;
}
//-----------------------------------------------------------------------------
// 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()
{
if (m_fElapsedTime < 0.0001f) {
m_fElapsedTime = 0.0001f;
}
FLOAT fSecsPerFrame = m_fElapsedTime;
// adjust the rotation and scale. The rotation and scale numbers are used
// to determine how much to scale/rotate the original image onto the scratch
// surface.
m_fFeedbackRotation += m_fFeedbackRotationAccel*m_fElapsedTime;
if (m_fFeedbackRotation > 100.0f) m_fFeedbackRotationAccel = -5.0f;
if (m_fFeedbackRotation < -100.0f) m_fFeedbackRotationAccel = 5.0f;
m_fImageRotation += m_fImageRotationAccel*m_fElapsedTime;
if (m_fImageRotation > 100.0f) m_fImageRotationAccel = -100.0f;
if (m_fImageRotation < -100.0f) m_fImageRotationAccel = 100.0f;
m_fFeedbackScale += m_fFeedbackScaleAccel*m_fElapsedTime;
if (m_fFeedbackScale > 95.0f) m_fFeedbackScaleAccel = -10.1f;
if (m_fFeedbackScale < 5.0f) m_fFeedbackScaleAccel = 10.1f;
return S_OK;
}
//-----------------------------------------------------------------------------
// 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()
{
//static int b=0; if (b < 2)
ProcessFeedback();
//b++;
// For our world matrix, we will just leave it as the identity
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set up the projection matrix
FLOAT fAspectRatio = (FLOAT)m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &m_matProj, D3DXToRadian(60), fAspectRatio, 0.1f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProj );
// 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 );
// Output statistics
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );
m_pFontSmall->InitDeviceObjects( m_pd3dDevice );
D3DXVECTOR3 vEye = D3DXVECTOR3( 0.0f, -0.0f, -1.0f );
D3DXVECTOR3 vAt = D3DXVECTOR3( 0.0f, -0.0f, 0.0f );
D3DXVECTOR3 vUp = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &m_matView, &vEye, &vAt, &vUp );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
return S_OK;
}
/****************************************************************************
ProcessFeedback: This performs the Image Feedback effect. It takes what's
on m_pOrigTexture, rotates/scales it onto m_pScratchTexture, then blends
m_pScratchTexture back onto m_pOrigTexture so that the image "feeds back"
onto itself.
It also constantly blits the original texture onto m_pOrigTexture, so that
there's a constant source of new image that the system can "eat." This is
the programatic equivalent of holding an object in between the TV and the
camera.
****************************************************************************/
void CMyD3DApplication::ProcessFeedback()
{
// get the current depth buffer (we have to pass this into SetRenderTarget
// so we don't inadvertently drop our depth buffer.)
LPDIRECT3DSURFACE8 pDepthSurf;
m_pd3dDevice->GetDepthStencilSurface(&pDepthSurf);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -