📄 techniqueapplication.cpp
字号:
/***************************************************************
* TechniqueApplication.cpp *
* *
* This file contains the implementation of the *
* TechniqueApplication class. *
* To compile correctly, this file must be linked with: *
* kernel32.lib *
* user32.lib *
* d3dx8dt.lib *
* d3d8.lib *
* *
***************************************************************/
#include "TechniqueApplication.h"
#define TEX_DIMENSION 1024
#define D3DFVF_MESHVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)
#define D3DFVF_DISPLAYVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1 | D3DFVF_DIFFUSE)
struct MESH_VERTEX
{
float x, y, z;
float nx, ny, nz;
DWORD color;
};
struct DISPLAY_VERTEX
{
float x, y, z, rhw;
DWORD color;
float u, v;
};
CTechniqueApplication::CTechniqueApplication()
{
m_pDisplayVertexBuffer = NULL;
m_pMeshVertexBuffer = NULL;
m_pMeshIndexBuffer = NULL;
m_pMesh = NULL;
m_pMeshMaterials = NULL;
m_pDisplayTexture = NULL;
m_pDisplayTextureSurface = NULL;
m_pDisplayZSurface = NULL;
m_pBackBuffer = NULL;
m_pZBuffer = NULL;
m_NumMaterials = 0;
m_BasicShader = 0;
}
CTechniqueApplication::~CTechniqueApplication()
{
}
BOOL CTechniqueApplication::PostInitialize()
{
D3DDISPLAYMODE CurrentMode;
if (SUCCEEDED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&CurrentMode)))
{
ZeroMemory(&m_PresentParameters,
sizeof(D3DPRESENT_PARAMETERS));
m_PresentParameters.Windowed = TRUE;
m_PresentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
m_PresentParameters.BackBufferFormat = CurrentMode.Format;
m_PresentParameters.EnableAutoDepthStencil = TRUE;
m_PresentParameters.AutoDepthStencilFormat = D3DFMT_D24S8;
m_CreationParameters.AdapterOrdinal =
D3DADAPTER_DEFAULT;
m_CreationParameters.DeviceType = D3DDEVTYPE_HAL;
m_CreationParameters.hFocusWindow = m_hWnd;
D3DCAPS8 Caps;
m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &Caps);
if (Caps.VertexShaderVersion == D3DVS_VERSION(1,1))
m_CreationParameters.BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
m_CreationParameters.BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
if (FAILED(CreateDevice(&m_CreationParameters, &m_PresentParameters)))
return FALSE;
}
//Do the basic camera positioning, etc.
SetupDevice();
//Load the mesh object
LoadMesh();
//Create the buffers we're actually going to use
ExtractBuffers();
if (FAILED(CreateDisplayBuffer()))
return FALSE;
//Create the shader
if (FAILED(CreateShaders()))
return FALSE;
//Clear Once
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(100, 100, 200), 1.0f, 0);
return TRUE;
}
void CTechniqueApplication::Render()
{
//Set the view matrix based on the position above.
D3DXMatrixLookAtLH(&m_ViewMatrix, &D3DXVECTOR3(0.0, 0.0f, -40.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
//The following matrix operations will transform the mesh
D3DXMATRIX Rotation;
D3DXMATRIX Translation;
//These matrices will rotate and translate the mesh
D3DXMatrixRotationY(&Rotation, (float)GetTickCount() / 1000.0f);
D3DXMatrixTranslation(&Translation, 0.0f, 0.0f, 0.0f);
m_WorldMatrix = Rotation * Translation;
//Set some lighting constants
D3DXVECTOR4 Ambient (0.1, 0.1f, 0.1f, 0.0f);
m_pD3DDevice->SetVertexShaderConstant(5, &Ambient, 1);
//Set the light direction
D3DXVECTOR4 LightDir = D3DXVECTOR4(0.0f, -1.0f, 0.0f, 0.0f);
D3DXMATRIX InverseWorld;
D3DXMatrixInverse(&InverseWorld, NULL, &m_WorldMatrix);
D3DXVec4Transform(&LightDir, &LightDir, &InverseWorld);
D3DXVec4Normalize(&LightDir, &LightDir);
m_pD3DDevice->SetVertexShaderConstant(4, &LightDir, 1);
//Create the concatenated transformation matrix
D3DXMATRIX ShaderMatrix = m_WorldMatrix * m_ViewMatrix *
m_ProjectionMatrix;
D3DXMatrixTranspose(&ShaderMatrix, &ShaderMatrix);
m_pD3DDevice->SetVertexShaderConstant(0, &ShaderMatrix, 4);
//Set the viewport so that the rendered image matches the device.
//Get the viewport here and then set it when the render target changes
D3DVIEWPORT8 NormalViewport;
m_pD3DDevice->GetViewport(&NormalViewport);
//Set up the renderTarget
HRESULT hr = m_pD3DDevice->SetRenderTarget(m_pDisplayTextureSurface, m_pDisplayZSurface);
//Set the matching viewport
m_pD3DDevice->SetViewport(&NormalViewport);
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(100, 100, 200), 1.0f, 0);
//Set the current shader
hr = m_pD3DDevice->SetVertexShader(m_BasicShader);
//Draw the object
m_pD3DDevice->SetStreamSource(0, m_pMeshVertexBuffer, sizeof(MESH_VERTEX));
m_pD3DDevice->SetIndices(m_pMeshIndexBuffer, 0);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0,
m_pMesh->GetNumVertices(), 0,
m_pMesh->GetNumFaces());
//Set the renderTarget back to the normal back buffer
m_pD3DDevice->SetRenderTarget(m_pBackBuffer, m_pZBuffer);
//Set up the new texture to be drawn
m_pD3DDevice->SetTexture(0, m_pDisplayTexture);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
m_pD3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR, 0x55ffffff);
//Set display modes for a simple alpha blended rectangle
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
//This ensures everything is drawn
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
//Draw a rectangle showing the rendering
m_pD3DDevice->SetVertexShader(D3DFVF_DISPLAYVERTEX);
m_pD3DDevice->SetStreamSource(0, m_pDisplayVertexBuffer, sizeof(DISPLAY_VERTEX));
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
//Set things back to normal
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
m_pD3DDevice->SetTexture(0, NULL);
m_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
}
HRESULT CTechniqueApplication::LoadMesh()
{
LPD3DXBUFFER pD3DXMtrlBuffer;
LPD3DXMESH pOriginalMesh;
//Load and initialize the mesh. This is a repeat of the code
//from Chapter 10.
if(FAILED(D3DXLoadMeshFromX("..\\media\\fattorus.x",
D3DXMESH_MANAGED,
m_pD3DDevice, NULL, &pD3DXMtrlBuffer,
&m_NumMaterials, &pOriginalMesh)))
return FALSE;
D3DXMATERIAL* d3dxMaterials =
(D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
m_pMeshMaterials = new D3DMATERIAL8[m_NumMaterials];
for(long MatCount = 0; MatCount < m_NumMaterials; MatCount++)
m_pMeshMaterials[MatCount] = d3dxMaterials[MatCount].MatD3D;
pD3DXMtrlBuffer->Release();
//This is new. If the FVF doesn't match, clone the mesh and
//create one that does. Then, release the loaded mesh. If the
//FVF does match, set the member mesh and move on.
if (pOriginalMesh->GetFVF() != D3DFVF_MESHVERTEX)
{
pOriginalMesh->CloneMeshFVF(D3DXMESH_MANAGED,
D3DFVF_MESHVERTEX,
m_pD3DDevice, &m_pMesh);
pOriginalMesh->Release();
pOriginalMesh = NULL;
}
else
m_pMesh = pOriginalMesh;
return S_OK;
}
BOOL CTechniqueApplication::PreReset()
{
//Delete the shaders
m_pD3DDevice->DeleteVertexShader(m_BasicShader);
//Get rid of all the render target objects
CleanUpTarget();
return TRUE;
}
BOOL CTechniqueApplication::PostReset()
{
SetupDevice();
//Recreate the shader
if (FAILED(CreateShaders()))
return FALSE;
return TRUE;
}
BOOL CTechniqueApplication::PreTerminate()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -