source: opengl-game/new-game.cpp@ 64a70f4

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

Enable rendering of the textured square again and start implementing click detection for the square

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