【DX11地形篇】9-16位高度图

参考教程

Tutorial 8: RAW Height Maps

学习记录

  这篇文章中我们介绍使用16位高度图实现的较为精细的地形,本篇代码基于上一篇,无新增类。

  这篇中我们仅仅修改了 TerrainClass 类和 Setup.txt 文件,在 TerrainClass 中我们使用新的 LoadRawHeightMap() 方法来代替之前的 LoadBitmapHeightMap() ,由于我们新的高度图是一张存储高度的图片,所以读取的话只为 y 轴赋值,方法如下:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
bool TerrainClass::LoadRawHeightMap()
{
int error, i, j, index;
FILE* filePtr;
unsigned long long imageSize, count;
unsigned short* rawImage;


// Create the float array to hold the height map data.
m_heightMap = new HeightMapType[m_terrainWidth * m_terrainHeight];
if (!m_heightMap)
{
return false;
}

// Open the 16 bit raw height map file for reading in binary.
error = fopen_s(&filePtr, m_terrainFilename, "rb");
if (error != 0)
{
return false;
}

// Calculate the size of the raw image data.
imageSize = m_terrainHeight * m_terrainWidth;

// Allocate memory for the raw image data.
rawImage = new unsigned short[imageSize];
if(!rawImage)
{
return false;
}

// Read in the raw image data.
count = fread(rawImage, sizeof(unsigned short), imageSize, filePtr);
if(count != imageSize)
{
return false;
}

// Close the file.
error = fclose(filePtr);
if(error != 0)
{
return false;
}

// Copy the image data into the height map array.
for(j=0; j<m_terrainHeight; j++)
{
for(i=0; i<m_terrainWidth; i++)
{
index = (m_terrainWidth * j) + i;

// Store the height at this point in the height map array.
m_heightMap[index].y = (float)rawImage[index];
}
}

// Release the bitmap image data.
delete [] rawImage;
rawImage = 0;

// Release the terrain filename now that it has been read in.
delete [] m_terrainFilename;
m_terrainFilename = 0;

return true;
}

  同时我们将修改 Setup.txt 文件,渲染一张更大的地图:

1
2
3
4
5
Terrain Filename: ./data/heightmap.r16
Terrain Filename: ./data/colormap.bmp
Terrain Height: 1025
Terrain Width: 1025
Terrain Scaling: 300.0

  在 Initialize 方法里,使用 LoadRawHeightMap 代替 LoadBitmapHeightMap

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
bool TerrainClass::Initialize(ID3D11Device* device , char* setupFileName)
{
bool result;

// Get the terrain filename , dimensions , and so forth from the setup file
result = LoadSetupFile(setupFileName);
if (!result) {
return false;
}

// Initialize the terrain height map with the data from the raw file.
result = LoadRawHeightMap();
if(!result)
{
return false;
}

// result = LoadBitmapHeightMap();
// if (!result) {
// return false;
// }

SetTerrainCoordinates();

// Calculate the normals for the terrain data.
result = CalculateNormals();
if(!result)
{
return false;
}

// Load in the color map for the terrain.
result = LoadColorMap();
if(!result)
{
return false;
}

result = BuildTerrainModel();
if (!result) {
return false;
}

// We can now release the height map since it is no longer needed in memory once the 3D terrain model has been built.
ShutdownHeightMap();

// Calculate the tangent and binormal for the terrain model.
CalculateTerrainVectors();

// Load the rendering buffers with the terrain data.
result = InitializeBuffers(device);
if(!result)
{
return false;
}

ShutdownTerrainModel();

return true;
}

  最终效果:

1

  源代码:DX11TerrainTutorial-RawHeightMaps

0%