/
Terrain.cpp
329 lines (254 loc) · 6.76 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include <cstring>
#include "Image.h"
#include "Matrices.h"
#include "Vertex.h"
#include "Terrain.h"
#include <GL/glut.h>
using namespace std;
Terrain::Terrain(int width, int height)
:_size(width, height)
,_offset(0, 0)
,_dataSize(0)
,_heightMatrix(NULL)
{
if( this->_size.width >= 0 && this->_size.height >= 0 )
{
this->_dataSize = (unsigned int)((this->_size.width+1)*(this->_size.height+1));
this->_heightMatrix = new Vertex[this->_dataSize];
this->UpdateVertices();
}
#ifdef DEBUG
cout << "Terrain constructor " << this << endl;
#endif
}
Terrain::Terrain(Vector2i size)
:_size(size)
,_offset(0, 0)
,_dataSize(0)
,_heightMatrix(NULL)
{
if( this->_size.width >= 0 && this->_size.height >= 0 )
{
this->_dataSize = (unsigned int)((this->_size.width+1)*(this->_size.height+1));
this->_heightMatrix = new Vertex[this->_dataSize];
this->UpdateVertices();
}
#ifdef DEBUG
cout << "Terrain constructor " << this << endl;
#endif
}
Terrain::Terrain(Image* image)
:_size(0, 0)
,_offset(0, 0)
,_dataSize(0)
,_heightMatrix(NULL)
{
if( image )
{
this->_size.width = (int)image->GetSize().width-1;
this->_size.height = (int)image->GetSize().height-1;
if( this->_size.width >= 0 && this->_size.height >= 0 )
{
this->_dataSize = (unsigned int)((this->_size.width+1)*(this->_size.height+1));
this->_heightMatrix = new Vertex[this->_dataSize];
this->UpdateVertices();
for( int y = 0; y <= this->_size.height; ++y )
{
for( int x = 0; x <= this->_size.width; ++x )
{
unsigned char pixelValue = 255-*image->GetPixelColor(x, y);
this->SetHeight(x, y, (float)pixelValue/255.0f);
}
}
}
}
#ifdef DEBUG
cout << "Terrain constructor " << this << endl;
#endif
}
Terrain::~Terrain()
{
if( this->_heightMatrix )
{
// TODO (Viet Nguyen): Figure out what this fails.
//delete this->_heightMatrix;
}
#ifdef DEBUG
cout << "Terrain destructor " << this << endl;
#endif
}
bool Terrain::IsValid()
{
return (this->_heightMatrix != NULL);
}
void Terrain::LoadHeightMap(Image* image)
{
}
// TODO (Viet Nguyen): Preserve the previous height information.
void Terrain::SetSize(int width, int height)
{
if( width >= 0 && height >= 0 )
{
this->_size.Set(width, height);
if( this->IsValid() )
{
delete this->_heightMatrix;
this->_heightMatrix = NULL;
}
this->_dataSize = (unsigned int)((this->_size.width+1)*(this->_size.height+1));
this->_heightMatrix = new Vertex[this->_dataSize];
this->UpdateVertices();
}
}
void Terrain::SetSize(Vector2i size)
{
this->SetSize(size.width, size.height);
}
Vector2i Terrain::GetSize()
{
return this->_size;
}
void Terrain::SetOffset(int x, int y)
{
this->_offset.Set(x, y);
this->UpdateVertices();
}
void Terrain::SetOffset(Vector2i offset)
{
this->SetOffset(offset.x, offset.y);
}
Vector2i Terrain::GetOffset()
{
return this->_offset;
}
void Terrain::SetHeight(int x, int y, float height)
{
if( this->IsValid() )
{
if( x >= 0 && x <= this->_size.width &&
y >= 0 && y <= this->_size.height )
{
this->_heightMatrix[x+y*(this->_size.width+1)].Position().y = height;
}
}
}
void Terrain::SetHeight(Vector2i pos, float height)
{
this->SetHeight(pos.x, pos.y, height);
}
float Terrain::GetHeight(int x, int y)
{
if( this->IsValid() )
{
if( x >= 0 && x <= this->_size.width &&
y >= 0 && y <= this->_size.height )
{
return this->_heightMatrix[x+y*(this->_size.width+1)].Position().y;
}
}
return 0.0f;
}
float Terrain::GetHeight(Vector2i pos)
{
return this->GetHeight(pos.x, pos.y);
}
float Terrain::GetHeight(float x, float y, float z)
{
Vector4f worldPos(x, y, z, 1.0f);
Matrix44f worldToObject;
worldToObject = // WARNING (Viet Nguyen): The matrix multiply order is currently implemented INCORRECTLY. It should be scale, rotate, translate left to right.
Matrix44f::Translate(
-this->_position.x,
-this->_position.y,
-this->_position.z)*
Matrix44f::EulerRotate(
-this->_eulerRotation.x,
-this->_eulerRotation.y,
-this->_eulerRotation.z)*
Matrix44f::Scale(
1.0f/this->_scale.x,
1.0f/this->_scale.y,
1.0f/this->_scale.z);
Vector4f localPos = worldToObject*worldPos;
localPos.x = localPos.x-(float)this->_offset.x;
localPos.z = -localPos.z-(float)this->_offset.y;
if( localPos.x < 0 || localPos.x > this->_size.x ||
localPos.z < 0 || localPos.z > this->_size.y )
{
return 0.0f;
}
else
{
Vector2i lowPos((int)floor(localPos.x), (int)floor(localPos.z));
Vector2f posMod(localPos.x-floor(localPos.x), localPos.z-floor(localPos.z));
float leftHeight =
this->GetHeight(lowPos.x, lowPos.y)*(1.0f-posMod.y)+
this->GetHeight(lowPos.x, lowPos.y+1)*(posMod.y);
float rightHeight =
this->GetHeight(lowPos.x+1, lowPos.y)*(1.0f-posMod.y)+
this->GetHeight(lowPos.x+1, lowPos.y+1)*(posMod.y);
float height = leftHeight*(1.0f-posMod.x)+rightHeight*(posMod.x);
return height;
}
}
float Terrain::GetHeight(Vector3f pos)
{
return this->GetHeight(pos.x, pos.y, pos.z);
}
float Terrain::GetScaledHeight(int x, int y)
{
return this->GetHeight(x, y)*this->_scale.y;
}
float Terrain::GetScaledHeight(Vector2i pos)
{
return this->GetHeight(pos)*this->_scale.y;
}
float Terrain::GetScaledHeight(float x, float y, float z)
{
return this->GetHeight(x, y, z)*this->_scale.y;
}
float Terrain::GetScaledHeight(Vector3f pos)
{
return this->GetHeight(pos)*this->_scale.y;
}
void Terrain::Update()
{
if( this->IsValid() )
{
this->Object::StartRecording();
glBegin(GL_QUADS);
for( int y = 0; y < this->_size.height; ++y )
{
for( int x = 0; x < this->_size.width; ++x )
{
this->_heightMatrix[(x+0)+(y+0)*(this->_size.width+1)].Execute();
this->_heightMatrix[(x+1)+(y+0)*(this->_size.width+1)].Execute();
this->_heightMatrix[(x+1)+(y+1)*(this->_size.width+1)].Execute();
this->_heightMatrix[(x+0)+(y+1)*(this->_size.width+1)].Execute();
}
}
glEnd();
this->Object::StopRecording();
}
}
void Terrain::UpdateVertices()
{
if( this->IsValid() )
{
for( int y = 0; y <= this->_size.height; ++y )
{
for( int x = 0; x <= this->_size.width; ++x )
{
Vertex* vertex = this->_heightMatrix+x+y*(this->_size.width+1);
vertex->Position().x = (float)x + (float)this->_offset.x;
vertex->Position().z = -(float)y - (float)this->_offset.y;
vertex->TextureCoords().u = (float)x / (float)this->_size.width;
vertex->TextureCoords().v = (float)y / (float)this->_size.height;
vertex->Normal().x = 0.0f;
vertex->Normal().y = 1.0f;
vertex->Normal().z = 0.0f;
}
}
}
}