This repository was archived by the owner on Jul 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
480 lines (449 loc) · 13 KB
/
Environment.cpp
File metadata and controls
480 lines (449 loc) · 13 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "Environment.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <GLUT/glut.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/freeglut.h>
#endif
#include <iostream>
#include <cmath>
using namespace std;
// Include project files
#include "Interactivity.h"
#include "UserInterface.h"
// Parameters for fish position
float Fishes[3][6] = {{4,-9,0,0,30,0}, {7,-5,0,0,-20,0}, {6,-3,0,0,90,0}};
int InitFishPosit = 0;
// Keeps track if fish collide with water/sand path
bool getWaterHeight = true;
bool getSandHeight = true;
// Used to texture bricks
int brickWidth, brickHeight, brickMaxi;
GLuint brickTexture[1];
// Sets up the grids for water/sand
float waterHeightMap[100][100];
float sandHeightMap[100][100];
// Used for brick textures
GLubyte* brickImage;
/* Sets up textures for bricks */
void Environment::setTextures() {
glGenTextures(2, brickTexture);
// Set the brickImage parameters
glBindTexture(GL_TEXTURE_2D, brickTexture[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Loads in the .ppm file for textures
brickImage = Interactivity::loadPPM("images/brick.ppm", &brickWidth, &brickHeight, &brickMaxi);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, brickWidth, brickHeight, 0, GL_RGB,
GL_UNSIGNED_BYTE, brickImage);
}
/* Creates the real-time waves for water */
// terrain algorithm from assigment 2
void createWaves(int iterations, int size) {
// Will run for how many hills was specified by the user.
for(int i = 0; i < iterations; i++) {
int center_X = rand() % size; // Water Grid (x) midpoint of circle
int center_Z = rand() % size; // Water Grid (z) midpoint of circle
float terrainCircleSize = 35; // Random radius of circle
int randomHeight = (rand() % 1) + 1; // Random height for slope
// Will run for every vertex in the grid.
for(int x = 0; x < size; x++){
for(int z = 0; z < size; z++){
// Performs the circles algorithm
float distanceFromX = x - center_X;
float distanceFromZ = z - center_Z;
float totalDistance = sqrtf((distanceFromX * distanceFromX) + (distanceFromZ * distanceFromZ));
float pd = (totalDistance * 2) / terrainCircleSize;
if (fabs(pd) <= 1.0) {
waterHeightMap[x][z] += (randomHeight / 2.0 + cos(pd * 3.14) * randomHeight / 2.0);
}
}
}
}
}
/* Sets up slopes for sand */
// algorithm from assignment 2
void createSlopes(int iterations, int size) {
// Will run for how many hills was specified by the user.
for(int i = 0; i < iterations; i++){
int center_X = rand() % size; // Sand grid (x) midpoint of circle (1-gridLength)
int center_Z = rand() % size; // Sand grid (z) midpoint of circle (1-gridWidth)
float terrainCircleSize = 30; // Random radius of circle (1-5)
int randomHeight = (rand() % 4) + 1; // Random height for slope (1-5)
// Will run for every vertex in the grid.
for(int x = 0; x < size; x++){
for(int z = 0; z < size; z++){
// Performs the circles algorithm
float distanceFromX = x - center_X;
float distanceFromZ = z - center_Z;
float totalDistance = sqrtf((distanceFromX * distanceFromX) + (distanceFromZ * distanceFromZ));
float pd = (totalDistance * 2) / terrainCircleSize;
if (fabs(pd) <= 1.0) {
sandHeightMap[x][z] += (randomHeight / 2.0 + cos(pd * 3.14) * randomHeight / 2.0);
}
}
}
}
}
/* Draws in the texture for the wall */
void drawWall() {
glColor4f(1, 1, 1, 1);
glScalef(0.112, 0.034, 0);
// 2D flat surface in a 3d space
glBegin(GL_QUADS);
glTexCoord3f(0, 0, 0);
glVertex3f(0, 0, 0);
glTexCoord3f(1, 0, 0);
glVertex3f(265, 0, 0);
glTexCoord3f(1, 1, 0);
glVertex3f(265, 398, 0);
glTexCoord3f(0, 1, 0);
glVertex3f(0, 398, 0);
glEnd();
}
/* Draws the wall mesh */
void drawBorder() {
// enable textures and disable lighting
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glBindTexture(GL_TEXTURE_2D, brickTexture[0]);
// set the transformation values
float trans[5][3];
trans[0][0] = -8.3;
trans[0][1] = -6;
trans[0][2] = 7.4;
trans[1][0] = -8;
trans[1][1] = -13;
trans[1][2] = 21.5;
trans[2][0] = -8.6;
trans[2][1] = -13;
trans[2][2] = -8;
trans[3][0] = 7;
trans[3][1] = -6;
trans[3][2] = 22.5;
trans[4][0] = 22;
trans[4][1] = -13;
trans[4][2] = 21.95;
// Right block
glPushMatrix();
glColor3ub(194, 117, 87);
glTranslatef(trans[0][0], trans[0][1], trans[0][2]);
glScalef(0.5, 13, 30.7);
glutSolidCube(1);
glPopMatrix();
// Right texture front
glPushMatrix();
glTranslatef(trans[1][0], trans[1][1], trans[1][2]);
glRotatef(90, 0, 1, 0);
drawWall();
glPopMatrix();
// Right texture back
glPushMatrix();
glTranslatef(trans[2][0], trans[2][1], trans[2][2]);
glRotatef(270, 0, 1, 0);
drawWall();
glPopMatrix();
// Left block
glPushMatrix();
glColor3ub(194, 117, 87);
glTranslatef(trans[3][0], trans[3][1], trans[3][2]);
glRotatef(90, 0, 1, 0);
glScalef(0.5, 13, 30);
glutSolidCube(1);
glPopMatrix();
// Left texture front
glPushMatrix();
glTranslatef(trans[4][0], trans[4][1], trans[4][2]);
glRotatef(180, 0, 1, 0);
drawWall();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
}
/* Draws the baord that the game takes place on */
void Environment::drawBoard() {
glPushAttrib(GL_LIGHTING_BIT); // so materials dont affect other objects
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
// Set the material values and apply it to the blocks
float amb[4]; // cyan rubber
float spec[4];
float diff[4];
amb[0] = 0;
amb[1] = 0.5;
amb[2] = 0.5;
amb[3] = 1;
diff[0] = 0.4;
diff[1] = 0.5;
diff[2] = 0.5;
diff[3] = 1;
spec[0] = 0.04;
spec[1] = 0.7;
spec[2] = 0.7;
spec[3] = 1;
glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
glMaterialf(GL_FRONT, GL_SHININESS, 0.78125);
// Length of the board for the current level
int len = Interactivity::getLength();
// Colours for the board.
unsigned char blue[] = {0, 20, 113};
unsigned char yellow[] = {255, 235, 59};
// If the player visits the block
Structure::point3D* beenTo = Interactivity::getPlayerBeen();
// Checks all block in the current level
for(int i = 0; i < len; ++i) {
for(int j = 0; j < len - i; ++j) {
bool contains = false;
// Check if the player has landed on the spot
for (int k = 0; k < 50; ++k) {
if (beenTo[k].x == i * 2 && beenTo[k].z == j * 2 + i * 2) {
contains = true;
break;
}
}
// Set block to yellow if the player has landed on it
glColor3ubv(contains ? yellow : blue);
glPushMatrix();
// Draws the rows
glTranslatef(i * 2, j * 2, j * 2 + i * 2);
glutSolidCube(2);
// Draws the columns
for (int k=-12; k<j * 2; k+=2) {
glTranslatef(0, -2, 0);
glutSolidCube(2);
}
glPopMatrix();
}
}
glDisable(GL_COLOR_MATERIAL);
glPopAttrib();
}
/* Resets the water array (used to create waves) */
void resetArray(int size) {
for(int x = 0; x < size; x++){
for(int z = 0; z < size; z++){
waterHeightMap[x][z] = 0.0;
}
}
}
/* Draws the transparent quad strip for the water */
void drawWater(int step) {
glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT); // Prevents the materials from affecting other stuff
glTranslatef(-8, -3, -8);
// Initial load of sand
if (getSandHeight) {
getSandHeight = !getSandHeight;
createWaves(3, 30);
}
// Initial load of the waves
if ((step % 40 == 0 && !UserInterface::calculatingScore())) {
resetArray(30);
createWaves(3, 30);
}
// Material to make water plane look like water
float m_ambient[] = {0, 0.509, 1, 0.75};
glMaterialfv(GL_FRONT, GL_AMBIENT, m_ambient);
float m_diff[] = {0, 0.509, 0.501, 0.75};
glMaterialfv(GL_FRONT, GL_DIFFUSE, m_diff);
float m_specular[] = {0, 0.501, 0.501, 0.75};
glMaterialfv(GL_FRONT, GL_SPECULAR, m_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 0.1);
// Draws the water plane
for(int i = 0; i < 30; i++){
for(int j = 0; j < 30; j++){
glBegin(GL_QUAD_STRIP);
glNormal3f(0, 1, 0);
//Draws the quad strip
glVertex3f(i , waterHeightMap[i][j + 1] , j + 1);
glVertex3f(i + 1, waterHeightMap[i + 1][j + 1], j + 1);
glVertex3f(i , waterHeightMap[i][j] , j );
glVertex3f(i + 1, waterHeightMap[i + 1][j] , j );
glEnd();
}
}
glPopAttrib();
glPopMatrix();
}
/* Draws the quad strip for the sand */
void drawSand() {
glPushMatrix();
glPushAttrib(GL_LIGHTING_BIT); // So the materials don't affect other stuff
glTranslatef(-8, -12, -8);
// Initial load of the sand
if(getSandHeight) {
getSandHeight = !getSandHeight;
createSlopes(2, 30);
}
// set the material values and apply it
float amb[4];
float spec[4];
float diff[4];
amb[0] = 0.19225;
amb[1] = 0.19225;
amb[2] = 0.19225;
amb[3] = 1;
diff[0] = 0.50754;
diff[1] = 0.50754;
diff[2] = 0.50754;
diff[3] = 1;
spec[0] = 0.508274;
spec[1] = 0.508274;
spec[2] = 0.508274;
spec[3] = 1;
glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
glMaterialf(GL_FRONT, GL_SHININESS, 0.4);
// Draws the sand plane
for(int i = 0; i < 30; i++) {
for(int j = 0; j < 30; j++) {
glBegin(GL_QUAD_STRIP);
glNormal3f(0, 1, 0);
glVertex3f(i , sandHeightMap[i][j + 1], j + 1);
glVertex3f(i + 1, sandHeightMap[i + 1][j + 1], j + 1);
glVertex3f(i , sandHeightMap[i][j], j );
glVertex3f(i + 1, sandHeightMap[i + 1][j], j );
glEnd();
}
}
glPopAttrib();
glPopMatrix();
}
/* Initializes where the fish will start */
void InitFishPosition(void){
for(int i = 0; i < 3; i++) {
Fishes[i][0] = rand() % 5;
Fishes[i][4] = rand() % 360;
}
InitFishPosit = 1;
}
/* Draws the fish to the scene */
void drawFish(int n) {
glPushAttrib(GL_LIGHTING_BIT); // So the materials don't affect other stuff
int len = Interactivity::getLength() * 2;
// set the material values and apply it
float amb[4]; // silver
float spec[4];
float diff[4];
amb[0] = 0.19225;
amb[1] = 0.19225;
amb[2] = 0.19225;
amb[3] = 1;
diff[0] = 0.50754;
diff[1] = 0.50754;
diff[2] = 0.50754;
diff[3] = 1;
spec[0] = 0.508274;
spec[1] = 0.508274;
spec[2] = 0.508274;
spec[3] = 1;
glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
glMaterialf(GL_FRONT, GL_SHININESS, 0.4);
// Start drawing
glPushMatrix();
glRotatef((n == 1 ? 1 : -1) * Fishes[n][4], 0, 1, 0);
glTranslatef(Fishes[n][0]+len,Fishes[n][1],0);
glPushMatrix();
glRotatef((n == 1 ? 180 : 0), 0, 1, 0);
glScalef(0.5,0.5,0.5);
// Draw body
glColor3f(0,0,1);
glScalef(1,1,1.5);
glutSolidSphere(1, 100, 100);
// Right eye
glPushMatrix();
glScalef(1,1,0.66);
glTranslatef(-0.35,0.3,1.1);
glColor3f(1,1,1);
glutSolidSphere(0.4, 100, 100);
// Right pupil
glPushMatrix();
glTranslatef(0,0,0.3);
glColor3f(0,0,0);
glutSolidSphere(0.2,100,100);
glPopMatrix();
glPopMatrix();
// Left eye
glPushMatrix();
glScalef(1,1,0.66);
glTranslatef(0.35,0.3,1.1);
glColor3f(1,1,1);
glutSolidSphere(0.4, 100, 100);
// Left pupil
glPushMatrix();
glTranslatef(0,0,0.3);
glColor3f(0,0,0);
glutSolidSphere(0.2,100,100);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor3f(0,0,1);
glScalef(0.8,0.8,0.8);
// Tail
glTranslatef(0,0,-2);
int flip = rand()%2;
glRotatef((flip == 1? 10 : -10),0,1,0);
glutSolidCone(1, 1, 100, 100);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopAttrib();
}
/* Gives the fish movement during the game */
void renderFish(int step) {
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
// Initialize fish position
if(InitFishPosit == 0) {
InitFishPosition();
}
else {
int len = Interactivity::getLength();
glPushMatrix();
glScalef(0.7, 0.7, 0.7);
if (step % 1 == 0) {
// draw each fish
for(int i = 0; i < 3 ; i++){
glPushMatrix();
glTranslatef(len,0,len);
switch(i) { // set the speeds
case 0:
Fishes[i][4] += 1; // rotation speed
break;
case 1:
Fishes[i][4] += 0.3; // rotation speed
break;
case 2:
Fishes[i][4] += 0.6; // rotation speed
break;
}
drawFish(i);
glPopMatrix();
}
}
glPopMatrix();
}
glDisable(GL_COLOR_MATERIAL);
}
/* Draws everything except for the player and the enemies */
void Environment::drawEnvironment(int step) {
// draw in an order of:
// 1. opaque objects (back drawn first)
// 2. Translucent objects
drawSand();
Environment::drawBoard();
drawBorder();
renderFish(step);
drawWater(step);
}