DirectX/10.0/Direct3D/视锥剔除
屏幕上三维观察区域,所有内容都绘制到该区域,称为视锥体。视锥体内部的所有内容都将由显卡渲染到屏幕上。视锥体外部的所有内容都将由显卡进行检查,然后在渲染过程中丢弃。
但是,依赖显卡进行剔除的过程对于大型场景来说可能非常昂贵。例如,假设我们有一个场景包含 2000 多个模型,每个模型有 5,000 个多边形,但在任何给定时间只有 10-20 个模型是可见的。显卡必须检查所有 2000 个模型中的每个三角形,以从场景中删除 1990 个模型,以便我们只绘制 10 个模型。正如您所看到的,这非常低效。
视锥剔除如何解决我们的问题是,我们可以在渲染之前确定模型是否在我们的视锥体内。这使我们不必将所有三角形发送到显卡,并且只允许我们发送需要绘制的三角形。我们做到这一点的方法是在每个模型周围放置一个立方体、矩形或球体,然后计算该立方体、矩形或球体是否可见。执行此操作的数学运算通常只需要几行代码,从而消除了可能需要测试数千个三角形的需求。
为了演示其工作原理,我们将首先创建一个场景,其中包含 25 个随机放置的球体。然后,我们将手动旋转相机以测试使用左右箭头键对视域外的球体进行剔除。我们还将使用一个计数器并显示正在绘制且未剔除的球体数量以进行确认。我们将使用来自之前几个教程的代码来创建场景。
框架
[edit | edit source]框架主要包含来自之前几个教程的类。我们确实有三个新类,称为 FrustumClass、PositionClass 和 ModelListClass。FrustumClass 将封装本教程关注的视锥剔除功能。ModelListClass 将包含将随机生成的 25 个球体的 位置和颜色信息列表。PositionClass 将根据用户是否按下左右箭头键来处理相机的查看旋转。
Frustumclass.h
[edit | edit source]FrustumClass 的头文件非常简单。该类不需要任何初始化或关闭。在每一帧中,在相机首次渲染后,都会调用 ConstructFrustum 函数。ConstructFrustum 函数使用私有的 m_planes 根据更新后的查看位置计算并存储视锥体的六个平面。从那里,我们可以调用四个检查函数中的任何一个,以查看点、立方体、球体或矩形是否在查看视锥体内。
////////////////////////////////////////////////////////////////////////////////
// Filename: frustumclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _FRUSTUMCLASS_H_
#define _FRUSTUMCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <d3dx10math.h>
////////////////////////////////////////////////////////////////////////////////
// Class name: FrustumClass
////////////////////////////////////////////////////////////////////////////////
class FrustumClass
{
public:
FrustumClass();
FrustumClass(const FrustumClass&);
~FrustumClass();
void ConstructFrustum(float, D3DXMATRIX, D3DXMATRIX);
bool CheckPoint(float, float, float);
bool CheckCube(float, float, float, float);
bool CheckSphere(float, float, float, float);
bool CheckRectangle(float, float, float, float, float, float);
private:
D3DXPLANE m_planes[6];
};
#endif
Frustumclass.cpp
[edit | edit source]////////////////////////////////////////////////////////////////////////////////
// Filename: frustumclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "frustumclass.h"
FrustumClass::FrustumClass()
{
}
FrustumClass::FrustumClass(const FrustumClass& other)
{
}
FrustumClass::~FrustumClass()
{
}
ConstructFrustum 在每一帧中由 GraphicsClass 调用。它传递屏幕深度、投影矩阵和视图矩阵。然后,我们使用这些输入变量来计算该帧中视锥体的矩阵。有了新的视锥矩阵,我们就可以计算出构成视锥体的六个平面。
void FrustumClass::ConstructFrustum(float screenDepth, D3DXMATRIX projectionMatrix, D3DXMATRIX viewMatrix)
{
float zMinimum, r;
D3DXMATRIX matrix;
// Calculate the minimum Z distance in the frustum.
zMinimum = -projectionMatrix._43 / projectionMatrix._33;
r = screenDepth / (screenDepth - zMinimum);
projectionMatrix._33 = r;
projectionMatrix._43 = -r * zMinimum;
// Create the frustum matrix from the view matrix and updated projection matrix.
D3DXMatrixMultiply(&matrix, &viewMatrix, &projectionMatrix);
// Calculate near plane of frustum.
m_planes[0].a = matrix._14 + matrix._13;
m_planes[0].b = matrix._24 + matrix._23;
m_planes[0].c = matrix._34 + matrix._33;
m_planes[0].d = matrix._44 + matrix._43;
D3DXPlaneNormalize(&m_planes[0], &m_planes[0]);
// Calculate far plane of frustum.
m_planes[1].a = matrix._14 - matrix._13;
m_planes[1].b = matrix._24 - matrix._23;
m_planes[1].c = matrix._34 - matrix._33;
m_planes[1].d = matrix._44 - matrix._43;
D3DXPlaneNormalize(&m_planes[1], &m_planes[1]);
// Calculate left plane of frustum.
m_planes[2].a = matrix._14 + matrix._11;
m_planes[2].b = matrix._24 + matrix._21;
m_planes[2].c = matrix._34 + matrix._31;
m_planes[2].d = matrix._44 + matrix._41;
D3DXPlaneNormalize(&m_planes[2], &m_planes[2]);
// Calculate right plane of frustum.
m_planes[3].a = matrix._14 - matrix._11;
m_planes[3].b = matrix._24 - matrix._21;
m_planes[3].c = matrix._34 - matrix._31;
m_planes[3].d = matrix._44 - matrix._41;
D3DXPlaneNormalize(&m_planes[3], &m_planes[3]);
// Calculate top plane of frustum.
m_planes[4].a = matrix._14 - matrix._12;
m_planes[4].b = matrix._24 - matrix._22;
m_planes[4].c = matrix._34 - matrix._32;
m_planes[4].d = matrix._44 - matrix._42;
D3DXPlaneNormalize(&m_planes[4], &m_planes[4]);
// Calculate bottom plane of frustum.
m_planes[5].a = matrix._14 + matrix._12;
m_planes[5].b = matrix._24 + matrix._22;
m_planes[5].c = matrix._34 + matrix._32;
m_planes[5].d = matrix._44 + matrix._42;
D3DXPlaneNormalize(&m_planes[5], &m_planes[5]);
return;
}
CheckPoint 检查单个点是否在查看视锥体内。这是四个检查算法中最通用的算法,但如果在正确的情况下正确使用,与其他检查方法相比,它可能会非常有效。它接收点并检查它是否在所有六个平面内。如果点在所有六个平面内,则返回 true,否则如果不在,则返回 false。
bool FrustumClass::CheckPoint(float x, float y, float z)
{
int i;
// Check if the point is inside all six planes of the view frustum.
for(i=0; i
CheckCube 检查立方体的八个角点是否在查看视锥体内。它只需要立方体的中心点和半径作为输入,它使用这些信息来计算立方体的 8 个角点。然后,它检查查看视锥体的 6 个平面中是否有一个角点。如果它确实在所有六个平面内找到一个点,则返回 true,否则返回 false。
bool FrustumClass::CheckCube(float xCenter, float yCenter, float zCenter, float radius)
{
int i;
// Check if any one point of the cube is in the view frustum.
for(i=0; i= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + radius), (yCenter - radius), (zCenter - radius))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter - radius), (yCenter + radius), (zCenter - radius))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + radius), (yCenter + radius), (zCenter - radius))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter - radius), (yCenter - radius), (zCenter + radius))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + radius), (yCenter - radius), (zCenter + radius))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter - radius), (yCenter + radius), (zCenter + radius))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + radius), (yCenter + radius), (zCenter + radius))) >= 0.0f)
{
continue;
}
return false;
}
return true;
}
CheckSphere 检查从中心点开始的球体的半径是否在查看视锥体的六个平面内。如果它在任何平面之外,则球体不可见,函数将返回 false。如果它在所有六个平面内,则函数返回 true,表示球体可见。
bool FrustumClass::CheckSphere(float xCenter, float yCenter, float zCenter, float radius)
{
int i;
// Check if the radius of the sphere is inside the view frustum.
for(i=0; i
CheckRectangle 的工作原理与 CheckCube 相同,只是它接收矩形的 x 半径、y 半径和 z 半径作为输入,而不是立方体的单个半径。然后,它可以计算矩形的 8 个角点,并执行类似于 CheckCube 函数的视锥检查。
bool FrustumClass::CheckRectangle(float xCenter, float yCenter, float zCenter, float xSize, float ySize, float zSize)
{
int i;
// Check if any of the 6 planes of the rectangle are inside the view frustum.
for(i=0; i= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + xSize), (yCenter - ySize), (zCenter - zSize))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter - xSize), (yCenter + ySize), (zCenter - zSize))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter - xSize), (yCenter - ySize), (zCenter + zSize))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + xSize), (yCenter + ySize), (zCenter - zSize))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + xSize), (yCenter - ySize), (zCenter + zSize))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter - xSize), (yCenter + ySize), (zCenter + zSize))) >= 0.0f)
{
continue;
}
if(D3DXPlaneDotCoord(&m_planes[i], &D3DXVECTOR3((xCenter + xSize), (yCenter + ySize), (zCenter + zSize))) >= 0.0f)
{
continue;
}
return false;
}
return true;
}
Modellistclass.h
[edit | edit source]ModelListClass 是一个新的类,用于维护有关场景中所有模型的信息。在本教程中,它只维护球体模型的大小和颜色,因为我们只有一种模型类型。此类可以扩展以维护场景中所有不同类型的模型以及它们对 ModelClass 的索引,但我现在将本教程保持简单。
///////////////////////////////////////////////////////////////////////////////
// Filename: modellistclass.h
///////////////////////////////////////////////////////////////////////////////
#ifndef _MODELLISTCLASS_H_
#define _MODELLISTCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <d3dx10math.h>
#include <stdlib.h>
#include <time.h>
///////////////////////////////////////////////////////////////////////////////
// Class name: ModelListClass
///////////////////////////////////////////////////////////////////////////////
class ModelListClass
{
private:
struct ModelInfoType
{
D3DXVECTOR4 color;
float positionX, positionY, positionZ;
};
public:
ModelListClass();
ModelListClass(const ModelListClass&);
~ModelListClass();
bool Initialize(int);
void Shutdown();
int GetModelCount();
void GetData(int, float&, float&, float&, D3DXVECTOR4&);
private:
int m_modelCount;
ModelInfoType* m_ModelInfoList;
};
#endif
Modellistclass.cpp
[edit | edit source]/////////////////////////////////////////////////////////////////////////////// // Filename: modellistclass.cpp /////////////////////////////////////////////////////////////////////////////// #include "modellistclass.h"
类构造函数将模型信息列表初始化为 null。
ModelListClass::ModelListClass()
{
m_ModelInfoList = 0;
}
ModelListClass::ModelListClass(const ModelListClass& other)
{
}
ModelListClass::~ModelListClass()
{
}
bool ModelListClass::Initialize(int numModels)
{
int i;
float red, green, blue;
首先存储要使用的模型数量,然后使用 ModelInfoType 结构创建它们列表数组。
// Store the number of models.
m_modelCount = numModels;
// Create a list array of the model information.
m_ModelInfoList = new ModelInfoType[m_modelCount];
if(!m_ModelInfoList)
{
return false;
}
使用当前时间对随机数生成器进行播种,然后随机生成模型的位置和颜色,并将它们存储在列表数组中。
// Seed the random generator with the current time.
srand((unsigned int)time(NULL));
// Go through all the models and randomly generate the model color and position.
for(i=0; i<m_modelCount; i++)
{
// Generate a random color for the model.
red = (float)rand() / RAND_MAX;
green = (float)rand() / RAND_MAX;
blue = (float)rand() / RAND_MAX;
m_ModelInfoList[i].color = D3DXVECTOR4(red, green, blue, 1.0f);
// Generate a random position in front of the viewer for the mode.
m_ModelInfoList[i].positionX = (((float)rand()-(float)rand())/RAND_MAX) * 10.0f;
m_ModelInfoList[i].positionY = (((float)rand()-(float)rand())/RAND_MAX) * 10.0f;
m_ModelInfoList[i].positionZ = ((((float)rand()-(float)rand())/RAND_MAX) * 10.0f) + 5.0f;
}
return true;
}
Shutdown 函数释放模型信息列表数组。
void ModelListClass::Shutdown()
{
// Release the model information list.
if(m_ModelInfoList)
{
delete [] m_ModelInfoList;
m_ModelInfoList = 0;
}
return;
}
GetModelCount 返回此类维护信息的模型数量。
int ModelListClass::GetModelCount()
{
return m_modelCount;
}
GetData 函数在给定输入索引位置提取球体的位置和颜色。
void ModelListClass::GetData(int index, float& positionX, float& positionY, float& positionZ, D3DXVECTOR4& color)
{
positionX = m_ModelInfoList[index].positionX;
positionY = m_ModelInfoList[index].positionY;
positionZ = m_ModelInfoList[index].positionZ;
color = m_ModelInfoList[index].color;
return;
}
Graphicsclass.h
[edit | edit source]//////////////////////////////////////////////////////////////////////////////// // Filename: graphicsclass.h //////////////////////////////////////////////////////////////////////////////// #ifndef _GRAPHICSCLASS_H_ #define _GRAPHICSCLASS_H_ ///////////// // GLOBALS // ///////////// const bool FULL_SCREEN = true; const bool VSYNC_ENABLED = true; const float SCREEN_DEPTH = 1000.0f; const float SCREEN_NEAR = 0.1f;
本教程的 GraphicsClass 包含我们在之前教程中使用的一些类。它还包含 frustumclass.h 和 modellistclass.h 头文件,这两个头文件是新的。
///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "d3dclass.h"
#include "cameraclass.h"
#include "textclass.h"
#include "modelclass.h"
#include "lightshaderclass.h"
#include "lightclass.h"
#include "modellistclass.h"
#include "frustumclass.h"
////////////////////////////////////////////////////////////////////////////////
// Class name: GraphicsClass
////////////////////////////////////////////////////////////////////////////////
class GraphicsClass
{
public:
GraphicsClass();
GraphicsClass(const GraphicsClass&);
~GraphicsClass();
bool Initialize(int, int, HWND);
void Shutdown();
bool Frame(float);
bool Render();
private:
两个新的私有类对象是 m_Frustum 和 m_ModelList。
D3DClass* m_D3D; CameraClass* m_Camera; TextClass* m_Text; ModelClass* m_Model; LightShaderClass* m_LightShader; LightClass* m_Light; ModelListClass* m_ModelList; FrustumClass* m_Frustum; }; #endif
Graphicsclass.cpp
[edit | edit source]我将只介绍自上一个教程以来发生改变的函数。
//////////////////////////////////////////////////////////////////////////////// // Filename: graphicsclass.cpp //////////////////////////////////////////////////////////////////////////////// #include "graphicsclass.h"
类构造函数将私有成员变量初始化为 null。
GraphicsClass::GraphicsClass()
{
m_D3D = 0;
m_Camera = 0;
m_Text = 0;
m_Model = 0;
m_LightShader = 0;
m_Light = 0;
m_ModelList = 0;
m_Frustum = 0;
}
bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
bool result;
D3DXMATRIX baseViewMatrix;
// Create the Direct3D object.
m_D3D = new D3DClass;
if(!m_D3D)
{
return false;
}
// Initialize the Direct3D object.
result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
if(!result)
{
MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);
return false;
}
// Create the camera object.
m_Camera = new CameraClass;
if(!m_Camera)
{
return false;
}
// Initialize a base view matrix with the camera for 2D user interface rendering.
m_Camera->SetPosition(0.0f, 0.0f, -1.0f);
m_Camera->Render();
m_Camera->GetViewMatrix(baseViewMatrix);
// Create the text object.
m_Text = new TextClass;
if(!m_Text)
{
return false;
}
// Initialize the text object.
result = m_Text->Initialize(m_D3D->GetDevice(), m_D3D->GetDeviceContext(), hwnd, screenWidth, screenHeight, baseViewMatrix);
if(!result)
{
MessageBox(hwnd, L"Could not initialize the text object.", L"Error", MB_OK);
return false;
}
// Create the model object.
m_Model = new ModelClass;
if(!m_Model)
{
return false;
}
在本教程中,我们加载了一个球体模型,而不是一个立方体模型。
// Initialize the model object.
result = m_Model->Initialize(m_D3D->GetDevice(), L"../Engine/data/seafloor.dds", "../Engine/data/sphere.txt");
if(!result)
{
MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
return false;
}
// Create the light shader object.
m_LightShader = new LightShaderClass;
if(!m_LightShader)
{
return false;
}
// Initialize the light shader object.
result = m_LightShader->Initialize(m_D3D->GetDevice(), hwnd);
if(!result)
{
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
return false;
}
// Create the light object.
m_Light = new LightClass;
if(!m_Light)
{
return false;
}
// Initialize the light object.
m_Light->SetDirection(0.0f, 0.0f, 1.0f);
在这里,我们创建了新的 ModelListClass 对象,并让它创建 25 个随机放置/着色的球体模型。
// Create the model list object.
m_ModelList = new ModelListClass;
if(!m_ModelList)
{
return false;
}
// Initialize the model list object.
result = m_ModelList->Initialize(25);
if(!result)
{
MessageBox(hwnd, L"Could not initialize the model list object.", L"Error", MB_OK);
return false;
}
在这里,我们创建了新的 FrustumClass 对象。它不需要任何初始化,因为这在每一帧中使用 ConstructFrustum 函数完成。
// Create the frustum object.
m_Frustum = new FrustumClass;
if(!m_Frustum)
{
return false;
}
return true;
}
void GraphicsClass::Shutdown()
{
在 Shutdown 函数中,我们在这里释放了新的 FrustumClass 和 ModelListClass 对象。
// Release the frustum object.
if(m_Frustum)
{
delete m_Frustum;
m_Frustum = 0;
}
// Release the model list object.
if(m_ModelList)
{
m_ModelList->Shutdown();
delete m_ModelList;
m_ModelList = 0;
}
// Release the light object.
if(m_Light)
{
delete m_Light;
m_Light = 0;
}
// Release the light shader object.
if(m_LightShader)
{
m_LightShader->Shutdown();
delete m_LightShader;
m_LightShader = 0;
}
// Release the model object.
if(m_Model)
{
m_Model->Shutdown();
delete m_Model;
m_Model = 0;
}
// Release the text object.
if(m_Text)
{
m_Text->Shutdown();
delete m_Text;
m_Text = 0;
}
// Release the camera object.
if(m_Camera)
{
delete m_Camera;
m_Camera = 0;
}
// Release the Direct3D object.
if(m_D3D)
{
m_D3D->Shutdown();
delete m_D3D;
m_D3D = 0;
}
return;
}
Frame 函数现在从调用它的 SystemClass 中获取相机的旋转。然后设置相机的 位置和旋转,以便可以在 Render 函数中正确更新视图矩阵。
bool GraphicsClass::Frame(float rotationY)
{
// Set the position of the camera.
m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
// Set the rotation of the camera.
m_Camera->SetRotation(0.0f, rotationY, 0.0f);
return true;
}
bool GraphicsClass::Render()
{
D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix, orthoMatrix;
int modelCount, renderCount, index;
float positionX, positionY, positionZ, radius;
D3DXVECTOR4 color;
bool renderModel, result;
// Clear the buffers to begin the scene.
m_D3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
// Generate the view matrix based on the camera's position.
m_Camera->Render();
// Get the world, view, projection, and ortho matrices from the camera and d3d objects.
m_D3D->GetWorldMatrix(worldMatrix);
m_Camera->GetViewMatrix(viewMatrix);
m_D3D->GetProjectionMatrix(projectionMatrix);
m_D3D->GetOrthoMatrix(orthoMatrix);
渲染函数的主要变化是,我们现在根据更新的视图矩阵在每一帧构建视锥体。每次视图矩阵发生变化,都需要进行这种构建,否则视锥体剔除检查将不准确。
// Construct the frustum. m_Frustum->ConstructFrustum(SCREEN_DEPTH, projectionMatrix, viewMatrix); // Get the number of models that will be rendered. modelCount = m_ModelList->GetModelCount(); // Initialize the count of models that have been rendered. renderCount = 0;
现在遍历 ModelListClass 对象中的所有模型。
// Go through all the models and render them only if they can be seen by the camera view.
for(index=0; index<modelCount; index++)
{
// Get the position and color of the sphere model at this index.
m_ModelList->GetData(index, positionX, positionY, positionZ, color);
// Set the radius of the sphere to 1.0 since this is already known.
radius = 1.0f;
在这里,我们使用新的 FrustumClass 对象。我们检查球体是否在视锥体中可见。如果可见,则渲染它;如果不可见,则跳过它并检查下一个。这就是我们使用视锥体剔除来获得所有速度的地方。
// Check if the sphere model is in the view frustum.
renderModel = m_Frustum->CheckSphere(positionX, positionY, positionZ, radius);
// If it can be seen then render it, if not skip this model and check the next sphere.
if(renderModel)
{
// Move the model to the location it should be rendered at.
D3DXMatrixTranslation(&worldMatrix, positionX, positionY, positionZ);
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
m_Model->Render(m_D3D->GetDeviceContext());
// Render the model using the light shader.
m_LightShader->Render(m_D3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(), m_Light->GetDirection(), color);
// Reset to the original world matrix.
m_D3D->GetWorldMatrix(worldMatrix);
// Since this model was rendered then increase the count for this frame.
renderCount++;
}
}
我们使用稍微修改过的 TextClass 来显示实际渲染了多少个球体。我们还可以从这个数字中推断出,未渲染的球体是使用新的 FrustumClass 对象剔除的。
// Set the number of models that was actually rendered this frame.
result = m_Text->SetRenderCount(renderCount, m_D3D->GetDeviceContext());
if(!result)
{
return false;
}
// Turn off the Z buffer to begin all 2D rendering.
m_D3D->TurnZBufferOff();
// Turn on the alpha blending before rendering the text.
m_D3D->TurnOnAlphaBlending();
// Render the text string of the render count.
m_Text->Render(m_D3D->GetDeviceContext(), worldMatrix, orthoMatrix);
if(!result)
{
return false;
}
// Turn off alpha blending after rendering the text.
m_D3D->TurnOffAlphaBlending();
// Turn the Z buffer back on now that all 2D rendering has completed.
m_D3D->TurnZBufferOn();
// Present the rendered scene to the screen.
m_D3D->EndScene();
return true;
}
Positionclass.h
[edit | edit source]为了允许使用本教程中的左右箭头键进行相机移动,我们创建了一个新类来计算和维护观察者的位置。这个类目前只处理左右转向,但可以扩展以维护所有不同的移动变化。移动还包括加速度和减速,以创建平滑的相机效果。
////////////////////////////////////////////////////////////////////////////////
// Filename: positionclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _POSITIONCLASS_H_
#define _POSITIONCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <math.h>
////////////////////////////////////////////////////////////////////////////////
// Class name: PositionClass
////////////////////////////////////////////////////////////////////////////////
class PositionClass
{
public:
PositionClass();
PositionClass(const PositionClass&);
~PositionClass();
void SetFrameTime(float);
void GetRotation(float&);
void TurnLeft(bool);
void TurnRight(bool);
private:
float m_frameTime;
float m_rotationY;
float m_leftTurnSpeed, m_rightTurnSpeed;
};
#endif
Positionclass.cpp
[edit | edit source]//////////////////////////////////////////////////////////////////////////////// // Filename: positionclass.cpp //////////////////////////////////////////////////////////////////////////////// #include "positionclass.h"
类构造函数将私有成员变量初始化为零。
PositionClass::PositionClass()
{
m_frameTime = 0.0f;
m_rotationY = 0.0f;
m_leftTurnSpeed = 0.0f;
m_rightTurnSpeed = 0.0f;
}
PositionClass::PositionClass(const PositionClass& other)
{
}
PositionClass::~PositionClass()
{
}
SetFrameTime 函数用于设置此类中的帧速率。PositionClass 将使用该帧速率来计算观察者的移动和旋转速度。在使用此类移动观察者位置之前,应始终在每一帧的开头调用此函数。
void PositionClass::SetFrameTime(float time)
{
m_frameTime = time;
return;
}
GetRotation 返回观察者的 Y 轴旋转。这是本教程中唯一需要的辅助函数,但可以扩展以获取有关观察者位置的更多信息。
void PositionClass::GetRotation(float& y)
{
y = m_rotationY;
return;
}
移动函数的工作原理相同。这两个函数都在每一帧被调用。传递给每个函数的 keydown 输入变量指示用户是否按下了左键或右键。如果按下键,则每一帧的速度都会加速,直到达到最大值。这样,摄像机速度会类似于车辆的加速度,从而产生平滑移动和高响应性的效果。同样,如果用户释放键,并且 keydown 变量为 false,则每一帧会平滑地减速,直到速度降至零。速度根据帧时间进行计算,以确保无论帧速率如何,移动速度都保持一致。然后,每个函数使用一些基本数学来计算摄像机的新的位置。
void PositionClass::TurnLeft(bool keydown)
{
// If the key is pressed increase the speed at which the camera turns left. If not slow down the turn speed.
if(keydown)
{
m_leftTurnSpeed += m_frameTime * 0.01f;
if(m_leftTurnSpeed > (m_frameTime * 0.15f))
{
m_leftTurnSpeed = m_frameTime * 0.15f;
}
}
else
{
m_leftTurnSpeed -= m_frameTime* 0.005f;
if(m_leftTurnSpeed (m_frameTime * 0.15f))
{
m_rightTurnSpeed = m_frameTime * 0.15f;
}
}
else
{
m_rightTurnSpeed -= m_frameTime* 0.005f;
if(m_rightTurnSpeed 360.0f)
{
m_rotationY -= 360.0f;
}
return;
}
Systemclass.h
[edit | edit source]SystemClass 已被修改为使用新的 PostionClass。
////////////////////////////////////////////////////////////////////////////////
// Filename: systemclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _SYSTEMCLASS_H_
#define _SYSTEMCLASS_H_
///////////////////////////////
// PRE-PROCESSING DIRECTIVES //
///////////////////////////////
#define WIN32_LEAN_AND_MEAN
//////////////
// INCLUDES //
//////////////
#include <windows.h>
///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "inputclass.h"
#include "graphicsclass.h"
#include "timerclass.h"
#include "positionclass.h"
////////////////////////////////////////////////////////////////////////////////
// Class name: SystemClass
////////////////////////////////////////////////////////////////////////////////
class SystemClass
{
public:
SystemClass();
SystemClass(const SystemClass&);
~SystemClass();
bool Initialize();
void Shutdown();
void Run();
LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);
private:
bool Frame();
void InitializeWindows(int&, int&);
void ShutdownWindows();
private:
LPCWSTR m_applicationName;
HINSTANCE m_hinstance;
HWND m_hwnd;
InputClass* m_Input;
GraphicsClass* m_Graphics;
TimerClass* m_Timer;
PositionClass* m_Position;
};
/////////////////////////
// FUNCTION PROTOTYPES //
/////////////////////////
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
/////////////
// GLOBALS //
/////////////
static SystemClass* ApplicationHandle = 0;
#endif
Systemclass.cpp
[edit | edit source]我将只介绍自上一教程以来此类中发生更改的函数。
////////////////////////////////////////////////////////////////////////////////
// Filename: systemclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "systemclass.h"
SystemClass::SystemClass()
{
m_Input = 0;
m_Graphics = 0;
m_Timer = 0;
新的 PositionClass 对象在类构造函数中初始化为 null。
m_Position = 0;
}
bool SystemClass::Initialize()
{
int screenWidth, screenHeight;
bool result;
// Initialize the width and height of the screen to zero before sending the variables into the function.
screenWidth = 0;
screenHeight = 0;
// Initialize the windows api.
InitializeWindows(screenWidth, screenHeight);
// Create the input object. This object will be used to handle reading the keyboard input from the user.
m_Input = new InputClass;
if(!m_Input)
{
return false;
}
// Initialize the input object.
result = m_Input->Initialize(m_hinstance, m_hwnd, screenWidth, screenHeight);
if(!result)
{
MessageBox(m_hwnd, L"Could not initialize the input object.", L"Error", MB_OK);
return false;
}
// Create the graphics object. This object will handle rendering all the graphics for this application.
m_Graphics = new GraphicsClass;
if(!m_Graphics)
{
return false;
}
// Initialize the graphics object.
result = m_Graphics->Initialize(screenWidth, screenHeight, m_hwnd);
if(!result)
{
return false;
}
// Create the timer object.
m_Timer = new TimerClass;
if(!m_Timer)
{
return false;
}
// Initialize the timer object.
result = m_Timer->Initialize();
if(!result)
{
MessageBox(m_hwnd, L"Could not initialize the Timer object.", L"Error", MB_OK);
return false;
}
在此处创建新的 PositionClass 对象。它不需要任何初始化。
// Create the position object.
m_Position = new PositionClass;
if(!m_Position)
{
return false;
}
return true;
}
void SystemClass::Shutdown()
{
PositionClass 对象在此处的 Shutdown 函数中释放。
// Release the position object.
if(m_Position)
{
delete m_Position;
m_Position = 0;
}
// Release the timer object.
if(m_Timer)
{
delete m_Timer;
m_Timer = 0;
}
// Release the graphics object.
if(m_Graphics)
{
m_Graphics->Shutdown();
delete m_Graphics;
m_Graphics = 0;
}
// Release the input object.
if(m_Input)
{
m_Input->Shutdown();
delete m_Input;
m_Input = 0;
}
// Shutdown the window.
ShutdownWindows();
return;
}
bool SystemClass::Frame()
{
bool keyDown, result;
float rotationY;
// Update the system stats.
m_Timer->Frame();
// Do the input frame processing.
result = m_Input->Frame();
if(!result)
{
return false;
}
在每一帧中,PositionClass 对象都会使用帧时间进行更新。
// Set the frame time for calculating the updated position. m_Position->SetFrameTime(m_Timer->GetTime());
在帧时间更新之后,可以使用当前的键盘状态更新 PositionClass 移动函数。移动函数将更新摄像机的位置到本帧的新的位置。
// Check if the left or right arrow key has been pressed, if so rotate the camera accordingly. keyDown = m_Input->IsLeftArrowPressed(); m_Position->TurnLeft(keyDown); keyDown = m_Input->IsRightArrowPressed(); m_Position->TurnRight(keyDown);
检索新的摄像机旋转,并将其发送到 Graphics::Frame 函数以更新摄像机位置。
// Get the current view point rotation.
m_Position->GetRotation(rotationY);
// Do the frame processing for the graphics object.
result = m_Graphics->Frame(rotationY);
if(!result)
{
return false;
}
// Finally render the graphics to the screen.
result = m_Graphics->Render();
if(!result)
{
return false;
}
return true;
}
总结
[edit | edit source]现在,您已经了解了如何剔除对象。从这里开始的唯一技巧是确定对于剔除不同的对象,使用立方体、矩形、球体还是巧妙地使用点更好。
待办事项练习
[edit | edit source]1. 重新编译并运行程序。使用左右箭头键移动摄像机并更新左上角的渲染计数。
2. 加载立方体模型,并将剔除检查更改为 CheckCube。
3. 创建一些不同的模型,并测试哪种剔除检查最适合它们。