source: opengl-game/new-game.cpp@ 33a9664

feature/imgui-sdl points-test
Last change on this file since 33a9664 was 33a9664, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

Debug object detection for mouse clicks

  • Property mode set to 100644
File size: 21.6 KB
Line 
1#include "logger.h"
2
3#include "stb_image.h"
4
5#define GLM_SWIZZLE
6#include <glm/mat4x4.hpp>
7#include <glm/gtc/matrix_transform.hpp>
8#include <glm/gtc/type_ptr.hpp>
9
10#include <GL/glew.h>
11#include <GLFW/glfw3.h>
12
13#include <cstdio>
14#include <iostream>
15#include <fstream>
16
17#define _USE_MATH_DEFINES
18#include <cmath>
19
20using namespace std;
21using namespace glm;
22
23#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
24
25const bool FULLSCREEN = false;
26int width = 640;
27int height = 480;
28
29vec3 cam_pos;
30
31vec3 face_point1, face_point2, face_point3;
32
33bool clicked = false;
34int colors_i = 0;
35
36mat4 view_mat;
37mat4 proj_mat;
38
39bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3);
40
41GLuint loadShader(GLenum type, string file);
42GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
43unsigned char* loadImage(string file_name, int* x, int* y);
44
45void glfw_error_callback(int error, const char* description) {
46 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
47}
48
49void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
50 double mouse_x, mouse_y;
51 glfwGetCursorPos(window, &mouse_x, &mouse_y);
52
53 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
54 cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
55
56 float x = (2.0f*mouse_x) / width - 1.0f;
57 float y = 1.0f - (2.0f*mouse_y) / height;
58 cout << "x: " << x << ", y: " << y << endl;
59
60 // Since the projection matrix gets applied before the view matrix,
61 // treat the initial camera position (aka origin of the ray) as (0, 0, 0)
62
63 // When getting the ray direction, you can use near and fov to get the
64 // coordinates
65
66 vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); // this should have a z equal to the near clipping plane
67 vec4 ray_eye = inverse(proj_mat) * ray_clip;
68 ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f);
69 vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
70
71 /* LATEST NOTES:
72 *
73 * Normalizing the world ray caused issues, although it should make sense with the projection
74 * matrix, since the z coordinate has meaning there.
75 *
76 * Now, I need to figure out the correct intersection test in 2D space
77 * Also, need to check that the global triangle points are correct
78 */
79
80 // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to
81 // it, only to subtract it later
82
83 vec3 click_point = cam_pos + ray_world;
84
85 /* Now, we need to generate the constants for the equations describing
86 * a 3D line:
87 * (x - x0) / a = (y - y0) / b = (z - z0) / c
88 *
89 * The line goes through the camera position, so
90 * cam_pos = <x0, y0, z0>
91 */
92
93 // upper right corner is 1, 1 in opengl
94
95 cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
96 cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl;
97 cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl;
98
99 float a = 1.0f;
100 float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x);
101 float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x);
102
103 cout << "(x - " << cam_pos.x << ") / " << a << " = ";
104 cout << "(y - " << cam_pos.y << ") / " << b << " = ";
105 cout << "(z - " << cam_pos.z << ") / " << c << endl;;
106
107 /* Now, we need to generate the constants for the equations describing
108 * a 3D plane:
109 * dx + ey +fz +g = 0
110 */
111
112 vec3 fp1 = face_point1;
113 vec3 fp2 = face_point2;
114 vec3 fp3 = face_point3;
115
116 cout << "Points on the plane" << endl;
117 cout << "(" << fp1.x << ", " << fp1.y << ", " << fp1.z << ")" << endl;
118 cout << "(" << fp2.x << ", " << fp2.y << ", " << fp2.z << ")" << endl;
119 cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
120
121 float pa = (fp2.y-fp1.y)*(fp3.z-fp1.z) - (fp3.y-fp1.y)*(fp2.z-fp1.z);
122 float pb = (fp2.z-fp1.z)*(fp3.x-fp1.x) - (fp3.z-fp1.z)*(fp2.x-fp1.x);
123 float pc = (fp2.x-fp1.x)*(fp3.y-fp1.y) - (fp3.x-fp1.x)*(fp2.y-fp1.y);
124 float pd = -(pa*fp1.x+pb*fp1.y+pc*fp1.z);
125
126 cout << pa << "x+" << pb << "y+" << pc << "z+" << pd << "=0" << endl;
127
128 // get intersection
129
130 // the intersection this computes is incorrect
131 // it doesn't match the equation of the plane
132 vec3 i;
133 i.z = -cam_pos.z - pc*pd/(pa*a+pb*b);
134 i.x = cam_pos.x + a * (i.z-cam_pos.z) / c;
135 i.y = cam_pos.y + b * (i.z-cam_pos.z) / c;
136
137 cout << "The holy grail?" << endl;
138 cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
139
140 bool hit = insideTriangle(i, fp1, fp2, fp3);
141 cout << (hit ? "true" : "false") << endl;
142
143 if (hit) {
144 clicked = true;
145 }
146 }
147}
148
149void mouse_button_callback_new(GLFWwindow* window, int button, int action, int mods) {
150 double mouse_x, mouse_y;
151 glfwGetCursorPos(window, &mouse_x, &mouse_y);
152
153 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
154 cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
155
156 float x = (2.0f*mouse_x) / width - 1.0f;
157 float y = 1.0f - (2.0f*mouse_y) / height;
158 cout << "x: " << x << ", y: " << y << endl;
159
160 // CHECK: Looks good up to here
161
162 // Since the projection matrix gets applied before the view matrix,
163 // treat the initial camera position (aka origin of the ray) as (0, 0, 0)
164
165 // When getting the ray direction, you can use near and fov to get the
166 // coordinates
167
168 // vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); // this should have a z equal to the near clipping plane
169 // vec4 ray_eye = inverse(proj_mat) * ray_clip;
170 // ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f);
171 // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
172
173 vec4 ray_clip = vec4(x, y, 1.0f, 1.0f); // this should have a z equal to the near clipping plane
174 vec3 ray_world = (inverse(view_mat) * ray_clip).xyz();
175
176 /* LATEST NOTES:
177 *
178 * Normalizing the world ray caused issues, although it should make sense with the projection
179 * matrix, since the z coordinate has meaning there.
180 *
181 * Now, I need to figure out the correct intersection test in 2D space
182 * Also, need to check that the global triangle points are correct
183 */
184
185 // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to
186 // it, only to subtract it later
187
188 vec3 click_point = cam_pos + ray_world;
189
190 /* Now, we need to generate the constants for the equations describing
191 * a 3D line:
192 * (x - x0) / a = (y - y0) / b = (z - z0) / c
193 *
194 * The line goes through the camera position, so
195 * cam_pos = <x0, y0, z0>
196 */
197
198 // upper right corner is 1, 1 in opengl
199
200 cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
201 cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl;
202 cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl;
203
204 float a = 1.0f;
205 float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x);
206 float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x);
207
208 cout << "(x - " << cam_pos.x << ") / " << a << " = ";
209 cout << "(y - " << cam_pos.y << ") / " << b << " = ";
210 cout << "(z - " << cam_pos.z << ") / " << c << endl;;
211
212 /* Now, we need to generate the constants for the equations describing
213 * a 3D plane:
214 * dx + ey +fz +g = 0
215 */
216
217 vec3 fp1 = face_point1;
218 vec3 fp2 = face_point2;
219 vec3 fp3 = face_point3;
220
221 cout << "Points on the plane" << endl;
222 cout << "(" << fp1.x << ", " << fp1.y << ", " << fp1.z << ")" << endl;
223 cout << "(" << fp2.x << ", " << fp2.y << ", " << fp2.z << ")" << endl;
224 cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
225
226 float pa = (fp2.y-fp1.y)*(fp3.z-fp1.z) - (fp3.y-fp1.y)*(fp2.z-fp1.z);
227 float pb = (fp2.z-fp1.z)*(fp3.x-fp1.x) - (fp3.z-fp1.z)*(fp2.x-fp1.x);
228 float pc = (fp2.x-fp1.x)*(fp3.y-fp1.y) - (fp3.x-fp1.x)*(fp2.y-fp1.y);
229 float pd = -(pa*fp1.x+pb*fp1.y+pc*fp1.z);
230
231 cout << pa << "x+" << pb << "y+" << pc << "z+" << pd << "=0" << endl;
232
233 // get intersection
234
235 // the intersection this computes is incorrect
236 // it doesn't match the equation of the plane
237 vec3 i;
238 i.z = -cam_pos.z - pc*pd/(pa*a+pb*b);
239 i.x = cam_pos.x + a * (i.z-cam_pos.z) / c;
240 i.y = cam_pos.y + b * (i.z-cam_pos.z) / c;
241
242 cout << "The holy grail?" << endl;
243 cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
244
245 bool hit = insideTriangle(i, fp1, fp2, fp3);
246 cout << (hit ? "true" : "false") << endl;
247
248 if (hit) {
249 clicked = true;
250 }
251 }
252}
253
254int main(int argc, char* argv[]) {
255 cout << "New OpenGL Game" << endl;
256
257 if (!restart_gl_log()) {}
258 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
259
260 glfwSetErrorCallback(glfw_error_callback);
261 if (!glfwInit()) {
262 fprintf(stderr, "ERROR: could not start GLFW3\n");
263 return 1;
264 }
265
266#ifdef __APPLE__
267 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
268 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
269 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
270 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
271#endif
272
273 glfwWindowHint(GLFW_SAMPLES, 4);
274
275 GLFWwindow* window = NULL;
276
277 if (FULLSCREEN) {
278 GLFWmonitor* mon = glfwGetPrimaryMonitor();
279 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
280
281 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
282 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
283
284 width = vmode->width;
285 height = vmode->height;
286 } else {
287 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
288 }
289
290 if (!window) {
291 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
292 glfwTerminate();
293 return 1;
294 }
295
296 glfwSetMouseButtonCallback(window, mouse_button_callback);
297
298 glfwMakeContextCurrent(window);
299 glewExperimental = GL_TRUE;
300 glewInit();
301
302 // glViewport(0, 0, width*2, height*2);
303
304 const GLubyte* renderer = glGetString(GL_RENDERER);
305 const GLubyte* version = glGetString(GL_VERSION);
306 printf("Renderer: %s\n", renderer);
307 printf("OpenGL version supported %s\n", version);
308
309 glEnable(GL_DEPTH_TEST);
310 glDepthFunc(GL_LESS);
311
312 glEnable(GL_CULL_FACE);
313 // glCullFace(GL_BACK);
314 // glFrontFace(GL_CW);
315
316 int x, y;
317 unsigned char* texImage = loadImage("test.png", &x, &y);
318 if (texImage) {
319 cout << "Yay, I loaded an image!" << endl;
320 cout << x << endl;
321 cout << y << endl;
322 printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
323 }
324
325 GLuint tex = 0;
326 glGenTextures(1, &tex);
327 glActiveTexture(GL_TEXTURE0);
328 glBindTexture(GL_TEXTURE_2D, tex);
329 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
330
331 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
332 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
333 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
334 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
335
336 GLfloat points[] = {
337 0.0f, 0.5f, -0.001f,
338 -0.5f, -0.5f, 0.0f,
339 0.5f, -0.5f, 0.0f,
340 0.5f, -0.5f, 0.0f,
341 -0.5f, -0.5f, 0.0f,
342 0.0f, 0.5f, -0.001f,
343 };
344 /*
345 GLfloat points[] = {
346 0.0f, 1.0f, 0.0f,
347 -1.0f, 0.0f, 0.0f,
348 1.0f, 0.0f, 0.0f,
349 1.0f, 0.0f, 0.0f,
350 -1.0f, 0.0f, 0.0f,
351 0.0f, 1.0f, 0.0f,
352 };
353 */
354
355 // initialize global variables for click intersection test
356 face_point1 = vec3(points[0], points[1], points[2]);
357 face_point2 = vec3(points[3], points[4], points[5]);
358 face_point3 = vec3(points[6], points[7], points[8]);
359
360 GLfloat colors[] = {
361 1.0, 0.0, 0.0,
362 0.0, 0.0, 1.0,
363 0.0, 1.0, 0.0,
364 0.0, 1.0, 0.0,
365 0.0, 0.0, 1.0,
366 1.0, 0.0, 0.0,
367 };
368
369 GLfloat colors_new[] = {
370 0.0, 1.0, 0.0,
371 0.0, 1.0, 0.0,
372 0.0, 1.0, 0.0,
373 0.0, 1.0, 0.0,
374 0.0, 1.0, 0.0,
375 0.0, 1.0, 0.0,
376 };
377
378 // Each point is made of 3 floats
379 int numPoints = (sizeof(points) / sizeof(float)) / 3;
380
381 GLfloat points2[] = {
382 0.5f, 0.5f, 0.0f,
383 -0.5f, 0.5f, 0.0f,
384 -0.5f, -0.5f, 0.0f,
385 0.5f, 0.5f, 0.0f,
386 -0.5f, -0.5f, 0.0f,
387 0.5f, -0.5f, 0.0f,
388 };
389
390 /*
391 GLfloat colors2[] = {
392 0.0, 0.9, 0.9,
393 0.0, 0.9, 0.9,
394 0.0, 0.9, 0.9,
395 0.0, 0.9, 0.9,
396 0.0, 0.9, 0.9,
397 0.0, 0.9, 0.9,
398 };
399 */
400
401 GLfloat texcoords[] = {
402 1.0f, 1.0f,
403 0.0f, 1.0f,
404 0.0, 0.0,
405 1.0, 1.0,
406 0.0, 0.0,
407 1.0, 0.0
408 };
409
410 // Each point is made of 3 floats
411 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
412
413 /*
414 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
415 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
416 */
417 mat4 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
418 mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
419 mat4 model_mat = T_model*R_model;
420
421 mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
422 // mat4 T_model2 = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
423 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
424 mat4 model_mat2 = T_model2*R_model2;
425
426 GLuint points_vbo = 0;
427 glGenBuffers(1, &points_vbo);
428 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
429 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
430
431 GLuint colors_vbo = 0;
432 glGenBuffers(1, &colors_vbo);
433 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
434 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
435
436 GLuint vao = 0;
437 glGenVertexArrays(1, &vao);
438 glBindVertexArray(vao);
439 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
440 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
441 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
442 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
443
444 glEnableVertexAttribArray(0);
445 glEnableVertexAttribArray(1);
446
447 GLuint points2_vbo = 0;
448 glGenBuffers(1, &points2_vbo);
449 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
450 glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
451
452 /*
453 GLuint colors2_vbo = 0;
454 glGenBuffers(1, &colors2_vbo);
455 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
456 glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
457 */
458
459 GLuint vt_vbo;
460 glGenBuffers(1, &vt_vbo);
461 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
462 glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
463
464 GLuint vao2 = 0;
465 glGenVertexArrays(1, &vao2);
466 glBindVertexArray(vao2);
467 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
468 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
469 // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
470 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
471 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
472 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
473
474 glEnableVertexAttribArray(0);
475 glEnableVertexAttribArray(1);
476
477 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
478 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
479
480 float speed = 1.0f;
481 float last_position = 0.0f;
482
483 float cam_speed = 1.0f;
484 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
485
486 cam_pos = vec3(0.0f, 0.0f, 2.0f);
487 //cam_pos = vec3(0.0f, 0.0f, 0.0f);
488 float cam_yaw = 0.0f;
489
490 mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
491 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
492 /*
493 mat4 T = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
494 mat4 R = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
495 */
496 view_mat = R*T;
497
498 float near = 0.1f;
499 float far = 100.0f;
500 float fov = 67.0f * ONE_DEG_IN_RAD;
501 float aspect = (float)width / (float)height;
502
503 float range = tan(fov * 0.5f) * near;
504 float Sx = near / (range * aspect);
505 float Sy = near / range;
506 float Sz = -(far + near) / (far - near);
507 float Pz = -(2.0f * far * near) / (far - near);
508
509 float proj_arr[] = {
510 Sx, 0.0f, 0.0f, 0.0f,
511 0.0f, Sy, 0.0f, 0.0f,
512 0.0f, 0.0f, Sz, -1.0f,
513 0.0f, 0.0f, Pz, 0.0f,
514 };
515 /*
516 float proj_arr[] = {
517 1.0f, 0.0f, 0.0f, 0.0f,
518 0.0f, 1.0f, 0.0f, 0.0f,
519 0.0f, 0.0f, 1.0f, 0.0f,
520 0.0f, 0.0f, 0.0f, 1.0f,
521 };
522 */
523 proj_mat = make_mat4(proj_arr);
524
525 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
526 GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
527 GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
528
529 GLint model_test_loc = glGetUniformLocation(shader_program, "model");
530 GLint view_test_loc = glGetUniformLocation(shader_program, "view");
531 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
532
533 glUseProgram(shader_program);
534 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
535 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
536
537 glUseProgram(shader_program2);
538 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
539 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
540
541 // glUniform1i(tex_loc, 0);
542
543 bool cam_moved = false;
544
545 double previous_seconds = glfwGetTime();
546 while (!glfwWindowShouldClose(window)) {
547 double current_seconds = glfwGetTime();
548 double elapsed_seconds = current_seconds - previous_seconds;
549 previous_seconds = current_seconds;
550
551 if (fabs(last_position) > 1.0f) {
552 speed = -speed;
553 }
554
555 if (clicked) {
556 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
557
558 if (colors_i == 0) {
559 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
560 colors_i = 1;
561 } else {
562 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
563 colors_i = 0;
564 }
565
566 clicked = false;
567 }
568
569 /*
570 model[12] = last_position + speed*elapsed_seconds;
571 last_position = model[12];
572 */
573
574 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
575
576 glUseProgram(shader_program);
577 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
578
579 glBindVertexArray(vao);
580
581 glDrawArrays(GL_TRIANGLES, 0, numPoints);
582
583 glUseProgram(shader_program2);
584 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
585
586 glBindVertexArray(vao2);
587
588 int numPoints3 = numPoints2;
589 numPoints2 = numPoints3;
590 // glDrawArrays(GL_TRIANGLES, 0, numPoints2);
591
592 glfwPollEvents();
593 glfwSwapBuffers(window);
594
595 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
596 glfwSetWindowShouldClose(window, 1);
597 }
598
599 float dist = cam_speed * elapsed_seconds;
600 if (glfwGetKey(window, GLFW_KEY_A)) {
601 cam_pos.x -= cos(cam_yaw)*dist;
602 cam_pos.z += sin(cam_yaw)*dist;
603 cam_moved = true;
604 }
605 if (glfwGetKey(window, GLFW_KEY_D)) {
606 cam_pos.x += cos(cam_yaw)*dist;
607 cam_pos.z -= sin(cam_yaw)*dist;
608 cam_moved = true;
609 }
610 if (glfwGetKey(window, GLFW_KEY_W)) {
611 cam_pos.x -= sin(cam_yaw)*dist;
612 cam_pos.z -= cos(cam_yaw)*dist;
613 cam_moved = true;
614 }
615 if (glfwGetKey(window, GLFW_KEY_S)) {
616 cam_pos.x += sin(cam_yaw)*dist;
617 cam_pos.z += cos(cam_yaw)*dist;
618 cam_moved = true;
619 }
620 if (glfwGetKey(window, GLFW_KEY_LEFT)) {
621 cam_yaw += cam_yaw_speed * elapsed_seconds;
622 cam_moved = true;
623 }
624 if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
625 cam_yaw -= cam_yaw_speed * elapsed_seconds;
626 cam_moved = true;
627 }
628 if (cam_moved) {
629 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
630 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
631 // view_mat = R*T;
632
633 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
634 cam_moved = false;
635 }
636 }
637
638 glfwTerminate();
639 return 0;
640}
641
642GLuint loadShader(GLenum type, string file) {
643 cout << "Loading shader from file " << file << endl;
644
645 ifstream shaderFile(file);
646 GLuint shaderId = 0;
647
648 if (shaderFile.is_open()) {
649 string line, shaderString;
650
651 while(getline(shaderFile, line)) {
652 shaderString += line + "\n";
653 }
654 shaderFile.close();
655 const char* shaderCString = shaderString.c_str();
656
657 shaderId = glCreateShader(type);
658 glShaderSource(shaderId, 1, &shaderCString, NULL);
659 glCompileShader(shaderId);
660
661 cout << "Loaded successfully" << endl;
662 } else {
663 cout << "Failed to loade the file" << endl;
664 }
665
666 return shaderId;
667}
668
669GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
670 GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
671 GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
672
673 GLuint shader_program = glCreateProgram();
674 glAttachShader(shader_program, vs);
675 glAttachShader(shader_program, fs);
676
677 glLinkProgram(shader_program);
678
679 return shader_program;
680}
681
682unsigned char* loadImage(string file_name, int* x, int* y) {
683 int n;
684 int force_channels = 4;
685 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
686 if (!image_data) {
687 fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
688 }
689 return image_data;
690}
691
692bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3) {
693 vec3 v21 = v2-v1;
694 vec3 v31 = v3-v1;
695 vec3 pv1 = p-v1;
696
697 float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
698 float x = (pv1.x-y*v31.x) / v21.x;
699
700 cout << "(" << x << ", " << y << ")" << endl;
701
702 return x > 0.0f && y > 0.0f && x+y < 1.0f;
703}
Note: See TracBrowser for help on using the repository browser.