source: opengl-game/new-game.cpp@ d12d003

feature/imgui-sdl points-test
Last change on this file since d12d003 was d12d003, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Continue debugging object detection for mouse clicks

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