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