| 1 | #include "logger.h"
|
|---|
| 2 |
|
|---|
| 3 | #include "stb_image.h"
|
|---|
| 4 |
|
|---|
| 5 | #define _USE_MATH_DEFINES
|
|---|
| 6 | #define GLM_SWIZZLE
|
|---|
| 7 |
|
|---|
| 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 |
|
|---|
| 12 | #include <glm/mat4x4.hpp>
|
|---|
| 13 | #include <glm/gtc/matrix_transform.hpp>
|
|---|
| 14 | #include <glm/gtc/type_ptr.hpp>
|
|---|
| 15 |
|
|---|
| 16 | #include <GL/glew.h>
|
|---|
| 17 | #include <GLFW/glfw3.h>
|
|---|
| 18 |
|
|---|
| 19 | #include <cstdio>
|
|---|
| 20 | #include <iostream>
|
|---|
| 21 | #include <fstream>
|
|---|
| 22 | #include <cmath>
|
|---|
| 23 | #include <string>
|
|---|
| 24 | #include <array>
|
|---|
| 25 | #include <vector>
|
|---|
| 26 |
|
|---|
| 27 | using namespace std;
|
|---|
| 28 | using namespace glm;
|
|---|
| 29 |
|
|---|
| 30 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
|---|
| 31 |
|
|---|
| 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
|
|---|
| 42 | struct SceneObject {
|
|---|
| 43 | mat4 model_mat;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | struct ObjectFace {
|
|---|
| 47 | unsigned int object_id;
|
|---|
| 48 | array<vec3, 3> points;
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | const bool FULLSCREEN = false;
|
|---|
| 52 | int width = 640;
|
|---|
| 53 | int height = 480;
|
|---|
| 54 |
|
|---|
| 55 | vec3 cam_pos;
|
|---|
| 56 |
|
|---|
| 57 | mat4 view_mat;
|
|---|
| 58 | mat4 proj_mat;
|
|---|
| 59 |
|
|---|
| 60 | vector<SceneObject> objects;
|
|---|
| 61 | vector<ObjectFace> faces;
|
|---|
| 62 |
|
|---|
| 63 | SceneObject* clickedObject = NULL;
|
|---|
| 64 | SceneObject* selectedObject = NULL;
|
|---|
| 65 |
|
|---|
| 66 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
|
|---|
| 67 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
|---|
| 68 |
|
|---|
| 69 | GLuint loadShader(GLenum type, string file);
|
|---|
| 70 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
|---|
| 71 | unsigned char* loadImage(string file_name, int* x, int* y);
|
|---|
| 72 |
|
|---|
| 73 | void printVector(string label, vec3 v);
|
|---|
| 74 | void print4DVector(string label, vec4 v);
|
|---|
| 75 |
|
|---|
| 76 | float NEAR_CLIP = 0.1f;
|
|---|
| 77 | float FAR_CLIP = 100.0f;
|
|---|
| 78 |
|
|---|
| 79 | void glfw_error_callback(int error, const char* description) {
|
|---|
| 80 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
|---|
| 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;
|
|---|
| 89 | selectedObject = NULL;
|
|---|
| 90 |
|
|---|
| 91 | float x = (2.0f*mouse_x) / width - 1.0f;
|
|---|
| 92 | float y = 1.0f - (2.0f*mouse_y) / height;
|
|---|
| 93 |
|
|---|
| 94 | cout << "x: " << x << ", y: " << y << endl;
|
|---|
| 95 |
|
|---|
| 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);
|
|---|
| 99 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
|---|
| 100 |
|
|---|
| 101 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
|---|
| 102 |
|
|---|
| 103 | vec4 click_point;
|
|---|
| 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
|
|---|
| 105 | int closest_face_id = -1;
|
|---|
| 106 |
|
|---|
| 107 | for (int i = 0; i<faces.size(); i++) {
|
|---|
| 108 | if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
|
|---|
| 109 | click_point = view_mat * click_point;
|
|---|
| 110 |
|
|---|
| 111 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
|---|
| 112 | closest_point = click_point.xyz();
|
|---|
| 113 | closest_face_id = i;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (closest_face_id == -1) {
|
|---|
| 119 | cout << "No object was clicked" << endl;
|
|---|
| 120 | } else {
|
|---|
| 121 | clickedObject = &objects[faces[closest_face_id].object_id];
|
|---|
| 122 | cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | int main(int argc, char* argv[]) {
|
|---|
| 128 | cout << "New OpenGL Game" << endl;
|
|---|
| 129 |
|
|---|
| 130 | if (!restart_gl_log()) {}
|
|---|
| 131 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
|---|
| 132 |
|
|---|
| 133 | glfwSetErrorCallback(glfw_error_callback);
|
|---|
| 134 | if (!glfwInit()) {
|
|---|
| 135 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
|---|
| 136 | return 1;
|
|---|
| 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
|
|---|
| 145 |
|
|---|
| 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 |
|
|---|
| 163 | if (!window) {
|
|---|
| 164 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
|---|
| 165 | glfwTerminate();
|
|---|
| 166 | return 1;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
|---|
| 170 |
|
|---|
| 171 | glfwMakeContextCurrent(window);
|
|---|
| 172 | glewExperimental = GL_TRUE;
|
|---|
| 173 | glewInit();
|
|---|
| 174 |
|
|---|
| 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
|
|---|
| 177 | // glViewport(0, 0, width*2, height*2);
|
|---|
| 178 |
|
|---|
| 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);
|
|---|
| 183 |
|
|---|
| 184 | glEnable(GL_DEPTH_TEST);
|
|---|
| 185 | glDepthFunc(GL_LESS);
|
|---|
| 186 |
|
|---|
| 187 | glEnable(GL_CULL_FACE);
|
|---|
| 188 | // glCullFace(GL_BACK);
|
|---|
| 189 | // glFrontFace(GL_CW);
|
|---|
| 190 |
|
|---|
| 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 |
|
|---|
| 211 | GLfloat points[] = {
|
|---|
| 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,
|
|---|
| 218 | };
|
|---|
| 219 |
|
|---|
| 220 | GLfloat colors[] = {
|
|---|
| 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,
|
|---|
| 227 | };
|
|---|
| 228 |
|
|---|
| 229 | GLfloat colors_new[] = {
|
|---|
| 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,
|
|---|
| 236 | };
|
|---|
| 237 |
|
|---|
| 238 | // Each point is made of 3 floats
|
|---|
| 239 | int numPoints = (sizeof(points) / sizeof(float)) / 3;
|
|---|
| 240 |
|
|---|
| 241 | GLfloat points2[] = {
|
|---|
| 242 | 0.5f, 0.5f, 0.0f,
|
|---|
| 243 | -0.5f, 0.5f, 0.0f,
|
|---|
| 244 | -0.5f, -0.5f, 0.0f,
|
|---|
| 245 | 0.5f, 0.5f, 0.0f,
|
|---|
| 246 | -0.5f, -0.5f, 0.0f,
|
|---|
| 247 | 0.5f, -0.5f, 0.0f,
|
|---|
| 248 | };
|
|---|
| 249 |
|
|---|
| 250 | GLfloat colors2[] = {
|
|---|
| 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,
|
|---|
| 257 | };
|
|---|
| 258 |
|
|---|
| 259 | GLfloat texcoords[] = {
|
|---|
| 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
|
|---|
| 266 | };
|
|---|
| 267 |
|
|---|
| 268 | // Each point is made of 3 floats
|
|---|
| 269 | int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
|
|---|
| 270 |
|
|---|
| 271 | mat4 T_model, R_model;
|
|---|
| 272 |
|
|---|
| 273 | // triangle
|
|---|
| 274 | objects.push_back(SceneObject());
|
|---|
| 275 |
|
|---|
| 276 | T_model = translate(mat4(), vec3(0.25f, 0.0f, 0.0f));
|
|---|
| 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());
|
|---|
| 281 | faces[0].object_id = 0;
|
|---|
| 282 | faces[0].points = {
|
|---|
| 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 |
|
|---|
| 288 | // square
|
|---|
| 289 | objects.push_back(SceneObject());
|
|---|
| 290 |
|
|---|
| 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));
|
|---|
| 293 | objects[1].model_mat = T_model*R_model;
|
|---|
| 294 |
|
|---|
| 295 | faces.push_back(ObjectFace());
|
|---|
| 296 | faces[1].object_id = 1;
|
|---|
| 297 | faces[1].points = {
|
|---|
| 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 |
|
|---|
| 303 | faces.push_back(ObjectFace());
|
|---|
| 304 | faces[2].object_id = 1;
|
|---|
| 305 | faces[2].points = {
|
|---|
| 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 |
|
|---|
| 311 | GLuint points_vbo = 0;
|
|---|
| 312 | glGenBuffers(1, &points_vbo);
|
|---|
| 313 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
|---|
| 314 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
|---|
| 315 |
|
|---|
| 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 |
|
|---|
| 321 | GLuint vao = 0;
|
|---|
| 322 | glGenVertexArrays(1, &vao);
|
|---|
| 323 | glBindVertexArray(vao);
|
|---|
| 324 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
|---|
| 325 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 326 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
|---|
| 327 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 328 |
|
|---|
| 329 | glEnableVertexAttribArray(0);
|
|---|
| 330 | glEnableVertexAttribArray(1);
|
|---|
| 331 |
|
|---|
| 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);
|
|---|
| 356 |
|
|---|
| 357 | glEnableVertexAttribArray(0);
|
|---|
| 358 | glEnableVertexAttribArray(1);
|
|---|
| 359 |
|
|---|
| 360 | GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
|
|---|
| 361 | GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
|
|---|
| 362 |
|
|---|
| 363 | float speed = 1.0f;
|
|---|
| 364 | float last_position = 0.0f;
|
|---|
| 365 |
|
|---|
| 366 | float cam_speed = 1.0f;
|
|---|
| 367 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
|---|
| 368 |
|
|---|
| 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);
|
|---|
| 373 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
|---|
| 374 |
|
|---|
| 375 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
|---|
| 376 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 377 | view_mat = R*T;
|
|---|
| 378 |
|
|---|
| 379 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
|---|
| 380 | float aspect = (float)width / (float)height;
|
|---|
| 381 |
|
|---|
| 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);
|
|---|
| 387 |
|
|---|
| 388 | float proj_arr[] = {
|
|---|
| 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 | };
|
|---|
| 394 | proj_mat = make_mat4(proj_arr);
|
|---|
| 395 |
|
|---|
| 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");
|
|---|
| 399 |
|
|---|
| 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 |
|
|---|
| 404 | glUseProgram(shader_program);
|
|---|
| 405 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
|---|
| 406 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 407 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
|---|
| 408 |
|
|---|
| 409 | glUseProgram(shader_program2);
|
|---|
| 410 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
|---|
| 411 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 412 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
|---|
| 413 |
|
|---|
| 414 | bool cam_moved = false;
|
|---|
| 415 |
|
|---|
| 416 | double previous_seconds = glfwGetTime();
|
|---|
| 417 | while (!glfwWindowShouldClose(window)) {
|
|---|
| 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 |
|
|---|
| 426 | if (clickedObject == &objects[0]) {
|
|---|
| 427 | selectedObject = &objects[0];
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 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);
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | /*
|
|---|
| 440 | model[12] = last_position + speed*elapsed_seconds;
|
|---|
| 441 | last_position = model[12];
|
|---|
| 442 | */
|
|---|
| 443 |
|
|---|
| 444 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|---|
| 445 |
|
|---|
| 446 | glUseProgram(shader_program);
|
|---|
| 447 |
|
|---|
| 448 | // Since every object will have a different model matrix, maybe it shouldn't be a uniform
|
|---|
| 449 |
|
|---|
| 450 | // this is temporary.
|
|---|
| 451 | // It's needed to offset the code for the recoloring of the square working during click detection
|
|---|
| 452 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
|---|
| 453 |
|
|---|
| 454 | glBindVertexArray(vao);
|
|---|
| 455 |
|
|---|
| 456 | glDrawArrays(GL_TRIANGLES, 0, numPoints);
|
|---|
| 457 |
|
|---|
| 458 | if (clickedObject == &objects[1]) {
|
|---|
| 459 | selectedObject = &objects[1];
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | if (selectedObject == &objects[1]) {
|
|---|
| 463 | glUseProgram(shader_program);
|
|---|
| 464 |
|
|---|
| 465 | // this is temporary.
|
|---|
| 466 | // It's needed to get the recoloring of the square working during click detection
|
|---|
| 467 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
|---|
| 468 |
|
|---|
| 469 | glBindVertexArray(vao2);
|
|---|
| 470 |
|
|---|
| 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 | }
|
|---|
| 481 |
|
|---|
| 482 | glDrawArrays(GL_TRIANGLES, 0, numPoints2);
|
|---|
| 483 |
|
|---|
| 484 | clickedObject = NULL;
|
|---|
| 485 |
|
|---|
| 486 | glfwPollEvents();
|
|---|
| 487 | glfwSwapBuffers(window);
|
|---|
| 488 |
|
|---|
| 489 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
|---|
| 490 | glfwSetWindowShouldClose(window, 1);
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | float dist = cam_speed * elapsed_seconds;
|
|---|
| 494 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
|---|
| 495 | cam_pos.x -= cos(cam_yaw)*dist;
|
|---|
| 496 | cam_pos.z += sin(cam_yaw)*dist;
|
|---|
| 497 | cam_moved = true;
|
|---|
| 498 | }
|
|---|
| 499 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
|---|
| 500 | cam_pos.x += cos(cam_yaw)*dist;
|
|---|
| 501 | cam_pos.z -= sin(cam_yaw)*dist;
|
|---|
| 502 | cam_moved = true;
|
|---|
| 503 | }
|
|---|
| 504 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
|---|
| 505 | cam_pos.x -= sin(cam_yaw)*dist;
|
|---|
| 506 | cam_pos.z -= cos(cam_yaw)*dist;
|
|---|
| 507 | cam_moved = true;
|
|---|
| 508 | }
|
|---|
| 509 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
|---|
| 510 | cam_pos.x += sin(cam_yaw)*dist;
|
|---|
| 511 | cam_pos.z += cos(cam_yaw)*dist;
|
|---|
| 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) {
|
|---|
| 523 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
|---|
| 524 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 525 | view_mat = R*T;
|
|---|
| 526 |
|
|---|
| 527 | glUseProgram(shader_program);
|
|---|
| 528 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 529 |
|
|---|
| 530 | glUseProgram(shader_program2);
|
|---|
| 531 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 532 |
|
|---|
| 533 | cam_moved = false;
|
|---|
| 534 | }
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | glfwTerminate();
|
|---|
| 538 | return 0;
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | GLuint 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 | }
|
|---|
| 567 |
|
|---|
| 568 | GLuint 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 |
|
|---|
| 581 | unsigned 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 | }
|
|---|
| 590 |
|
|---|
| 591 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
|
|---|
| 592 | // LINE EQUATION: P = O + Dt
|
|---|
| 593 | // O = cam
|
|---|
| 594 | // D = ray_world
|
|---|
| 595 |
|
|---|
| 596 | // PLANE EQUATION: P dot n + d = 0
|
|---|
| 597 | // n is the normal vector
|
|---|
| 598 | // d is the offset from the origin
|
|---|
| 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);
|
|---|
| 605 |
|
|---|
| 606 | print4DVector("Full world ray", world_ray);
|
|---|
| 607 |
|
|---|
| 608 | SceneObject* obj = &objects[face->object_id];
|
|---|
| 609 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
|---|
| 610 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
|---|
| 611 |
|
|---|
| 612 | local_ray = local_ray - local_cam;
|
|---|
| 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 |
|
|---|
| 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 | }
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | bool 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];
|
|---|
| 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 | }
|
|---|
| 643 |
|
|---|
| 644 | void printVector(string label, vec3 v) {
|
|---|
| 645 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | void print4DVector(string label, vec4 v) {
|
|---|
| 649 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
|---|
| 650 | }
|
|---|