-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.h
98 lines (72 loc) · 1.95 KB
/
terrain.h
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#if !defined(TERRAIN_H)
#define TERRAIN_H
#include <vector>
#include "bitmap.h"
#include "mathlib.h"
class HeightMap
{
public:
HeightMap();
~HeightMap();
float getHeightScale() const
{ return m_heightScale; }
int getSize() const
{ return m_size; }
int getGridSpacing() const
{ return m_gridSpacing; }
const float *getHeights() const
{ return &m_heights[0]; }
bool create(int size, int gridSpacing, float scale);
void destroy();
void generateDiamondSquareFractal(float roughness);
float heightAt(float x, float z) const;
float heightAtPixel(int x, int z) const
{ return m_heights[z * m_size + x]; };
void normalAt(float x, float z, Vector3 &n) const;
void normalAtPixel(int x, int z, Vector3 &n) const;
private:
void blur(float amount);
unsigned int heightIndexAt(int x, int z) const;
void smooth();
int m_size;
int m_gridSpacing;
float m_heightScale;
std::vector<float> m_heights;
};
class Terrain
{
public:
Terrain();
virtual ~Terrain();
bool create(int size, int gridSpacing, float scale);
void destroy();
void draw();
bool generateUsingDiamondSquareFractal(float roughness);
void update(const Vector3 &cameraPos);
const HeightMap &getHeightMap() const
{ return m_heightMap; }
HeightMap &getHeightMap()
{ return m_heightMap; }
protected:
virtual bool terrainCreate(int size, int gridSpacing, float scale);
virtual void terrainDestroy();
virtual void terrainDraw();
virtual void terrainUpdate(const Vector3 &cameraPos);
private:
struct Vertex
{
float x, y, z;
float nx, ny, nz;
float s, t;
};
bool generateIndices();
bool generateVertices();
bool use16BitIndices() const
{ return m_totalVertices <= 65536; }
unsigned int m_vertexBuffer;
unsigned int m_indexBuffer;
int m_totalVertices;
int m_totalIndices;
HeightMap m_heightMap;
};
#endif