-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTerrainGenerator.cpp
345 lines (280 loc) · 7.67 KB
/
TerrainGenerator.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
/*
* Geneva Smith
* Terrrain Generator
* File: Terrain Generator
* --------------------------------------
* This program generates a terrain using the HeightMapMesh class. It adds extra features, such as lighting,
* and allows a user to toggle between different lighting and shading modes. It also allows for a mobile
* viewing angle.
*/
//C++ libraries
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <ctime>
#include <math.h>
//OPenGL libraries
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
//Programmer defined libraries and classes
#include "Height Map Mesh.h"
using namespace std; //Allows the use of vectors and I/O
/* Global Variables */
vector <HeightMesh> maps;
//Map parameters
int width, height;
bool start = false;
//Wireframe options
int wireframeOption = 0;
bool wireframeToggle = false;
//Shading and light options
bool shader = true;
bool lights = false;
bool light0on = true;
GLfloat light0pos [] = {1, 10, 0, 1};
GLfloat light1pos [] = {-1, 10, 0, 1};
//Viewpoint rotation
int xrot = 0;
int yrot = 0;
//Drawing the 2D overview map
void OverviewMap(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
maps[0].DrawOverview();
//Clearing the pipeline
glFlush();
glutPostRedisplay();
}
//Drawing the main terrain graphics
void display(void)
{
//Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Transforms
glRotated(xrot, 1, 0, 0);
glRotated(yrot, 0, 1, 0);
glTranslated(0, -1 * maps[0].GetMaxHeight() / 2, 0);
//Lighting
if (lights == true)
{
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT1, GL_POSITION, light1pos);
glEnable(GL_LIGHT1);
}
else glDisable(GL_LIGHTING);
//Axis for referencing (x, y, z)
/*glPushMatrix();
glBegin(GL_LINES);
glColor3d(1, 0, 0);
glVertex3d(-50, 0, 0);
glVertex3d(50, 0, 0);
glColor3d(0, 1, 0);
glVertex3d(0, -50, 0);
glColor3d(0,0,0);
glVertex3d(0, 50, 0);
glColor3d(0, 0, 1);
glVertex3d(0, 0, -30);
glColor3d(1, 1, 1);
glVertex3d(0, 0, 30);
glEnd();
glPopMatrix();*/
//Draw Terrain
maps[0].DrawHeightMesh(wireframeToggle, shader);
if (wireframeOption == 2) maps[0].DrawHeightMesh(false, shader);
//Flush pipeline
glFlush();
//Double Buffering
glutSwapBuffers();
//Flush pipeline
glutPostRedisplay();
}
//Initializing the terrain after the initial parameters are given
void CreateTerrain(void)
{
//Generate flat terrain
HeightMesh terrain(width + 1, height + 1);
//Create hills
terrain.CircleTerrain();
//Force mesh to be global
maps.push_back(terrain);
//Get map parameters - width, length and maximum height
width = maps[0].GetWidth();
height = maps[0].GetLength();
light0pos[1] = maps[0].GetMaxHeight() + 8;
light1pos[1] = maps[0].GetMaxHeight() + 8;
}
void Help()
{
cout << endl;
cout << "W/w -> Turn on wireframe with/without the terrain itself underneath" << endl;
cout << "K/k -> Choose the shading mode (Flat(default) or Gouraud" << endl;
cout << "L/l -> Turn on/off the lights" << endl;
cout << "I/i -> Choose which light to control (two options)" << endl;
cout << "T/t and B/b -> move the light along the y axis (up and down respectively)" << endl;
cout << "G/g and H/h -> move the light along the x axis (left and right respectively)" << endl;
cout << "V/v and Y/y -> move the light along the z axis (forward and backward respectively)" << endl;
cout << "R/r -> Generate a new terrain" << endl;
cout << "Q/q -> Exit the generator" << endl;
cout << endl;
cout << "If you ever need to see the control list again, just hit A/a for Assistance" << endl;
cout << endl;
return;
}
//Initiates the creation of the terrain - user triggered
void getTerrainSize(void)
{
double iwidth, iheight;
cout << "Please enter the width of the terrain: ";
cin >> iwidth;
width = floor(iwidth);
cout << "Please enter the length of the terrain: ";
cin >> iheight;
height = floor(iheight);
CreateTerrain();
}
/* kbd -- the GLUT keyboard function
* key -- the key pressed
* x and y - mouse x and y coordinates at the time the function is called
* Q/q -> Quit
* R/r -> create a new terrain
* I/i -> Toggle between light0 and light1 controls
* T/t -> Move light up y axis
* B/b -> Move light down the y axis
* G/g -> Move light left along the x axis
* H/h -> Move light right along the x axis
* V/v -> Move light forward along the z axis
* Y/y -> Move light backwards along the z axis
* L/l -> Toggle lighting on or off
* W/w -> Toggle wireframe modes
* K/k -> Toggle between flat and smooth shading
* A/a -> Display help script
*/
void kbd(unsigned char key, int x, int y)
{
//Quit
if(key == 'q' || key == 'Q')
{
exit(0);
}
//Reset
else if (key == 'r' || key == 'R')
{
maps.erase(maps.begin());
getTerrainSize();
}
//Help
else if (key == 'A' || key == 'a') Help();
//Toggle light controls
else if (key == 'i' || key == 'I') light0on = !light0on;
//Light controls
//y axis
else if (key == 't' || key == 'T')
{
if (light0on == true) light0pos[1] += 1;
else light1pos[1] += 1;
}
else if (key == 'b' || key == 'B')
{
if (light0on == true) light0pos[1] -= 1;
else light1pos[1] -= 1;
}
//x axis
else if (key == 'g' || key == 'G')
{
if (light0on == true) light0pos[0] -= 1;
else light1pos[0] -= 1;
}
else if (key == 'h' || key == 'H')
{
if (light0on == true) light0pos[0] += 1;
else light1pos[0] += 1;
}
//z axis
else if (key == 'v' || key == 'V')
{
if (light0on == true) light0pos[2] += 1;
else light1pos[2] += 1;
}
else if (key == 'y' || key == 'Y')
{
if (light0on == true) light0pos[2] -= 1;
else light1pos[2] -= 1;
}
//Toggle lighting
else if (key == 'l' || key == 'L') lights = !lights;
//Toggle wireframes
else if (key == 'w' || key == 'W')
{
wireframeOption = (wireframeOption + 1) % 3;
if (wireframeOption != 0) wireframeToggle = true;
else wireframeToggle = false;
}
//Toggle shading
else if (key == 'k' || key == 'K') shader = !shader;
}
//Controlling what the arrow keys trigger
void arrows(GLint specialKey, GLint xMouse, GLint ymouse)
{
//Control the up/down rotation of the viewpoint
if (specialKey == GLUT_KEY_UP)
{
if (xrot - 1 > -90) xrot--;
}
else if (specialKey == GLUT_KEY_DOWN)
{
if (xrot + 1 < 90) xrot++;
}
//Control the left/right rotation of the viewpoint
else if (specialKey == GLUT_KEY_LEFT) yrot++;
else if (specialKey == GLUT_KEY_RIGHT) yrot--;
}
/* MAIN PROGRAM */
int main(int argc, char** argv)
{
//Creating initial terrain
if (start == false)
{
cout << "Welcome to the Terrain Generator! This is an interactive environment where you can control how the ";
cout << "terrain appears. Here are a few controls that you should know about:" << endl;
cout << endl;
Help();
getTerrainSize();
start = true;
}
//Initialization of GLUT windows
glutInit(&argc, argv);
//Overview window
glutInitWindowSize(height, width);
int subWin = glutCreateWindow("Overview");
glutPositionWindow(700, 70);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glOrtho(0, height, width, 0, -5, 5);
glutDisplayFunc(OverviewMap);
//Terrain window
glutInitWindowSize(600, 600);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
int mainWin = glutCreateWindow("Terrain Generator");
//Depth testing
glEnable(GL_DEPTH_TEST);
//Face culling
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glMatrixMode(GL_PROJECTION);
glOrtho(-height / 2, height / 2, -width / 2, width / 2, -width * height / 2, width * height / 2);
//Black Background
glClearColor(0, 0, 0, 0);
//GLUT Callbacks
glutKeyboardFunc(kbd);
glutSpecialFunc(arrows);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}