[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 |
|
---|
[c62eee6] | 8 | #include <glm/mat4x4.hpp>
|
---|
[7ee66ea] | 9 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 10 | #include <glm/gtc/type_ptr.hpp>
|
---|
| 11 |
|
---|
[5272b6b] | 12 | #include <GL/glew.h>
|
---|
| 13 | #include <GLFW/glfw3.h>
|
---|
| 14 |
|
---|
[22b2c37] | 15 | #include <cstdio>
|
---|
| 16 | #include <iostream>
|
---|
[ec4456b] | 17 | #include <fstream>
|
---|
[93baa0e] | 18 | #include <cmath>
|
---|
[1099b95] | 19 | #include <string>
|
---|
[bc6d8f6] | 20 | #include <array>
|
---|
[22b2c37] | 21 |
|
---|
[5272b6b] | 22 | using namespace std;
|
---|
[7ee66ea] | 23 | using namespace glm;
|
---|
| 24 |
|
---|
| 25 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
---|
[c62eee6] | 26 |
|
---|
[485424b] | 27 | const bool FULLSCREEN = false;
|
---|
[c62eee6] | 28 | int width = 640;
|
---|
| 29 | int height = 480;
|
---|
| 30 |
|
---|
| 31 | vec3 cam_pos;
|
---|
| 32 |
|
---|
[bc6d8f6] | 33 | array<vec3, 3> triangle_face;
|
---|
| 34 |
|
---|
| 35 | array<vec3,3> colored_triangle;
|
---|
| 36 | array<vec3, 3> square_triangle1;
|
---|
| 37 | array<vec3, 3> square_triangle2;
|
---|
[c62eee6] | 38 |
|
---|
[33a9664] | 39 | bool clicked = false;
|
---|
| 40 | int colors_i = 0;
|
---|
| 41 |
|
---|
[64a70f4] | 42 | bool clicked_square = false;
|
---|
| 43 |
|
---|
[bc6d8f6] | 44 | mat4 model_mat;
|
---|
| 45 | mat4 model_mat2;
|
---|
| 46 |
|
---|
[c62eee6] | 47 | mat4 view_mat;
|
---|
| 48 | mat4 proj_mat;
|
---|
[5272b6b] | 49 |
|
---|
[bc6d8f6] | 50 | bool insideTriangle(vec3 p, array<vec3, 3> triangle);
|
---|
[33a9664] | 51 |
|
---|
[ec4456b] | 52 | GLuint loadShader(GLenum type, string file);
|
---|
[485424b] | 53 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
| 54 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
[ec4456b] | 55 |
|
---|
[d12d003] | 56 | void printVector(string label, vec3 v);
|
---|
| 57 |
|
---|
| 58 | float NEAR_CLIP = 0.1f;
|
---|
| 59 | float FAR_CLIP = 100.0f;
|
---|
| 60 |
|
---|
[ec4456b] | 61 | void glfw_error_callback(int error, const char* description) {
|
---|
| 62 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[c62eee6] | 65 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
| 66 | double mouse_x, mouse_y;
|
---|
| 67 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 68 |
|
---|
| 69 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 70 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
| 71 |
|
---|
| 72 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
| 73 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
[33a9664] | 74 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 75 |
|
---|
| 76 | // Since the projection matrix gets applied before the view matrix,
|
---|
| 77 | // treat the initial camera position (aka origin of the ray) as (0, 0, 0)
|
---|
| 78 |
|
---|
| 79 | // When getting the ray direction, you can use near and fov to get the
|
---|
| 80 | // coordinates
|
---|
[c62eee6] | 81 |
|
---|
[33a9664] | 82 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); // this should have a z equal to the near clipping plane
|
---|
[c62eee6] | 83 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
| 84 | ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f);
|
---|
| 85 | vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
|
---|
| 86 |
|
---|
[33a9664] | 87 | /* LATEST NOTES:
|
---|
| 88 | *
|
---|
| 89 | * Normalizing the world ray caused issues, although it should make sense with the projection
|
---|
| 90 | * matrix, since the z coordinate has meaning there.
|
---|
| 91 | *
|
---|
| 92 | * Now, I need to figure out the correct intersection test in 2D space
|
---|
| 93 | * Also, need to check that the global triangle points are correct
|
---|
| 94 | */
|
---|
| 95 |
|
---|
| 96 | // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to
|
---|
| 97 | // it, only to subtract it later
|
---|
| 98 |
|
---|
[c62eee6] | 99 | vec3 click_point = cam_pos + ray_world;
|
---|
| 100 |
|
---|
| 101 | /* Now, we need to generate the constants for the equations describing
|
---|
| 102 | * a 3D line:
|
---|
| 103 | * (x - x0) / a = (y - y0) / b = (z - z0) / c
|
---|
| 104 | *
|
---|
| 105 | * The line goes through the camera position, so
|
---|
| 106 | * cam_pos = <x0, y0, z0>
|
---|
| 107 | */
|
---|
| 108 |
|
---|
[33a9664] | 109 | // upper right corner is 1, 1 in opengl
|
---|
| 110 |
|
---|
[c62eee6] | 111 | cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
|
---|
| 112 | cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl;
|
---|
| 113 | cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl;
|
---|
| 114 |
|
---|
| 115 | float a = 1.0f;
|
---|
| 116 | float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x);
|
---|
| 117 | float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x);
|
---|
| 118 |
|
---|
| 119 | cout << "(x - " << cam_pos.x << ") / " << a << " = ";
|
---|
| 120 | cout << "(y - " << cam_pos.y << ") / " << b << " = ";
|
---|
| 121 | cout << "(z - " << cam_pos.z << ") / " << c << endl;;
|
---|
| 122 |
|
---|
| 123 | /* Now, we need to generate the constants for the equations describing
|
---|
| 124 | * a 3D plane:
|
---|
| 125 | * dx + ey +fz +g = 0
|
---|
| 126 | */
|
---|
| 127 |
|
---|
[bc6d8f6] | 128 | vec3 fp1 = triangle_face[0];
|
---|
| 129 | vec3 fp2 = triangle_face[1];
|
---|
| 130 | vec3 fp3 = triangle_face[2];
|
---|
[33a9664] | 131 |
|
---|
[c62eee6] | 132 | cout << "Points on the plane" << endl;
|
---|
[33a9664] | 133 | cout << "(" << fp1.x << ", " << fp1.y << ", " << fp1.z << ")" << endl;
|
---|
| 134 | cout << "(" << fp2.x << ", " << fp2.y << ", " << fp2.z << ")" << endl;
|
---|
| 135 | cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
|
---|
| 136 |
|
---|
| 137 | float pa = (fp2.y-fp1.y)*(fp3.z-fp1.z) - (fp3.y-fp1.y)*(fp2.z-fp1.z);
|
---|
| 138 | float pb = (fp2.z-fp1.z)*(fp3.x-fp1.x) - (fp3.z-fp1.z)*(fp2.x-fp1.x);
|
---|
| 139 | float pc = (fp2.x-fp1.x)*(fp3.y-fp1.y) - (fp3.x-fp1.x)*(fp2.y-fp1.y);
|
---|
| 140 | float pd = -(pa*fp1.x+pb*fp1.y+pc*fp1.z);
|
---|
| 141 |
|
---|
| 142 | cout << pa << "x+" << pb << "y+" << pc << "z+" << pd << "=0" << endl;
|
---|
[c62eee6] | 143 |
|
---|
| 144 | // get intersection
|
---|
[33a9664] | 145 |
|
---|
| 146 | // the intersection this computes is incorrect
|
---|
| 147 | // it doesn't match the equation of the plane
|
---|
| 148 | vec3 i;
|
---|
| 149 | i.z = -cam_pos.z - pc*pd/(pa*a+pb*b);
|
---|
| 150 | i.x = cam_pos.x + a * (i.z-cam_pos.z) / c;
|
---|
| 151 | i.y = cam_pos.y + b * (i.z-cam_pos.z) / c;
|
---|
| 152 |
|
---|
| 153 | cout << "The holy grail?" << endl;
|
---|
| 154 | cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
|
---|
| 155 |
|
---|
[bc6d8f6] | 156 | clicked = insideTriangle(i, triangle_face);
|
---|
[d12d003] | 157 | cout << (clicked ? "true" : "false") << endl;
|
---|
[33a9664] | 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[bc6d8f6] | 161 | /* REFACTORING PLAN:
|
---|
| 162 | *
|
---|
| 163 | * Have an array of object structs
|
---|
| 164 | * Each object struct has:
|
---|
| 165 | * -a model matrix
|
---|
| 166 | * -a selected boolean
|
---|
| 167 | *
|
---|
| 168 | * Have an array of face structs
|
---|
| 169 | * Each face struct has
|
---|
| 170 | * -an object index indicating which object it is a part of
|
---|
| 171 | * -an array of three points
|
---|
| 172 | *
|
---|
| 173 | * The mouse button callback will:
|
---|
| 174 | * -Set all selected flags in the objects array to false
|
---|
| 175 | * -iterate through the faces array
|
---|
| 176 | * -For each face, it will call faceClicked() with the following params:
|
---|
| 177 | * -An array of 3 points representing the face
|
---|
| 178 | * -The object struct represnting the object the face is a part of
|
---|
| 179 | */
|
---|
| 180 |
|
---|
[33a9664] | 181 | void mouse_button_callback_new(GLFWwindow* window, int button, int action, int mods) {
|
---|
| 182 | double mouse_x, mouse_y;
|
---|
| 183 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 184 |
|
---|
| 185 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 186 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
| 187 |
|
---|
| 188 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
| 189 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
[d12d003] | 190 |
|
---|
[33a9664] | 191 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 192 |
|
---|
| 193 | // CHECK: Looks good up to here
|
---|
| 194 |
|
---|
| 195 | // Since the projection matrix gets applied before the view matrix,
|
---|
| 196 | // treat the initial camera position (aka origin of the ray) as (0, 0, 0)
|
---|
| 197 |
|
---|
| 198 | // When getting the ray direction, you can use near and fov to get the
|
---|
| 199 | // coordinates
|
---|
| 200 |
|
---|
| 201 | // vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); // this should have a z equal to the near clipping plane
|
---|
| 202 | // vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
| 203 | // ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f);
|
---|
| 204 | // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
|
---|
| 205 |
|
---|
[a5b5e95] | 206 | vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
|
---|
[d12d003] | 207 | vec4 ray_eye = ray_clip;
|
---|
[bc6d8f6] | 208 | vec3 ray_world = (inverse(model_mat) * inverse(view_mat) * ray_eye).xyz();
|
---|
[33a9664] | 209 |
|
---|
| 210 | /* LATEST NOTES:
|
---|
| 211 | *
|
---|
| 212 | * Normalizing the world ray caused issues, although it should make sense with the projection
|
---|
| 213 | * matrix, since the z coordinate has meaning there.
|
---|
[bc6d8f6] | 214 | * Plus, we really want to normalize it only once we recompute it below as the difference of two points,
|
---|
| 215 | * although doing so shouldn't effect the results. Check the book to see if there is a good reason for doing so.
|
---|
[33a9664] | 216 | */
|
---|
| 217 |
|
---|
[d12d003] | 218 | printVector("Initial world ray:", ray_world);
|
---|
[33a9664] | 219 |
|
---|
[a5b5e95] | 220 | vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
|
---|
[bc6d8f6] | 221 | vec3 cam_pos_temp = (inverse(model_mat) * inverse(view_mat) * cam_pos_origin).xyz();
|
---|
[33a9664] | 222 |
|
---|
[d12d003] | 223 | ray_world = ray_world-cam_pos_temp;
|
---|
[33a9664] | 224 |
|
---|
[d12d003] | 225 | cout << "Ray clip -> (" << ray_clip.x << "," << ray_clip.y << "," << ray_clip.z << ")" << endl << endl;;
|
---|
| 226 | cout << "Ray world -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
|
---|
| 227 | cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
|
---|
[33a9664] | 228 |
|
---|
[bc6d8f6] | 229 | vec3 fp1 = triangle_face[0];
|
---|
| 230 | vec3 fp2 = triangle_face[1];
|
---|
| 231 | vec3 fp3 = triangle_face[2];
|
---|
[33a9664] | 232 |
|
---|
| 233 | cout << "Points on the plane" << endl;
|
---|
| 234 | cout << "(" << fp1.x << ", " << fp1.y << ", " << fp1.z << ")" << endl;
|
---|
| 235 | cout << "(" << fp2.x << ", " << fp2.y << ", " << fp2.z << ")" << endl;
|
---|
| 236 | cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
|
---|
| 237 |
|
---|
[d12d003] | 238 | // LINE EQUATION: P = O + Dt
|
---|
| 239 | // O = cam_pos
|
---|
| 240 | // D = ray_world
|
---|
[33a9664] | 241 |
|
---|
[d12d003] | 242 | // PLANE EQUATION: P dot n + d = 0 (n is the normal vector and d is the offset from the origin)
|
---|
[33a9664] | 243 |
|
---|
[d12d003] | 244 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
| 245 | vec3 v1 = fp2 - fp1;
|
---|
| 246 | vec3 v2 = fp3 - fp1;
|
---|
[33a9664] | 247 |
|
---|
[d12d003] | 248 | 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);
|
---|
| 249 | printVector("v1", v1);
|
---|
| 250 | printVector("v2", v2);
|
---|
| 251 | printVector("Cross", normal);
|
---|
| 252 | cout << "Test theory: " << glm::dot(cam_pos_temp, normal) << endl;
|
---|
| 253 | cout << "Test 2: " << glm::dot(ray_world, normal) << endl;
|
---|
[33a9664] | 254 |
|
---|
[d12d003] | 255 | float d = -glm::dot(fp1, normal);
|
---|
| 256 | cout << "d: " << d << endl;
|
---|
[33a9664] | 257 |
|
---|
[d12d003] | 258 | float t = - (glm::dot(cam_pos_temp, normal) + d) / glm::dot(ray_world, normal);
|
---|
| 259 | cout << "t: " << t << endl;
|
---|
[33a9664] | 260 |
|
---|
[d12d003] | 261 | vec3 intersection = cam_pos_temp+t*ray_world;
|
---|
| 262 | printVector("Intersection", intersection);
|
---|
| 263 |
|
---|
[bc6d8f6] | 264 | clicked = insideTriangle(intersection, triangle_face);
|
---|
[d12d003] | 265 | cout << (clicked ? "true" : "false") << endl;
|
---|
[64a70f4] | 266 |
|
---|
| 267 | clicked_square = !clicked_square;
|
---|
[c62eee6] | 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[5272b6b] | 271 | int main(int argc, char* argv[]) {
|
---|
| 272 | cout << "New OpenGL Game" << endl;
|
---|
| 273 |
|
---|
[ec4456b] | 274 | if (!restart_gl_log()) {}
|
---|
| 275 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 276 |
|
---|
[ec4456b] | 277 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 278 | if (!glfwInit()) {
|
---|
| 279 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 280 | return 1;
|
---|
[be246ad] | 281 | }
|
---|
| 282 |
|
---|
| 283 | #ifdef __APPLE__
|
---|
| 284 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 285 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 286 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 287 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 288 | #endif
|
---|
[5272b6b] | 289 |
|
---|
[ec4456b] | 290 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 291 |
|
---|
| 292 | GLFWwindow* window = NULL;
|
---|
| 293 |
|
---|
| 294 | if (FULLSCREEN) {
|
---|
| 295 | GLFWmonitor* mon = glfwGetPrimaryMonitor();
|
---|
| 296 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 297 |
|
---|
| 298 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
| 299 | window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
|
---|
| 300 |
|
---|
| 301 | width = vmode->width;
|
---|
| 302 | height = vmode->height;
|
---|
| 303 | } else {
|
---|
| 304 | window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[5272b6b] | 307 | if (!window) {
|
---|
| 308 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 309 | glfwTerminate();
|
---|
| 310 | return 1;
|
---|
| 311 | }
|
---|
[c62eee6] | 312 |
|
---|
[d12d003] | 313 | glfwSetMouseButtonCallback(window, mouse_button_callback_new);
|
---|
[c62eee6] | 314 |
|
---|
[644a2e4] | 315 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 316 | glewExperimental = GL_TRUE;
|
---|
| 317 | glewInit();
|
---|
| 318 |
|
---|
[ec4456b] | 319 | // glViewport(0, 0, width*2, height*2);
|
---|
| 320 |
|
---|
[5272b6b] | 321 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 322 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 323 | printf("Renderer: %s\n", renderer);
|
---|
| 324 | printf("OpenGL version supported %s\n", version);
|
---|
[93baa0e] | 325 |
|
---|
[5272b6b] | 326 | glEnable(GL_DEPTH_TEST);
|
---|
| 327 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 328 |
|
---|
[93baa0e] | 329 | glEnable(GL_CULL_FACE);
|
---|
| 330 | // glCullFace(GL_BACK);
|
---|
| 331 | // glFrontFace(GL_CW);
|
---|
| 332 |
|
---|
[485424b] | 333 | int x, y;
|
---|
| 334 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
| 335 | if (texImage) {
|
---|
| 336 | cout << "Yay, I loaded an image!" << endl;
|
---|
| 337 | cout << x << endl;
|
---|
| 338 | cout << y << endl;
|
---|
| 339 | printf ("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | GLuint tex = 0;
|
---|
| 343 | glGenTextures(1, &tex);
|
---|
| 344 | glActiveTexture(GL_TEXTURE0);
|
---|
| 345 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
| 346 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 347 |
|
---|
| 348 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 349 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 350 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 351 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 352 |
|
---|
[516668e] | 353 | GLfloat points[] = {
|
---|
[d12d003] | 354 | 0.0f, 0.5f, 0.0f,
|
---|
| 355 | -0.5f, -0.5f, 0.0f,
|
---|
| 356 | 0.5f, -0.5f, 0.0f,
|
---|
| 357 | 0.5f, -0.5f, 0.0f,
|
---|
| 358 | -0.5f, -0.5f, 0.0f,
|
---|
| 359 | 0.0f, 0.5f, 0.0f,
|
---|
[516668e] | 360 | };
|
---|
[c62eee6] | 361 |
|
---|
[8b7cfcf] | 362 | GLfloat colors[] = {
|
---|
[64a70f4] | 363 | 1.0, 0.0, 0.0,
|
---|
| 364 | 0.0, 0.0, 1.0,
|
---|
| 365 | 0.0, 1.0, 0.0,
|
---|
| 366 | 0.0, 1.0, 0.0,
|
---|
| 367 | 0.0, 0.0, 1.0,
|
---|
| 368 | 1.0, 0.0, 0.0,
|
---|
[93baa0e] | 369 | };
|
---|
| 370 |
|
---|
[33a9664] | 371 | GLfloat colors_new[] = {
|
---|
[64a70f4] | 372 | 0.0, 1.0, 0.0,
|
---|
| 373 | 0.0, 1.0, 0.0,
|
---|
| 374 | 0.0, 1.0, 0.0,
|
---|
| 375 | 0.0, 1.0, 0.0,
|
---|
| 376 | 0.0, 1.0, 0.0,
|
---|
| 377 | 0.0, 1.0, 0.0,
|
---|
[33a9664] | 378 | };
|
---|
| 379 |
|
---|
[485424b] | 380 | // Each point is made of 3 floats
|
---|
| 381 | int numPoints = (sizeof(points) / sizeof(float)) / 3;
|
---|
| 382 |
|
---|
| 383 | GLfloat points2[] = {
|
---|
[64a70f4] | 384 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 385 | -0.5f, 0.5f, 0.0f,
|
---|
| 386 | -0.5f, -0.5f, 0.0f,
|
---|
[64a70f4] | 387 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 388 | -0.5f, -0.5f, 0.0f,
|
---|
[64a70f4] | 389 | 0.5f, -0.5f, 0.0f,
|
---|
| 390 | };
|
---|
[485424b] | 391 |
|
---|
| 392 | GLfloat colors2[] = {
|
---|
[64a70f4] | 393 | 0.0, 0.9, 0.9,
|
---|
| 394 | 0.0, 0.9, 0.9,
|
---|
| 395 | 0.0, 0.9, 0.9,
|
---|
| 396 | 0.0, 0.9, 0.9,
|
---|
| 397 | 0.0, 0.9, 0.9,
|
---|
| 398 | 0.0, 0.9, 0.9,
|
---|
[485424b] | 399 | };
|
---|
| 400 |
|
---|
| 401 | GLfloat texcoords[] = {
|
---|
[64a70f4] | 402 | 1.0f, 1.0f,
|
---|
| 403 | 0.0f, 1.0f,
|
---|
| 404 | 0.0, 0.0,
|
---|
| 405 | 1.0, 1.0,
|
---|
| 406 | 0.0, 0.0,
|
---|
| 407 | 1.0, 0.0
|
---|
[485424b] | 408 | };
|
---|
| 409 |
|
---|
| 410 | // Each point is made of 3 floats
|
---|
| 411 | int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
|
---|
| 412 |
|
---|
[bc6d8f6] | 413 | // initialize global variables for click intersection tests
|
---|
| 414 |
|
---|
| 415 | colored_triangle = {
|
---|
| 416 | vec3(points[0], points[1], points[2]),
|
---|
| 417 | vec3(points[3], points[4], points[5]),
|
---|
| 418 | vec3(points[6], points[7], points[8]),
|
---|
| 419 | };
|
---|
| 420 |
|
---|
| 421 | square_triangle1 = {
|
---|
| 422 | vec3(points2[0], points2[1], points2[2]),
|
---|
| 423 | vec3(points2[3], points2[4], points2[5]),
|
---|
| 424 | vec3(points2[6], points2[7], points2[8]),
|
---|
| 425 | };
|
---|
| 426 |
|
---|
| 427 | square_triangle2 = {
|
---|
| 428 | vec3(points2[9], points2[10], points2[11]),
|
---|
| 429 | vec3(points2[12], points2[13], points2[14]),
|
---|
| 430 | vec3(points2[15], points2[16], points2[17]),
|
---|
| 431 | };
|
---|
| 432 |
|
---|
| 433 | triangle_face = colored_triangle;
|
---|
| 434 |
|
---|
[c62eee6] | 435 | /*
|
---|
[201e2f8] | 436 | mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[c62eee6] | 437 | */
|
---|
[bc6d8f6] | 438 | mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
|
---|
[c62eee6] | 439 | mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[bc6d8f6] | 440 | model_mat = T_model*R_model;
|
---|
[8b7cfcf] | 441 |
|
---|
[64a70f4] | 442 | // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
|
---|
[bc6d8f6] | 443 | mat4 T_model2 = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
|
---|
[485424b] | 444 | mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[bc6d8f6] | 445 | model_mat2 = T_model2*R_model2;
|
---|
[485424b] | 446 |
|
---|
[8b7cfcf] | 447 | GLuint points_vbo = 0;
|
---|
| 448 | glGenBuffers(1, &points_vbo);
|
---|
| 449 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[516668e] | 450 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
| 451 |
|
---|
[8b7cfcf] | 452 | GLuint colors_vbo = 0;
|
---|
| 453 | glGenBuffers(1, &colors_vbo);
|
---|
| 454 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 455 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
| 456 |
|
---|
[644a2e4] | 457 | GLuint vao = 0;
|
---|
[516668e] | 458 | glGenVertexArrays(1, &vao);
|
---|
| 459 | glBindVertexArray(vao);
|
---|
[8b7cfcf] | 460 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[516668e] | 461 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
[8b7cfcf] | 462 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 463 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
[516668e] | 464 |
|
---|
[8b7cfcf] | 465 | glEnableVertexAttribArray(0);
|
---|
| 466 | glEnableVertexAttribArray(1);
|
---|
[644a2e4] | 467 |
|
---|
[485424b] | 468 | GLuint points2_vbo = 0;
|
---|
| 469 | glGenBuffers(1, &points2_vbo);
|
---|
| 470 | glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
|
---|
| 471 | glBufferData(GL_ARRAY_BUFFER, sizeof(points2), points2, GL_STATIC_DRAW);
|
---|
| 472 |
|
---|
| 473 | GLuint colors2_vbo = 0;
|
---|
| 474 | glGenBuffers(1, &colors2_vbo);
|
---|
| 475 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
| 476 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors2), colors2, GL_STATIC_DRAW);
|
---|
| 477 |
|
---|
| 478 | GLuint vt_vbo;
|
---|
| 479 | glGenBuffers(1, &vt_vbo);
|
---|
| 480 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
| 481 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, GL_STATIC_DRAW);
|
---|
| 482 |
|
---|
| 483 | GLuint vao2 = 0;
|
---|
| 484 | glGenVertexArrays(1, &vao2);
|
---|
| 485 | glBindVertexArray(vao2);
|
---|
| 486 | glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
|
---|
| 487 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 488 | // glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
| 489 | // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 490 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
| 491 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
[644a2e4] | 492 |
|
---|
[485424b] | 493 | glEnableVertexAttribArray(0);
|
---|
| 494 | glEnableVertexAttribArray(1);
|
---|
[8b7cfcf] | 495 |
|
---|
[485424b] | 496 | GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
|
---|
| 497 | GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
[644a2e4] | 498 |
|
---|
[93baa0e] | 499 | float speed = 1.0f;
|
---|
| 500 | float last_position = 0.0f;
|
---|
| 501 |
|
---|
[7ee66ea] | 502 | float cam_speed = 1.0f;
|
---|
[201e2f8] | 503 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[7ee66ea] | 504 |
|
---|
[d12d003] | 505 | //cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
[64a70f4] | 506 | cam_pos = vec3(0.0f, 0.0f, 0.3f);
|
---|
| 507 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[7ee66ea] | 508 |
|
---|
[c62eee6] | 509 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 510 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[33a9664] | 511 | /*
|
---|
| 512 | mat4 T = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
|
---|
| 513 | mat4 R = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 514 | */
|
---|
[c62eee6] | 515 | view_mat = R*T;
|
---|
[7ee66ea] | 516 |
|
---|
| 517 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 518 | float aspect = (float)width / (float)height;
|
---|
| 519 |
|
---|
[d12d003] | 520 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 521 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 522 | float Sy = NEAR_CLIP / range;
|
---|
| 523 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 524 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[7ee66ea] | 525 |
|
---|
[d12d003] | 526 | /*
|
---|
[c62eee6] | 527 | float proj_arr[] = {
|
---|
[7ee66ea] | 528 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 529 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 530 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 531 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 532 | };
|
---|
[d12d003] | 533 | */
|
---|
[33a9664] | 534 | float proj_arr[] = {
|
---|
| 535 | 1.0f, 0.0f, 0.0f, 0.0f,
|
---|
| 536 | 0.0f, 1.0f, 0.0f, 0.0f,
|
---|
| 537 | 0.0f, 0.0f, 1.0f, 0.0f,
|
---|
| 538 | 0.0f, 0.0f, 0.0f, 1.0f,
|
---|
| 539 | };
|
---|
[c62eee6] | 540 | proj_mat = make_mat4(proj_arr);
|
---|
[7ee66ea] | 541 |
|
---|
[485424b] | 542 | GLint model_test_loc = glGetUniformLocation(shader_program, "model");
|
---|
| 543 | GLint view_test_loc = glGetUniformLocation(shader_program, "view");
|
---|
| 544 | GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
|
---|
[7ee66ea] | 545 |
|
---|
[bc6d8f6] | 546 | GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
|
---|
| 547 | GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
|
---|
| 548 | GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
|
---|
| 549 |
|
---|
[7ee66ea] | 550 | glUseProgram(shader_program);
|
---|
[485424b] | 551 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
|
---|
[bc6d8f6] | 552 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 553 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[485424b] | 554 |
|
---|
| 555 | glUseProgram(shader_program2);
|
---|
| 556 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
|
---|
[bc6d8f6] | 557 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 558 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[7ee66ea] | 559 |
|
---|
| 560 | bool cam_moved = false;
|
---|
| 561 |
|
---|
[93baa0e] | 562 | double previous_seconds = glfwGetTime();
|
---|
[644a2e4] | 563 | while (!glfwWindowShouldClose(window)) {
|
---|
[93baa0e] | 564 | double current_seconds = glfwGetTime();
|
---|
| 565 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 566 | previous_seconds = current_seconds;
|
---|
| 567 |
|
---|
| 568 | if (fabs(last_position) > 1.0f) {
|
---|
| 569 | speed = -speed;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[64a70f4] | 572 | if (clicked) {
|
---|
| 573 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[33a9664] | 574 |
|
---|
[64a70f4] | 575 | if (colors_i == 0) {
|
---|
| 576 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
|
---|
| 577 | colors_i = 1;
|
---|
| 578 | } else {
|
---|
| 579 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
|
---|
| 580 | colors_i = 0;
|
---|
| 581 | }
|
---|
[33a9664] | 582 |
|
---|
[64a70f4] | 583 | clicked = false;
|
---|
| 584 | }
|
---|
[33a9664] | 585 |
|
---|
[7ee66ea] | 586 | /*
|
---|
[93baa0e] | 587 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 588 | last_position = model[12];
|
---|
[7ee66ea] | 589 | */
|
---|
[93baa0e] | 590 |
|
---|
| 591 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[485424b] | 592 |
|
---|
| 593 | glUseProgram(shader_program);
|
---|
[bc6d8f6] | 594 |
|
---|
| 595 | // this is temporary.
|
---|
| 596 | // It's needed to offset the code for the recoloring of the square working during click detection
|
---|
| 597 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
|
---|
[485424b] | 598 |
|
---|
[644a2e4] | 599 | glBindVertexArray(vao);
|
---|
[93baa0e] | 600 |
|
---|
[7ee66ea] | 601 | glDrawArrays(GL_TRIANGLES, 0, numPoints);
|
---|
[ec4456b] | 602 |
|
---|
[64a70f4] | 603 | if (clicked_square) {
|
---|
| 604 | glUseProgram(shader_program);
|
---|
[bc6d8f6] | 605 |
|
---|
| 606 | // this is temporary.
|
---|
| 607 | // It's needed to get the recoloring of the square working during click detection
|
---|
| 608 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat2));
|
---|
[64a70f4] | 609 |
|
---|
| 610 | glBindVertexArray(vao2);
|
---|
[485424b] | 611 |
|
---|
[64a70f4] | 612 | glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
|
---|
| 613 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 614 | } else {
|
---|
| 615 | glUseProgram(shader_program2);
|
---|
| 616 |
|
---|
| 617 | glBindVertexArray(vao2);
|
---|
| 618 |
|
---|
| 619 | glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
|
---|
| 620 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
| 621 | }
|
---|
[485424b] | 622 |
|
---|
[64a70f4] | 623 | glDrawArrays(GL_TRIANGLES, 0, numPoints2);
|
---|
[485424b] | 624 |
|
---|
[644a2e4] | 625 | glfwPollEvents();
|
---|
| 626 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 627 |
|
---|
| 628 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 629 | glfwSetWindowShouldClose(window, 1);
|
---|
| 630 | }
|
---|
[7ee66ea] | 631 |
|
---|
| 632 | float dist = cam_speed * elapsed_seconds;
|
---|
| 633 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
[c62eee6] | 634 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
| 635 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 636 | cam_moved = true;
|
---|
| 637 | }
|
---|
| 638 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
[c62eee6] | 639 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
| 640 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 641 | cam_moved = true;
|
---|
| 642 | }
|
---|
| 643 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
[c62eee6] | 644 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
| 645 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 646 | cam_moved = true;
|
---|
| 647 | }
|
---|
| 648 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
[c62eee6] | 649 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
| 650 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 651 | cam_moved = true;
|
---|
| 652 | }
|
---|
| 653 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
| 654 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 655 | cam_moved = true;
|
---|
| 656 | }
|
---|
| 657 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
| 658 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 659 | cam_moved = true;
|
---|
| 660 | }
|
---|
| 661 | if (cam_moved) {
|
---|
[c62eee6] | 662 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 663 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[33a9664] | 664 | // view_mat = R*T;
|
---|
[7ee66ea] | 665 |
|
---|
| 666 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 667 | cam_moved = false;
|
---|
| 668 | }
|
---|
[644a2e4] | 669 | }
|
---|
| 670 |
|
---|
[5272b6b] | 671 | glfwTerminate();
|
---|
| 672 | return 0;
|
---|
| 673 | }
|
---|
[ec4456b] | 674 |
|
---|
| 675 | GLuint loadShader(GLenum type, string file) {
|
---|
| 676 | cout << "Loading shader from file " << file << endl;
|
---|
| 677 |
|
---|
| 678 | ifstream shaderFile(file);
|
---|
| 679 | GLuint shaderId = 0;
|
---|
| 680 |
|
---|
| 681 | if (shaderFile.is_open()) {
|
---|
| 682 | string line, shaderString;
|
---|
| 683 |
|
---|
| 684 | while(getline(shaderFile, line)) {
|
---|
| 685 | shaderString += line + "\n";
|
---|
| 686 | }
|
---|
| 687 | shaderFile.close();
|
---|
| 688 | const char* shaderCString = shaderString.c_str();
|
---|
| 689 |
|
---|
| 690 | shaderId = glCreateShader(type);
|
---|
| 691 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 692 | glCompileShader(shaderId);
|
---|
| 693 |
|
---|
| 694 | cout << "Loaded successfully" << endl;
|
---|
| 695 | } else {
|
---|
| 696 | cout << "Failed to loade the file" << endl;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | return shaderId;
|
---|
| 700 | }
|
---|
[485424b] | 701 |
|
---|
| 702 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 703 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 704 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 705 |
|
---|
| 706 | GLuint shader_program = glCreateProgram();
|
---|
| 707 | glAttachShader(shader_program, vs);
|
---|
| 708 | glAttachShader(shader_program, fs);
|
---|
| 709 |
|
---|
| 710 | glLinkProgram(shader_program);
|
---|
| 711 |
|
---|
| 712 | return shader_program;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 716 | int n;
|
---|
| 717 | int force_channels = 4;
|
---|
| 718 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
| 719 | if (!image_data) {
|
---|
| 720 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
| 721 | }
|
---|
| 722 | return image_data;
|
---|
| 723 | }
|
---|
[33a9664] | 724 |
|
---|
[bc6d8f6] | 725 | bool insideTriangle(vec3 p, array<vec3,3> triangle) {
|
---|
| 726 | vec3 v21 = triangle[1]-triangle[0];
|
---|
| 727 | vec3 v31 = triangle[2]-triangle[0];
|
---|
| 728 | vec3 pv1 = p-triangle[0];
|
---|
[33a9664] | 729 |
|
---|
| 730 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 731 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 732 |
|
---|
| 733 | cout << "(" << x << ", " << y << ")" << endl;
|
---|
| 734 |
|
---|
| 735 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 736 | }
|
---|
[d12d003] | 737 |
|
---|
| 738 | void printVector(string label, vec3 v) {
|
---|
| 739 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
| 740 | }
|
---|