source: opengl-game/new-game.cpp@ 267c4c5

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

Re-enable movement controls

  • Property mode set to 100644
File size: 19.2 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
[5c9d193]8// This is to fix a non-alignment issue when passing vec4 params.
9// Check if it got fixed in a later version of GLM
10#define GLM_FORCE_PURE
11
[c62eee6]12#include <glm/mat4x4.hpp>
[7ee66ea]13#include <glm/gtc/matrix_transform.hpp>
14#include <glm/gtc/type_ptr.hpp>
15
[5272b6b]16#include <GL/glew.h>
17#include <GLFW/glfw3.h>
18
[22b2c37]19#include <cstdio>
20#include <iostream>
[ec4456b]21#include <fstream>
[93baa0e]22#include <cmath>
[1099b95]23#include <string>
[19c9338]24#include <array>
[df652d5]25#include <vector>
[22b2c37]26
[5272b6b]27using namespace std;
[7ee66ea]28using namespace glm;
29
30#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
[c62eee6]31
[b73cb3b]32/*
33 * If I use one array to store the points for all the object faces in the scene, I'll probably remove the ObjectFace object,
34 * and store the start and end indices of a given object's point coordinates in that array in the SceneObject.
35 *
36 * Should probably do something similar with colors and texture coordinates, once I figure out the best way to store tex coords
37 * for all objects in one array.
38 */
39
40
41// might also want to store the shader to be used for the object
[df652d5]42struct SceneObject {
43 mat4 model_mat;
44};
45
46struct ObjectFace {
[e82692b]47 unsigned int object_id;
[df652d5]48 array<vec3, 3> points;
49};
50
[485424b]51const bool FULLSCREEN = false;
[c62eee6]52int width = 640;
53int height = 480;
54
55vec3 cam_pos;
56
57mat4 view_mat;
58mat4 proj_mat;
[5272b6b]59
[df652d5]60vector<SceneObject> objects;
61vector<ObjectFace> faces;
62
[147ac6d]63SceneObject* clickedObject = NULL;
64SceneObject* selectedObject = NULL;
65
[e82692b]66bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
[5c9d193]67bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
[33a9664]68
[ec4456b]69GLuint loadShader(GLenum type, string file);
[485424b]70GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
71unsigned char* loadImage(string file_name, int* x, int* y);
[ec4456b]72
[d12d003]73void printVector(string label, vec3 v);
[b73cb3b]74void print4DVector(string label, vec4 v);
[d12d003]75
76float NEAR_CLIP = 0.1f;
77float FAR_CLIP = 100.0f;
78
[ec4456b]79void glfw_error_callback(int error, const char* description) {
80 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
81}
82
[c62eee6]83void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
[33a9664]84 double mouse_x, mouse_y;
85 glfwGetCursorPos(window, &mouse_x, &mouse_y);
86
87 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
88 cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
[147ac6d]89 selectedObject = NULL;
[33a9664]90
91 float x = (2.0f*mouse_x) / width - 1.0f;
92 float y = 1.0f - (2.0f*mouse_y) / height;
[d12d003]93
[33a9664]94 cout << "x: " << x << ", y: " << y << endl;
95
[b73cb3b]96 vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
97 vec4 ray_eye = inverse(proj_mat) * ray_clip;
98 ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
[5c9d193]99 vec4 ray_world = inverse(view_mat) * ray_eye;
[33a9664]100
[b73cb3b]101 vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
[33a9664]102
[e82692b]103 vec4 click_point;
[b73cb3b]104 vec3 closest_point = vec3(0.0f, 0.0f, -FAR_CLIP); // Any valid point will be closer than the far clipping plane, so initial value to that
[e82692b]105 int closest_face_id = -1;
106
[5c9d193]107 for (int i = 0; i<faces.size(); i++) {
[e82692b]108 if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
109 click_point = view_mat * click_point;
110
[b73cb3b]111 if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
[e82692b]112 closest_point = click_point.xyz();
113 closest_face_id = i;
114 }
[5c9d193]115 }
116 }
[d12d003]117
[e82692b]118 if (closest_face_id == -1) {
[5c9d193]119 cout << "No object was clicked" << endl;
[e82692b]120 } else {
121 clickedObject = &objects[faces[closest_face_id].object_id];
122 cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
[147ac6d]123 }
[c62eee6]124 }
125}
126
[5272b6b]127int main(int argc, char* argv[]) {
128 cout << "New OpenGL Game" << endl;
129
[ec4456b]130 if (!restart_gl_log()) {}
131 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
[22b2c37]132
[ec4456b]133 glfwSetErrorCallback(glfw_error_callback);
[5272b6b]134 if (!glfwInit()) {
135 fprintf(stderr, "ERROR: could not start GLFW3\n");
136 return 1;
[be246ad]137 }
138
139#ifdef __APPLE__
140 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
141 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
142 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
143 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
144#endif
[5272b6b]145
[ec4456b]146 glfwWindowHint(GLFW_SAMPLES, 4);
147
148 GLFWwindow* window = NULL;
149
150 if (FULLSCREEN) {
151 GLFWmonitor* mon = glfwGetPrimaryMonitor();
152 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
153
154 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
155 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
156
157 width = vmode->width;
158 height = vmode->height;
159 } else {
160 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
161 }
162
[5272b6b]163 if (!window) {
164 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
165 glfwTerminate();
166 return 1;
167 }
[c62eee6]168
[b73cb3b]169 glfwSetMouseButtonCallback(window, mouse_button_callback);
[c62eee6]170
[644a2e4]171 glfwMakeContextCurrent(window);
[5272b6b]172 glewExperimental = GL_TRUE;
173 glewInit();
174
[b73cb3b]175 // Check the extended initialization section of the book to learn how to use this
176 // Maybe move this and other OpenGL setup/settings code into a separate function
[ec4456b]177 // glViewport(0, 0, width*2, height*2);
178
[5272b6b]179 const GLubyte* renderer = glGetString(GL_RENDERER);
180 const GLubyte* version = glGetString(GL_VERSION);
181 printf("Renderer: %s\n", renderer);
182 printf("OpenGL version supported %s\n", version);
[93baa0e]183
[5272b6b]184 glEnable(GL_DEPTH_TEST);
185 glDepthFunc(GL_LESS);
[516668e]186
[93baa0e]187 glEnable(GL_CULL_FACE);
188 // glCullFace(GL_BACK);
189 // glFrontFace(GL_CW);
190
[485424b]191 int x, y;
192 unsigned char* texImage = loadImage("test.png", &x, &y);
193 if (texImage) {
194 cout << "Yay, I loaded an image!" << endl;
195 cout << x << endl;
196 cout << y << endl;
197 printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
198 }
199
200 GLuint tex = 0;
201 glGenTextures(1, &tex);
202 glActiveTexture(GL_TEXTURE0);
203 glBindTexture(GL_TEXTURE_2D, tex);
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
205
206 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
208 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
209 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
210
[516668e]211 GLfloat points[] = {
[d12d003]212 0.0f, 0.5f, 0.0f,
213 -0.5f, -0.5f, 0.0f,
214 0.5f, -0.5f, 0.0f,
215 0.5f, -0.5f, 0.0f,
216 -0.5f, -0.5f, 0.0f,
217 0.0f, 0.5f, 0.0f,
[516668e]218 };
[c62eee6]219
[8b7cfcf]220 GLfloat colors[] = {
[64a70f4]221 1.0, 0.0, 0.0,
222 0.0, 0.0, 1.0,
223 0.0, 1.0, 0.0,
224 0.0, 1.0, 0.0,
225 0.0, 0.0, 1.0,
226 1.0, 0.0, 0.0,
[93baa0e]227 };
228
[33a9664]229 GLfloat colors_new[] = {
[64a70f4]230 0.0, 1.0, 0.0,
231 0.0, 1.0, 0.0,
232 0.0, 1.0, 0.0,
233 0.0, 1.0, 0.0,
234 0.0, 1.0, 0.0,
235 0.0, 1.0, 0.0,
[33a9664]236 };
237
[485424b]238 // Each point is made of 3 floats
239 int numPoints = (sizeof(points) / sizeof(float)) / 3;
240
241 GLfloat points2[] = {
[b73cb3b]242 0.5f, 0.5f, 0.0f,
[d12d003]243 -0.5f, 0.5f, 0.0f,
244 -0.5f, -0.5f, 0.0f,
[b73cb3b]245 0.5f, 0.5f, 0.0f,
[d12d003]246 -0.5f, -0.5f, 0.0f,
[b73cb3b]247 0.5f, -0.5f, 0.0f,
[64a70f4]248 };
[485424b]249
250 GLfloat colors2[] = {
[64a70f4]251 0.0, 0.9, 0.9,
252 0.0, 0.9, 0.9,
253 0.0, 0.9, 0.9,
254 0.0, 0.9, 0.9,
255 0.0, 0.9, 0.9,
256 0.0, 0.9, 0.9,
[485424b]257 };
258
259 GLfloat texcoords[] = {
[64a70f4]260 1.0f, 1.0f,
261 0.0f, 1.0f,
262 0.0, 0.0,
263 1.0, 1.0,
264 0.0, 0.0,
265 1.0, 0.0
[485424b]266 };
267
268 // Each point is made of 3 floats
269 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
270
[df652d5]271 mat4 T_model, R_model;
272
273 // triangle
274 objects.push_back(SceneObject());
275
[b73cb3b]276 T_model = translate(mat4(), vec3(0.25f, 0.0f, 0.0f));
[df652d5]277 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
278 objects[0].model_mat = T_model*R_model;
279
280 faces.push_back(ObjectFace());
[e82692b]281 faces[0].object_id = 0;
[df652d5]282 faces[0].points = {
[19c9338]283 vec3(points[0], points[1], points[2]),
284 vec3(points[3], points[4], points[5]),
285 vec3(points[6], points[7], points[8]),
286 };
287
[df652d5]288 // square
289 objects.push_back(SceneObject());
290
[b73cb3b]291 T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
292 R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
[df652d5]293 objects[1].model_mat = T_model*R_model;
294
295 faces.push_back(ObjectFace());
[e82692b]296 faces[1].object_id = 1;
[df652d5]297 faces[1].points = {
[19c9338]298 vec3(points2[0], points2[1], points2[2]),
299 vec3(points2[3], points2[4], points2[5]),
300 vec3(points2[6], points2[7], points2[8]),
301 };
302
[df652d5]303 faces.push_back(ObjectFace());
[e82692b]304 faces[2].object_id = 1;
[df652d5]305 faces[2].points = {
[19c9338]306 vec3(points2[9], points2[10], points2[11]),
307 vec3(points2[12], points2[13], points2[14]),
308 vec3(points2[15], points2[16], points2[17]),
309 };
310
[8b7cfcf]311 GLuint points_vbo = 0;
312 glGenBuffers(1, &points_vbo);
313 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]314 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
315
[8b7cfcf]316 GLuint colors_vbo = 0;
317 glGenBuffers(1, &colors_vbo);
318 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
319 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
320
[644a2e4]321 GLuint vao = 0;
[516668e]322 glGenVertexArrays(1, &vao);
323 glBindVertexArray(vao);
[8b7cfcf]324 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
[516668e]325 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[8b7cfcf]326 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
327 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
[516668e]328
[8b7cfcf]329 glEnableVertexAttribArray(0);
330 glEnableVertexAttribArray(1);
[644a2e4]331
[485424b]332 GLuint points2_vbo = 0;
333 glGenBuffers(1, &points2_vbo);
334 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
335 glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
336
337 GLuint colors2_vbo = 0;
338 glGenBuffers(1, &colors2_vbo);
339 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
340 glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
341
342 GLuint vt_vbo;
343 glGenBuffers(1, &vt_vbo);
344 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
345 glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
346
347 GLuint vao2 = 0;
348 glGenVertexArrays(1, &vao2);
349 glBindVertexArray(vao2);
350 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
351 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
352 // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
353 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
354 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
355 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
[644a2e4]356
[485424b]357 glEnableVertexAttribArray(0);
358 glEnableVertexAttribArray(1);
[8b7cfcf]359
[485424b]360 GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
361 GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
[644a2e4]362
[93baa0e]363 float speed = 1.0f;
364 float last_position = 0.0f;
365
[7ee66ea]366 float cam_speed = 1.0f;
[201e2f8]367 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
[7ee66ea]368
[b73cb3b]369 // glm::lookAt can create the view matrix
370 // glm::perspective can create the projection matrix
371
372 cam_pos = vec3(0.0f, 0.0f, 2.0f);
[64a70f4]373 float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
[7ee66ea]374
[c62eee6]375 mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
[7ee66ea]376 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
[c62eee6]377 view_mat = R*T;
[7ee66ea]378
379 float fov = 67.0f * ONE_DEG_IN_RAD;
380 float aspect = (float)width / (float)height;
381
[d12d003]382 float range = tan(fov * 0.5f) * NEAR_CLIP;
383 float Sx = NEAR_CLIP / (range * aspect);
384 float Sy = NEAR_CLIP / range;
385 float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
386 float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
[7ee66ea]387
[c62eee6]388 float proj_arr[] = {
[7ee66ea]389 Sx, 0.0f, 0.0f, 0.0f,
390 0.0f, Sy, 0.0f, 0.0f,
391 0.0f, 0.0f, Sz, -1.0f,
392 0.0f, 0.0f, Pz, 0.0f,
393 };
[c62eee6]394 proj_mat = make_mat4(proj_arr);
[7ee66ea]395
[485424b]396 GLint model_test_loc = glGetUniformLocation(shader_program, "model");
397 GLint view_test_loc = glGetUniformLocation(shader_program, "view");
398 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
[7ee66ea]399
[19c9338]400 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
401 GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
402 GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
403
[7ee66ea]404 glUseProgram(shader_program);
[df652d5]405 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
[19c9338]406 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
[c62eee6]407 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
[485424b]408
409 glUseProgram(shader_program2);
[df652d5]410 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
[19c9338]411 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
[c62eee6]412 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
[7ee66ea]413
414 bool cam_moved = false;
415
[93baa0e]416 double previous_seconds = glfwGetTime();
[644a2e4]417 while (!glfwWindowShouldClose(window)) {
[93baa0e]418 double current_seconds = glfwGetTime();
419 double elapsed_seconds = current_seconds - previous_seconds;
420 previous_seconds = current_seconds;
421
422 if (fabs(last_position) > 1.0f) {
423 speed = -speed;
424 }
425
[147ac6d]426 if (clickedObject == &objects[0]) {
427 selectedObject = &objects[0];
428 }
[33a9664]429
[147ac6d]430 // At some point, I should change this to only rebind the buffer once per click, not once per frame
431 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
432 if (selectedObject == &objects[0]) {
433 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
434 }
435 else {
436 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
[64a70f4]437 }
[33a9664]438
[7ee66ea]439 /*
[93baa0e]440 model[12] = last_position + speed*elapsed_seconds;
441 last_position = model[12];
[7ee66ea]442 */
[93baa0e]443
444 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[485424b]445
446 glUseProgram(shader_program);
[19c9338]447
[b73cb3b]448 // Since every object will have a different model matrix, maybe it shouldn't be a uniform
449
[19c9338]450 // this is temporary.
451 // It's needed to offset the code for the recoloring of the square working during click detection
[df652d5]452 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
[485424b]453
[644a2e4]454 glBindVertexArray(vao);
[93baa0e]455
[7ee66ea]456 glDrawArrays(GL_TRIANGLES, 0, numPoints);
[ec4456b]457
[147ac6d]458 if (clickedObject == &objects[1]) {
459 selectedObject = &objects[1];
[df652d5]460 }
461
[147ac6d]462 if (selectedObject == &objects[1]) {
[64a70f4]463 glUseProgram(shader_program);
[19c9338]464
465 // this is temporary.
466 // It's needed to get the recoloring of the square working during click detection
[df652d5]467 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
[64a70f4]468
469 glBindVertexArray(vao2);
[485424b]470
[64a70f4]471 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
472 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
473 } else {
474 glUseProgram(shader_program2);
475
476 glBindVertexArray(vao2);
477
478 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
479 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
480 }
[485424b]481
[64a70f4]482 glDrawArrays(GL_TRIANGLES, 0, numPoints2);
[485424b]483
[147ac6d]484 clickedObject = NULL;
[df652d5]485
[644a2e4]486 glfwPollEvents();
487 glfwSwapBuffers(window);
[ec4456b]488
489 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
490 glfwSetWindowShouldClose(window, 1);
491 }
[7ee66ea]492
493 float dist = cam_speed * elapsed_seconds;
494 if (glfwGetKey(window, GLFW_KEY_A)) {
[c62eee6]495 cam_pos.x -= cos(cam_yaw)*dist;
496 cam_pos.z += sin(cam_yaw)*dist;
[7ee66ea]497 cam_moved = true;
498 }
499 if (glfwGetKey(window, GLFW_KEY_D)) {
[c62eee6]500 cam_pos.x += cos(cam_yaw)*dist;
501 cam_pos.z -= sin(cam_yaw)*dist;
[7ee66ea]502 cam_moved = true;
503 }
504 if (glfwGetKey(window, GLFW_KEY_W)) {
[c62eee6]505 cam_pos.x -= sin(cam_yaw)*dist;
506 cam_pos.z -= cos(cam_yaw)*dist;
[7ee66ea]507 cam_moved = true;
508 }
509 if (glfwGetKey(window, GLFW_KEY_S)) {
[c62eee6]510 cam_pos.x += sin(cam_yaw)*dist;
511 cam_pos.z += cos(cam_yaw)*dist;
[7ee66ea]512 cam_moved = true;
513 }
514 if (glfwGetKey(window, GLFW_KEY_LEFT)) {
515 cam_yaw += cam_yaw_speed * elapsed_seconds;
516 cam_moved = true;
517 }
518 if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
519 cam_yaw -= cam_yaw_speed * elapsed_seconds;
520 cam_moved = true;
521 }
522 if (cam_moved) {
[c62eee6]523 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
[7ee66ea]524 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
[267c4c5]525 view_mat = R*T;
[7ee66ea]526
[267c4c5]527 glUseProgram(shader_program);
528 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
529
530 glUseProgram(shader_program2);
[7ee66ea]531 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
[267c4c5]532
[7ee66ea]533 cam_moved = false;
534 }
[644a2e4]535 }
536
[5272b6b]537 glfwTerminate();
538 return 0;
539}
[ec4456b]540
541GLuint loadShader(GLenum type, string file) {
542 cout << "Loading shader from file " << file << endl;
543
544 ifstream shaderFile(file);
545 GLuint shaderId = 0;
546
547 if (shaderFile.is_open()) {
548 string line, shaderString;
549
550 while(getline(shaderFile, line)) {
551 shaderString += line + "\n";
552 }
553 shaderFile.close();
554 const char* shaderCString = shaderString.c_str();
555
556 shaderId = glCreateShader(type);
557 glShaderSource(shaderId, 1, &shaderCString, NULL);
558 glCompileShader(shaderId);
559
560 cout << "Loaded successfully" << endl;
561 } else {
562 cout << "Failed to loade the file" << endl;
563 }
564
565 return shaderId;
566}
[485424b]567
568GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
569 GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
570 GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
571
572 GLuint shader_program = glCreateProgram();
573 glAttachShader(shader_program, vs);
574 glAttachShader(shader_program, fs);
575
576 glLinkProgram(shader_program);
577
578 return shader_program;
579}
580
581unsigned char* loadImage(string file_name, int* x, int* y) {
582 int n;
583 int force_channels = 4;
584 unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
585 if (!image_data) {
586 fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
587 }
588 return image_data;
589}
[33a9664]590
[e82692b]591bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
[5c9d193]592 // LINE EQUATION: P = O + Dt
[b73cb3b]593 // O = cam
[5c9d193]594 // D = ray_world
595
[b73cb3b]596 // PLANE EQUATION: P dot n + d = 0
597 // n is the normal vector
598 // d is the offset from the origin
[5c9d193]599
600 // Take the cross-product of two vectors on the plane to get the normal
601 vec3 v1 = face->points[1] - face->points[0];
602 vec3 v2 = face->points[2] - face->points[0];
603
604 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);
[b73cb3b]605
606 print4DVector("Full world ray", world_ray);
[5c9d193]607
[e82692b]608 SceneObject* obj = &objects[face->object_id];
[5c9d193]609 vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
610 vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
611
[b73cb3b]612 local_ray = local_ray - local_cam;
[5c9d193]613
614 float d = -glm::dot(face->points[0], normal);
615 cout << "d: " << d << endl;
616
617 float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
618 cout << "t: " << t << endl;
619
620 vec3 intersection = local_cam + t*local_ray;
621 printVector("Intersection", intersection);
622
[e82692b]623 if (insideTriangle(intersection, face->points)) {
624 click_point = obj->model_mat * vec4(intersection, 1.0f);
625 return true;
626 } else {
627 return false;
628 }
[5c9d193]629}
630
631bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
632 vec3 v21 = triangle_points[1]- triangle_points[0];
633 vec3 v31 = triangle_points[2]- triangle_points[0];
634 vec3 pv1 = p- triangle_points[0];
[33a9664]635
636 float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
637 float x = (pv1.x-y*v31.x) / v21.x;
638
639 cout << "(" << x << ", " << y << ")" << endl;
640
641 return x > 0.0f && y > 0.0f && x+y < 1.0f;
642}
[d12d003]643
644void printVector(string label, vec3 v) {
645 cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
646}
[b73cb3b]647
648void print4DVector(string label, vec4 v) {
649 cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
650}
Note: See TracBrowser for help on using the repository browser.