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