| 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 | double fps;
|
|---|
| 67 |
|
|---|
| 68 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
|
|---|
| 69 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
|---|
| 70 |
|
|---|
| 71 | GLuint loadShader(GLenum type, string file);
|
|---|
| 72 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
|---|
| 73 | unsigned char* loadImage(string file_name, int* x, int* y);
|
|---|
| 74 |
|
|---|
| 75 | void printVector(string label, vec3 v);
|
|---|
| 76 | void print4DVector(string label, vec4 v);
|
|---|
| 77 |
|
|---|
| 78 | float NEAR_CLIP = 0.1f;
|
|---|
| 79 | float FAR_CLIP = 100.0f;
|
|---|
| 80 |
|
|---|
| 81 | void glfw_error_callback(int error, const char* description) {
|
|---|
| 82 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
|---|
| 86 | double mouse_x, mouse_y;
|
|---|
| 87 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
|---|
| 88 |
|
|---|
| 89 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
|---|
| 90 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
|---|
| 91 | selectedObject = NULL;
|
|---|
| 92 |
|
|---|
| 93 | float x = (2.0f*mouse_x) / width - 1.0f;
|
|---|
| 94 | float y = 1.0f - (2.0f*mouse_y) / height;
|
|---|
| 95 |
|
|---|
| 96 | cout << "x: " << x << ", y: " << y << endl;
|
|---|
| 97 |
|
|---|
| 98 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
|---|
| 99 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
|---|
| 100 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
|---|
| 101 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
|---|
| 102 |
|
|---|
| 103 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
|---|
| 104 |
|
|---|
| 105 | vec4 click_point;
|
|---|
| 106 | 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
|
|---|
| 107 | int closest_face_id = -1;
|
|---|
| 108 |
|
|---|
| 109 | for (int i = 0; i<faces.size(); i++) {
|
|---|
| 110 | if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
|
|---|
| 111 | click_point = view_mat * click_point;
|
|---|
| 112 |
|
|---|
| 113 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
|---|
| 114 | closest_point = click_point.xyz();
|
|---|
| 115 | closest_face_id = i;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | if (closest_face_id == -1) {
|
|---|
| 121 | cout << "No object was clicked" << endl;
|
|---|
| 122 | } else {
|
|---|
| 123 | clickedObject = &objects[faces[closest_face_id].object_id];
|
|---|
| 124 | cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | int main(int argc, char* argv[]) {
|
|---|
| 130 | cout << "New OpenGL Game" << endl;
|
|---|
| 131 |
|
|---|
| 132 | if (!restart_gl_log()) {}
|
|---|
| 133 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
|---|
| 134 |
|
|---|
| 135 | glfwSetErrorCallback(glfw_error_callback);
|
|---|
| 136 | if (!glfwInit()) {
|
|---|
| 137 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
|---|
| 138 | return 1;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | #ifdef __APPLE__
|
|---|
| 142 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|---|
| 143 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|---|
| 144 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|---|
| 145 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|---|
| 146 | #endif
|
|---|
| 147 |
|
|---|
| 148 | glfwWindowHint(GLFW_SAMPLES, 4);
|
|---|
| 149 |
|
|---|
| 150 | GLFWwindow* window = NULL;
|
|---|
| 151 | GLFWmonitor* mon = NULL;
|
|---|
| 152 |
|
|---|
| 153 | if (FULLSCREEN) {
|
|---|
| 154 | mon = glfwGetPrimaryMonitor();
|
|---|
| 155 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
|---|
| 156 |
|
|---|
| 157 | width = vmode->width;
|
|---|
| 158 | height = vmode->height;
|
|---|
| 159 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
|---|
| 160 | }
|
|---|
| 161 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
|---|
| 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 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
|---|
| 176 | const GLubyte* version = glGetString(GL_VERSION);
|
|---|
| 177 | printf("Renderer: %s\n", renderer);
|
|---|
| 178 | printf("OpenGL version supported %s\n", version);
|
|---|
| 179 |
|
|---|
| 180 | glEnable(GL_DEPTH_TEST);
|
|---|
| 181 | glDepthFunc(GL_LESS);
|
|---|
| 182 |
|
|---|
| 183 | glEnable(GL_CULL_FACE);
|
|---|
| 184 | // glCullFace(GL_BACK);
|
|---|
| 185 | // glFrontFace(GL_CW);
|
|---|
| 186 |
|
|---|
| 187 | int x, y;
|
|---|
| 188 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
|---|
| 189 | if (texImage) {
|
|---|
| 190 | cout << "Yay, I loaded an image!" << endl;
|
|---|
| 191 | cout << x << endl;
|
|---|
| 192 | cout << y << endl;
|
|---|
| 193 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | GLuint tex = 0;
|
|---|
| 197 | glGenTextures(1, &tex);
|
|---|
| 198 | glActiveTexture(GL_TEXTURE0);
|
|---|
| 199 | glBindTexture(GL_TEXTURE_2D, tex);
|
|---|
| 200 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
|---|
| 201 |
|
|---|
| 202 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|---|
| 203 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|---|
| 204 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|---|
| 205 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|---|
| 206 |
|
|---|
| 207 | GLfloat points[] = {
|
|---|
| 208 | 0.0f, 0.5f, 0.0f,
|
|---|
| 209 | -0.5f, -0.5f, 0.0f,
|
|---|
| 210 | 0.5f, -0.5f, 0.0f,
|
|---|
| 211 | 0.5f, -0.5f, 0.0f,
|
|---|
| 212 | -0.5f, -0.5f, 0.0f,
|
|---|
| 213 | 0.0f, 0.5f, 0.0f,
|
|---|
| 214 | };
|
|---|
| 215 |
|
|---|
| 216 | GLfloat colors[] = {
|
|---|
| 217 | 1.0, 0.0, 0.0,
|
|---|
| 218 | 0.0, 0.0, 1.0,
|
|---|
| 219 | 0.0, 1.0, 0.0,
|
|---|
| 220 | 0.0, 1.0, 0.0,
|
|---|
| 221 | 0.0, 0.0, 1.0,
|
|---|
| 222 | 1.0, 0.0, 0.0,
|
|---|
| 223 | };
|
|---|
| 224 |
|
|---|
| 225 | GLfloat colors_new[] = {
|
|---|
| 226 | 0.0, 1.0, 0.0,
|
|---|
| 227 | 0.0, 1.0, 0.0,
|
|---|
| 228 | 0.0, 1.0, 0.0,
|
|---|
| 229 | 0.0, 1.0, 0.0,
|
|---|
| 230 | 0.0, 1.0, 0.0,
|
|---|
| 231 | 0.0, 1.0, 0.0,
|
|---|
| 232 | };
|
|---|
| 233 |
|
|---|
| 234 | // Each point is made of 3 floats
|
|---|
| 235 | int numPoints = (sizeof(points) / sizeof(float)) / 3;
|
|---|
| 236 |
|
|---|
| 237 | GLfloat points2[] = {
|
|---|
| 238 | 0.5f, 0.5f, 0.0f,
|
|---|
| 239 | -0.5f, 0.5f, 0.0f,
|
|---|
| 240 | -0.5f, -0.5f, 0.0f,
|
|---|
| 241 | 0.5f, 0.5f, 0.0f,
|
|---|
| 242 | -0.5f, -0.5f, 0.0f,
|
|---|
| 243 | 0.5f, -0.5f, 0.0f,
|
|---|
| 244 | };
|
|---|
| 245 |
|
|---|
| 246 | GLfloat colors2[] = {
|
|---|
| 247 | 0.0, 0.9, 0.9,
|
|---|
| 248 | 0.0, 0.9, 0.9,
|
|---|
| 249 | 0.0, 0.9, 0.9,
|
|---|
| 250 | 0.0, 0.9, 0.9,
|
|---|
| 251 | 0.0, 0.9, 0.9,
|
|---|
| 252 | 0.0, 0.9, 0.9,
|
|---|
| 253 | };
|
|---|
| 254 |
|
|---|
| 255 | GLfloat texcoords[] = {
|
|---|
| 256 | 1.0f, 1.0f,
|
|---|
| 257 | 0.0f, 1.0f,
|
|---|
| 258 | 0.0, 0.0,
|
|---|
| 259 | 1.0, 1.0,
|
|---|
| 260 | 0.0, 0.0,
|
|---|
| 261 | 1.0, 0.0
|
|---|
| 262 | };
|
|---|
| 263 |
|
|---|
| 264 | // Each point is made of 3 floats
|
|---|
| 265 | int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
|
|---|
| 266 |
|
|---|
| 267 | mat4 T_model, R_model;
|
|---|
| 268 |
|
|---|
| 269 | // triangle
|
|---|
| 270 | objects.push_back(SceneObject());
|
|---|
| 271 |
|
|---|
| 272 | T_model = translate(mat4(), vec3(0.25f, 0.0f, 0.0f));
|
|---|
| 273 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 274 | objects[0].model_mat = T_model*R_model;
|
|---|
| 275 |
|
|---|
| 276 | faces.push_back(ObjectFace());
|
|---|
| 277 | faces[0].object_id = 0;
|
|---|
| 278 | faces[0].points = {
|
|---|
| 279 | vec3(points[0], points[1], points[2]),
|
|---|
| 280 | vec3(points[3], points[4], points[5]),
|
|---|
| 281 | vec3(points[6], points[7], points[8]),
|
|---|
| 282 | };
|
|---|
| 283 |
|
|---|
| 284 | // square
|
|---|
| 285 | objects.push_back(SceneObject());
|
|---|
| 286 |
|
|---|
| 287 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
|---|
| 288 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 289 | objects[1].model_mat = T_model*R_model;
|
|---|
| 290 |
|
|---|
| 291 | faces.push_back(ObjectFace());
|
|---|
| 292 | faces[1].object_id = 1;
|
|---|
| 293 | faces[1].points = {
|
|---|
| 294 | vec3(points2[0], points2[1], points2[2]),
|
|---|
| 295 | vec3(points2[3], points2[4], points2[5]),
|
|---|
| 296 | vec3(points2[6], points2[7], points2[8]),
|
|---|
| 297 | };
|
|---|
| 298 |
|
|---|
| 299 | faces.push_back(ObjectFace());
|
|---|
| 300 | faces[2].object_id = 1;
|
|---|
| 301 | faces[2].points = {
|
|---|
| 302 | vec3(points2[9], points2[10], points2[11]),
|
|---|
| 303 | vec3(points2[12], points2[13], points2[14]),
|
|---|
| 304 | vec3(points2[15], points2[16], points2[17]),
|
|---|
| 305 | };
|
|---|
| 306 |
|
|---|
| 307 | GLuint points_vbo = 0;
|
|---|
| 308 | glGenBuffers(1, &points_vbo);
|
|---|
| 309 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
|---|
| 310 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
|---|
| 311 |
|
|---|
| 312 | GLuint colors_vbo = 0;
|
|---|
| 313 | glGenBuffers(1, &colors_vbo);
|
|---|
| 314 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
|---|
| 315 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
|---|
| 316 |
|
|---|
| 317 | GLuint vao = 0;
|
|---|
| 318 | glGenVertexArrays(1, &vao);
|
|---|
| 319 | glBindVertexArray(vao);
|
|---|
| 320 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
|---|
| 321 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 322 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
|---|
| 323 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 324 |
|
|---|
| 325 | glEnableVertexAttribArray(0);
|
|---|
| 326 | glEnableVertexAttribArray(1);
|
|---|
| 327 |
|
|---|
| 328 | GLuint points2_vbo = 0;
|
|---|
| 329 | glGenBuffers(1, &points2_vbo);
|
|---|
| 330 | glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
|
|---|
| 331 | glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
|
|---|
| 332 |
|
|---|
| 333 | GLuint colors2_vbo = 0;
|
|---|
| 334 | glGenBuffers(1, &colors2_vbo);
|
|---|
| 335 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
|---|
| 336 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
|
|---|
| 337 |
|
|---|
| 338 | GLuint vt_vbo;
|
|---|
| 339 | glGenBuffers(1, &vt_vbo);
|
|---|
| 340 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
|---|
| 341 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
|
|---|
| 342 |
|
|---|
| 343 | GLuint vao2 = 0;
|
|---|
| 344 | glGenVertexArrays(1, &vao2);
|
|---|
| 345 | glBindVertexArray(vao2);
|
|---|
| 346 | glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
|
|---|
| 347 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 348 | // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
|---|
| 349 | // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 350 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
|---|
| 351 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 352 |
|
|---|
| 353 | glEnableVertexAttribArray(0);
|
|---|
| 354 | glEnableVertexAttribArray(1);
|
|---|
| 355 |
|
|---|
| 356 | GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
|
|---|
| 357 | GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
|
|---|
| 358 |
|
|---|
| 359 | float speed = 1.0f;
|
|---|
| 360 | float last_position = 0.0f;
|
|---|
| 361 |
|
|---|
| 362 | float cam_speed = 1.0f;
|
|---|
| 363 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
|---|
| 364 |
|
|---|
| 365 | // glm::lookAt can create the view matrix
|
|---|
| 366 | // glm::perspective can create the projection matrix
|
|---|
| 367 |
|
|---|
| 368 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
|---|
| 369 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
|---|
| 370 |
|
|---|
| 371 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
|---|
| 372 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 373 | view_mat = R*T;
|
|---|
| 374 |
|
|---|
| 375 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
|---|
| 376 | float aspect = (float)width / (float)height;
|
|---|
| 377 |
|
|---|
| 378 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
|---|
| 379 | float Sx = NEAR_CLIP / (range * aspect);
|
|---|
| 380 | float Sy = NEAR_CLIP / range;
|
|---|
| 381 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
|---|
| 382 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
|---|
| 383 |
|
|---|
| 384 | float proj_arr[] = {
|
|---|
| 385 | Sx, 0.0f, 0.0f, 0.0f,
|
|---|
| 386 | 0.0f, Sy, 0.0f, 0.0f,
|
|---|
| 387 | 0.0f, 0.0f, Sz, -1.0f,
|
|---|
| 388 | 0.0f, 0.0f, Pz, 0.0f,
|
|---|
| 389 | };
|
|---|
| 390 | proj_mat = make_mat4(proj_arr);
|
|---|
| 391 |
|
|---|
| 392 | GLint model_test_loc = glGetUniformLocation(shader_program, "model");
|
|---|
| 393 | GLint view_test_loc = glGetUniformLocation(shader_program, "view");
|
|---|
| 394 | GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
|
|---|
| 395 |
|
|---|
| 396 | GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
|
|---|
| 397 | GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
|
|---|
| 398 | GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
|
|---|
| 399 |
|
|---|
| 400 | glUseProgram(shader_program);
|
|---|
| 401 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
|---|
| 402 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 403 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
|---|
| 404 |
|
|---|
| 405 | glUseProgram(shader_program2);
|
|---|
| 406 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
|---|
| 407 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 408 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
|---|
| 409 |
|
|---|
| 410 | bool cam_moved = false;
|
|---|
| 411 |
|
|---|
| 412 | int frame_count = 0;
|
|---|
| 413 | double elapsed_seconds_fps = 0.0f, previous_seconds_fps;
|
|---|
| 414 |
|
|---|
| 415 | double previous_seconds = glfwGetTime();
|
|---|
| 416 |
|
|---|
| 417 | while (!glfwWindowShouldClose(window)) {
|
|---|
| 418 | double current_seconds = glfwGetTime();
|
|---|
| 419 | double elapsed_seconds = current_seconds - previous_seconds;
|
|---|
| 420 | previous_seconds = current_seconds;
|
|---|
| 421 |
|
|---|
| 422 | elapsed_seconds_fps += elapsed_seconds;
|
|---|
| 423 | if (elapsed_seconds_fps > 0.25f) {
|
|---|
| 424 | fps = (double)frame_count / elapsed_seconds_fps;
|
|---|
| 425 | cout << "FPS: " << fps << endl;
|
|---|
| 426 |
|
|---|
| 427 | frame_count = 0;
|
|---|
| 428 | elapsed_seconds_fps = 0.0f;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | frame_count++;
|
|---|
| 432 |
|
|---|
| 433 | if (fabs(last_position) > 1.0f) {
|
|---|
| 434 | speed = -speed;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | if (clickedObject == &objects[0]) {
|
|---|
| 438 | selectedObject = &objects[0];
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | // At some point, I should change this to only rebind the buffer once per click, not once per frame
|
|---|
| 442 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
|---|
| 443 | if (selectedObject == &objects[0]) {
|
|---|
| 444 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
|
|---|
| 445 | }
|
|---|
| 446 | else {
|
|---|
| 447 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | /*
|
|---|
| 451 | model[12] = last_position + speed*elapsed_seconds;
|
|---|
| 452 | last_position = model[12];
|
|---|
| 453 | */
|
|---|
| 454 |
|
|---|
| 455 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|---|
| 456 |
|
|---|
| 457 | glUseProgram(shader_program);
|
|---|
| 458 |
|
|---|
| 459 | // Since every object will have a different model matrix, maybe it shouldn't be a uniform
|
|---|
| 460 |
|
|---|
| 461 | // this is temporary.
|
|---|
| 462 | // It's needed to offset the code for the recoloring of the square working during click detection
|
|---|
| 463 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
|---|
| 464 |
|
|---|
| 465 | glBindVertexArray(vao);
|
|---|
| 466 |
|
|---|
| 467 | glDrawArrays(GL_TRIANGLES, 0, numPoints);
|
|---|
| 468 |
|
|---|
| 469 | if (clickedObject == &objects[1]) {
|
|---|
| 470 | selectedObject = &objects[1];
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | if (selectedObject == &objects[1]) {
|
|---|
| 474 | glUseProgram(shader_program);
|
|---|
| 475 |
|
|---|
| 476 | // this is temporary.
|
|---|
| 477 | // It's needed to get the recoloring of the square working during click detection
|
|---|
| 478 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
|---|
| 479 |
|
|---|
| 480 | glBindVertexArray(vao2);
|
|---|
| 481 |
|
|---|
| 482 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
|---|
| 483 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 484 | } else {
|
|---|
| 485 | glUseProgram(shader_program2);
|
|---|
| 486 |
|
|---|
| 487 | glBindVertexArray(vao2);
|
|---|
| 488 |
|
|---|
| 489 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
|---|
| 490 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | glDrawArrays(GL_TRIANGLES, 0, numPoints2);
|
|---|
| 494 |
|
|---|
| 495 | clickedObject = NULL;
|
|---|
| 496 |
|
|---|
| 497 | glfwPollEvents();
|
|---|
| 498 | glfwSwapBuffers(window);
|
|---|
| 499 |
|
|---|
| 500 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
|---|
| 501 | glfwSetWindowShouldClose(window, 1);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | float dist = cam_speed * elapsed_seconds;
|
|---|
| 505 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
|---|
| 506 | cam_pos.x -= cos(cam_yaw)*dist;
|
|---|
| 507 | cam_pos.z += sin(cam_yaw)*dist;
|
|---|
| 508 | cam_moved = true;
|
|---|
| 509 | }
|
|---|
| 510 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
|---|
| 511 | cam_pos.x += cos(cam_yaw)*dist;
|
|---|
| 512 | cam_pos.z -= sin(cam_yaw)*dist;
|
|---|
| 513 | cam_moved = true;
|
|---|
| 514 | }
|
|---|
| 515 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
|---|
| 516 | cam_pos.x -= sin(cam_yaw)*dist;
|
|---|
| 517 | cam_pos.z -= cos(cam_yaw)*dist;
|
|---|
| 518 | cam_moved = true;
|
|---|
| 519 | }
|
|---|
| 520 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
|---|
| 521 | cam_pos.x += sin(cam_yaw)*dist;
|
|---|
| 522 | cam_pos.z += cos(cam_yaw)*dist;
|
|---|
| 523 | cam_moved = true;
|
|---|
| 524 | }
|
|---|
| 525 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
|---|
| 526 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
|---|
| 527 | cam_moved = true;
|
|---|
| 528 | }
|
|---|
| 529 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
|---|
| 530 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
|---|
| 531 | cam_moved = true;
|
|---|
| 532 | }
|
|---|
| 533 | if (cam_moved) {
|
|---|
| 534 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
|---|
| 535 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
|---|
| 536 | view_mat = R*T;
|
|---|
| 537 |
|
|---|
| 538 | glUseProgram(shader_program);
|
|---|
| 539 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 540 |
|
|---|
| 541 | glUseProgram(shader_program2);
|
|---|
| 542 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
|---|
| 543 |
|
|---|
| 544 | cam_moved = false;
|
|---|
| 545 | }
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | glfwTerminate();
|
|---|
| 549 | return 0;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | GLuint loadShader(GLenum type, string file) {
|
|---|
| 553 | cout << "Loading shader from file " << file << endl;
|
|---|
| 554 |
|
|---|
| 555 | ifstream shaderFile(file);
|
|---|
| 556 | GLuint shaderId = 0;
|
|---|
| 557 |
|
|---|
| 558 | if (shaderFile.is_open()) {
|
|---|
| 559 | string line, shaderString;
|
|---|
| 560 |
|
|---|
| 561 | while(getline(shaderFile, line)) {
|
|---|
| 562 | shaderString += line + "\n";
|
|---|
| 563 | }
|
|---|
| 564 | shaderFile.close();
|
|---|
| 565 | const char* shaderCString = shaderString.c_str();
|
|---|
| 566 |
|
|---|
| 567 | shaderId = glCreateShader(type);
|
|---|
| 568 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
|---|
| 569 | glCompileShader(shaderId);
|
|---|
| 570 |
|
|---|
| 571 | cout << "Loaded successfully" << endl;
|
|---|
| 572 | } else {
|
|---|
| 573 | cout << "Failed to load the file" << endl;
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | return shaderId;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
|---|
| 580 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
|---|
| 581 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
|---|
| 582 |
|
|---|
| 583 | GLuint shader_program = glCreateProgram();
|
|---|
| 584 | glAttachShader(shader_program, vs);
|
|---|
| 585 | glAttachShader(shader_program, fs);
|
|---|
| 586 |
|
|---|
| 587 | glLinkProgram(shader_program);
|
|---|
| 588 |
|
|---|
| 589 | return shader_program;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
|---|
| 593 | int n;
|
|---|
| 594 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
|---|
| 595 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
|---|
| 596 |
|
|---|
| 597 | int width_in_bytes = *x * 4;
|
|---|
| 598 | unsigned char *top = NULL;
|
|---|
| 599 | unsigned char *bottom = NULL;
|
|---|
| 600 | unsigned char temp = 0;
|
|---|
| 601 | int half_height = *y / 2;
|
|---|
| 602 |
|
|---|
| 603 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
|---|
| 604 | for (int row = 0; row < half_height; row++) {
|
|---|
| 605 | top = image_data + row * width_in_bytes;
|
|---|
| 606 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
|---|
| 607 | for (int col = 0; col < width_in_bytes; col++) {
|
|---|
| 608 | temp = *top;
|
|---|
| 609 | *top = *bottom;
|
|---|
| 610 | *bottom = temp;
|
|---|
| 611 | top++;
|
|---|
| 612 | bottom++;
|
|---|
| 613 | }
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (!image_data) {
|
|---|
| 617 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | // Not Power-of-2 check
|
|---|
| 621 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
|---|
| 622 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | return image_data;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
|
|---|
| 629 | // LINE EQUATION: P = O + Dt
|
|---|
| 630 | // O = cam
|
|---|
| 631 | // D = ray_world
|
|---|
| 632 |
|
|---|
| 633 | // PLANE EQUATION: P dot n + d = 0
|
|---|
| 634 | // n is the normal vector
|
|---|
| 635 | // d is the offset from the origin
|
|---|
| 636 |
|
|---|
| 637 | // Take the cross-product of two vectors on the plane to get the normal
|
|---|
| 638 | vec3 v1 = face->points[1] - face->points[0];
|
|---|
| 639 | vec3 v2 = face->points[2] - face->points[0];
|
|---|
| 640 |
|
|---|
| 641 | 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);
|
|---|
| 642 |
|
|---|
| 643 | print4DVector("Full world ray", world_ray);
|
|---|
| 644 |
|
|---|
| 645 | SceneObject* obj = &objects[face->object_id];
|
|---|
| 646 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
|---|
| 647 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
|---|
| 648 |
|
|---|
| 649 | local_ray = local_ray - local_cam;
|
|---|
| 650 |
|
|---|
| 651 | float d = -glm::dot(face->points[0], normal);
|
|---|
| 652 | cout << "d: " << d << endl;
|
|---|
| 653 |
|
|---|
| 654 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
|---|
| 655 | cout << "t: " << t << endl;
|
|---|
| 656 |
|
|---|
| 657 | vec3 intersection = local_cam + t*local_ray;
|
|---|
| 658 | printVector("Intersection", intersection);
|
|---|
| 659 |
|
|---|
| 660 | if (insideTriangle(intersection, face->points)) {
|
|---|
| 661 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
|---|
| 662 | return true;
|
|---|
| 663 | } else {
|
|---|
| 664 | return false;
|
|---|
| 665 | }
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
|---|
| 669 | vec3 v21 = triangle_points[1]- triangle_points[0];
|
|---|
| 670 | vec3 v31 = triangle_points[2]- triangle_points[0];
|
|---|
| 671 | vec3 pv1 = p- triangle_points[0];
|
|---|
| 672 |
|
|---|
| 673 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
|---|
| 674 | float x = (pv1.x-y*v31.x) / v21.x;
|
|---|
| 675 |
|
|---|
| 676 | cout << "(" << x << ", " << y << ")" << endl;
|
|---|
| 677 |
|
|---|
| 678 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | void printVector(string label, vec3 v) {
|
|---|
| 682 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | void print4DVector(string label, vec4 v) {
|
|---|
| 686 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
|---|
| 687 | }
|
|---|