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