-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptg.cpp
More file actions
383 lines (314 loc) · 9.89 KB
/
Copy pathptg.cpp
File metadata and controls
383 lines (314 loc) · 9.89 KB
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
/**
* @author Pranav Nair
* CS 334 Final Project
* Procedural Terrain Generation
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <sstream>
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
#include "glui/include/GL/glui.h"
#else
#include "GL/glut.h"
#endif
#include "mesh.h"
/* Window Variables */
int windowWidth = 800;
int windowHeight = 800;
/* Camera Variables */
float fovy = 60.0;
float aspect = windowWidth / windowHeight;
float zNear = 1.0;
float zFar = 100.0;
GLfloat cameraX = 0, cameraZ = -7, cameraY = 0; // Starting camera coordinates
float cameraVX = 0.4, cameraVZ = 0.4, cameraVY = 0.4;
float cameraRotateX = 0, cameraRotateY = 0;
GLfloat cameraRotateV = 10.0;
GLfloat myModelMat[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, -1, 0, 1 }
};
/* Mesh variables*/
bool generateWater = false;
Mesh *terrain_mesh;
Mesh *water_mesh;
/* Color constants */
#define DEFAULT_TERRAIN_R 0.92
#define DEFAULT_TERRAIN_G 0.71
#define DEFAULT_TERRAIN_B 0.20
#define DEFAULT_WATER_R 0.0
#define DEFAULT_WATER_G 0.0
#define DEFAULT_WATER_B 1.0
/**
* @brief Generate meshes from grammar file
*
* @param filename grammar file
* @return int return code (0 for success)
*/
int generateFromGrammar(std::string filename) {
// Mesh variables
bool dimensionCheck, terrainCheck;
int dimX, dimZ;
float height, density, water_level;
int gen_water, roughness;
srand(time(NULL));
// Read grammar file
std::ifstream infile(filename);
std::string line;
while (std::getline(infile, line)) {
// Ignore comments
if (line[0] == '#' || line.empty())
continue;
std::istringstream iss(line);
// Read and set properties
if (!dimensionCheck) {
// Check terrain dimensions first
iss >> dimX >> dimZ;
std::cout << "dimx: " << dimX << ", dimZ: " << dimZ << std::endl;
dimensionCheck = true;
} else if (!terrainCheck) {
// Obtain terrain characteristics second
iss >> height >> roughness >> density >> gen_water >> water_level;
std::cout << "height: " << height << ", roughness: " << roughness << ", mountain density: " << density
<< ", generate water: " << (gen_water == 1 ? "yes" : "no") << ", water level: " << water_level << std::endl;
// Create meshes
// TEMP: rand() does not prodice a 32 bit random number
terrain_mesh = new Mesh(dimX, dimZ, rand(), height, 0.0, density, roughness);
terrain_mesh->r = DEFAULT_TERRAIN_R;
terrain_mesh->g = DEFAULT_TERRAIN_G;
terrain_mesh->b = DEFAULT_TERRAIN_B;
if (gen_water == 1) {
generateWater = true;
water_mesh = new Mesh(dimX, dimZ, rand(), 0.0, water_level, 0.0, 1);
water_mesh->r = DEFAULT_WATER_R;
water_mesh->g = DEFAULT_WATER_G;
water_mesh->b = DEFAULT_WATER_B;
}
terrainCheck = true;
} else {
// Encouragers (Encourage certain features in terrain)
std::string token;
iss >> token;
if (token == "M") {
// Encourage mountain
int x, z;
float height, width;
iss >> x >> z >> height >> width;
terrain_mesh->encourageMountain(x, z, height, width);
std::cout << "(Mountain) Location: (" << x << ", " << z << "), Height: "
<< height << ", Width: " << width << std::endl;
} else if (token == "L") {
// Encourage lake
int x, z;
float width;
iss >> x >> z >> width;
terrain_mesh->encourageLake(x, z, width);
std::cout << "(Lake) Location: (" << x << ", " << z << "), Width: " << width << std::endl;
} else if (token == "R") {
// Encourage river
int x0, z0, x1, z1;
// float width;
iss >> x0 >> z0 >> x1 >> z1;
terrain_mesh->encourageRiver(x0, z0, x1, z1);
std::cout << "(River) Start point: (" << x0 << ", " << z0 << "), End point: ("
<< x1 << ", " << z1 << ")" << std::endl;
} else if (token == "C") {
// Custom terrain color
if (generateWater) {
iss >> terrain_mesh->r >> terrain_mesh->g >> terrain_mesh->b
>> water_mesh->r >> water_mesh->g >> water_mesh->b;
std::cout << "(Color) Terrain: (" << terrain_mesh->r << ", " << terrain_mesh->g
<< ", " << terrain_mesh->b << ") Water: (" << water_mesh->r << ", "
<< water_mesh->g << ", " << water_mesh->b << ")" << std::endl;
} else {
iss >> terrain_mesh->r >> terrain_mesh->g >> terrain_mesh->b;
std::cout << "(Color) Terrain: (" << terrain_mesh->r << ", " << terrain_mesh->g
<< ", " << terrain_mesh->b << ")" << std::endl;
}
} else {
// Unknown syntax
// std::cout << "ERROR: Unknown grammar: "<< token << std::endl;
// return 1;
}
}
}
// Generate Meshes
terrain_mesh->generateMesh();
if (generateWater)
water_mesh->generateMesh();
return 0;
}
void openGLInit() {
/* Set clear color */
glClearColor(1.0, 1.0, 1.0, 0.0);
/* Enable the depth buffer */
glEnable(GL_DEPTH_TEST);
printf("%s\n", glGetString(GL_VERSION));
}
/**
* Function invoked when window system events are not being received
*/
void idle()
{
/* Redraw the window */
glutPostRedisplay();
}
/**
* #### Function invoked when an event on special keys occur
*/
void special(int key, int x, int y)
{
// Rotate by counterclockwise 90 offset
float forwardX = -sinf(M_PI * (cameraRotateV * (cameraRotateY)) / 180.0);
float forwardZ = cosf(M_PI * (cameraRotateV * (cameraRotateY)) / 180.0);
float magnitude = sqrtf(powf(forwardX, 2.0) + powf(forwardZ, 2.0));
forwardX /= magnitude;
forwardZ /= magnitude;
float rightX = -forwardZ;
float rightZ = forwardX;
if (key == GLUT_KEY_UP) {
// std::cout << "Moving +z" << std::endl;
// cameraZ++;
cameraZ += forwardZ;
cameraX += forwardX;
} else if (key == GLUT_KEY_DOWN) {
// std::cout << "Moving -z" << std::endl;
// cameraZ--;
cameraZ -= forwardZ;
cameraX -= forwardX;
} else if (key == GLUT_KEY_RIGHT) {
// std::cout << "Moving -x" << std::endl;
// cameraX--;
cameraZ += rightZ;
cameraX += rightX;
} else if (key == GLUT_KEY_LEFT) {
// std::cout << "Moving +x" << std::endl;
// cameraX++;
cameraZ -= rightZ;
cameraX -= rightX;
}
// std::cout << "Position: x: " << cameraX << ", z: " << cameraZ << std::endl;
// std::cout << "Forward: x: " << forwardX << ", z: " << forwardZ << std::endl;
}
/**
* #### Function invoked when an event on regular keys occur
*/
void keyboard(unsigned char k, int x, int y)
{
/* Show which key was pressed */
// std::cout << "Pressed \"" << k << "\" ASCII: " << (int)k << std::endl;
if (k == 'd') {
// std::cout << "Rotating +y" << std::endl;
cameraRotateY++;
} else if (k == 'a') {
// std::cout << "Rotating -y" << std::endl;
cameraRotateY--;
} else if (k == 's') {
// std::cout << "Rotating +x" << std::endl;
cameraRotateX++;
} else if (k == 'w') {
// std::cout << "Rotating -x" << std::endl;
cameraRotateX--;
} else if (k == 27) {
/* Close application if ESC is pressed */
exit(0);
} else if (k == 'z') {
// std::cout << "Moving +y" << std::endl;
cameraY--;
} else if (k == 'x') {
// std::cout << "Moving -y" << std::endl;
cameraY++;
}
// std::cout << "RotationY: " << cameraRotateY << std::endl;
}
/**
* Function invoked for drawing using OpenGL
*/
void display()
{
static int frameCount=0;
/* #### frame count, might come in handy for animations */
frameCount++;
/* Clear the window */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Set the perspective projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, aspect, zNear, zFar);
/* #### Load/set the model view matrix */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixf((GLfloat *)myModelMat);
// Translate to camera coordinates
glTranslatef(0, 0, 0);
// Rotate camera to current rotation
glRotatef(cameraRotateV * cameraRotateX, 1.0, 0, 0);
glRotatef(cameraRotateV * cameraRotateY, 0, 1.0, 0);
// Translate to camera coordinates
glTranslatef(cameraX * cameraVX, cameraY * cameraVY, cameraZ * cameraVZ);
/* Enable client */
glEnableClientState(GL_VERTEX_ARRAY);
/* Draw Terrain */
glVertexPointer(3, GL_FLOAT, 0, terrain_mesh->verts);
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glColor3f(terrain_mesh->r, terrain_mesh->g, terrain_mesh->b);
glDrawElements(GL_TRIANGLES, terrain_mesh->indiciesSize, GL_UNSIGNED_SHORT, terrain_mesh->indicies);
glColor3f(0.0, 0.0, 0.0);
// DO NOT USE GL_LINE_STRIP: Causes weird random lines
glDrawElements(GL_LINES, terrain_mesh->indiciesSize, GL_UNSIGNED_SHORT, terrain_mesh->indicies);
/* Draw Water */
if (generateWater) {
glVertexPointer(3, GL_FLOAT, 0, water_mesh->verts);
glTranslatef(0.0, 0.0, 0.0);
glColor3f(water_mesh->r, water_mesh->g, water_mesh->b);
glDrawElements(GL_TRIANGLES, water_mesh->indiciesSize, GL_UNSIGNED_SHORT, water_mesh->indicies);
glColor3f(0.0, 0.0, 0.0);
// DO NOT USE GL_LINE_STRIP: Causes weird random lines
glDrawElements(GL_LINES, water_mesh->indiciesSize, GL_UNSIGNED_SHORT, terrain_mesh->indicies);
glPopMatrix();
}
/* Disable client */
glDisableClientState(GL_VERTEX_ARRAY);
/* Force execution of OpenGL commands */
glFlush();
/* Swap buffers for animation */
glutSwapBuffers();
}
int main(int argc, char **argv) {
// Check for grammar file
if (argc != 2) {
std::cout << "Please provide grammar file..." << std::endl;
exit(1);
}
/* Initialize the GLUT window */
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(30, 30);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Procedural Terrain Generation");
/* Set OpenGL initial state */
openGLInit();
/* Callback functions */
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
// Generate Meshes
std::string grammar_file(argv[1]);
if (generateFromGrammar(grammar_file) != 0) {
exit(1);
}
std::cout << std::endl << "Verts: " << terrain_mesh->vertsSize << std::endl;
/* Start the main GLUT loop */
glutMainLoop();
//TODO: destroy mallocs
}