📄 ch16p1_simpleblur.cpp
字号:
/*
#############################################################################
Ch16p1_SimpleBlur.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 "Ch16p1_resource.h"
#include "Ch16p1_ImageManipulator.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_pBlurredTexture;
// width and height of texture image
int m_iTextureWidth, m_iTextureHeight;
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("Ch16p1_SimpleBlur");
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_pBlurredTexture = 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;
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()
{
// 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_pBlurredTexture );
// 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 );
m_pFont->DrawText( 2, 60, D3DCOLOR_ARGB(255, 255, 0, 0), "B = Blur this image");
m_pFont->DrawText( 2, 80, D3DCOLOR_ARGB(255, 255, 0, 0), "X = XTreme Blur this image");
m_pFont->DrawText( 2, 100, D3DCOLOR_ARGB(255, 255, 0, 0), "S = Sharpen this image");
m_pFont->DrawText( 2, 120, D3DCOLOR_ARGB(255, 255, 0, 0), "E = Detect Edges of this image");
// 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;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
HRESULT hr;
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
// Setup render states
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Create vertex buffer
{
CUSTOMVERTEX* pVertices;
if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 6*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return hr;
if( FAILED( hr = m_pVB->Lock( 0, m_dwNumVertices*sizeof(CUSTOMVERTEX), (BYTE**)&pVertices, 0 ) ) )
return hr;
// first triangle
pVertices[0].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
pVertices[0].color = D3DCOLOR_ARGB(200,0,0,0);
pVertices[0].tu = 0.0f;
pVertices[0].tv = 0.0f;
pVertices[1].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
pVertices[1].color = D3DCOLOR_ARGB(200,0,0,0);
pVertices[1].tu = 1.0f;
pVertices[1].tv = 0.0f;
pVertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
pVertices[2].color = D3DCOLOR_ARGB(200,0,0,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 = D3DCOLOR_ARGB(200,0,0,0);
pVertices[3].tu = 0.0f;
pVertices[3].tv = 0.0f;
pVertices[4].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
pVertices[4].color = D3DCOLOR_ARGB(200,0,0,0);
pVertices[4].tu = 1.0f;
pVertices[4].tv = 1.0f;
pVertices[5].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
pVertices[5].color = D3DCOLOR_ARGB(200,0,0,0);
pVertices[5].tu = 0.0f;
pVertices[5].tv = 1.0f;
if( FAILED( hr = m_pVB->Unlock() ) )
return hr;
}
// create original image texture. This must be created as a render target.
if (FAILED(hr = D3DXCreateTextureFromFileEx(m_pd3dDevice, "Ch16p1_Island.png",
0, 0, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
0, NULL, NULL, &m_pOrigTexture))) {
return(hr);
}
if (FAILED(hr = D3DXCreateTextureFromFileEx(m_pd3dDevice, "Ch16p1_Island.png",
0, 0, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
0, NULL, NULL, &m_pBlurredTexture))) {
return(hr);
}
D3DSURFACE_DESC desc;
m_pOrigTexture->GetLevelDesc(0, &desc);
m_iTextureWidth = desc.Width;
m_iTextureHeight = desc.Height;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pFont->InvalidateDeviceObjects();
m_pFontSmall->InvalidateDeviceObjects();
SAFE_RELEASE( m_pVB );
SAFE_RELEASE( m_pOrigTexture );
SAFE_RELEASE( m_pBlurredTexture );
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 )
{
switch(uMsg) {
case WM_KEYDOWN:
{
CImageManipulator manip;
switch(toupper(wParam)) {
case 'X':
{
CExtraBlurKernel extrablurkernel;
manip.ProcessImage(m_pOrigTexture, m_pBlurredTexture,
m_iTextureWidth, m_iTextureHeight, extrablurkernel);
}
break;
case 'B':
{
CBlurKernel blurkernel;
manip.ProcessImage(m_pOrigTexture, m_pBlurredTexture,
m_iTextureWidth, m_iTextureHeight, blurkernel);
}
break;
case 'S':
{
CSharpeningKernel sharpeningkernel;
manip.ProcessImage(m_pOrigTexture, m_pBlurredTexture,
m_iTextureWidth, m_iTextureHeight, sharpeningkernel);
}
break;
case 'E':
{
CEdgeDetectionKernel edgedetectionkernel;
manip.ProcessImage(m_pOrigTexture, m_pBlurredTexture,
m_iTextureWidth, m_iTextureHeight, edgedetectionkernel);
}
break;
}
}
break;
}
// Pass remaining messages to default handler
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -