/
the_maze_2.cpp
491 lines (392 loc) · 15.9 KB
/
the_maze_2.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
#include "the_maze_2.hpp"
TheMaze2::TheMaze2() {
window_width_ = DEFAULT_WINDOW_WIDTH;
window_height_ = DEFAULT_WINDOW_HEIGHT;
}
void TheMaze2::initGl() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
void TheMaze2::initVao() {
glGenVertexArrays(1, &vao_);
glBindVertexArray(vao_);
}
void TheMaze2::initVbo() {
/*
for (int i = 0; i < SURFACE_EBO_COUNT; i++) {
Surface::elems_[i] += WALL_SEGMENT_VERT_COUNT;
}
*/
for (int i = 0; i < WALL_SEGMENT_EBO_COUNT; i++) {
WallSegment::elems_[i] += SURFACE_VERT_COUNT;
}
for (int i = 0; i < PROJECTILE_EBO_COUNT; i++) {
Projectile::elems_[i] += SURFACE_VERT_COUNT + WALL_SEGMENT_VERT_COUNT;
}
glGenBuffers(1, &vbo_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
// total vbo size is size of all models added together
int vbo_size = sizeof(Surface::verts_);
vbo_size += sizeof(WallSegment::verts_);
vbo_size += sizeof(Projectile::verts_);
// allocate space for verticies
glBufferData(GL_ARRAY_BUFFER, vbo_size, NULL, GL_STATIC_DRAW);
// load each model's vertex data into the vbo
glBufferSubData(GL_ARRAY_BUFFER, SURFACE_VBO_POS, sizeof(Surface::verts_), Surface::verts_);
glBufferSubData(GL_ARRAY_BUFFER, WALL_SEGMENT_VBO_POS, sizeof(WallSegment::verts_), WallSegment::verts_);
glBufferSubData(GL_ARRAY_BUFFER, PROJECTILE_VBO_POS, sizeof(Projectile::verts_), Projectile::verts_);
// init element array buffer
glGenBuffers(1, &ebo_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
// total ebo size is size of all models added together
int ebo_size = sizeof(Surface::elems_);
ebo_size += sizeof(WallSegment::elems_);
ebo_size += sizeof(Projectile::elems_);
// allocate space for elements
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ebo_size, NULL, GL_STATIC_DRAW);
// load each model's element data into the ebo
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, SURFACE_EBO_POS, sizeof(Surface::elems_), Surface::elems_);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, WALL_SEGMENT_EBO_POS, sizeof(WallSegment::elems_), WallSegment::elems_);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, PROJECTILE_EBO_POS, sizeof(Projectile::elems_), Projectile::elems_);
}
void TheMaze2::initShaders() {
GLuint vertex_shader = shaderFromFile("shaders/vertex.glsl", GL_VERTEX_SHADER);
GLuint frag_shader = shaderFromFile("shaders/fragment.glsl", GL_FRAGMENT_SHADER);
if (vertex_shader == 0 || frag_shader == 0) {
// TODO: thow something meaningful
throw "shader error";
}
shader_prog_ = glCreateProgram();
glAttachShader(shader_prog_, vertex_shader);
glAttachShader(shader_prog_, frag_shader);
glBindFragDataLocation(shader_prog_, 0, "out_color");
glLinkProgram(shader_prog_);
glUseProgram(shader_prog_);
}
void TheMaze2::initTextures() {
//wall_text_ = gli::createTexture2D("textures/test_texture.dds");
//wall_text_ = gli::createTexture2D("textures/ice_wall.dds");
//wall_text_ = gli::createTexture2D("textures/stone_wall.dds");
wall_text_ = gli::createTexture2D("textures/rough_stone_wall.dds");
glGenerateMipmap(GL_TEXTURE_2D);
}
void TheMaze2::initAttributes() {
position_attr_ = glGetAttribLocation(shader_prog_, "position_ms");
glVertexAttribPointer(position_attr_, 3, GL_FLOAT, GL_FALSE, 11*sizeof(GLfloat), (void*)(0*sizeof(GLfloat)));
normal_attr_ = glGetAttribLocation(shader_prog_, "normal_ms");
glVertexAttribPointer(normal_attr_, 3, GL_FLOAT, GL_FALSE, 11*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
text_coord_attr_ = glGetAttribLocation(shader_prog_, "in_text");
glVertexAttribPointer(text_coord_attr_, 2, GL_FLOAT, GL_FALSE, 11*sizeof(GLfloat), (void*)(6*sizeof(GLfloat)));
color_attr_ = glGetAttribLocation(shader_prog_, "in_color");
glVertexAttribPointer(color_attr_, 3, GL_FLOAT, GL_FALSE, 11*sizeof(GLfloat), (void*)(8*sizeof(GLfloat)));
proj_uni_ = glGetUniformLocation(shader_prog_, "proj");
view_uni_ = glGetUniformLocation(shader_prog_, "view");
vert_model_uni_ = glGetUniformLocation(shader_prog_, "vert_model");
norm_model_uni_ = glGetUniformLocation(shader_prog_, "norm_model");
light_pos_cs_uni_ = glGetUniformLocation(shader_prog_, "light_pos_cs");
light_count_uni_ = glGetUniformLocation(shader_prog_, "light_count");
text_samp_uni_ = glGetUniformLocation(shader_prog_, "texture_sampler");
proj_matrix_ = glm::perspective(35.0f, window_width_ / float(window_height_), 0.1f, 200.0f);
glUniformMatrix4fv(proj_uni_, 1, GL_FALSE, glm::value_ptr(proj_matrix_));
}
GLuint TheMaze2::shaderFromFile(std::string filename, GLenum type) {
std::ifstream in_stream(filename.c_str());
std::string source_str((std::istreambuf_iterator<char>(in_stream)),
std::istreambuf_iterator<char>());
const char *source_ca = source_str.c_str();
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source_ca, NULL);
glCompileShader(shader);
GLint isCompiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE) {
GLint max_len = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_len);
std::vector<GLchar> info_log(max_len);
glGetShaderInfoLog(shader, max_len, &max_len, &info_log[0]);
std::cout << "Error compiling shader " << filename << std::endl;
for (GLchar c : info_log) {
std::cout << c;
}
std::cout << std::endl;
return 0;
}
return shader;
}
void TheMaze2::render() {
glEnableVertexAttribArray(position_attr_);
glEnableVertexAttribArray(normal_attr_);
glEnableVertexAttribArray(text_coord_attr_);
glEnableVertexAttribArray(color_attr_);
player_.updateModelMatrix();
//player_.model_matrix_ = glm::inverse(player_.model_matrix_);
glUniformMatrix4fv(view_uni_, 1, GL_FALSE, glm::value_ptr(player_.model_matrix_));
int light_count = light_positions_ws_.size();
glUniform1i(light_count_uni_, light_count);
// convert each light position to camera space
glm::vec3 light_pos_cs[16];
for (int i = 0; i < light_count; i++) {
glm::vec3 light_pos_ws = light_positions_ws_[i];
light_pos_cs[i] = (glm::vec3)(player_.model_matrix_ * glm::vec4(light_pos_ws, 1.0));
}
glUniform3fv(light_pos_cs_uni_, light_count, glm::value_ptr(light_pos_cs[0]));
for (Surface *sf : surfaces_) {
sf->updateModelMatrix();
glUniformMatrix4fv(vert_model_uni_, 1, GL_FALSE, glm::value_ptr(sf->model_matrix_));
glUniformMatrix4fv(norm_model_uni_, 1, GL_FALSE, glm::value_ptr(sf->model_matrix_));
glUniform1i(text_samp_uni_, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, wall_text_);
glDrawElements(GL_TRIANGLES, sf->ebo_count_, GL_UNSIGNED_SHORT, sf->ebo_pos_);
}
for (Model *ws : walls_) {
ws->updateModelMatrix();
glUniformMatrix4fv(vert_model_uni_, 1, GL_FALSE, glm::value_ptr(ws->model_matrix_));
glUniformMatrix4fv(norm_model_uni_, 1, GL_FALSE, glm::value_ptr(ws->model_matrix_));
glUniform1i(text_samp_uni_, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, wall_text_);
glDrawElements(GL_TRIANGLES, ws->ebo_count_, GL_UNSIGNED_SHORT, ws->ebo_pos_);
}
for (Projectile *proj : projectiles_) {
proj->updateModelMatrix();
glUniformMatrix4fv(vert_model_uni_, 1, GL_FALSE, glm::value_ptr(proj->model_matrix_));
glUniformMatrix4fv(norm_model_uni_, 1, GL_FALSE, glm::value_ptr(proj->model_matrix_));
glUniform1i(text_samp_uni_, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, wall_text_);
glDrawElements(GL_TRIANGLES, proj->ebo_count_, GL_UNSIGNED_SHORT, proj->ebo_pos_);
}
glDisableVertexAttribArray(position_attr_);
glDisableVertexAttribArray(normal_attr_);
glDisableVertexAttribArray(text_coord_attr_);
glDisableVertexAttribArray(color_attr_);
}
void TheMaze2::handleMovement(double delta) {
player_.move(delta, walls_);
player_.update(delta);
walls_[0]->move(delta, walls_);
// move and update each projectile
for (Projectile *proj : projectiles_) {
proj->move(delta, walls_);
proj->update(delta);
}
}
void TheMaze2::addWall(int xPos, int yPos, int zPos) {
WallSegment *ws = new WallSegment();
ws->pos_.x_ = xPos;
ws->pos_.y_ = yPos;
ws->pos_.z_ = zPos;
walls_.push_back(ws);
}
void TheMaze2::addWalls(int xPos, int yPos, int zPos, int xOff, int yOff) {
if (xOff != 0) {
int abs_off = abs(xOff);
int delta = xOff / abs_off;
for (int i = 0; i != abs_off*delta; i += delta) {
addWall(xPos + i*1000, yPos, zPos);
}
}
if (yOff != 0) {
int abs_off = abs(yOff);
int delta = yOff / abs_off;
for (int i = 0; i != abs_off*delta; i += delta) {
addWall(xPos, yPos + i*1000, zPos);
}
}
}
void TheMaze2::addSurface(int xPos, int yPos, int zPos) {
Surface *sf = new Surface();
sf->pos_.x_ = xPos;
sf->pos_.y_ = yPos;
sf->pos_.z_ = zPos;
surfaces_.push_back(sf);
}
void TheMaze2::addProjectile(int xPos, int yPos, int zPos, float xFacing, float yFacing) {
Projectile *proj = new Projectile(xPos, yPos, zPos, xFacing, yFacing);
projectiles_.push_back(proj);
}
void TheMaze2::addLight(float xPos, float yPos, float zPos) {
// convert in game dimensions to gl dimensions
glm::vec3 light = glm::vec3(xPos, zPos, yPos);
light_positions_ws_.push_back(light);
}
int main() {
if (!glfwInit()) {
std::cout << "Error initializing GLFW\n";
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
if (!glfwOpenWindow(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT,
8, 8, 8, 0, 24, 0, GLFW_WINDOW)) {
std::cout << "Error opening GLFW window\n";
glfwTerminate();
return -1;
}
glfwSetWindowTitle("The Maze 2!");
glfwDisable(GLFW_MOUSE_CURSOR);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Error initing GLEW\n";
glfwTerminate();
return -1;
}
TheMaze2 &maze = TheMaze2::getInstance();
maze.initGl();
maze.initVao();
maze.initVbo();
maze.initShaders();
maze.initAttributes();
maze.initTextures();
maze.addLight(25.0, 25.0, 0.0);
maze.addLight(-55.0, 0.0, 0.0);
maze.addSurface(0, 0, -5000);
maze.addSurface(0, 0, 5000); // need to flip this
maze.addWall(0, 0, 0);
maze.addWall(0, 1000, 0);
maze.addWall(0, 2000, 0);
maze.addWall(2000, -20000, 0);
maze.walls_[3]->setScale(3.0f);
maze.walls_[3]->pos_.z_ = 10000;
maze.addWalls(-70000, -70000, 0, 140, 140);
maze.addWalls(70000, 70000, 0, -140, -140);
maze.player_.pos_.x_ = 20000;
maze.player_.pos_.y_ = 20000;
double fps_timer = 0;
glfwSetKeyCallback(TheMaze2::handleKeyInput);
glfwSetMouseButtonCallback(TheMaze2::handleMouseButtonInput);
glfwSetWindowSizeCallback(TheMaze2::handleWindowResize);
double prev_time = glfwGetTime();
while (glfwGetWindowParam(GLFW_OPENED)) {
double cur_time = glfwGetTime();
double time_delta = cur_time - prev_time;
prev_time = cur_time;
// print framerate
fps_timer += time_delta;
if (fps_timer > 1) {
int fps = 1 / time_delta;
std::cout << fps << "\r" << std::flush;
fps_timer = 0;
}
// logic
maze.handleMouseInput(time_delta);
maze.handleMovement(time_delta);
//maze.cleanupObjects();
// drawing
glClearColor(0.8f, 0.8f, 0.85f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
maze.render();
glfwSwapBuffers();
int error = glGetError();
if (error != 0) {
std::cout << "GL Error " << error << std::endl;
//return -1;
}
}
glfwTerminate();
return 0;
}
void TheMaze2::handleMouseInput(double time_delta) {
int xPos, yPos;
glfwGetMousePos(&xPos, &yPos);
player_.xFacing_ -= player_.look_speed_ * time_delta * float(window_width_/2 - xPos);
player_.yFacing_ += player_.look_speed_ * time_delta * float(window_height_/2 - yPos);
glfwSetMousePos(window_width_/2, window_height_/2);
}
void TheMaze2::handleMouseButtonInput(int button, int action) {
TheMaze2 &maze = TheMaze2::getInstance();
switch (button) {
// move the camera/player
case GLFW_MOUSE_BUTTON_LEFT:
if (action == GLFW_RELEASE) {
maze.addProjectile(maze.player_.pos_.x_, maze.player_.pos_.y_, maze.player_.pos_.z_,
maze.player_.xFacing_, maze.player_.yFacing_);
}
default:
break;
}
}
void TheMaze2::handleKeyInput(int key, int action) {
TheMaze2 &maze = TheMaze2::getInstance();
switch (key) {
// move the camera/player
case 87: // W
if (action == GLFW_PRESS) {
maze.player_.move_dir_ = DIR_FORWARD;
} else if (action == GLFW_RELEASE) {
maze.player_.move_dir_ = DIR_NONE;
}
break;
case 83: // S
if (action == GLFW_PRESS) {
maze.player_.move_dir_ = DIR_REVERSE;
} else if (action == GLFW_RELEASE) {
maze.player_.move_dir_ = DIR_NONE;
}
break;
case 65: // A
if (action == GLFW_PRESS) {
maze.player_.strafe_dir_ = DIR_LEFT;
} else if (action == GLFW_RELEASE) {
maze.player_.strafe_dir_ = DIR_NONE;
}
break;
case 68: // D
if (action == GLFW_PRESS) {
maze.player_.strafe_dir_ = DIR_RIGHT;
} else if (action == GLFW_RELEASE) {
maze.player_.strafe_dir_ = DIR_NONE;
}
break;
case 81: // Q
if (action == GLFW_PRESS) {
maze.player_.rot_dir_ = DIR_LEFT;
} else if (action == GLFW_RELEASE) {
maze.player_.rot_dir_ = DIR_NONE;
}
break;
case 69: // E
if (action == GLFW_PRESS) {
maze.player_.rot_dir_ = DIR_RIGHT;
} else if (action == GLFW_RELEASE) {
maze.player_.rot_dir_ = DIR_NONE;
}
break;
// move the wall
case 73: // I
if (action == GLFW_PRESS) {
maze.walls_[0]->move_dir_ = DIR_FORWARD;
} else if (action == GLFW_RELEASE) {
maze.walls_[0]->move_dir_ = DIR_NONE;
}
break;
case 75: // K
if (action == GLFW_PRESS) {
maze.walls_[0]->move_dir_ = DIR_REVERSE;
} else if (action == GLFW_RELEASE) {
maze.walls_[0]->move_dir_ = DIR_NONE;
}
break;
case 74: // J
if (action == GLFW_PRESS) {
maze.walls_[0]->rot_dir_ = DIR_LEFT;
} else if (action == GLFW_RELEASE) {
maze.walls_[0]->rot_dir_ = DIR_NONE;
}
break;
case 76: // L
if (action == GLFW_PRESS) {
maze.walls_[0]->rot_dir_ = DIR_RIGHT;
} else if (action == GLFW_RELEASE) {
maze.walls_[0]->rot_dir_ = DIR_NONE;
}
break;
default:
break;
}
}
void TheMaze2::handleWindowResize(int width, int height) {
TheMaze2 &maze = TheMaze2::getInstance();
maze.window_width_ = width;
maze.window_height_ = height;
}