📄 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 D3DFVF_MESHVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)
struct MESH_VERTEX
{
float x, y, z;
float nx, ny, nz;
DWORD color;
};
CTechniqueApplication::CTechniqueApplication()
{
m_pPlaneVertexBuffer = NULL;
m_pMeshVertexBuffer = NULL;
m_pMeshIndexBuffer = NULL;
m_pMesh = NULL;
m_pMeshMaterials = NULL;
m_NumMaterials = 0;
m_BasicShader = 0;
}
CTechniqueApplication::~CTechniqueApplication()
{
}
BOOL CTechniqueApplication::PostInitialize()
{
D3DCAPS8 Caps;
m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &Caps);
if (Caps.VertexShaderVersion == D3DVS_VERSION(1,1))
{
if (FAILED(EasyCreateWindowed(m_hWnd, D3DDEVTYPE_HAL,
D3DCREATE_HARDWARE_VERTEXPROCESSING)))
return FALSE;
}
else
{
if (FAILED(EasyCreateWindowed(m_hWnd, D3DDEVTYPE_HAL,
D3DCREATE_SOFTWARE_VERTEXPROCESSING)))
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(CreatePlaneBuffer()))
return FALSE;
//Create the shader
if (FAILED(CreateShaders()))
return FALSE;
return TRUE;
}
void CTechniqueApplication::Render()
{
//Set the eye position
D3DXVECTOR4 EyePos(0.0, 60.0f, -60.0f, 0.0f);
//Set the view matrix based on the position above.
D3DXMatrixLookAtLH(&m_ViewMatrix, &(D3DXVECTOR3)EyePos,
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
//Set the current light shader
m_pD3DDevice->SetVertexShader(m_BasicShader);
//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, 15.0f, 0.0f);
m_WorldMatrix = Rotation * Translation;
//Helper constant
D3DXVECTOR4 OneAndZero (1.0, 1.0f, 0.0f, 0.0f);
m_pD3DDevice->SetVertexShaderConstant(6, &OneAndZero, 1);
//Render the shadow
RenderPlaneAndShadow();
//The ambient light constant gets set by the shadow code.
//Set it to a real value. The light direction gets set in the
//shadow code.
D3DXVECTOR4 Ambient (0.1, 0.1f, 0.1f, 0.0f);
m_pD3DDevice->SetVertexShaderConstant(5, &Ambient, 1);
//Create the concatenated transformation matrix
D3DXMATRIX ShaderMatrix = m_WorldMatrix * m_ViewMatrix *
m_ProjectionMatrix;
//Get the transpose
D3DXMatrixTranspose(&ShaderMatrix, &ShaderMatrix);
//Pass the transposed matrix to the shader
m_pD3DDevice->SetVertexShaderConstant(0, &ShaderMatrix, 4);
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());
}
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;
m_pMeshMaterials[MatCount].Ambient =
m_pMeshMaterials[MatCount].Diffuse;
}
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);
return TRUE;
}
BOOL CTechniqueApplication::PostReset()
{
SetupDevice();
//Recreate the shader
if (FAILED(CreateShaders()))
return FALSE;
return TRUE;
}
BOOL CTechniqueApplication::PreTerminate()
{
//Delete the shaders
m_pD3DDevice->DeleteVertexShader(m_BasicShader);
//Clean up
if (m_pPlaneVertexBuffer)
{
m_pPlaneVertexBuffer->Release();
m_pPlaneVertexBuffer = NULL;
}
if (m_pMeshVertexBuffer)
{
m_pMeshVertexBuffer->Release();
m_pMeshVertexBuffer = NULL;
}
if (m_pMeshIndexBuffer)
{
m_pMeshIndexBuffer->Release();
m_pMeshIndexBuffer = NULL;
}
if (m_pMesh)
{
m_pMesh->Release();
m_pMesh = NULL;
}
if (m_pMeshMaterials)
{
delete m_pMeshMaterials;
m_pMeshMaterials = NULL;
}
return TRUE;
}
void CTechniqueApplication::SetupDevice()
{
//Do all the basic setup
RECT WindowRect;
GetClientRect(m_hWnd, &WindowRect);
D3DXMatrixPerspectiveFovLH(&m_ProjectionMatrix,
D3DX_PI / 4,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -