[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;
|
---|
[baa5848] | 44 | GLuint shader_program;
|
---|
[05e43cf] | 45 | unsigned int num_points;
|
---|
[07ed460] | 46 | GLvoid* vertex_vbo_offset;
|
---|
| 47 | GLvoid* texture_vbo_offset;
|
---|
| 48 | vector<GLfloat> points;
|
---|
| 49 | vector<GLfloat> colors;
|
---|
| 50 | vector<GLfloat> texcoords;
|
---|
| 51 | vector<GLfloat> selected_colors;
|
---|
[df652d5] | 52 | };
|
---|
| 53 |
|
---|
| 54 | struct ObjectFace {
|
---|
[e82692b] | 55 | unsigned int object_id;
|
---|
[df652d5] | 56 | array<vec3, 3> points;
|
---|
| 57 | };
|
---|
| 58 |
|
---|
[485424b] | 59 | const bool FULLSCREEN = false;
|
---|
[c62eee6] | 60 | int width = 640;
|
---|
| 61 | int height = 480;
|
---|
| 62 |
|
---|
| 63 | vec3 cam_pos;
|
---|
| 64 |
|
---|
| 65 | mat4 view_mat;
|
---|
| 66 | mat4 proj_mat;
|
---|
[5272b6b] | 67 |
|
---|
[df652d5] | 68 | vector<SceneObject> objects;
|
---|
| 69 | vector<ObjectFace> faces;
|
---|
| 70 |
|
---|
[147ac6d] | 71 | SceneObject* clickedObject = NULL;
|
---|
[baa5848] | 72 | SceneObject* selectedObject;
|
---|
[147ac6d] | 73 |
|
---|
[046ce72] | 74 | double fps;
|
---|
| 75 |
|
---|
[e82692b] | 76 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
[5c9d193] | 77 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
[33a9664] | 78 |
|
---|
[ec4456b] | 79 | GLuint loadShader(GLenum type, string file);
|
---|
[485424b] | 80 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
| 81 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
[ec4456b] | 82 |
|
---|
[d12d003] | 83 | void printVector(string label, vec3 v);
|
---|
[b73cb3b] | 84 | void print4DVector(string label, vec4 v);
|
---|
[d12d003] | 85 |
|
---|
| 86 | float NEAR_CLIP = 0.1f;
|
---|
| 87 | float FAR_CLIP = 100.0f;
|
---|
| 88 |
|
---|
[ec4456b] | 89 | void glfw_error_callback(int error, const char* description) {
|
---|
| 90 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[c62eee6] | 93 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
[33a9664] | 94 | double mouse_x, mouse_y;
|
---|
| 95 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 96 |
|
---|
| 97 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 98 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
[147ac6d] | 99 | selectedObject = NULL;
|
---|
[33a9664] | 100 |
|
---|
| 101 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
| 102 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
[d12d003] | 103 |
|
---|
[33a9664] | 104 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 105 |
|
---|
[b73cb3b] | 106 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
| 107 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
| 108 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
---|
[5c9d193] | 109 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
[33a9664] | 110 |
|
---|
[b73cb3b] | 111 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
[33a9664] | 112 |
|
---|
[e82692b] | 113 | vec4 click_point;
|
---|
[b73cb3b] | 114 | 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] | 115 | int closest_face_id = -1;
|
---|
| 116 |
|
---|
[5c9d193] | 117 | for (int i = 0; i<faces.size(); i++) {
|
---|
[e82692b] | 118 | if (faceClicked(&faces[i], ray_world, cam_pos_temp, click_point)) {
|
---|
| 119 | click_point = view_mat * click_point;
|
---|
| 120 |
|
---|
[b73cb3b] | 121 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
[e82692b] | 122 | closest_point = click_point.xyz();
|
---|
| 123 | closest_face_id = i;
|
---|
| 124 | }
|
---|
[5c9d193] | 125 | }
|
---|
| 126 | }
|
---|
[d12d003] | 127 |
|
---|
[e82692b] | 128 | if (closest_face_id == -1) {
|
---|
[5c9d193] | 129 | cout << "No object was clicked" << endl;
|
---|
[e82692b] | 130 | } else {
|
---|
| 131 | clickedObject = &objects[faces[closest_face_id].object_id];
|
---|
| 132 | cout << "Clicked object: " << faces[closest_face_id].object_id << endl;
|
---|
[147ac6d] | 133 | }
|
---|
[c62eee6] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[5272b6b] | 137 | int main(int argc, char* argv[]) {
|
---|
| 138 | cout << "New OpenGL Game" << endl;
|
---|
| 139 |
|
---|
[ec4456b] | 140 | if (!restart_gl_log()) {}
|
---|
| 141 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 142 |
|
---|
[ec4456b] | 143 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 144 | if (!glfwInit()) {
|
---|
| 145 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 146 | return 1;
|
---|
[be246ad] | 147 | }
|
---|
| 148 |
|
---|
| 149 | #ifdef __APPLE__
|
---|
| 150 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 151 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 152 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 153 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 154 | #endif
|
---|
[5272b6b] | 155 |
|
---|
[ec4456b] | 156 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 157 |
|
---|
| 158 | GLFWwindow* window = NULL;
|
---|
[e856d62] | 159 | GLFWmonitor* mon = NULL;
|
---|
[ec4456b] | 160 |
|
---|
| 161 | if (FULLSCREEN) {
|
---|
[e856d62] | 162 | mon = glfwGetPrimaryMonitor();
|
---|
[ec4456b] | 163 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 164 |
|
---|
| 165 | width = vmode->width;
|
---|
| 166 | height = vmode->height;
|
---|
[e856d62] | 167 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
[ec4456b] | 168 | }
|
---|
[e856d62] | 169 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
[ec4456b] | 170 |
|
---|
[5272b6b] | 171 | if (!window) {
|
---|
| 172 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 173 | glfwTerminate();
|
---|
| 174 | return 1;
|
---|
| 175 | }
|
---|
[c62eee6] | 176 |
|
---|
[b73cb3b] | 177 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
[c62eee6] | 178 |
|
---|
[644a2e4] | 179 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 180 | glewExperimental = GL_TRUE;
|
---|
| 181 | glewInit();
|
---|
| 182 |
|
---|
| 183 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 184 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 185 | printf("Renderer: %s\n", renderer);
|
---|
| 186 | printf("OpenGL version supported %s\n", version);
|
---|
[93baa0e] | 187 |
|
---|
[5272b6b] | 188 | glEnable(GL_DEPTH_TEST);
|
---|
| 189 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 190 |
|
---|
[93baa0e] | 191 | glEnable(GL_CULL_FACE);
|
---|
| 192 | // glCullFace(GL_BACK);
|
---|
| 193 | // glFrontFace(GL_CW);
|
---|
| 194 |
|
---|
[485424b] | 195 | int x, y;
|
---|
| 196 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
| 197 | if (texImage) {
|
---|
| 198 | cout << "Yay, I loaded an image!" << endl;
|
---|
| 199 | cout << x << endl;
|
---|
| 200 | cout << y << endl;
|
---|
[e856d62] | 201 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
[485424b] | 202 | }
|
---|
| 203 |
|
---|
| 204 | GLuint tex = 0;
|
---|
| 205 | glGenTextures(1, &tex);
|
---|
| 206 | glActiveTexture(GL_TEXTURE0);
|
---|
| 207 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
| 208 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 209 |
|
---|
| 210 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 211 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 212 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 213 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 214 |
|
---|
[07ed460] | 215 | mat4 T_model, R_model;
|
---|
| 216 |
|
---|
| 217 | // triangle
|
---|
| 218 | objects.push_back(SceneObject());
|
---|
| 219 | objects[0].shader_program = 0;
|
---|
| 220 | objects[0].vertex_vbo_offset = (GLvoid*) (0 * sizeof(float) * 3);
|
---|
| 221 | objects[0].texture_vbo_offset = (GLvoid*)(0 * sizeof(float) * 2);
|
---|
| 222 | objects[0].points = {
|
---|
[d12d003] | 223 | 0.0f, 0.5f, 0.0f,
|
---|
| 224 | -0.5f, -0.5f, 0.0f,
|
---|
| 225 | 0.5f, -0.5f, 0.0f,
|
---|
| 226 | 0.5f, -0.5f, 0.0f,
|
---|
| 227 | -0.5f, -0.5f, 0.0f,
|
---|
| 228 | 0.0f, 0.5f, 0.0f,
|
---|
[516668e] | 229 | };
|
---|
[07ed460] | 230 | objects[0].colors = {
|
---|
| 231 | 1.0f, 0.0f, 0.0f,
|
---|
| 232 | 0.0f, 0.0f, 1.0f,
|
---|
| 233 | 0.0f, 1.0f, 0.0f,
|
---|
| 234 | 0.0f, 1.0f, 0.0f,
|
---|
| 235 | 0.0f, 0.0f, 1.0f,
|
---|
| 236 | 1.0f, 0.0f, 0.0f,
|
---|
[93baa0e] | 237 | };
|
---|
[07ed460] | 238 | objects[0].texcoords = {
|
---|
| 239 | 1.0f, 1.0f,
|
---|
| 240 | 0.0f, 1.0f,
|
---|
| 241 | 0.0f, 0.0f,
|
---|
| 242 | 1.0f, 1.0f,
|
---|
| 243 | 0.0f, 0.0f,
|
---|
| 244 | 1.0f, 0.0f
|
---|
[33a9664] | 245 | };
|
---|
[07ed460] | 246 | objects[0].selected_colors = {
|
---|
| 247 | 0.0f, 1.0f, 0.0f,
|
---|
| 248 | 0.0f, 1.0f, 0.0f,
|
---|
| 249 | 0.0f, 1.0f, 0.0f,
|
---|
| 250 | 0.0f, 1.0f, 0.0f,
|
---|
| 251 | 0.0f, 1.0f, 0.0f,
|
---|
| 252 | 0.0f, 1.0f, 0.0f,
|
---|
| 253 | };
|
---|
| 254 | objects[0].num_points = objects[0].points.size() / 3;
|
---|
| 255 |
|
---|
| 256 | T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
|
---|
| 257 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 258 | objects[0].model_mat = T_model*R_model;
|
---|
[33a9664] | 259 |
|
---|
[07ed460] | 260 | // square
|
---|
| 261 | objects.push_back(SceneObject());
|
---|
| 262 | objects[1].shader_program = 0;
|
---|
| 263 | objects[1].vertex_vbo_offset = (GLvoid*) (6 * sizeof(float) * 3);
|
---|
| 264 | objects[1].texture_vbo_offset = (GLvoid*)(6 * sizeof(float) * 2);
|
---|
| 265 | objects[1].points = {
|
---|
[b73cb3b] | 266 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 267 | -0.5f, 0.5f, 0.0f,
|
---|
| 268 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 269 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 270 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 271 | 0.5f, -0.5f, 0.0f,
|
---|
[64a70f4] | 272 | };
|
---|
[07ed460] | 273 | objects[1].colors = {
|
---|
| 274 | 1.0f, 0.0f, 0.0f,
|
---|
| 275 | 0.0f, 0.0f, 1.0f,
|
---|
| 276 | 0.0f, 1.0f, 0.0f,
|
---|
| 277 | 0.0f, 1.0f, 0.0f,
|
---|
| 278 | 0.0f, 0.0f, 1.0f,
|
---|
| 279 | 1.0f, 0.0f, 0.0f,
|
---|
[485424b] | 280 | };
|
---|
[07ed460] | 281 | objects[1].texcoords = {
|
---|
[64a70f4] | 282 | 1.0f, 1.0f,
|
---|
| 283 | 0.0f, 1.0f,
|
---|
[07ed460] | 284 | 0.0f, 0.0f,
|
---|
| 285 | 1.0f, 1.0f,
|
---|
| 286 | 0.0f, 0.0f,
|
---|
| 287 | 1.0f, 0.0f
|
---|
[485424b] | 288 | };
|
---|
[07ed460] | 289 | objects[1].selected_colors = {
|
---|
| 290 | 0.0f, 0.9f, 0.9f,
|
---|
| 291 | 0.0f, 0.9f, 0.9f,
|
---|
| 292 | 0.0f, 0.9f, 0.9f,
|
---|
| 293 | 0.0f, 0.9f, 0.9f,
|
---|
| 294 | 0.0f, 0.9f, 0.9f,
|
---|
| 295 | 0.0f, 0.9f, 0.9f,
|
---|
[19c9338] | 296 | };
|
---|
[07ed460] | 297 | objects[1].num_points = objects[1].points.size() / 3;
|
---|
[df652d5] | 298 |
|
---|
[b73cb3b] | 299 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
---|
| 300 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[df652d5] | 301 | objects[1].model_mat = T_model*R_model;
|
---|
| 302 |
|
---|
[07ed460] | 303 | vector<SceneObject>::iterator obj_it;
|
---|
| 304 | GLsizeiptr offset;
|
---|
[19c9338] | 305 |
|
---|
[07ed460] | 306 | GLsizeiptr points_buffer_size = 0;
|
---|
| 307 | GLsizeiptr textures_buffer_size = 0;
|
---|
| 308 |
|
---|
| 309 | faces.clear();
|
---|
| 310 |
|
---|
| 311 | unsigned int object_id = 0;
|
---|
| 312 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 313 | points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
|
---|
| 314 | textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
| 315 |
|
---|
| 316 | for (int p_idx = 0; p_idx < obj_it->points.size(); p_idx += 9) {
|
---|
| 317 | faces.push_back(ObjectFace());
|
---|
| 318 | faces.back().object_id = object_id;
|
---|
| 319 | faces.back().points = {
|
---|
| 320 | vec3(obj_it->points[p_idx], obj_it->points[p_idx + 1], obj_it->points[p_idx + 2]),
|
---|
| 321 | vec3(obj_it->points[p_idx + 3], obj_it->points[p_idx + 4], obj_it->points[p_idx + 5]),
|
---|
| 322 | vec3(obj_it->points[p_idx + 6], obj_it->points[p_idx + 7], obj_it->points[p_idx + 8]),
|
---|
| 323 | };
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | object_id++;
|
---|
| 327 | }
|
---|
[19c9338] | 328 |
|
---|
[8b7cfcf] | 329 | GLuint points_vbo = 0;
|
---|
| 330 | glGenBuffers(1, &points_vbo);
|
---|
| 331 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[07ed460] | 332 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
[05e43cf] | 333 |
|
---|
[07ed460] | 334 | offset = 0;
|
---|
| 335 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 336 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]);
|
---|
| 337 | offset += obj_it->points.size() * sizeof(GLfloat);
|
---|
| 338 | }
|
---|
[516668e] | 339 |
|
---|
[8b7cfcf] | 340 | GLuint colors_vbo = 0;
|
---|
| 341 | glGenBuffers(1, &colors_vbo);
|
---|
| 342 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[07ed460] | 343 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 344 |
|
---|
| 345 | offset = 0;
|
---|
| 346 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 347 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]);
|
---|
| 348 | offset += obj_it->colors.size() * sizeof(GLfloat);
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | GLuint selected_colors_vbo = 0;
|
---|
| 352 | glGenBuffers(1, &selected_colors_vbo);
|
---|
| 353 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 354 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 355 |
|
---|
| 356 | offset = 0;
|
---|
| 357 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 358 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]);
|
---|
| 359 | offset += obj_it->selected_colors.size() * sizeof(GLfloat);
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | GLuint texcoords_vbo = 0;
|
---|
| 363 | glGenBuffers(1, &texcoords_vbo);
|
---|
| 364 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 365 | glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 366 |
|
---|
| 367 | offset = 0;
|
---|
| 368 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 369 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]);
|
---|
| 370 | offset += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
| 371 | }
|
---|
[8b7cfcf] | 372 |
|
---|
[644a2e4] | 373 | GLuint vao = 0;
|
---|
[516668e] | 374 | glGenVertexArrays(1, &vao);
|
---|
| 375 | glBindVertexArray(vao);
|
---|
| 376 |
|
---|
[8b7cfcf] | 377 | glEnableVertexAttribArray(0);
|
---|
| 378 | glEnableVertexAttribArray(1);
|
---|
[644a2e4] | 379 |
|
---|
[485424b] | 380 | GLuint vao2 = 0;
|
---|
| 381 | glGenVertexArrays(1, &vao2);
|
---|
| 382 | glBindVertexArray(vao2);
|
---|
[644a2e4] | 383 |
|
---|
[485424b] | 384 | glEnableVertexAttribArray(0);
|
---|
| 385 | glEnableVertexAttribArray(1);
|
---|
[8b7cfcf] | 386 |
|
---|
[1a530df] | 387 | // I can create a vbo to store all points for all models,
|
---|
| 388 | // and another vbo to store all colors for all models, but how do I allow alternating between
|
---|
| 389 | // using colors and textures for each model?
|
---|
| 390 | // Do I create a third vbo for texture coordinates and change which vertex attribute array I have bound
|
---|
| 391 | // when I want to draw a textured model?
|
---|
| 392 | // Or do I create one vao with vertices and colors and another with vertices and textures and switch between the two?
|
---|
| 393 | // Since I would have to switch shader programs to toggle between using colors or textures,
|
---|
| 394 | // I think I should use one vao for both cases and have a points vbo, a colors vbo, and a textures vbo
|
---|
| 395 | // One program will use the points and colors, and the other will use the points and texture coords
|
---|
| 396 | // Review how to bind vbos to vertex attributes in the shader.
|
---|
| 397 | //
|
---|
| 398 | // Binding vbos is done using glVertexAttribPointer(...) on a per-vao basis and is not tied to any specific shader.
|
---|
| 399 | // This means, I could create two vaos, one for each shader and have one use points+colors, while the other
|
---|
| 400 | // uses points+texxcoords.
|
---|
| 401 | //
|
---|
| 402 | // At some point, when I have lots of objects, I want to group them by shader when drawing them.
|
---|
| 403 | // I'd probably create some sort of set per shader and have each set contain the ids of all objects currently using that shader
|
---|
| 404 | // Most likely, I'd want to implement each set using a bit field. Makes it constant time for updates and iterating through them
|
---|
| 405 | // should not be much of an issue either.
|
---|
| 406 | // Assuming making lots of draw calls instead of one is not innefficient, I should be fine.
|
---|
| 407 | // I might also want to use one glDrawElements call per shader to draw multiple non-memory-adjacent models
|
---|
| 408 | //
|
---|
| 409 | // DECISION: Use a glDrawElements call per shader since I use a regular array to specify the elements to draw
|
---|
| 410 | // Actually, this will only work once I get UBOs working since each object will have a different model matrix
|
---|
| 411 | // For now, I could implement this with a glDrawElements call per object and update the model uniform for each object
|
---|
| 412 |
|
---|
[485424b] | 413 | GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
|
---|
| 414 | GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
[644a2e4] | 415 |
|
---|
[93baa0e] | 416 | float speed = 1.0f;
|
---|
| 417 | float last_position = 0.0f;
|
---|
| 418 |
|
---|
[7ee66ea] | 419 | float cam_speed = 1.0f;
|
---|
[201e2f8] | 420 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[7ee66ea] | 421 |
|
---|
[b73cb3b] | 422 | // glm::lookAt can create the view matrix
|
---|
| 423 | // glm::perspective can create the projection matrix
|
---|
| 424 |
|
---|
| 425 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
[64a70f4] | 426 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[7ee66ea] | 427 |
|
---|
[c62eee6] | 428 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 429 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[c62eee6] | 430 | view_mat = R*T;
|
---|
[7ee66ea] | 431 |
|
---|
| 432 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 433 | float aspect = (float)width / (float)height;
|
---|
| 434 |
|
---|
[d12d003] | 435 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 436 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 437 | float Sy = NEAR_CLIP / range;
|
---|
| 438 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 439 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[7ee66ea] | 440 |
|
---|
[c62eee6] | 441 | float proj_arr[] = {
|
---|
[7ee66ea] | 442 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 443 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 444 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 445 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 446 | };
|
---|
[c62eee6] | 447 | proj_mat = make_mat4(proj_arr);
|
---|
[7ee66ea] | 448 |
|
---|
[485424b] | 449 | GLint model_test_loc = glGetUniformLocation(shader_program, "model");
|
---|
| 450 | GLint view_test_loc = glGetUniformLocation(shader_program, "view");
|
---|
| 451 | GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
|
---|
[7ee66ea] | 452 |
|
---|
[19c9338] | 453 | GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
|
---|
| 454 | GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
|
---|
| 455 | GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
|
---|
| 456 |
|
---|
[7ee66ea] | 457 | glUseProgram(shader_program);
|
---|
[df652d5] | 458 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
---|
[19c9338] | 459 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 460 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[485424b] | 461 |
|
---|
| 462 | glUseProgram(shader_program2);
|
---|
[df652d5] | 463 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
---|
[19c9338] | 464 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 465 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[7ee66ea] | 466 |
|
---|
[baa5848] | 467 | objects[0].shader_program = shader_program;
|
---|
| 468 | objects[1].shader_program = shader_program2;
|
---|
| 469 |
|
---|
| 470 | vector<int> program1_objects, program2_objects;
|
---|
| 471 | vector<int>::iterator it;
|
---|
| 472 |
|
---|
[7ee66ea] | 473 | bool cam_moved = false;
|
---|
| 474 |
|
---|
[046ce72] | 475 | int frame_count = 0;
|
---|
[f70ab75] | 476 | double elapsed_seconds_fps = 0.0f;
|
---|
[93baa0e] | 477 | double previous_seconds = glfwGetTime();
|
---|
[046ce72] | 478 |
|
---|
[644a2e4] | 479 | while (!glfwWindowShouldClose(window)) {
|
---|
[93baa0e] | 480 | double current_seconds = glfwGetTime();
|
---|
| 481 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 482 | previous_seconds = current_seconds;
|
---|
| 483 |
|
---|
[046ce72] | 484 | elapsed_seconds_fps += elapsed_seconds;
|
---|
| 485 | if (elapsed_seconds_fps > 0.25f) {
|
---|
| 486 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
| 487 | cout << "FPS: " << fps << endl;
|
---|
| 488 |
|
---|
| 489 | frame_count = 0;
|
---|
| 490 | elapsed_seconds_fps = 0.0f;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | frame_count++;
|
---|
| 494 |
|
---|
[93baa0e] | 495 | if (fabs(last_position) > 1.0f) {
|
---|
| 496 | speed = -speed;
|
---|
| 497 | }
|
---|
| 498 |
|
---|
[baa5848] | 499 | program1_objects.clear();
|
---|
| 500 | program2_objects.clear();
|
---|
| 501 |
|
---|
| 502 | // Handle events (Ideally, move all event-handling code
|
---|
| 503 | // before the render code)
|
---|
| 504 |
|
---|
| 505 | clickedObject = NULL;
|
---|
| 506 | glfwPollEvents();
|
---|
| 507 |
|
---|
[147ac6d] | 508 | if (clickedObject == &objects[0]) {
|
---|
| 509 | selectedObject = &objects[0];
|
---|
| 510 | }
|
---|
[baa5848] | 511 | if (clickedObject == &objects[1]) {
|
---|
| 512 | selectedObject = &objects[1];
|
---|
| 513 | }
|
---|
[33a9664] | 514 |
|
---|
[baa5848] | 515 | if (selectedObject == &objects[1] &&
|
---|
| 516 | objects[1].shader_program == shader_program2) {
|
---|
| 517 | objects[1].shader_program = shader_program;
|
---|
| 518 | } else if (selectedObject != &objects[1] &&
|
---|
| 519 | objects[1].shader_program == shader_program) {
|
---|
| 520 | objects[1].shader_program = shader_program2;
|
---|
[147ac6d] | 521 | }
|
---|
[baa5848] | 522 |
|
---|
| 523 | // group scene objects by shader
|
---|
| 524 | for (int i=0; i < objects.size(); i++) {
|
---|
| 525 | if (objects[i].shader_program == shader_program) {
|
---|
| 526 | program1_objects.push_back(i);
|
---|
| 527 | } else if (objects[i].shader_program == shader_program2) {
|
---|
| 528 | program2_objects.push_back(i);
|
---|
| 529 | }
|
---|
[64a70f4] | 530 | }
|
---|
[33a9664] | 531 |
|
---|
[7ee66ea] | 532 | /*
|
---|
[93baa0e] | 533 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 534 | last_position = model[12];
|
---|
[7ee66ea] | 535 | */
|
---|
[93baa0e] | 536 |
|
---|
| 537 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[485424b] | 538 |
|
---|
| 539 | glUseProgram(shader_program);
|
---|
[644a2e4] | 540 | glBindVertexArray(vao);
|
---|
[93baa0e] | 541 |
|
---|
[baa5848] | 542 | for (it=program1_objects.begin(); it != program1_objects.end(); it++) {
|
---|
| 543 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
[ec4456b] | 544 |
|
---|
[05e43cf] | 545 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[07ed460] | 546 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
|
---|
[05e43cf] | 547 |
|
---|
[baa5848] | 548 | if (selectedObject == &objects[*it]) {
|
---|
[07ed460] | 549 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
[baa5848] | 550 | } else {
|
---|
| 551 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 552 | }
|
---|
[07ed460] | 553 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
|
---|
[05e43cf] | 554 |
|
---|
| 555 | glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
|
---|
[64a70f4] | 556 | }
|
---|
[485424b] | 557 |
|
---|
[baa5848] | 558 | glUseProgram(shader_program2);
|
---|
| 559 | glBindVertexArray(vao2);
|
---|
[485424b] | 560 |
|
---|
[baa5848] | 561 | for (it = program2_objects.begin(); it != program2_objects.end(); it++) {
|
---|
| 562 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
| 563 |
|
---|
[05e43cf] | 564 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[07ed460] | 565 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
|
---|
[05e43cf] | 566 |
|
---|
[07ed460] | 567 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 568 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, objects[*it].texture_vbo_offset);
|
---|
[05e43cf] | 569 |
|
---|
| 570 | glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
|
---|
[baa5848] | 571 | }
|
---|
[df652d5] | 572 |
|
---|
[644a2e4] | 573 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 574 |
|
---|
| 575 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 576 | glfwSetWindowShouldClose(window, 1);
|
---|
| 577 | }
|
---|
[7ee66ea] | 578 |
|
---|
| 579 | float dist = cam_speed * elapsed_seconds;
|
---|
| 580 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
[c62eee6] | 581 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
| 582 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 583 | cam_moved = true;
|
---|
| 584 | }
|
---|
| 585 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
[c62eee6] | 586 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
| 587 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 588 | cam_moved = true;
|
---|
| 589 | }
|
---|
| 590 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
[c62eee6] | 591 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
| 592 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 593 | cam_moved = true;
|
---|
| 594 | }
|
---|
| 595 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
[c62eee6] | 596 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
| 597 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 598 | cam_moved = true;
|
---|
| 599 | }
|
---|
| 600 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
| 601 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 602 | cam_moved = true;
|
---|
| 603 | }
|
---|
| 604 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
| 605 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 606 | cam_moved = true;
|
---|
| 607 | }
|
---|
| 608 | if (cam_moved) {
|
---|
[c62eee6] | 609 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 610 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[267c4c5] | 611 | view_mat = R*T;
|
---|
[7ee66ea] | 612 |
|
---|
[267c4c5] | 613 | glUseProgram(shader_program);
|
---|
| 614 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 615 |
|
---|
| 616 | glUseProgram(shader_program2);
|
---|
[7ee66ea] | 617 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[267c4c5] | 618 |
|
---|
[7ee66ea] | 619 | cam_moved = false;
|
---|
| 620 | }
|
---|
[644a2e4] | 621 | }
|
---|
| 622 |
|
---|
[5272b6b] | 623 | glfwTerminate();
|
---|
| 624 | return 0;
|
---|
| 625 | }
|
---|
[ec4456b] | 626 |
|
---|
| 627 | GLuint loadShader(GLenum type, string file) {
|
---|
| 628 | cout << "Loading shader from file " << file << endl;
|
---|
| 629 |
|
---|
| 630 | ifstream shaderFile(file);
|
---|
| 631 | GLuint shaderId = 0;
|
---|
| 632 |
|
---|
| 633 | if (shaderFile.is_open()) {
|
---|
| 634 | string line, shaderString;
|
---|
| 635 |
|
---|
| 636 | while(getline(shaderFile, line)) {
|
---|
| 637 | shaderString += line + "\n";
|
---|
| 638 | }
|
---|
| 639 | shaderFile.close();
|
---|
| 640 | const char* shaderCString = shaderString.c_str();
|
---|
| 641 |
|
---|
| 642 | shaderId = glCreateShader(type);
|
---|
| 643 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 644 | glCompileShader(shaderId);
|
---|
| 645 |
|
---|
| 646 | cout << "Loaded successfully" << endl;
|
---|
| 647 | } else {
|
---|
[e856d62] | 648 | cout << "Failed to load the file" << endl;
|
---|
[ec4456b] | 649 | }
|
---|
| 650 |
|
---|
| 651 | return shaderId;
|
---|
| 652 | }
|
---|
[485424b] | 653 |
|
---|
| 654 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 655 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 656 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 657 |
|
---|
| 658 | GLuint shader_program = glCreateProgram();
|
---|
| 659 | glAttachShader(shader_program, vs);
|
---|
| 660 | glAttachShader(shader_program, fs);
|
---|
| 661 |
|
---|
| 662 | glLinkProgram(shader_program);
|
---|
| 663 |
|
---|
| 664 | return shader_program;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 668 | int n;
|
---|
[e856d62] | 669 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
[485424b] | 670 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
[e856d62] | 671 |
|
---|
| 672 | int width_in_bytes = *x * 4;
|
---|
| 673 | unsigned char *top = NULL;
|
---|
| 674 | unsigned char *bottom = NULL;
|
---|
| 675 | unsigned char temp = 0;
|
---|
| 676 | int half_height = *y / 2;
|
---|
| 677 |
|
---|
| 678 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
| 679 | for (int row = 0; row < half_height; row++) {
|
---|
| 680 | top = image_data + row * width_in_bytes;
|
---|
| 681 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
| 682 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
| 683 | temp = *top;
|
---|
| 684 | *top = *bottom;
|
---|
| 685 | *bottom = temp;
|
---|
| 686 | top++;
|
---|
| 687 | bottom++;
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
| 690 |
|
---|
[485424b] | 691 | if (!image_data) {
|
---|
| 692 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
| 693 | }
|
---|
[e856d62] | 694 |
|
---|
| 695 | // Not Power-of-2 check
|
---|
| 696 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
| 697 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
| 698 | }
|
---|
| 699 |
|
---|
[485424b] | 700 | return image_data;
|
---|
| 701 | }
|
---|
[33a9664] | 702 |
|
---|
[e82692b] | 703 | bool faceClicked(ObjectFace* face, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
[5c9d193] | 704 | // LINE EQUATION: P = O + Dt
|
---|
[b73cb3b] | 705 | // O = cam
|
---|
[5c9d193] | 706 | // D = ray_world
|
---|
| 707 |
|
---|
[b73cb3b] | 708 | // PLANE EQUATION: P dot n + d = 0
|
---|
| 709 | // n is the normal vector
|
---|
| 710 | // d is the offset from the origin
|
---|
[5c9d193] | 711 |
|
---|
| 712 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
| 713 | vec3 v1 = face->points[1] - face->points[0];
|
---|
| 714 | vec3 v2 = face->points[2] - face->points[0];
|
---|
| 715 |
|
---|
| 716 | 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] | 717 |
|
---|
| 718 | print4DVector("Full world ray", world_ray);
|
---|
[5c9d193] | 719 |
|
---|
[e82692b] | 720 | SceneObject* obj = &objects[face->object_id];
|
---|
[5c9d193] | 721 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
| 722 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
| 723 |
|
---|
[b73cb3b] | 724 | local_ray = local_ray - local_cam;
|
---|
[5c9d193] | 725 |
|
---|
| 726 | float d = -glm::dot(face->points[0], normal);
|
---|
| 727 | cout << "d: " << d << endl;
|
---|
| 728 |
|
---|
| 729 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
| 730 | cout << "t: " << t << endl;
|
---|
| 731 |
|
---|
| 732 | vec3 intersection = local_cam + t*local_ray;
|
---|
| 733 | printVector("Intersection", intersection);
|
---|
| 734 |
|
---|
[e82692b] | 735 | if (insideTriangle(intersection, face->points)) {
|
---|
| 736 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
| 737 | return true;
|
---|
| 738 | } else {
|
---|
| 739 | return false;
|
---|
| 740 | }
|
---|
[5c9d193] | 741 | }
|
---|
| 742 |
|
---|
| 743 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
| 744 | vec3 v21 = triangle_points[1]- triangle_points[0];
|
---|
| 745 | vec3 v31 = triangle_points[2]- triangle_points[0];
|
---|
| 746 | vec3 pv1 = p- triangle_points[0];
|
---|
[33a9664] | 747 |
|
---|
| 748 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 749 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 750 |
|
---|
| 751 | cout << "(" << x << ", " << y << ")" << endl;
|
---|
| 752 |
|
---|
| 753 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 754 | }
|
---|
[d12d003] | 755 |
|
---|
| 756 | void printVector(string label, vec3 v) {
|
---|
| 757 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
| 758 | }
|
---|
[b73cb3b] | 759 |
|
---|
| 760 | void print4DVector(string label, vec4 v) {
|
---|
| 761 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
| 762 | }
|
---|