📄 ch22p1_simplelensflare.cpp
字号:
/*
#############################################################################
Ch22p1_SimpleLensFlare.cpp: a program that demonstrates the lens flare
effect in 2D.
#############################################################################
*/
// 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 "Ch22p1_resource.h"
#include "Camera.h"
#include "InputManager.h"
#include "SkyBox.h"
#include "Ch22p1_LensFlare.h"
const int NUMSPRITES = 5;
//-----------------------------------------------------------------------------
// 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
CUserControlledCamera m_Camera;
// Mouse Input
CInputManager m_InputManager;
// skybox
CSkyBox m_SkyBox;
// lens flare
CLensFlare m_LensFlare;
protected:
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 );
void ProcessInput();
int m_iLightPosX;
int m_iLightPosY;
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("Ch22p1_SimpleLensFlare");
m_bUseDepthBuffer = TRUE;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
m_pFontSmall = new CD3DFont( _T("Arial"), 9, D3DFONT_BOLD );
}
//-----------------------------------------------------------------------------
// 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;
}
CTimer::UpdateAll(m_fElapsedTime);
return S_OK;
}
void CMyD3DApplication::ProcessInput()
{
const float fSpeed = 0.5f;
unsigned char m_bKey[256];
ZeroMemory( m_bKey, 256 );
GetKeyboardState(m_bKey);
// Process keyboard input
if(m_bKey['D'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(fSpeed, 0.0f, 0.0f)); // Slide Right
if(m_bKey['A'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(-fSpeed, 0.0f, 0.0f));// Slide Left
if(m_bKey['Q'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, fSpeed, 0.0f)); // Slide Up
if(m_bKey['Z'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, -fSpeed, 0.0f));// Slide Down
if(m_bKey['W'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, fSpeed)); // Slide Foward
if(m_bKey['S'] & 128) m_Camera.AddToVelocity(D3DXVECTOR3(0.0f, 0.0f, -fSpeed));// Slide Back
if(m_bKey['L'] & 128) m_Camera.AddToYawPitchRoll(fSpeed, 0.0f, 0.0f); // Turn Right
if(m_bKey['J'] & 128) m_Camera.AddToYawPitchRoll(-fSpeed, 0.0f, 0.0f); // Turn Left
if(m_bKey['K'] & 128) m_Camera.AddToYawPitchRoll(0.0f, fSpeed, 0.0f); // Turn Down
if(m_bKey['I'] & 128) m_Camera.AddToYawPitchRoll(0.0f, -fSpeed, 0.0f);// Turn Up
// mouse look
DIMOUSESTATE2 dims2;
m_InputManager.ReadMouse(dims2);
m_iLightPosX += dims2.lX;
m_iLightPosY += dims2.lY;
if (m_iLightPosX < 0) m_iLightPosX = 0;
if (m_iLightPosX > m_d3dsdBackBuffer.Width) m_iLightPosX = m_d3dsdBackBuffer.Width;
if (m_iLightPosY < 0) m_iLightPosY = 0;
if (m_iLightPosY > m_d3dsdBackBuffer.Height) m_iLightPosY = m_d3dsdBackBuffer.Height;
}
//-----------------------------------------------------------------------------
// 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()
{
// these are done here so that you can move the camera around during a freeze
// frame, ala The Matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
ProcessInput();
m_Camera.Update(m_fElapsedTime);
// Clear the backbuffer
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x000000, 1.0f, 0L );
// Now that our fire texture's updated, we can begin rendering the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// draw skybox
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.GetViewMatrix());
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
m_SkyBox.Render(matWorld);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
m_LensFlare.Render(m_iLightPosX, m_iLightPosY, m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height);
// 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 );
m_SkyBox.SetSize(100.0f);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_iLightPosX = 0;
m_iLightPosY = 0;
m_InputManager.CreateDevices(m_hWnd, false, true);
m_SkyBox.RestoreDeviceObjects(m_pd3dDevice,
"Ch22p1_SkyBox_Top.bmp",
"Ch22p1_SkyBox_Bottom.bmp",
"Ch22p1_SkyBox_Front.bmp",
"Ch22p1_SkyBox_Back.bmp",
"Ch22p1_SkyBox_Left.bmp",
"Ch22p1_SkyBox_Right.bmp"
);
m_LensFlare.RestoreDeviceObjects(m_pd3dDevice);
LPDIRECT3DTEXTURE8 texHalo = m_LensFlare.AddTexture("Ch22p1_LensFlare_Halo.dds");
LPDIRECT3DTEXTURE8 tex1 = m_LensFlare.AddTexture("Ch22p1_LensFlare_01.dds");
LPDIRECT3DTEXTURE8 tex2 = m_LensFlare.AddTexture("Ch22p1_LensFlare_02.dds");
LPDIRECT3DTEXTURE8 tex3 = m_LensFlare.AddTexture("Ch22p1_LensFlare_03.dds");
m_LensFlare.AddSpot(CLensFlareSpot(texHalo, 1.20f, 1.0f, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.10f, 0.8f, D3DXCOLOR(0.7f, 0.5f, 0.0f, 0.2f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.01f, 0.7f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.7f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, 0.6f, D3DXCOLOR(1.0f, 1.0f, 0.0f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.13f, 0.5f, D3DXCOLOR(1.0f, 1.0f, 0.0f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, 0.4f, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.05f, 0.1f, D3DXCOLOR(1.0f, 1.0f, 0.5f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, -0.2f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.09f, -0.3f, D3DXCOLOR(1.0f, 1.0f, 0.6f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.15f, -0.4f, D3DXCOLOR(1.0f, 0.7f, 0.0f, 0.3f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.15f, -0.7f, D3DXCOLOR(1.0f, 0.5f, 0.0f, 0.2f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.32f, -1.0f, D3DXCOLOR(1.0f, 0.7f, 0.0f, 0.2f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.40f, -1.3f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.5f)));
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
m_Camera.SetPosition(D3DXVECTOR3(0.0f, 7.0f, -20.0f));
// Set the world matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 0.1f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_InputManager.DestroyDevices();
m_pFont->InvalidateDeviceObjects();
m_pFontSmall->InvalidateDeviceObjects();
m_SkyBox.InvalidateDeviceObjects();
m_LensFlare.InvalidateDeviceObjects();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
m_pFontSmall->DeleteDeviceObjects();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
SAFE_DELETE( m_pFont );
SAFE_DELETE( m_pFontSmall );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
// Pass remaining messages to default handler
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -