forked from chaostheory1994/Final-Project
/
main.cpp
490 lines (419 loc) · 14 KB
/
main.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
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
481
482
483
484
485
486
487
488
489
490
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#define _USE_MATH_DEFINES
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include "Player.h"
#include "Entity.h"
#include "Map.h"
#include "Defines.h"
#ifdef __WIN32
#include <windows.h>
#endif
#define TRUE 1
#define FALSE 0
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define my_assert(X,Y) ((X)?(void) 0:(printf("error:%s in %s at %d", Y, __FILE__, __LINE__), myabort()))
#define DELTA_TIME 10 /* defined to be 10 msec */
#define GAME_UPDATE_SPEED 25 /* in fps, how many times a second the game should update. */
#define MAX_FRAME_SKIP 5 /* Max number of frames that the program can skip to update game mechanics */
#define CAMERA_ANGLE 0 /* WARNING: right now anything but 0 will mess with mouse raycast */
#define DEFAULT_WINDOW_SIZE_X 200.0
#define DEFAULT_WINDOW_SIZE_Y 200.0
void myabort(void);
void glut_setup(void);
void gl_setup(void);
void my_setup(int argc, char **argv);
void my_display(void);
void my_reshape(int w, int h);
void my_keyboard(unsigned char key, int x, int y);
void my_mouse_drag(int x, int y);
void my_mouse(int button, int state, int mousex, int mousey);
void my_idle(void);
#ifdef __linux__
unsigned GetTickCount();
#endif
bool gluInvertMatrix(const float*, float*);
// Variables.
const int SKIP_TICKS = 1000 / GAME_UPDATE_SPEED;
unsigned long long next_game_tick;
unsigned long long next_fps_update;
float interpolation;
int loops;
int width;
int height;
// Game Variables
Player* p;
Map* m;
// Camera Variables.
int camera_offsetX;
int camera_offsetZ;
int cameraY;
int camera_distance;
void myabort(void) {
abort();
exit(1); /* exit so g++ knows we don't return. */
}
int main(int argc, char **argv) {
/* General initialization for GLUT and OpenGL
Must be called first */
glutInit(&argc, argv);
/* we define these setup procedures */
glut_setup();
gl_setup();
my_setup(argc, argv);
/* go into the main event loop */
glutMainLoop();
return (0);
}
/* This function sets up the windowing related glut calls */
void glut_setup(void) {
/* specify display mode -- here we ask for a double buffer and RGB coloring */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
/* make a 400x300 window with the title of "GLUT Skeleton" placed at the top left corner */
glutInitWindowSize(800, 600);
width = 800;
height = 600;
glutInitWindowPosition(0, 0);
glutCreateWindow("GLUT Demo 1");
/*initialize callback functions */
glutDisplayFunc(my_display);
glutReshapeFunc(my_reshape);
glutMouseFunc(my_mouse);
glutMotionFunc(my_mouse_drag);
glutKeyboardFunc(my_keyboard);
glutIdleFunc(my_idle);
return;
}
/* This function sets up the initial states of OpenGL related enivornment */
void gl_setup(void) {
/* specifies a background color: black in this case */
glClearColor(0, 0, 0, 0);
/* setup for simple 2d projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* set the viewable portion */
gluPerspective(20, width / height, 1, 100.0);
glMatrixMode(GL_MODELVIEW);
return;
}
void my_setup(int argc, char **argv) {
float rad;
next_game_tick = GetTickCount();
next_fps_update = GetTickCount();
p = new Player(0, 0);
m = new Map(100, 100);
m->set_player(p);
// We are going to setup the camera location.
// The default camera distance will be defined as a macro.
// We can adjust the degrees using the macro.
rad = (CAMERA_ANGLE * M_PI) / 180;
camera_distance = 50;
cameraY = cos(rad) * camera_distance;
camera_offsetZ = sin(rad) * camera_distance;
camera_offsetX = 0;
#ifdef DEBUG_MESSAGES
// Section for Debug Messages.
#endif
return;
}
void my_reshape(int w, int h) {
/* define viewport -- x, y, (origin is at lower left corner) width, height */
glViewport(0, 0, w, h);
width = w;
height = h;
}
/* Sample keyboard callback function.
This allows the user to quit the program
by typing the letter q.
*/
void my_keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'q':
case 'Q':
exit(0);
break;
#ifdef DEBUG_MESSAGES
case '1':
printf("Player Coord: %f, %f\n", p->getX(), p->getZ());
break;
case '2':
p->move(1, 1);
break;
case '3':
p->move(-1, -1);
break;
#endif
default: break;
}
return;
}
/* currently doesn't do anything */
void my_mouse_drag(int x, int y) {
return;
}
/* stubbed but doesn't do anything either
(mousex, mousey) are in screen coordinates
that is, (0,0) is at the upper left corner
*/
void my_mouse(int button, int state, int mousex, int mousey) {
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
/* The Following Section is from
* www.antongerdelan.net/opengl/raycasting.html
*
float model[16];
float proj[16];
float temp[16];
float norm;
// We have to remember that the mouse coordinate does not directly relate to map dist.
// We have to reverse the graphics pipeline to convert
// mouse coordinates into world coordinates.
// We start by normalizing the coordinates.
// mousex/width would give 0-1 range
// but we want -1 to 1 range so we multiply by 2
// to get the range of 2 and then subtrace by one
// to get the range we want,
float tempx, tempy, tempz, tempp;
float x = (2.0f * mousex) / width - 1.0f;
// the y coordinate is a little different because
// the origin of the y is from the top left.
float y = 1.0f - (2.0f * mousey) / height;
// We dont really have a z axis as of yet
float z = -1.0f;
// Next step is to convert the normalized point into
// Homogeneous clip coordinates.
float h = 1.0f;
#ifdef DEBUG_MESSAGES
printf("Original X,Y,Z,P\n%f, %f, %f, %f\n", x, y, z, h);
#endif
// Now we have a matrix of [x, y, -1, 1]T
// Next we convert to camera eye coordinates.
// First we nead the project matrix.
glGetFloatv(GL_PROJECTION_MATRIX, temp);
// We need to do the inverse to get the back to the Camera Coordinates.
gluInvertMatrix(temp, proj);
// Now we simply multiply our homogeneous coordinates by the inverse.
tempx = proj[0] * x + proj[4] * y +
proj[8] * z + proj[12] * h;
tempy = proj[1] * x + proj[5] * y +
proj[9] * z + proj[13] * h;
tempz = proj[2] * x + proj[6] * y +
proj[10] * z + proj[14] * h;
tempp = proj[3] * x + proj[7] * y +
proj[11] * z + proj[15] * h;
#ifdef DEBUG_MESSAGES
printf("Post Inverse Projection\n%f, %f, %f, %f\n", tempx, tempy, tempz, tempp);
#endif
// Reverse to the world coordinates.
glGetFloatv(GL_MODELVIEW_MATRIX, temp);
// Inverse
gluInvertMatrix(temp, model);
// Multiply the inverse and the new matrix.
x = model[0] * tempx + model[4] * tempy +
model[8] * tempz + model[12] * tempp;
y = model[1] * tempx + model[5] * tempy +
model[9] * tempz + model[13] * tempp;
z = model[2] * tempx + model[6] * tempy +
model[10] * tempz + model[14] * tempp;
h = model[3] * tempx + model[7] * tempy +
model[11] * tempz + model[15] * tempp;
#ifdef DEBUG_MESSAGES
printf("Post ModelView\n%f, %f, %f\n", x, y, z);
#endif
// Now we need to normalize the matix.
// We dont actually need to normalize since our
// Map is on the 0 y axis.
norm = sqrt(x*x+y*y+z*z);
x /= norm;
y /= norm;
z /= norm;
#ifdef DEBUG_MESSAGES
printf("%f, %f, %f\n", x, y, z);
#endif
* End Of Section*/
/* This version of ray casting
* uses a glu call gluUnProject*/
double model[16];
double proj[16];
int view[4];
double ray_origin[3];
double ray_point[3];
double ray[3];
int i;
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluUnProject(mousex, view[3] - mousey, 0.0,
model, proj, view,
ray_origin, ray_origin+1, ray_origin+2);
gluUnProject(mousex, view[3] - mousey, 1.0,
model, proj, view,
ray_point, ray_point+1, ray_point+2);
for(i = 0; i < 3; i++)
ray[i] = ray_point[i] - ray_origin[i];
#ifdef DEBUG_MESSAGES
printf("%f, %f, %f\n", ray[0], ray[1], ray[2]);
//printf("%f, %f, %f\n", ray[0] * 50, ray[1] * 50, ray[2] * 50);
#endif
p->move(ray[0] * (49.0/99.0), ray[2] * (49.0/99.0));
}
}
void my_display(void) {
/* clear the buffer */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 0);
/* draw something */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(p->getX() + camera_offsetX, cameraY, p->getZ() + camera_offsetZ,
p->getX(), 0, p->getZ(),
0, 0, -1);
// To make the game look good on all resolutions, gonna scale everything to look nice.
// Does not quite work yet
//glScalef(DEFAULT_WINDOW_SIZE_X / width, 1, DEFAULT_WINDOW_SIZE_Y / height);
m->draw();
//p.draw(0.0);
/* buffer is ready */
glutSwapBuffers();
return;
}
void my_idle(void) {
loops = 0;
while(GetTickCount() > next_game_tick && loops < MAX_FRAME_SKIP){
// Update Game Stuff
m->update_movement();
m->update();
// Update Timing Stuff
next_game_tick += SKIP_TICKS;
loops++;
}
// Display Screen
my_display();
return;
}
#ifdef __linux__
unsigned GetTickCount() {
timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts)) {
return 0;
}
return ((long long) ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
}
#endif
/* Method Copied From
* http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
* Simply inverts a 4x4 matrix. */
bool gluInvertMatrix(const float m[16], float invOut[16])
{
double inv[16], det;
int i;
inv[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];
inv[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];
inv[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];
inv[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];
inv[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];
inv[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];
inv[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];
inv[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];
inv[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];
inv[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];
inv[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];
inv[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];
inv[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];
inv[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];
inv[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];
inv[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0)
return false;
det = 1.0 / det;
for (i = 0; i < 16; i++)
invOut[i] = inv[i] * det;
return true;
}