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
RevLine 
[22b2c37]1#include "logger.h"
[5272b6b]2
[485424b]3#include "stb_image.h"
4
[1099b95]5#define _USE_MATH_DEFINES
[c62eee6]6#define GLM_SWIZZLE
[1099b95]7
[c62eee6]8#include <glm/mat4x4.hpp>
[7ee66ea]9#include <glm/gtc/matrix_transform.hpp>
10#include <glm/gtc/type_ptr.hpp>
11
[5272b6b]12#include <GL/glew.h>
13#include <GLFW/glfw3.h>
14
[22b2c37]15#include <cstdio>
16#include <iostream>
[ec4456b]17#include <fstream>
[93baa0e]18#include <cmath>
[1099b95]19#include <string>
[22b2c37]20
[5272b6b]21using namespace std;
[7ee66ea]22using namespace glm;
23
24#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
[c62eee6]25
[485424b]26const bool FULLSCREEN = false;
[c62eee6]27int width = 640;
28int height = 480;
29
30vec3 cam_pos;
31
32vec3 face_point1, face_point2, face_point3;
33
[33a9664]34bool clicked = false;
35int colors_i = 0;
36
[c62eee6]37mat4 view_mat;
38mat4 proj_mat;
[5272b6b]39
[33a9664]40bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3);
41
[ec4456b]42GLuint loadShader(GLenum type, string file);
[485424b]43GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
44unsigned char* loadImage(string file_name, int* x, int* y);
[ec4456b]45
[d12d003]46void printVector(string label, vec3 v);
47
48float NEAR_CLIP = 0.1f;
49float FAR_CLIP = 100.0f;
50
[ec4456b]51void glfw_error_callback(int error, const char* description) {
52 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
53}
54
[c62eee6]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;
[33a9664]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
[c62eee6]71
[33a9664]72 vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); // this should have a z equal to the near clipping plane
[c62eee6]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
[33a9664]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
[c62eee6]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
[33a9664]99 // upper right corner is 1, 1 in opengl
100
[c62eee6]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
[33a9664]118 vec3 fp1 = face_point1;
119 vec3 fp2 = face_point2;
120 vec3 fp3 = face_point3;
121
[c62eee6]122 cout << "Points on the plane" << endl;
[33a9664]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;
[c62eee6]133
134 // get intersection
[33a9664]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
[d12d003]146 clicked = insideTriangle(i, fp1, fp2, fp3);
147 cout << (clicked ? "true" : "false") << endl;
[33a9664]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;
[d12d003]160
161 //x = -.1f;
162 x = -.25f;
163 //x = -.5f;
164
165 y = .1f;
166
[33a9664]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
[d12d003]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();
[33a9664]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
[d12d003]196 printVector("Initial world ray:", ray_world);
[33a9664]197
[d12d003]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);
[33a9664]203
[d12d003]204 vec3 cam_pos_temp = vec3(ray_world.xy(), 0.0f);
205 ray_world = ray_world-cam_pos_temp;
[33a9664]206
207 // upper right corner is 1, 1 in opengl
208
[d12d003]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;
[33a9664]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
[d12d003]222 // LINE EQUATION: P = O + Dt
223 // O = cam_pos
224 // D = ray_world
[33a9664]225
[d12d003]226 // PLANE EQUATION: P dot n + d = 0 (n is the normal vector and d is the offset from the origin)
[33a9664]227
[d12d003]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;
[33a9664]231
[d12d003]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;
[33a9664]238
[d12d003]239 float d = -glm::dot(fp1, normal);
240 cout << "d: " << d << endl;
[33a9664]241
[d12d003]242 float t = - (glm::dot(cam_pos_temp, normal) + d) / glm::dot(ray_world, normal);
243 cout << "t: " << t << endl;
[33a9664]244
[d12d003]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;
[c62eee6]250 }
251}
252
[5272b6b]253int main(int argc, char* argv[]) {
254 cout << "New OpenGL Game" << endl;
255
[ec4456b]256 if (!restart_gl_log()) {}
257 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
[22b2c37]258
[ec4456b]259 glfwSetErrorCallback(glfw_error_callback);
[5272b6b]260 if (!glfwInit()) {
261 fprintf(stderr, "ERROR: could not start GLFW3\n");
262 return 1;
[be246ad]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
[5272b6b]271
[ec4456b]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
[5272b6b]289 if (!window) {
290 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
291 glfwTerminate();
292 return 1;
293 }
[c62eee6]294
[d12d003]295 glfwSetMouseButtonCallback(window, mouse_button_callback_new);
[c62eee6]296
[644a2e4]297 glfwMakeContextCurrent(window);
[5272b6b]298 glewExperimental = GL_TRUE;
299 glewInit();
300
[ec4456b]301 // glViewport(0, 0, width*2, height*2);
302
[5272b6b]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);
[93baa0e]307
[5272b6b]308 glEnable(GL_DEPTH_TEST);
309 glDepthFunc(GL_LESS);
[516668e]310
[93baa0e]311 glEnable(GL_CULL_FACE);
312 // glCullFace(GL_BACK);
313 // glFrontFace(GL_CW);
314
[485424b]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
[516668e]335 GLfloat points[] = {
[d12d003]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,
[516668e]342 };
[33a9664]343 /*
344 GLfloat points[] = {
[d12d003]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,
[33a9664]351 };
352 */
[516668e]353
[c62eee6]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
[8b7cfcf]359 GLfloat colors[] = {
360 1.0, 0.0, 0.0,
361 0.0, 0.0, 1.0,
[93baa0e]362 0.0, 1.0, 0.0,
[7ee66ea]363 0.0, 1.0, 0.0,
364 0.0, 0.0, 1.0,
365 1.0, 0.0, 0.0,
[93baa0e]366 };
367
[33a9664]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
[485424b]377 // Each point is made of 3 floats
378 int numPoints = (sizeof(points) / sizeof(float)) / 3;
379
380 GLfloat points2[] = {
[d12d003]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,
[485424b]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
[c62eee6]412 /*
[201e2f8]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));
[c62eee6]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));
[201e2f8]418 mat4 model_mat = T_model*R_model;
[8b7cfcf]419
[485424b]420 mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
[33a9664]421 // mat4 T_model2 = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
[485424b]422 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
423 mat4 model_mat2 = T_model2*R_model2;
424
[8b7cfcf]425 GLuint points_vbo = 0;
426 glGenBuffers(1, &points_vbo);
427 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]428 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
429
[8b7cfcf]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
[644a2e4]435 GLuint vao = 0;
[516668e]436 glGenVertexArrays(1, &vao);
437 glBindVertexArray(vao);
[8b7cfcf]438 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]439 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[8b7cfcf]440 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
441 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[516668e]442
[8b7cfcf]443 glEnableVertexAttribArray(0);
444 glEnableVertexAttribArray(1);
[644a2e4]445
[485424b]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);
[644a2e4]472
[485424b]473 glEnableVertexAttribArray(0);
474 glEnableVertexAttribArray(1);
[8b7cfcf]475
[485424b]476 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
477 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
[644a2e4]478
[93baa0e]479 float speed = 1.0f;
480 float last_position = 0.0f;
481
[7ee66ea]482 float cam_speed = 1.0f;
[201e2f8]483 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
[7ee66ea]484
[d12d003]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;
[7ee66ea]488
[c62eee6]489 mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
[7ee66ea]490 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
[33a9664]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 */
[c62eee6]495 view_mat = R*T;
[7ee66ea]496
497 float fov = 67.0f * ONE_DEG_IN_RAD;
498 float aspect = (float)width / (float)height;
499
[d12d003]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);
[7ee66ea]505
[d12d003]506 /*
[c62eee6]507 float proj_arr[] = {
[7ee66ea]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 };
[d12d003]513 */
[33a9664]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 };
[c62eee6]520 proj_mat = make_mat4(proj_arr);
[7ee66ea]521
[485424b]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");
[7ee66ea]529
530 glUseProgram(shader_program);
[485424b]531 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
[c62eee6]532 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
[485424b]533
[d12d003]534 /*
[485424b]535 glUseProgram(shader_program2);
536 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
[c62eee6]537 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
[d12d003]538 */
[7ee66ea]539
[485424b]540 // glUniform1i(tex_loc, 0);
541
[7ee66ea]542 bool cam_moved = false;
543
[93baa0e]544 double previous_seconds = glfwGetTime();
[644a2e4]545 while (!glfwWindowShouldClose(window)) {
[93baa0e]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
[33a9664]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
[7ee66ea]568 /*
[93baa0e]569 model[12] = last_position + speed*elapsed_seconds;
570 last_position = model[12];
[7ee66ea]571 */
[93baa0e]572
573 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[485424b]574
575 glUseProgram(shader_program);
576 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
577
[644a2e4]578 glBindVertexArray(vao);
[93baa0e]579
[7ee66ea]580 glDrawArrays(GL_TRIANGLES, 0, numPoints);
[ec4456b]581
[485424b]582 glUseProgram(shader_program2);
583 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
584
585 glBindVertexArray(vao2);
586
[33a9664]587 int numPoints3 = numPoints2;
588 numPoints2 = numPoints3;
589 // glDrawArrays(GL_TRIANGLES, 0, numPoints2);
[485424b]590
[644a2e4]591 glfwPollEvents();
592 glfwSwapBuffers(window);
[ec4456b]593
594 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
595 glfwSetWindowShouldClose(window, 1);
596 }
[7ee66ea]597
598 float dist = cam_speed * elapsed_seconds;
599 if (glfwGetKey(window, GLFW_KEY_A)) {
[c62eee6]600 cam_pos.x -= cos(cam_yaw)*dist;
601 cam_pos.z += sin(cam_yaw)*dist;
[7ee66ea]602 cam_moved = true;
603 }
604 if (glfwGetKey(window, GLFW_KEY_D)) {
[c62eee6]605 cam_pos.x += cos(cam_yaw)*dist;
606 cam_pos.z -= sin(cam_yaw)*dist;
[7ee66ea]607 cam_moved = true;
608 }
609 if (glfwGetKey(window, GLFW_KEY_W)) {
[c62eee6]610 cam_pos.x -= sin(cam_yaw)*dist;
611 cam_pos.z -= cos(cam_yaw)*dist;
[7ee66ea]612 cam_moved = true;
613 }
614 if (glfwGetKey(window, GLFW_KEY_S)) {
[c62eee6]615 cam_pos.x += sin(cam_yaw)*dist;
616 cam_pos.z += cos(cam_yaw)*dist;
[7ee66ea]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) {
[c62eee6]628 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
[7ee66ea]629 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
[33a9664]630 // view_mat = R*T;
[7ee66ea]631
632 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
633 cam_moved = false;
634 }
[644a2e4]635 }
636
[5272b6b]637 glfwTerminate();
638 return 0;
639}
[ec4456b]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}
[485424b]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}
[33a9664]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}
[d12d003]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.