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
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;
[a5b5e95]162 //x = -.25f;
[d12d003]163 //x = -.5f;
164
[a5b5e95]165 //y = .1f;
[d12d003]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
[a5b5e95]183 vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
[d12d003]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
[d12d003]193 printVector("Initial world ray:", ray_world);
[33a9664]194
[a5b5e95]195 vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
196 vec3 cam_pos_temp = (inverse(view_mat) * cam_pos_origin).xyz();
[33a9664]197
[d12d003]198 ray_world = ray_world-cam_pos_temp;
[33a9664]199
[d12d003]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;
[33a9664]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
[d12d003]213 // LINE EQUATION: P = O + Dt
214 // O = cam_pos
215 // D = ray_world
[33a9664]216
[d12d003]217 // PLANE EQUATION: P dot n + d = 0 (n is the normal vector and d is the offset from the origin)
[33a9664]218
[d12d003]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;
[33a9664]222
[d12d003]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;
[33a9664]229
[d12d003]230 float d = -glm::dot(fp1, normal);
231 cout << "d: " << d << endl;
[33a9664]232
[d12d003]233 float t = - (glm::dot(cam_pos_temp, normal) + d) / glm::dot(ray_world, normal);
234 cout << "t: " << t << endl;
[33a9664]235
[d12d003]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;
[c62eee6]241 }
242}
243
[5272b6b]244int main(int argc, char* argv[]) {
245 cout << "New OpenGL Game" << endl;
246
[ec4456b]247 if (!restart_gl_log()) {}
248 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
[22b2c37]249
[ec4456b]250 glfwSetErrorCallback(glfw_error_callback);
[5272b6b]251 if (!glfwInit()) {
252 fprintf(stderr, "ERROR: could not start GLFW3\n");
253 return 1;
[be246ad]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
[5272b6b]262
[ec4456b]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
[5272b6b]280 if (!window) {
281 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
282 glfwTerminate();
283 return 1;
284 }
[c62eee6]285
[d12d003]286 glfwSetMouseButtonCallback(window, mouse_button_callback_new);
[c62eee6]287
[644a2e4]288 glfwMakeContextCurrent(window);
[5272b6b]289 glewExperimental = GL_TRUE;
290 glewInit();
291
[ec4456b]292 // glViewport(0, 0, width*2, height*2);
293
[5272b6b]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);
[93baa0e]298
[5272b6b]299 glEnable(GL_DEPTH_TEST);
300 glDepthFunc(GL_LESS);
[516668e]301
[93baa0e]302 glEnable(GL_CULL_FACE);
303 // glCullFace(GL_BACK);
304 // glFrontFace(GL_CW);
305
[485424b]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
[516668e]326 GLfloat points[] = {
[d12d003]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,
[516668e]333 };
[33a9664]334 /*
335 GLfloat points[] = {
[d12d003]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,
[33a9664]342 };
343 */
[516668e]344
[c62eee6]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
[8b7cfcf]350 GLfloat colors[] = {
351 1.0, 0.0, 0.0,
352 0.0, 0.0, 1.0,
[93baa0e]353 0.0, 1.0, 0.0,
[7ee66ea]354 0.0, 1.0, 0.0,
355 0.0, 0.0, 1.0,
356 1.0, 0.0, 0.0,
[93baa0e]357 };
358
[33a9664]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
[485424b]368 // Each point is made of 3 floats
369 int numPoints = (sizeof(points) / sizeof(float)) / 3;
370
371 GLfloat points2[] = {
[d12d003]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,
[485424b]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
[c62eee6]403 /*
[201e2f8]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));
[c62eee6]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));
[201e2f8]409 mat4 model_mat = T_model*R_model;
[8b7cfcf]410
[485424b]411 mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
[33a9664]412 // mat4 T_model2 = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
[485424b]413 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
414 mat4 model_mat2 = T_model2*R_model2;
415
[8b7cfcf]416 GLuint points_vbo = 0;
417 glGenBuffers(1, &points_vbo);
418 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]419 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
420
[8b7cfcf]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
[644a2e4]426 GLuint vao = 0;
[516668e]427 glGenVertexArrays(1, &vao);
428 glBindVertexArray(vao);
[8b7cfcf]429 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]430 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[8b7cfcf]431 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
432 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[516668e]433
[8b7cfcf]434 glEnableVertexAttribArray(0);
435 glEnableVertexAttribArray(1);
[644a2e4]436
[485424b]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);
[644a2e4]463
[485424b]464 glEnableVertexAttribArray(0);
465 glEnableVertexAttribArray(1);
[8b7cfcf]466
[485424b]467 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
468 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
[644a2e4]469
[93baa0e]470 float speed = 1.0f;
471 float last_position = 0.0f;
472
[7ee66ea]473 float cam_speed = 1.0f;
[201e2f8]474 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
[7ee66ea]475
[d12d003]476 //cam_pos = vec3(0.0f, 0.0f, 2.0f);
[a5b5e95]477 cam_pos = vec3(0.3f, 0.0f, 0.3f);
[d12d003]478 float cam_yaw = 45.0f * 2.0f * 3.14159f / 360.0f;
[7ee66ea]479
[c62eee6]480 mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
[7ee66ea]481 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
[33a9664]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 */
[c62eee6]486 view_mat = R*T;
[7ee66ea]487
488 float fov = 67.0f * ONE_DEG_IN_RAD;
489 float aspect = (float)width / (float)height;
490
[d12d003]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);
[7ee66ea]496
[d12d003]497 /*
[c62eee6]498 float proj_arr[] = {
[7ee66ea]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 };
[d12d003]504 */
[33a9664]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 };
[c62eee6]511 proj_mat = make_mat4(proj_arr);
[7ee66ea]512
[485424b]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");
[7ee66ea]520
521 glUseProgram(shader_program);
[485424b]522 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
[c62eee6]523 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
[485424b]524
[d12d003]525 /*
[485424b]526 glUseProgram(shader_program2);
527 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
[c62eee6]528 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
[d12d003]529 */
[7ee66ea]530
[485424b]531 // glUniform1i(tex_loc, 0);
532
[7ee66ea]533 bool cam_moved = false;
534
[93baa0e]535 double previous_seconds = glfwGetTime();
[644a2e4]536 while (!glfwWindowShouldClose(window)) {
[93baa0e]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
[33a9664]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
[7ee66ea]559 /*
[93baa0e]560 model[12] = last_position + speed*elapsed_seconds;
561 last_position = model[12];
[7ee66ea]562 */
[93baa0e]563
564 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[485424b]565
566 glUseProgram(shader_program);
567 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
568
[644a2e4]569 glBindVertexArray(vao);
[93baa0e]570
[7ee66ea]571 glDrawArrays(GL_TRIANGLES, 0, numPoints);
[ec4456b]572
[485424b]573 glUseProgram(shader_program2);
574 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
575
576 glBindVertexArray(vao2);
577
[33a9664]578 int numPoints3 = numPoints2;
579 numPoints2 = numPoints3;
580 // glDrawArrays(GL_TRIANGLES, 0, numPoints2);
[485424b]581
[644a2e4]582 glfwPollEvents();
583 glfwSwapBuffers(window);
[ec4456b]584
585 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
586 glfwSetWindowShouldClose(window, 1);
587 }
[7ee66ea]588
589 float dist = cam_speed * elapsed_seconds;
590 if (glfwGetKey(window, GLFW_KEY_A)) {
[c62eee6]591 cam_pos.x -= cos(cam_yaw)*dist;
592 cam_pos.z += sin(cam_yaw)*dist;
[7ee66ea]593 cam_moved = true;
594 }
595 if (glfwGetKey(window, GLFW_KEY_D)) {
[c62eee6]596 cam_pos.x += cos(cam_yaw)*dist;
597 cam_pos.z -= sin(cam_yaw)*dist;
[7ee66ea]598 cam_moved = true;
599 }
600 if (glfwGetKey(window, GLFW_KEY_W)) {
[c62eee6]601 cam_pos.x -= sin(cam_yaw)*dist;
602 cam_pos.z -= cos(cam_yaw)*dist;
[7ee66ea]603 cam_moved = true;
604 }
605 if (glfwGetKey(window, GLFW_KEY_S)) {
[c62eee6]606 cam_pos.x += sin(cam_yaw)*dist;
607 cam_pos.z += cos(cam_yaw)*dist;
[7ee66ea]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) {
[c62eee6]619 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
[7ee66ea]620 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
[33a9664]621 // view_mat = R*T;
[7ee66ea]622
623 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
624 cam_moved = false;
625 }
[644a2e4]626 }
627
[5272b6b]628 glfwTerminate();
629 return 0;
630}
[ec4456b]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}
[485424b]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}
[33a9664]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}
[d12d003]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.