-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terrain.cpp
406 lines (338 loc) · 14.5 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
CS 3GC3 Assignment 3
Stuart Douglas - 1214422
Anagh Goswami - 1217426
November 10th, 2014
*/
#include "Terrain.h"
#include <vector>
#include <stdlib.h>
#include <math.h>
#ifdef __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <GLUT/glut.h>
#else
# include <windows.h>
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/freeglut.h>
#endif
/*****************************************
* GLOBAL VARIABLES
****************************************/
const float MAX_HEIGHT = 40;
float vertexNormals[MAX_TERRAIN_SIZE][MAX_TERRAIN_SIZE][3];
float faceNormals[MAX_TERRAIN_SIZE][MAX_TERRAIN_SIZE][3];
float maxTerrainHeightValue = 0;
/*****************************************
* Constructor
****************************************/
Terrain::Terrain(int size) {
//normalize size to acceptable value
if (size < 50)
terrainSize = 50;
if (size > MAX_TERRAIN_SIZE)
terrainSize = MAX_TERRAIN_SIZE;
else
terrainSize = size;
//initialize heightmap 2D array
heightMap = new float*[terrainSize];
for (int i = 0; i < terrainSize; ++i)
heightMap[i] = new float[terrainSize];
//make the terrain
generateTerrain();
}
/*****************************************
* Initializes heightMap with heights as
* per line cutting & circles algorithms
****************************************/
void Terrain::generateTerrain() {
//reset heightmap
maxTerrainHeightValue = 0;
for (int x = 0; x < terrainSize; x++) {
for (int z = 0; z < terrainSize; z++) {
heightMap[x][z] = 10;
}
}
//the following algorithms use code from
//www.lighthouse3d.com/opengl/terrain/index.php
//(the website linked in the assignment)
if (terrainAlgorithm == CIRCLE) {
//these values all depend on the terrain size (big terrain needs more circles)
int iterations = terrainSize*3 + 600;
float displacement = 2 + 3*(terrainSize/100.0);
float terrainCircleRadius = 10 + 5*(terrainSize/100.0);
//choose a circle, raise points in it
for (int i = 0; i < iterations; i++) {
//choose random point
float randX = ((double) rand() / RAND_MAX) * terrainSize;
float randZ = ((double) rand() / RAND_MAX) * terrainSize;
//check all points in circle, raise them appropriately
for (int x = randX-terrainCircleRadius; x <= randX+terrainCircleRadius; x++) {
for (int z = randZ-terrainCircleRadius; z <= randZ+terrainCircleRadius; z++) {
//if our point in terrain, raise it
if (x >= 0 && x < terrainSize && z >= 0 && z < terrainSize) {
//determine distance to circle w/ distance algorithm
float pointDist = sqrtf(((x-randX)*(x-randX) + (z-randZ)*(z-randZ)))*2.0/terrainCircleRadius;
if (fabs(pointDist) <= 1.0)
heightMap[x][z] += displacement/2.0 + cosf(pointDist*3.14)*displacement/2.0;
if (heightMap[x][z] > maxTerrainHeightValue)
maxTerrainHeightValue = heightMap[x][z];
}
}
}
displacement = displacement >= 0.06 ? displacement-0.01 : 0.05;
}
}
//line fault algorithm
else {
int iterations = (terrainSize*terrainSize*terrainSize)/10000+200, v;
float displacement = 1.2, a, b, c, d;
for (int i = 0; i < iterations; i++) {
//choose random line
v = rand();
a = sinf(v);
b = cosf(v);
d = sqrtf(2.0*(terrainSize*terrainSize));
c = ((double) rand()/RAND_MAX) * d - d/2.0;
//iterate over all points in heightmap
for (int x = 0; x < terrainSize; x++) {
for (int z = 0; z < terrainSize; z++) {
//increase the height
if (a*x + b*z - c < 0) {
heightMap[x][z] = heightMap[x][z]+displacement < MAX_HEIGHT ? heightMap[x][z] += displacement : MAX_HEIGHT;
if (heightMap[x][z] > maxTerrainHeightValue)
maxTerrainHeightValue = heightMap[x][z];
}
//decrease the height
else
heightMap[x][z] = heightMap[x][z]-displacement > 0 ? heightMap[x][z] -= displacement : 0;
}
}
displacement = displacement > 0.2 ? displacement-0.001 : 0.2;
}
smoothTerrain(terrainSize/600.0+0.1);
}
calculateVertexNormals();
calculateFaceNormals();
}
/*****************************************
* Smooths out the rough terrain using
* algorithm from:
* www.lighthouse3d.com/opengl/terrain/index.php3?smoothing
****************************************/
void Terrain::smoothTerrain(float smooth) {
if (smooth < 0)
smooth = 0;
if (smooth > 0.9)
smooth = 0.9;
//rows, left to right
for (int x = 1; x < terrainSize; x++)
for (int z = 0; z < terrainSize; z++)
heightMap[x][z] = heightMap[x-1][z]*smooth + heightMap[x][z]*(1-smooth);
//rows, right to left
for (int x = terrainSize-2; x > -1; x--)
for (int z = 0; z < terrainSize; z++)
heightMap[x][z] = heightMap[x+1][z]*smooth + heightMap[x][z]*(1-smooth);
//columns, bottom to top
for (int x = 0; x < terrainSize; x++)
for (int z = 1; z < terrainSize; z++)
heightMap[x][z] = heightMap[x][z-1]*smooth + heightMap[x][z]*(1-smooth);
//columns, top to bottom
for (int x = 0; x < terrainSize; x++)
for (int z = terrainSize-2; z > -1; z--)
heightMap[x][z] = heightMap[x][z+1]*smooth + heightMap[x][z]*(1-smooth);
}
/*****************************************
* Draws the generated terrain
****************************************/
void Terrain::drawTerrain() {
float specular[4] = {0.1,0.1,0.1, 0.5};
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 2);
//colour is more white w/ more height, green for wireframe
float colour;
float terrainOffset = terrainSize/2.0;
//set wireframe mode
bool drawingWireframe = false;
bool firstIteration = true;
if (wireframeMode == SOLID) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
else if (wireframeMode == WIREFRAME) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawingWireframe = true;
}
else if (wireframeMode == BOTH) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawingWireframe = true;
}
//iterate over all values in heightmap
DrawTerrainLabel:
for (int x = 0; x < terrainSize-1; x++) {
for (int z = 0; z < terrainSize-1; z++) {
//set snow and water materials (it looks dumb with the circle algorithm so we disable it)
if (heightMap[x][z] <= 4){
float diffuseWater[4] = {0,0,0.8, 1};
float ambientWater[4] = {0,0,0.8, 1};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambientWater);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseWater);
}
else if (heightMap[x][z] > 4 && heightMap[x][z] <= 8 ) {
float diffuseGrass[4] = {0,0.3,0.2, 1};
float ambientGrass[4] = {0, 0.3, 0.2, 1};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambientGrass);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseGrass);
}
else if (heightMap[x][z] >= 21 ) {
float diffuseSnow[4] = {1,1,1, 1};
float ambientSnow[4] = {1,1,1, 1};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambientSnow);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseSnow);
}
else {
float diffuseDefault[4] = {0.52,0.26,0.08, 1.0};
float ambientDefault[4] = {0.52,0.26,0.08, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambientDefault);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseDefault);
}
//draw the quad
glBegin(GL_QUADS);
colour = (float) heightMap[x][z]/ (float) maxTerrainHeightValue;
drawingWireframe ? glColor3f(0, 0.8, 0.1) : glColor3f(colour, colour, colour);
usingVertexNormals ? glNormal3fv(vertexNormals[x][z]) : glNormal3fv(faceNormals[x][z]);
glVertex3f(x-terrainOffset, heightMap[x][z], z-terrainOffset);
colour = (float) heightMap[x][z+1]/ (float) maxTerrainHeightValue;
drawingWireframe ? glColor3f(0, 0.8, 0.1) : glColor3f(colour, colour, colour);
usingVertexNormals ? glNormal3fv(vertexNormals[x][z+1]) : glNormal3fv(faceNormals[x][z+1]);
glVertex3f(x-terrainOffset, heightMap[x][z+1], z+1-terrainOffset);
colour = (float) heightMap[x+1][z+1]/ (float) maxTerrainHeightValue;
drawingWireframe ? glColor3f(0, 0.8, 0.1) : glColor3f(colour, colour, colour);
usingVertexNormals ? glNormal3fv(vertexNormals[x+1][z+1]) : glNormal3fv(faceNormals[x+1][z+1]);
glVertex3f(x+1-terrainOffset, heightMap[x+1][z+1], z+1-terrainOffset);
colour = (float) heightMap[x+1][z]/ (float) maxTerrainHeightValue;
drawingWireframe ? glColor3f(0, 0.8, 0.1) : glColor3f(colour, colour, colour);
usingVertexNormals ? glNormal3fv(vertexNormals[x+1][z]) : glNormal3fv(faceNormals[x+1][z]);
glVertex3f(x+1-terrainOffset, heightMap[x+1][z], z-terrainOffset);
glEnd();
}
}
//draw again if wireframe = both
if (wireframeMode == BOTH && firstIteration) {
firstIteration = false;
drawingWireframe = false;
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
goto DrawTerrainLabel;
}
}
/**************************************************************
* calculates normals for every vertex in the heightmap.
* Code was taken from:
* www.lighthouse3d.com/opengl/terrain/index.php3?normals
* We modified the code to suit our needs.
**************************************************************/
void Terrain::calculateVertexNormals() {
//calculate normals
for (int x = 0; x < terrainSize-1; x++) {
for (int z = 0; z < terrainSize-1; z++) {
//x, z
float t1[3];
t1[0] = x; t1[1] = heightMap[x][z]; t1[2] = z;
//x+1, z
float t3[3];
t3[0] = x+1; t3[1] = heightMap[x+1][z]; t3[2] = z;
//x, z+1
float t2[3];
t2[0] = x; t2[1] = heightMap[x][z+1]; t2[2] = z+1;
float v1[3] = {t2[0]-t1[0], t2[1]-t1[1], t2[2]-t1[2]};
float v2[3] = {t3[0]-t1[0], t3[1]-t1[1], t3[2]-t1[2]};
float vx = v1[1]*v2[2] - v1[2]*v2[1];
float vy = v1[2]*v2[0] - v1[0]*v2[2];
float vz = v1[0]*v2[1] - v1[1]*v2[0];
float len = sqrtf(vx*vx + vy*vy + vz*vz);
float nv[3] = {vx/len, vy/len, vz/len};
vertexNormals[x][z][0] = nv[0];
vertexNormals[x][z][1] = nv[1];
vertexNormals[x][z][2] = nv[2];
}
}
}
/**************************************************************
* calculates normals for every FACE in the heightmap. The indice
* of the bottom corner (x=0,z=0 for bottom left quad) of each
* quad holds the face normal of that quad in the faceNormals array.
* www.lighthouse3d.com/opengl/terrain/index.php3?normals was
* used as a reference. Their code is for calculating vertex normals
* from face normals, we calculated faces by normalizing vertex ones.
**************************************************************/
void Terrain::calculateFaceNormals() {
//iterate over all values in heightmap except right most
//and topmost vertices (since bottom left holds faceNormal)
for (int x = 0; x < terrainSize-1; x++) {
for (int z = 0; z < terrainSize-1; z++) {
//x, z
float v1[3];
v1[0] = vertexNormals[x][z][0];
v1[1] = vertexNormals[x][z][1];
v1[2] = vertexNormals[x][z][2];
//x+1, z
float v2[3];
v2[0] = vertexNormals[x+1][z][0];
v2[1] = vertexNormals[x+1][z][1];
v2[2] = vertexNormals[x+1][z][2];
//x+1, z+1
float v3[3];
v3[0] = vertexNormals[x+1][z+1][0];
v3[1] = vertexNormals[x+1][z+1][1];
v3[2] = vertexNormals[x+1][z+1][2];
//x, z+1
float v4[3];
v4[0] = vertexNormals[x][z+1][0];
v4[1] = vertexNormals[x][z+1][1];
v4[2] = vertexNormals[x][z+1][2];
//normalize the 3 surrounding vertex normals
faceNormals[x][z][0] = (v1[0]+v2[0]+v3[0]+v4[0])/4.0;
faceNormals[x][z][1] = (v1[1]+v2[1]+v3[1]+v4[1])/4.0;
faceNormals[x][z][2] = (v1[2]+v2[2]+v3[2]+v4[2])/4.0;
}
}
}
/*****************************************
* Toggles through the 3 wireframe modes.
*****************************************/
void Terrain::changeWireframeMode() {
if (wireframeMode == SOLID)
wireframeMode = WIREFRAME;
else if (wireframeMode == WIREFRAME)
wireframeMode = BOTH;
else if (wireframeMode == BOTH)
wireframeMode = SOLID;
}
/*****************************************
* Toggles through the 3 wireframe modes.
*****************************************/
void Terrain::changeTerrainAlgorithm(TerrainAlgorithm algorithm) {
terrainAlgorithm = algorithm;
generateTerrain();
}
/*****************************************
* Returns string of wireframe mode
****************************************/
char* Terrain::getWireframeMode() {
if (wireframeMode == SOLID)
return (char*) "SOLID";
else if (wireframeMode == WIREFRAME)
return (char*) "WIREFRAME";
else
return (char*) "BOTH";
}
/*****************************************
* Returns string of wireframe mode
****************************************/
char* Terrain::getAlgorithm() {
if (terrainAlgorithm == FAULT)
return (char*) "FAULT";
else
return (char*) "CIRCLE";
}