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