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

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

Make mouse click object detection work with a non-identity view matrix

  • Property mode set to 100644
File size: 21.4 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, NEAR_CLIP, 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
193 printVector("Initial world ray:", ray_world);
194
195 vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
196 vec3 cam_pos_temp = (inverse(view_mat) * cam_pos_origin).xyz();
197
198 ray_world = ray_world-cam_pos_temp;
199
200 cout << "Ray clip -> (" << ray_clip.x << "," << ray_clip.y << "," << ray_clip.z << ")" << endl << endl;;
201 cout << "Ray world -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
202 cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
203
204 vec3 fp1 = face_point1;
205 vec3 fp2 = face_point2;
206 vec3 fp3 = face_point3;
207
208 cout << "Points on the plane" << endl;
209 cout << "(" << fp1.x << ", " << fp1.y << ", " << fp1.z << ")" << endl;
210 cout << "(" << fp2.x << ", " << fp2.y << ", " << fp2.z << ")" << endl;
211 cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
212
213 // LINE EQUATION: P = O + Dt
214 // O = cam_pos
215 // D = ray_world
216
217 // PLANE EQUATION: P dot n + d = 0 (n is the normal vector and d is the offset from the origin)
218
219 // Take the cross-product of two vectors on the plane to get the normal
220 vec3 v1 = fp2 - fp1;
221 vec3 v2 = fp3 - fp1;
222
223 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);
224 printVector("v1", v1);
225 printVector("v2", v2);
226 printVector("Cross", normal);
227 cout << "Test theory: " << glm::dot(cam_pos_temp, normal) << endl;
228 cout << "Test 2: " << glm::dot(ray_world, normal) << endl;
229
230 float d = -glm::dot(fp1, normal);
231 cout << "d: " << d << endl;
232
233 float t = - (glm::dot(cam_pos_temp, normal) + d) / glm::dot(ray_world, normal);
234 cout << "t: " << t << endl;
235
236 vec3 intersection = cam_pos_temp+t*ray_world;
237 printVector("Intersection", intersection);
238
239 clicked = insideTriangle(intersection, fp1, fp2, fp3);
240 cout << (clicked ? "true" : "false") << endl;
241 }
242}
243
244int main(int argc, char* argv[]) {
245 cout << "New OpenGL Game" << endl;
246
247 if (!restart_gl_log()) {}
248 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
249
250 glfwSetErrorCallback(glfw_error_callback);
251 if (!glfwInit()) {
252 fprintf(stderr, "ERROR: could not start GLFW3\n");
253 return 1;
254 }
255
256#ifdef __APPLE__
257 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
258 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
259 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
260 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
261#endif
262
263 glfwWindowHint(GLFW_SAMPLES, 4);
264
265 GLFWwindow* window = NULL;
266
267 if (FULLSCREEN) {
268 GLFWmonitor* mon = glfwGetPrimaryMonitor();
269 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
270
271 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
272 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
273
274 width = vmode->width;
275 height = vmode->height;
276 } else {
277 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
278 }
279
280 if (!window) {
281 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
282 glfwTerminate();
283 return 1;
284 }
285
286 glfwSetMouseButtonCallback(window, mouse_button_callback_new);
287
288 glfwMakeContextCurrent(window);
289 glewExperimental = GL_TRUE;
290 glewInit();
291
292 // glViewport(0, 0, width*2, height*2);
293
294 const GLubyte* renderer = glGetString(GL_RENDERER);
295 const GLubyte* version = glGetString(GL_VERSION);
296 printf("Renderer: %s\n", renderer);
297 printf("OpenGL version supported %s\n", version);
298
299 glEnable(GL_DEPTH_TEST);
300 glDepthFunc(GL_LESS);
301
302 glEnable(GL_CULL_FACE);
303 // glCullFace(GL_BACK);
304 // glFrontFace(GL_CW);
305
306 int x, y;
307 unsigned char* texImage = loadImage("test.png", &x, &y);
308 if (texImage) {
309 cout << "Yay, I loaded an image!" << endl;
310 cout << x << endl;
311 cout << y << endl;
312 printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
313 }
314
315 GLuint tex = 0;
316 glGenTextures(1, &tex);
317 glActiveTexture(GL_TEXTURE0);
318 glBindTexture(GL_TEXTURE_2D, tex);
319 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
320
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
322 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
323 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
324 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
325
326 GLfloat points[] = {
327 0.0f, 0.5f, 0.0f,
328 -0.5f, -0.5f, 0.0f,
329 0.5f, -0.5f, 0.0f,
330 0.5f, -0.5f, 0.0f,
331 -0.5f, -0.5f, 0.0f,
332 0.0f, 0.5f, 0.0f,
333 };
334 /*
335 GLfloat points[] = {
336 0.0f, 1.0f, 0.0f,
337 -1.0f, 0.0f, 0.0f,
338 1.0f, 0.0f, 0.0f,
339 1.0f, 0.0f, 0.0f,
340 -1.0f, 0.0f, 0.0f,
341 0.0f, 1.0f, 0.0f,
342 };
343 */
344
345 // initialize global variables for click intersection test
346 face_point1 = vec3(points[0], points[1], points[2]);
347 face_point2 = vec3(points[3], points[4], points[5]);
348 face_point3 = vec3(points[6], points[7], points[8]);
349
350 GLfloat colors[] = {
351 1.0, 0.0, 0.0,
352 0.0, 0.0, 1.0,
353 0.0, 1.0, 0.0,
354 0.0, 1.0, 0.0,
355 0.0, 0.0, 1.0,
356 1.0, 0.0, 0.0,
357 };
358
359 GLfloat colors_new[] = {
360 0.0, 1.0, 0.0,
361 0.0, 1.0, 0.0,
362 0.0, 1.0, 0.0,
363 0.0, 1.0, 0.0,
364 0.0, 1.0, 0.0,
365 0.0, 1.0, 0.0,
366 };
367
368 // Each point is made of 3 floats
369 int numPoints = (sizeof(points) / sizeof(float)) / 3;
370
371 GLfloat points2[] = {
372 0.5f, 0.5f, 0.0f,
373 -0.5f, 0.5f, 0.0f,
374 -0.5f, -0.5f, 0.0f,
375 0.5f, 0.5f, 0.0f,
376 -0.5f, -0.5f, 0.0f,
377 0.5f, -0.5f, 0.0f,
378 };
379
380 /*
381 GLfloat colors2[] = {
382 0.0, 0.9, 0.9,
383 0.0, 0.9, 0.9,
384 0.0, 0.9, 0.9,
385 0.0, 0.9, 0.9,
386 0.0, 0.9, 0.9,
387 0.0, 0.9, 0.9,
388 };
389 */
390
391 GLfloat texcoords[] = {
392 1.0f, 1.0f,
393 0.0f, 1.0f,
394 0.0, 0.0,
395 1.0, 1.0,
396 0.0, 0.0,
397 1.0, 0.0
398 };
399
400 // Each point is made of 3 floats
401 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
402
403 /*
404 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
405 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
406 */
407 mat4 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
408 mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
409 mat4 model_mat = T_model*R_model;
410
411 mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
412 // mat4 T_model2 = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
413 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
414 mat4 model_mat2 = T_model2*R_model2;
415
416 GLuint points_vbo = 0;
417 glGenBuffers(1, &points_vbo);
418 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
419 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
420
421 GLuint colors_vbo = 0;
422 glGenBuffers(1, &colors_vbo);
423 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
424 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
425
426 GLuint vao = 0;
427 glGenVertexArrays(1, &vao);
428 glBindVertexArray(vao);
429 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
430 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
431 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
432 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
433
434 glEnableVertexAttribArray(0);
435 glEnableVertexAttribArray(1);
436
437 GLuint points2_vbo = 0;
438 glGenBuffers(1, &points2_vbo);
439 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
440 glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
441
442 /*
443 GLuint colors2_vbo = 0;
444 glGenBuffers(1, &colors2_vbo);
445 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
446 glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
447 */
448
449 GLuint vt_vbo;
450 glGenBuffers(1, &vt_vbo);
451 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
452 glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
453
454 GLuint vao2 = 0;
455 glGenVertexArrays(1, &vao2);
456 glBindVertexArray(vao2);
457 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
458 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
459 // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
460 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
461 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
462 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
463
464 glEnableVertexAttribArray(0);
465 glEnableVertexAttribArray(1);
466
467 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
468 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
469
470 float speed = 1.0f;
471 float last_position = 0.0f;
472
473 float cam_speed = 1.0f;
474 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
475
476 //cam_pos = vec3(0.0f, 0.0f, 2.0f);
477 cam_pos = vec3(0.3f, 0.0f, 0.3f);
478 float cam_yaw = 45.0f * 2.0f * 3.14159f / 360.0f;
479
480 mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
481 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
482 /*
483 mat4 T = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
484 mat4 R = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
485 */
486 view_mat = R*T;
487
488 float fov = 67.0f * ONE_DEG_IN_RAD;
489 float aspect = (float)width / (float)height;
490
491 float range = tan(fov * 0.5f) * NEAR_CLIP;
492 float Sx = NEAR_CLIP / (range * aspect);
493 float Sy = NEAR_CLIP / range;
494 float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
495 float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
496
497 /*
498 float proj_arr[] = {
499 Sx, 0.0f, 0.0f, 0.0f,
500 0.0f, Sy, 0.0f, 0.0f,
501 0.0f, 0.0f, Sz, -1.0f,
502 0.0f, 0.0f, Pz, 0.0f,
503 };
504 */
505 float proj_arr[] = {
506 1.0f, 0.0f, 0.0f, 0.0f,
507 0.0f, 1.0f, 0.0f, 0.0f,
508 0.0f, 0.0f, 1.0f, 0.0f,
509 0.0f, 0.0f, 0.0f, 1.0f,
510 };
511 proj_mat = make_mat4(proj_arr);
512
513 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
514 GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
515 GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
516
517 GLint model_test_loc = glGetUniformLocation(shader_program, "model");
518 GLint view_test_loc = glGetUniformLocation(shader_program, "view");
519 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
520
521 glUseProgram(shader_program);
522 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
523 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
524
525 /*
526 glUseProgram(shader_program2);
527 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
528 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
529 */
530
531 // glUniform1i(tex_loc, 0);
532
533 bool cam_moved = false;
534
535 double previous_seconds = glfwGetTime();
536 while (!glfwWindowShouldClose(window)) {
537 double current_seconds = glfwGetTime();
538 double elapsed_seconds = current_seconds - previous_seconds;
539 previous_seconds = current_seconds;
540
541 if (fabs(last_position) > 1.0f) {
542 speed = -speed;
543 }
544
545 if (clicked) {
546 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
547
548 if (colors_i == 0) {
549 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
550 colors_i = 1;
551 } else {
552 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
553 colors_i = 0;
554 }
555
556 clicked = false;
557 }
558
559 /*
560 model[12] = last_position + speed*elapsed_seconds;
561 last_position = model[12];
562 */
563
564 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
565
566 glUseProgram(shader_program);
567 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
568
569 glBindVertexArray(vao);
570
571 glDrawArrays(GL_TRIANGLES, 0, numPoints);
572
573 glUseProgram(shader_program2);
574 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
575
576 glBindVertexArray(vao2);
577
578 int numPoints3 = numPoints2;
579 numPoints2 = numPoints3;
580 // glDrawArrays(GL_TRIANGLES, 0, numPoints2);
581
582 glfwPollEvents();
583 glfwSwapBuffers(window);
584
585 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
586 glfwSetWindowShouldClose(window, 1);
587 }
588
589 float dist = cam_speed * elapsed_seconds;
590 if (glfwGetKey(window, GLFW_KEY_A)) {
591 cam_pos.x -= cos(cam_yaw)*dist;
592 cam_pos.z += sin(cam_yaw)*dist;
593 cam_moved = true;
594 }
595 if (glfwGetKey(window, GLFW_KEY_D)) {
596 cam_pos.x += cos(cam_yaw)*dist;
597 cam_pos.z -= sin(cam_yaw)*dist;
598 cam_moved = true;
599 }
600 if (glfwGetKey(window, GLFW_KEY_W)) {
601 cam_pos.x -= sin(cam_yaw)*dist;
602 cam_pos.z -= cos(cam_yaw)*dist;
603 cam_moved = true;
604 }
605 if (glfwGetKey(window, GLFW_KEY_S)) {
606 cam_pos.x += sin(cam_yaw)*dist;
607 cam_pos.z += cos(cam_yaw)*dist;
608 cam_moved = true;
609 }
610 if (glfwGetKey(window, GLFW_KEY_LEFT)) {
611 cam_yaw += cam_yaw_speed * elapsed_seconds;
612 cam_moved = true;
613 }
614 if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
615 cam_yaw -= cam_yaw_speed * elapsed_seconds;
616 cam_moved = true;
617 }
618 if (cam_moved) {
619 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
620 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
621 // view_mat = R*T;
622
623 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
624 cam_moved = false;
625 }
626 }
627
628 glfwTerminate();
629 return 0;
630}
631
632GLuint loadShader(GLenum type, string file) {
633 cout << "Loading shader from file " << file << endl;
634
635 ifstream shaderFile(file);
636 GLuint shaderId = 0;
637
638 if (shaderFile.is_open()) {
639 string line, shaderString;
640
641 while(getline(shaderFile, line)) {
642 shaderString += line + "\n";
643 }
644 shaderFile.close();
645 const char* shaderCString = shaderString.c_str();
646
647 shaderId = glCreateShader(type);
648 glShaderSource(shaderId, 1, &shaderCString, NULL);
649 glCompileShader(shaderId);
650
651 cout << "Loaded successfully" << endl;
652 } else {
653 cout << "Failed to loade the file" << endl;
654 }
655
656 return shaderId;
657}
658
659GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
660 GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
661 GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
662
663 GLuint shader_program = glCreateProgram();
664 glAttachShader(shader_program, vs);
665 glAttachShader(shader_program, fs);
666
667 glLinkProgram(shader_program);
668
669 return shader_program;
670}
671
672unsigned char* loadImage(string file_name, int* x, int* y) {
673 int n;
674 int force_channels = 4;
675 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
676 if (!image_data) {
677 fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
678 }
679 return image_data;
680}
681
682bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3) {
683 vec3 v21 = v2-v1;
684 vec3 v31 = v3-v1;
685 vec3 pv1 = p-v1;
686
687 float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
688 float x = (pv1.x-y*v31.x) / v21.x;
689
690 cout << "(" << x << ", " << y << ")" << endl;
691
692 return x > 0.0f && y > 0.0f && x+y < 1.0f;
693}
694
695void printVector(string label, vec3 v) {
696 cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
697}
Note: See TracBrowser for help on using the repository browser.