/
terrain.cpp
152 lines (128 loc) · 4.61 KB
/
terrain.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "terrain.h"
Terrain::Terrain(int width, int depth)
{
this->width = width; this->depth = depth;
heightScale = 0.2f;
int numVertices = depth * width;
vertices.reserve(numVertices);
// fill vector with points
for(int i = 0; i < numVertices; ++i)
{
vertices.push_back(Vector3f((float)(i % width), 0.0f, (float)(int)(i / width)));
}
// make our terrain using fault formation
faultFormation = new FaultFormation(width, depth);
faultFormation->GenerateHeightMap(vertices);
faultFormation->ApplyErosionFilter(vertices, 0.4f);
ApproximateNormals();
GenerateTexture();
}
void Terrain::Render()
{
for(int z = 0; z < depth - 1; ++z)
{
glBegin(GL_TRIANGLE_STRIP);
for(int x = 0; x < width; ++x)
{
glNormal3f(VertexAt(x, z).nx, VertexAt(x, z).ny, VertexAt(x, z).nz);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (float)x / width, (float)z / depth);
//glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, x%2);
glVertex3f(x -(width * 0.5) , VertexAt(x, z).y, z -(depth * 0.5));
glNormal3f(VertexAt(x, z + 1).nx, VertexAt(x, z + 1).ny, VertexAt(x, z + 1).nz);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (float)x / width, (float)(z + 1) / depth);
//glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0f, x%2);
glVertex3f(x -(width * 0.5), VertexAt(x, z + 1).y, z + 1 -(depth * 0.5));
}
glEnd();
}
}
void Terrain::Regenerate()
{
faultFormation->GenerateHeightMap(vertices);
faultFormation->ApplyErosionFilter(vertices, 0.4f);
GenerateTexture();
ApproximateNormals();
}
void Terrain::RandomizeHeights()
{
for(unsigned int i = 0; i < vertices.size(); ++i)
vertices[i].y = (float)(rand() % 255);
ApproximateNormals();
}
void Terrain::GenerateHeightMapFaultFormation()
{
}
inline // allows access to the vector as if it were two dimensional
Vector3f Terrain::VertexAt(const int row, const int column)
{
return (row >= width || column >= depth || row < 0 || column < 0) ? Vector3f() : TrueVertexAt(row, column) * Vector3f(1, heightScale, 1);
}
inline // allows access to the vector as if it were two dimensional
Vector3f Terrain::TrueVertexAt(const int row, const int column)
{
return vertices[row + (column * width)];
}
void Terrain::ApproximateNormals()
{
for(int i = 1; i < width - 2; i++)
{
for(int j = 1; j < depth - 2; j++)
{
Vector3f v = (VertexAt(i + 1, j) - VertexAt(i - 1, j)).crossProduct(VertexAt(i, j + 1) - VertexAt(i, j - 1));
Vector3f& cv = VertexAt(i, j);
cv.nx = v.x; cv.ny = v.y; cv.nz = v.z;
}
}
}
void Terrain::GenerateTexture()
{
TEXTURE textures[2];
int numTextures = 2;
// Load in textures
LoadTexture("grass.tga", textures[0]);
LoadTexture("snow.tga", textures[1]);
// Give each texture a height range
int lastHeight = -1;
for(int i = 0; i < numTextures; ++i)
{
textures[i].lowHeight = lastHeight + 1;
lastHeight += 255 / numTextures;
textures[i].optimalHeight = lastHeight;
textures[i].highHeight = (lastHeight - textures[i].lowHeight) + lastHeight;
}
float scale = (float)width / textures[0].width;
// Cycle through all the textures, take a pixel from each one
// and mix them together based on the height of the terrain at
// that point
GLubyte texture_imageData[256][256][3];
for(int y = 0; y < textures[0].height; ++y)
{
for(int x = 0; x < textures[0].width; ++x)
{
float red = 0.0f, green = 0.0f, blue = 0.0f;
for(int i = 0; i < numTextures; ++i)
{
red += (textures[i].RedAt(x, y) * textures[i].PixelPercentageAtHeight(TrueVertexAt((int)(x * scale), (int)(y * scale)).y));
green += (textures[i].GreenAt(x, y) * textures[i].PixelPercentageAtHeight(TrueVertexAt((int)(x * scale), (int)(y * scale)).y));
blue += (textures[i].BlueAt(x, y) * textures[i].PixelPercentageAtHeight(TrueVertexAt((int)(x * scale), (int)(y * scale)).y));
}
texture_imageData[y][x][0] = red;
texture_imageData[y][x][1] = green;
texture_imageData[y][x][2] = blue;
}
}
// Typical Texture Generation Using Data From The TGA ( CHANGE )
glGenTextures(1, &texture1); // Create The Texture ( CHANGE )
glBindTexture(GL_TEXTURE_2D, texture1);
glTexImage2D(GL_TEXTURE_2D, 0, 24 / 8, textures[0].width, textures[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_imageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glActiveTextureARB(GL_TEXTURE0_ARB); //Set up texture Unit1
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glActiveTextureARB(GL_TEXTURE1_ARB); //Set up texture unit 2
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}