【DX11地形篇】1-地形篇初始框架

参考教程

Tutorial 1: Grid and Camera Movement

学习记录

  这篇文章我们介绍使用 DX11 渲染场景地形的基本框架。如图示:

1

  这篇文章我们先来介绍我们的 ShaderManagerClass 类,这个类管理我们所有的 ShaderClass 。这个类的声明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
////////////////////////////////////////////////////////////////////////////////
// Class name: ShaderManagerClass
////////////////////////////////////////////////////////////////////////////////
class ShaderManagerClass
{
public:
ShaderManagerClass();
ShaderManagerClass(const ShaderManagerClass&);
~ShaderManagerClass();

bool Initialize(ID3D11Device*, HWND);
void Shutdown();

bool RenderColorShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX);
bool RenderFontShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4);

private:
ColorShaderClass* m_ColorShader;
FontShaderClass* m_FontShader;
};

  这个类中声明了我们所需要的 ShaderClass 对象,并提供相应 Render 函数。相比较之前我们框架的结构,当着色器程序变多的时候,很明显是这个会比较适合一些。它的实现也只是调用 ShaderClass 的方法而已。

  除此之外,新增的还有 ZoneClass 类,其声明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
////////////////////////////////////////////////////////////////////////////////
// Class name: ZoneClass
////////////////////////////////////////////////////////////////////////////////
class ZoneClass
{
public:
ZoneClass();
ZoneClass(const ZoneClass&);
~ZoneClass();

bool Initialize(D3DClass*, HWND, int, int, float);
void Shutdown();
bool Frame(D3DClass*, InputClass*, ShaderManagerClass*, float, int);

private:
void HandleMovementInput(InputClass*, float);
bool Render(D3DClass*, ShaderManagerClass*);

private:
UserInterfaceClass* m_UserInterface;
CameraClass* m_Camera;
PositionClass* m_Position;
TerrainClass* m_Terrain;
bool m_displayUI;
};

  这个类我们用来管理 UserInterfaceClassCameraClassPositionClassTerrainClass 等类,原教程中对其描述如下:

  The zone class is the main wrapper class for all of the terrain processing as well as anything that would be related to the terrain.
  Currently it will just move the camera around the terrain grid, but in future tutorials it will be expanded to handle sky, trees, foliage, and other objects related to the terrain.
  We will also try to keep this class fairly generic so that in future tutorials it can just read a text file and populate the zone without any need for code changes.
  For this tutorial the ZoneClass will handle all the camera movement, render the terrain grid, and display the user interface.

  CameraClassPositionClass 我们都比较清楚。

  TerrainClass 封装了地形的模型和渲染功能,类似于 ModelClass,在现在的基础框架下,它只有简单的网格数据,其声明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
////////////////////////////////////////////////////////////////////////////////
// Class name: TerrainClass
////////////////////////////////////////////////////////////////////////////////
class TerrainClass
{
private:
struct VertexType
{
XMFLOAT3 position;
XMFLOAT4 color;
};

public:
TerrainClass();
TerrainClass(const TerrainClass&);
~TerrainClass();

bool Initialize(ID3D11Device*);
void Shutdown();
bool Render(ID3D11DeviceContext*);

int GetIndexCount();

private:
bool InitializeBuffers(ID3D11Device*);
void ShutdownBuffers();
void RenderBuffers(ID3D11DeviceContext*);

private:
ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;
int m_vertexCount, m_indexCount;
};

  UserInterfaceClass 则是简单的封装了对当前信息的输出,相当于我们玩游戏中的各种数据信息(类似于帧数,网络等等),声明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
////////////////////////////////////////////////////////////////////////////////
// Class name: UserInterfaceClass
////////////////////////////////////////////////////////////////////////////////
class UserInterfaceClass
{
public:
UserInterfaceClass();
UserInterfaceClass(const UserInterfaceClass&);
~UserInterfaceClass();

bool Initialize(D3DClass*, int, int);
void Shutdown();

bool Frame(ID3D11DeviceContext*, int, float, float, float, float, float, float);
bool Render(D3DClass*, ShaderManagerClass*, XMMATRIX, XMMATRIX, XMMATRIX);

private:
bool UpdateFpsString(ID3D11DeviceContext*, int);
bool UpdatePositionStrings(ID3D11DeviceContext*, float, float, float, float, float, float);

private:
FontClass* m_Font1;
TextClass *m_FpsString, *m_VideoStrings, *m_PositionStrings;
int m_previousFps;
int m_previousPosition[6];
};

  下篇文章中我们将具体介绍对于地形网格的绘制,着重于介绍 TerrainClass 类。

0%