[22b2c37] | 1 | #include "logger.h"
|
---|
[5272b6b] | 2 |
|
---|
[485424b] | 3 | #include "stb_image.h"
|
---|
| 4 |
|
---|
[4e0b82b] | 5 | // I think this was for the OpenGL 4 book font file tutorial
|
---|
| 6 | //#define STB_IMAGE_WRITE_IMPLEMENTATION
|
---|
| 7 | //#include "stb_image_write.h"
|
---|
| 8 |
|
---|
[1099b95] | 9 | #define _USE_MATH_DEFINES
|
---|
[c62eee6] | 10 | #define GLM_SWIZZLE
|
---|
[1099b95] | 11 |
|
---|
[5c9d193] | 12 | // This is to fix a non-alignment issue when passing vec4 params.
|
---|
| 13 | // Check if it got fixed in a later version of GLM
|
---|
| 14 | #define GLM_FORCE_PURE
|
---|
| 15 |
|
---|
[c62eee6] | 16 | #include <glm/mat4x4.hpp>
|
---|
[7ee66ea] | 17 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 18 | #include <glm/gtc/type_ptr.hpp>
|
---|
| 19 |
|
---|
[c1ca5b5] | 20 | #include "IMGUI/imgui.h"
|
---|
| 21 | #include "imgui_impl_glfw_gl3.h"
|
---|
| 22 |
|
---|
[5272b6b] | 23 | #include <GL/glew.h>
|
---|
| 24 | #include <GLFW/glfw3.h>
|
---|
| 25 |
|
---|
[22b2c37] | 26 | #include <cstdio>
|
---|
| 27 | #include <iostream>
|
---|
[ec4456b] | 28 | #include <fstream>
|
---|
[93baa0e] | 29 | #include <cmath>
|
---|
[1099b95] | 30 | #include <string>
|
---|
[19c9338] | 31 | #include <array>
|
---|
[df652d5] | 32 | #include <vector>
|
---|
[93462c6] | 33 | #include <queue>
|
---|
[22b2c37] | 34 |
|
---|
[5272b6b] | 35 | using namespace std;
|
---|
[7ee66ea] | 36 | using namespace glm;
|
---|
| 37 |
|
---|
| 38 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
|
---|
[c62eee6] | 39 |
|
---|
[df652d5] | 40 | struct SceneObject {
|
---|
[d9f99b2] | 41 | unsigned int id;
|
---|
[df652d5] | 42 | mat4 model_mat;
|
---|
[baa5848] | 43 | GLuint shader_program;
|
---|
[05e43cf] | 44 | unsigned int num_points;
|
---|
[cffca4d] | 45 | GLint vertex_vbo_offset;
|
---|
[07ed460] | 46 | vector<GLfloat> points;
|
---|
| 47 | vector<GLfloat> colors;
|
---|
| 48 | vector<GLfloat> texcoords;
|
---|
[9dd2eb7] | 49 | vector<GLfloat> normals;
|
---|
[07ed460] | 50 | vector<GLfloat> selected_colors;
|
---|
[df652d5] | 51 | };
|
---|
| 52 |
|
---|
[93462c6] | 53 | enum State {
|
---|
| 54 | STATE_MAIN_MENU,
|
---|
| 55 | STATE_GAME,
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | enum Event {
|
---|
| 59 | EVENT_GO_TO_MAIN_MENU,
|
---|
| 60 | EVENT_GO_TO_GAME,
|
---|
| 61 | EVENT_QUIT,
|
---|
| 62 | };
|
---|
| 63 |
|
---|
[485424b] | 64 | const bool FULLSCREEN = false;
|
---|
[c62eee6] | 65 | int width = 640;
|
---|
| 66 | int height = 480;
|
---|
| 67 |
|
---|
[c1ca5b5] | 68 | double fps;
|
---|
| 69 |
|
---|
[c62eee6] | 70 | vec3 cam_pos;
|
---|
| 71 |
|
---|
| 72 | mat4 view_mat;
|
---|
| 73 | mat4 proj_mat;
|
---|
[5272b6b] | 74 |
|
---|
[df652d5] | 75 | vector<SceneObject> objects;
|
---|
[93462c6] | 76 | queue<Event> events;
|
---|
[df652d5] | 77 |
|
---|
[147ac6d] | 78 | SceneObject* clickedObject = NULL;
|
---|
[baa5848] | 79 | SceneObject* selectedObject;
|
---|
[147ac6d] | 80 |
|
---|
[c1ca5b5] | 81 | float NEAR_CLIP = 0.1f;
|
---|
| 82 | float FAR_CLIP = 100.0f;
|
---|
| 83 |
|
---|
[5b3462b] | 84 | // Should really have some array or struct of UI-related variables
|
---|
| 85 | bool isRunning = true;
|
---|
| 86 |
|
---|
[c1ca5b5] | 87 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
---|
[046ce72] | 88 |
|
---|
[d9f99b2] | 89 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
[5c9d193] | 90 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
[33a9664] | 91 |
|
---|
[ec4456b] | 92 | GLuint loadShader(GLenum type, string file);
|
---|
[485424b] | 93 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
| 94 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
[ec4456b] | 95 |
|
---|
[d12d003] | 96 | void printVector(string label, vec3 v);
|
---|
[b73cb3b] | 97 | void print4DVector(string label, vec4 v);
|
---|
[d12d003] | 98 |
|
---|
[93462c6] | 99 | void renderMainMenu();
|
---|
| 100 | void renderMainMenuGui();
|
---|
| 101 |
|
---|
| 102 | void renderScene(vector<SceneObject>& objects,
|
---|
[cffca4d] | 103 | GLuint color_sp, GLuint texture_sp,
|
---|
[93462c6] | 104 | GLuint vao1, GLuint vao2,
|
---|
| 105 | GLuint shader1_mat_loc, GLuint shader2_mat_loc,
|
---|
| 106 | GLuint points_vbo, GLuint normals_vbo,
|
---|
| 107 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
| 108 | SceneObject* selectedObject);
|
---|
| 109 | void renderSceneGui();
|
---|
[d12d003] | 110 |
|
---|
[ec4456b] | 111 | void glfw_error_callback(int error, const char* description) {
|
---|
| 112 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[c62eee6] | 115 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
[33a9664] | 116 | double mouse_x, mouse_y;
|
---|
| 117 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 118 |
|
---|
| 119 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 120 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
[147ac6d] | 121 | selectedObject = NULL;
|
---|
[33a9664] | 122 |
|
---|
| 123 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
| 124 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
[d12d003] | 125 |
|
---|
[33a9664] | 126 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 127 |
|
---|
[b73cb3b] | 128 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
| 129 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
| 130 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
---|
[5c9d193] | 131 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
[33a9664] | 132 |
|
---|
[b73cb3b] | 133 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
[33a9664] | 134 |
|
---|
[e82692b] | 135 | vec4 click_point;
|
---|
[b73cb3b] | 136 | 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
|
---|
[d9f99b2] | 137 | SceneObject* closest_object = NULL;
|
---|
| 138 |
|
---|
| 139 | SceneObject* obj;
|
---|
| 140 | for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
| 141 | obj = &*it;
|
---|
| 142 |
|
---|
[4e0b82b] | 143 | for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {
|
---|
[d9f99b2] | 144 | if (faceClicked({
|
---|
| 145 | vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),
|
---|
| 146 | vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),
|
---|
| 147 | vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
|
---|
| 148 | },
|
---|
| 149 | obj, ray_world, cam_pos_temp, click_point)) {
|
---|
| 150 | click_point = view_mat * click_point;
|
---|
| 151 |
|
---|
| 152 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
| 153 | closest_point = click_point.xyz();
|
---|
| 154 | closest_object = obj;
|
---|
| 155 | }
|
---|
[e82692b] | 156 | }
|
---|
[5c9d193] | 157 | }
|
---|
| 158 | }
|
---|
[d12d003] | 159 |
|
---|
[d9f99b2] | 160 | if (closest_object == NULL) {
|
---|
[5c9d193] | 161 | cout << "No object was clicked" << endl;
|
---|
[e82692b] | 162 | } else {
|
---|
[d9f99b2] | 163 | clickedObject = closest_object;
|
---|
| 164 | cout << "Clicked object: " << clickedObject->id << endl;
|
---|
[147ac6d] | 165 | }
|
---|
[c62eee6] | 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[c1ca5b5] | 169 | int main(int argc, char* argv[]) {
|
---|
[5272b6b] | 170 | cout << "New OpenGL Game" << endl;
|
---|
| 171 |
|
---|
[ec4456b] | 172 | if (!restart_gl_log()) {}
|
---|
| 173 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 174 |
|
---|
[ec4456b] | 175 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 176 | if (!glfwInit()) {
|
---|
| 177 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 178 | return 1;
|
---|
[be246ad] | 179 | }
|
---|
| 180 |
|
---|
| 181 | #ifdef __APPLE__
|
---|
| 182 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 183 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 184 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 185 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 186 | #endif
|
---|
[5272b6b] | 187 |
|
---|
[ec4456b] | 188 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 189 |
|
---|
| 190 | GLFWwindow* window = NULL;
|
---|
[e856d62] | 191 | GLFWmonitor* mon = NULL;
|
---|
[ec4456b] | 192 |
|
---|
| 193 | if (FULLSCREEN) {
|
---|
[e856d62] | 194 | mon = glfwGetPrimaryMonitor();
|
---|
[ec4456b] | 195 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 196 |
|
---|
| 197 | width = vmode->width;
|
---|
| 198 | height = vmode->height;
|
---|
[e856d62] | 199 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
[ec4456b] | 200 | }
|
---|
[e856d62] | 201 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
[ec4456b] | 202 |
|
---|
[5272b6b] | 203 | if (!window) {
|
---|
| 204 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 205 | glfwTerminate();
|
---|
| 206 | return 1;
|
---|
| 207 | }
|
---|
[c62eee6] | 208 |
|
---|
[644a2e4] | 209 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 210 | glewExperimental = GL_TRUE;
|
---|
| 211 | glewInit();
|
---|
| 212 |
|
---|
[c1ca5b5] | 213 | // Setup Dear ImGui binding
|
---|
| 214 | IMGUI_CHECKVERSION();
|
---|
| 215 | ImGui::CreateContext();
|
---|
| 216 | ImGuiIO& io = ImGui::GetIO(); (void)io;
|
---|
| 217 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
---|
| 218 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
---|
| 219 | ImGui_ImplGlfwGL3_Init(window, true);
|
---|
| 220 |
|
---|
| 221 | // Setup style
|
---|
| 222 | ImGui::StyleColorsDark();
|
---|
| 223 | //ImGui::StyleColorsClassic();
|
---|
| 224 |
|
---|
| 225 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
| 226 |
|
---|
[5272b6b] | 227 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 228 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 229 | printf("Renderer: %s\n", renderer);
|
---|
| 230 | printf("OpenGL version supported %s\n", version);
|
---|
[93baa0e] | 231 |
|
---|
[5272b6b] | 232 | glEnable(GL_DEPTH_TEST);
|
---|
| 233 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 234 |
|
---|
[93baa0e] | 235 | glEnable(GL_CULL_FACE);
|
---|
| 236 | // glCullFace(GL_BACK);
|
---|
| 237 | // glFrontFace(GL_CW);
|
---|
| 238 |
|
---|
[485424b] | 239 | int x, y;
|
---|
| 240 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
| 241 | if (texImage) {
|
---|
| 242 | cout << "Yay, I loaded an image!" << endl;
|
---|
| 243 | cout << x << endl;
|
---|
| 244 | cout << y << endl;
|
---|
[e856d62] | 245 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
[485424b] | 246 | }
|
---|
| 247 |
|
---|
| 248 | GLuint tex = 0;
|
---|
| 249 | glGenTextures(1, &tex);
|
---|
| 250 | glActiveTexture(GL_TEXTURE0);
|
---|
| 251 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
| 252 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 253 |
|
---|
| 254 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 255 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 256 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 257 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 258 |
|
---|
[cffca4d] | 259 | // I can create a vbo to store all points for all models,
|
---|
| 260 | // and another vbo to store all colors for all models, but how do I allow alternating between
|
---|
| 261 | // using colors and textures for each model?
|
---|
| 262 | // Do I create a third vbo for texture coordinates and change which vertex attribute array I have bound
|
---|
| 263 | // when I want to draw a textured model?
|
---|
| 264 | // Or do I create one vao with vertices and colors and another with vertices and textures and switch between the two?
|
---|
| 265 | // Since I would have to switch shader programs to toggle between using colors or textures,
|
---|
| 266 | // I think I should use one vao for both cases and have a points vbo, a colors vbo, and a textures vbo
|
---|
| 267 | // One program will use the points and colors, and the other will use the points and texture coords
|
---|
| 268 | // Review how to bind vbos to vertex attributes in the shader.
|
---|
| 269 | //
|
---|
| 270 | // Binding vbos is done using glVertexAttribPointer(...) on a per-vao basis and is not tied to any specific shader.
|
---|
| 271 | // This means, I could create two vaos, one for each shader and have one use points+colors, while the other
|
---|
| 272 | // uses points+texxcoords.
|
---|
| 273 | //
|
---|
| 274 | // At some point, when I have lots of objects, I want to group them by shader when drawing them.
|
---|
| 275 | // I'd probably create some sort of set per shader and have each set contain the ids of all objects currently using that shader
|
---|
| 276 | // Most likely, I'd want to implement each set using a bit field. Makes it constant time for updates and iterating through them
|
---|
| 277 | // should not be much of an issue either.
|
---|
| 278 | // Assuming making lots of draw calls instead of one is not innefficient, I should be fine.
|
---|
| 279 | // I might also want to use one glDrawElements call per shader to draw multiple non-memory-adjacent models
|
---|
| 280 | //
|
---|
| 281 | // DECISION: Use a glDrawElements call per shader since I use a regular array to specify the elements to draw
|
---|
| 282 | // Actually, this will only work once I get UBOs working since each object will have a different model matrix
|
---|
| 283 | // For now, I could implement this with a glDrawElements call per object and update the model uniform for each object
|
---|
| 284 |
|
---|
| 285 | GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag");
|
---|
| 286 | GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
| 287 |
|
---|
[07ed460] | 288 | mat4 T_model, R_model;
|
---|
| 289 |
|
---|
| 290 | // triangle
|
---|
| 291 | objects.push_back(SceneObject());
|
---|
[d9f99b2] | 292 | objects[0].id = 0;
|
---|
[cffca4d] | 293 | objects[0].shader_program = color_sp;
|
---|
| 294 | objects[0].vertex_vbo_offset = 0;
|
---|
[07ed460] | 295 | objects[0].points = {
|
---|
[d12d003] | 296 | 0.0f, 0.5f, 0.0f,
|
---|
| 297 | -0.5f, -0.5f, 0.0f,
|
---|
| 298 | 0.5f, -0.5f, 0.0f,
|
---|
| 299 | 0.5f, -0.5f, 0.0f,
|
---|
| 300 | -0.5f, -0.5f, 0.0f,
|
---|
| 301 | 0.0f, 0.5f, 0.0f,
|
---|
[516668e] | 302 | };
|
---|
[07ed460] | 303 | objects[0].colors = {
|
---|
| 304 | 1.0f, 0.0f, 0.0f,
|
---|
| 305 | 0.0f, 0.0f, 1.0f,
|
---|
| 306 | 0.0f, 1.0f, 0.0f,
|
---|
| 307 | 0.0f, 1.0f, 0.0f,
|
---|
| 308 | 0.0f, 0.0f, 1.0f,
|
---|
| 309 | 1.0f, 0.0f, 0.0f,
|
---|
[93baa0e] | 310 | };
|
---|
[07ed460] | 311 | objects[0].texcoords = {
|
---|
| 312 | 1.0f, 1.0f,
|
---|
| 313 | 0.0f, 1.0f,
|
---|
| 314 | 0.0f, 0.0f,
|
---|
| 315 | 1.0f, 1.0f,
|
---|
| 316 | 0.0f, 0.0f,
|
---|
| 317 | 1.0f, 0.0f
|
---|
[33a9664] | 318 | };
|
---|
[9dd2eb7] | 319 | objects[0].normals = {
|
---|
| 320 | 0.0f, 0.0f, 1.0f,
|
---|
| 321 | 0.0f, 0.0f, 1.0f,
|
---|
| 322 | 0.0f, 0.0f, 1.0f,
|
---|
| 323 | 0.0f, 0.0f, -1.0f,
|
---|
| 324 | 0.0f, 0.0f, -1.0f,
|
---|
| 325 | 0.0f, 0.0f, -1.0f,
|
---|
| 326 | };
|
---|
[07ed460] | 327 | objects[0].selected_colors = {
|
---|
| 328 | 0.0f, 1.0f, 0.0f,
|
---|
| 329 | 0.0f, 1.0f, 0.0f,
|
---|
| 330 | 0.0f, 1.0f, 0.0f,
|
---|
| 331 | 0.0f, 1.0f, 0.0f,
|
---|
| 332 | 0.0f, 1.0f, 0.0f,
|
---|
| 333 | 0.0f, 1.0f, 0.0f,
|
---|
| 334 | };
|
---|
| 335 | objects[0].num_points = objects[0].points.size() / 3;
|
---|
| 336 |
|
---|
| 337 | T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
|
---|
| 338 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 339 | objects[0].model_mat = T_model*R_model;
|
---|
[33a9664] | 340 |
|
---|
[07ed460] | 341 | // square
|
---|
| 342 | objects.push_back(SceneObject());
|
---|
[d9f99b2] | 343 | objects[1].id = 1;
|
---|
[cffca4d] | 344 | objects[1].shader_program = texture_sp;
|
---|
| 345 | objects[1].vertex_vbo_offset = 6;
|
---|
[07ed460] | 346 | objects[1].points = {
|
---|
[b73cb3b] | 347 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 348 | -0.5f, 0.5f, 0.0f,
|
---|
| 349 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 350 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 351 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 352 | 0.5f, -0.5f, 0.0f,
|
---|
[64a70f4] | 353 | };
|
---|
[07ed460] | 354 | objects[1].colors = {
|
---|
| 355 | 1.0f, 0.0f, 0.0f,
|
---|
| 356 | 0.0f, 0.0f, 1.0f,
|
---|
| 357 | 0.0f, 1.0f, 0.0f,
|
---|
| 358 | 0.0f, 1.0f, 0.0f,
|
---|
| 359 | 0.0f, 0.0f, 1.0f,
|
---|
| 360 | 1.0f, 0.0f, 0.0f,
|
---|
[485424b] | 361 | };
|
---|
[07ed460] | 362 | objects[1].texcoords = {
|
---|
[64a70f4] | 363 | 1.0f, 1.0f,
|
---|
| 364 | 0.0f, 1.0f,
|
---|
[07ed460] | 365 | 0.0f, 0.0f,
|
---|
| 366 | 1.0f, 1.0f,
|
---|
| 367 | 0.0f, 0.0f,
|
---|
| 368 | 1.0f, 0.0f
|
---|
[485424b] | 369 | };
|
---|
[9dd2eb7] | 370 | objects[1].normals = {
|
---|
| 371 | 0.0f, 0.0f, 1.0f,
|
---|
| 372 | 0.0f, 0.0f, 1.0f,
|
---|
| 373 | 0.0f, 0.0f, 1.0f,
|
---|
| 374 | 0.0f, 0.0f, 1.0f,
|
---|
| 375 | 0.0f, 0.0f, 1.0f,
|
---|
| 376 | 0.0f, 0.0f, 1.0f,
|
---|
| 377 | };
|
---|
[07ed460] | 378 | objects[1].selected_colors = {
|
---|
[9f4986b] | 379 | 0.0f, 0.6f, 0.9f,
|
---|
| 380 | 0.0f, 0.6f, 0.9f,
|
---|
| 381 | 0.0f, 0.6f, 0.9f,
|
---|
| 382 | 0.0f, 0.6f, 0.9f,
|
---|
| 383 | 0.0f, 0.6f, 0.9f,
|
---|
| 384 | 0.0f, 0.6f, 0.9f,
|
---|
[19c9338] | 385 | };
|
---|
[07ed460] | 386 | objects[1].num_points = objects[1].points.size() / 3;
|
---|
[df652d5] | 387 |
|
---|
[b73cb3b] | 388 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
---|
| 389 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[df652d5] | 390 | objects[1].model_mat = T_model*R_model;
|
---|
| 391 |
|
---|
[07ed460] | 392 | vector<SceneObject>::iterator obj_it;
|
---|
| 393 | GLsizeiptr offset;
|
---|
[19c9338] | 394 |
|
---|
[07ed460] | 395 | GLsizeiptr points_buffer_size = 0;
|
---|
| 396 | GLsizeiptr textures_buffer_size = 0;
|
---|
| 397 |
|
---|
| 398 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 399 | points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
|
---|
| 400 | textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
| 401 | }
|
---|
[19c9338] | 402 |
|
---|
[8b7cfcf] | 403 | GLuint points_vbo = 0;
|
---|
| 404 | glGenBuffers(1, &points_vbo);
|
---|
| 405 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[07ed460] | 406 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
[05e43cf] | 407 |
|
---|
[07ed460] | 408 | offset = 0;
|
---|
| 409 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 410 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]);
|
---|
| 411 | offset += obj_it->points.size() * sizeof(GLfloat);
|
---|
| 412 | }
|
---|
[516668e] | 413 |
|
---|
[8b7cfcf] | 414 | GLuint colors_vbo = 0;
|
---|
| 415 | glGenBuffers(1, &colors_vbo);
|
---|
| 416 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[07ed460] | 417 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 418 |
|
---|
| 419 | offset = 0;
|
---|
| 420 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 421 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]);
|
---|
| 422 | offset += obj_it->colors.size() * sizeof(GLfloat);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | GLuint selected_colors_vbo = 0;
|
---|
| 426 | glGenBuffers(1, &selected_colors_vbo);
|
---|
| 427 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 428 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 429 |
|
---|
| 430 | offset = 0;
|
---|
| 431 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 432 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]);
|
---|
| 433 | offset += obj_it->selected_colors.size() * sizeof(GLfloat);
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | GLuint texcoords_vbo = 0;
|
---|
| 437 | glGenBuffers(1, &texcoords_vbo);
|
---|
| 438 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 439 | glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 440 |
|
---|
| 441 | offset = 0;
|
---|
| 442 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 443 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]);
|
---|
| 444 | offset += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
| 445 | }
|
---|
[8b7cfcf] | 446 |
|
---|
[9dd2eb7] | 447 | GLuint normals_vbo = 0;
|
---|
| 448 | glGenBuffers(1, &normals_vbo);
|
---|
| 449 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 450 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 451 |
|
---|
| 452 | offset = 0;
|
---|
| 453 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 454 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->normals.size() * sizeof(GLfloat), &obj_it->normals[0]);
|
---|
| 455 | offset += obj_it->normals.size() * sizeof(GLfloat);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
[644a2e4] | 458 | GLuint vao = 0;
|
---|
[516668e] | 459 | glGenVertexArrays(1, &vao);
|
---|
| 460 | glBindVertexArray(vao);
|
---|
| 461 |
|
---|
[8b7cfcf] | 462 | glEnableVertexAttribArray(0);
|
---|
| 463 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 464 | glEnableVertexAttribArray(2);
|
---|
[644a2e4] | 465 |
|
---|
[cffca4d] | 466 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 467 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 468 |
|
---|
| 469 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 470 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 471 |
|
---|
[485424b] | 472 | GLuint vao2 = 0;
|
---|
| 473 | glGenVertexArrays(1, &vao2);
|
---|
| 474 | glBindVertexArray(vao2);
|
---|
[644a2e4] | 475 |
|
---|
[485424b] | 476 | glEnableVertexAttribArray(0);
|
---|
| 477 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 478 | glEnableVertexAttribArray(2);
|
---|
[8b7cfcf] | 479 |
|
---|
[cffca4d] | 480 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 481 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[1a530df] | 482 |
|
---|
[cffca4d] | 483 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 484 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 485 |
|
---|
| 486 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 487 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[644a2e4] | 488 |
|
---|
[93baa0e] | 489 | float speed = 1.0f;
|
---|
| 490 | float last_position = 0.0f;
|
---|
| 491 |
|
---|
[7ee66ea] | 492 | float cam_speed = 1.0f;
|
---|
[201e2f8] | 493 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[7ee66ea] | 494 |
|
---|
[b73cb3b] | 495 | // glm::lookAt can create the view matrix
|
---|
| 496 | // glm::perspective can create the projection matrix
|
---|
| 497 |
|
---|
| 498 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
[64a70f4] | 499 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[7ee66ea] | 500 |
|
---|
[c62eee6] | 501 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 502 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[c62eee6] | 503 | view_mat = R*T;
|
---|
[7ee66ea] | 504 |
|
---|
| 505 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 506 | float aspect = (float)width / (float)height;
|
---|
| 507 |
|
---|
[d12d003] | 508 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 509 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 510 | float Sy = NEAR_CLIP / range;
|
---|
| 511 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 512 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[7ee66ea] | 513 |
|
---|
[c62eee6] | 514 | float proj_arr[] = {
|
---|
[7ee66ea] | 515 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 516 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 517 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 518 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 519 | };
|
---|
[c62eee6] | 520 | proj_mat = make_mat4(proj_arr);
|
---|
[7ee66ea] | 521 |
|
---|
[cffca4d] | 522 | GLuint model_test_loc = glGetUniformLocation(color_sp, "model");
|
---|
| 523 | GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
|
---|
| 524 | GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
|
---|
[7ee66ea] | 525 |
|
---|
[cffca4d] | 526 | GLuint model_mat_loc = glGetUniformLocation(texture_sp, "model");
|
---|
| 527 | GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
|
---|
| 528 | GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
|
---|
[19c9338] | 529 |
|
---|
[cffca4d] | 530 | glUseProgram(color_sp);
|
---|
[df652d5] | 531 | glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
|
---|
[19c9338] | 532 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 533 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[485424b] | 534 |
|
---|
[cffca4d] | 535 | glUseProgram(texture_sp);
|
---|
[df652d5] | 536 | glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
|
---|
[19c9338] | 537 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 538 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[7ee66ea] | 539 |
|
---|
| 540 | bool cam_moved = false;
|
---|
| 541 |
|
---|
[046ce72] | 542 | int frame_count = 0;
|
---|
[f70ab75] | 543 | double elapsed_seconds_fps = 0.0f;
|
---|
[93baa0e] | 544 | double previous_seconds = glfwGetTime();
|
---|
[046ce72] | 545 |
|
---|
[9dd2eb7] | 546 | // This draws wireframes. Useful for seeing separate faces and occluded objects.
|
---|
| 547 | //glPolygonMode(GL_FRONT, GL_LINE);
|
---|
| 548 |
|
---|
[1c81bf0] | 549 | // disable vsync to see real framerate
|
---|
| 550 | //glfwSwapInterval(0);
|
---|
| 551 |
|
---|
[93462c6] | 552 | State curState = STATE_MAIN_MENU;
|
---|
| 553 |
|
---|
[5b3462b] | 554 | while (!glfwWindowShouldClose(window) && isRunning) {
|
---|
[93baa0e] | 555 | double current_seconds = glfwGetTime();
|
---|
| 556 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 557 | previous_seconds = current_seconds;
|
---|
| 558 |
|
---|
[046ce72] | 559 | elapsed_seconds_fps += elapsed_seconds;
|
---|
| 560 | if (elapsed_seconds_fps > 0.25f) {
|
---|
| 561 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
| 562 | cout << "FPS: " << fps << endl;
|
---|
| 563 |
|
---|
| 564 | frame_count = 0;
|
---|
| 565 | elapsed_seconds_fps = 0.0f;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | frame_count++;
|
---|
| 569 |
|
---|
[93baa0e] | 570 | if (fabs(last_position) > 1.0f) {
|
---|
| 571 | speed = -speed;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[baa5848] | 574 | // Handle events (Ideally, move all event-handling code
|
---|
| 575 | // before the render code)
|
---|
| 576 |
|
---|
| 577 | clickedObject = NULL;
|
---|
| 578 | glfwPollEvents();
|
---|
| 579 |
|
---|
[93462c6] | 580 | while (!events.empty()) {
|
---|
| 581 | switch (events.front()) {
|
---|
| 582 | case EVENT_GO_TO_MAIN_MENU:
|
---|
| 583 | curState = STATE_MAIN_MENU;
|
---|
| 584 | break;
|
---|
| 585 | case EVENT_GO_TO_GAME:
|
---|
| 586 | curState = STATE_GAME;
|
---|
| 587 | break;
|
---|
| 588 | case EVENT_QUIT:
|
---|
| 589 | isRunning = false;
|
---|
| 590 | break;
|
---|
| 591 | }
|
---|
| 592 | events.pop();
|
---|
[147ac6d] | 593 | }
|
---|
[93462c6] | 594 |
|
---|
| 595 | if (curState == STATE_GAME) {
|
---|
| 596 | if (clickedObject == &objects[0]) {
|
---|
| 597 | selectedObject = &objects[0];
|
---|
| 598 | }
|
---|
| 599 | if (clickedObject == &objects[1]) {
|
---|
| 600 | selectedObject = &objects[1];
|
---|
| 601 | }
|
---|
[baa5848] | 602 | }
|
---|
[33a9664] | 603 |
|
---|
[7ee66ea] | 604 | /*
|
---|
[93baa0e] | 605 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 606 | last_position = model[12];
|
---|
[7ee66ea] | 607 | */
|
---|
[93baa0e] | 608 |
|
---|
| 609 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[485424b] | 610 |
|
---|
[93462c6] | 611 | switch (curState) {
|
---|
| 612 | case STATE_MAIN_MENU:
|
---|
| 613 | renderMainMenu();
|
---|
| 614 | renderMainMenuGui();
|
---|
| 615 | break;
|
---|
| 616 | case STATE_GAME:
|
---|
| 617 | renderScene(objects,
|
---|
[cffca4d] | 618 | color_sp, texture_sp,
|
---|
[93462c6] | 619 | vao, vao2,
|
---|
| 620 | model_test_loc, model_mat_loc,
|
---|
| 621 | points_vbo, normals_vbo,
|
---|
| 622 | colors_vbo, texcoords_vbo, selected_colors_vbo,
|
---|
| 623 | selectedObject);
|
---|
| 624 | renderSceneGui();
|
---|
| 625 | break;
|
---|
[baa5848] | 626 | }
|
---|
[df652d5] | 627 |
|
---|
[644a2e4] | 628 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 629 |
|
---|
| 630 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 631 | glfwSetWindowShouldClose(window, 1);
|
---|
| 632 | }
|
---|
[7ee66ea] | 633 |
|
---|
| 634 | float dist = cam_speed * elapsed_seconds;
|
---|
| 635 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
[c62eee6] | 636 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
| 637 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 638 | cam_moved = true;
|
---|
| 639 | }
|
---|
| 640 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
[c62eee6] | 641 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
| 642 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 643 | cam_moved = true;
|
---|
| 644 | }
|
---|
| 645 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
[c62eee6] | 646 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
| 647 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 648 | cam_moved = true;
|
---|
| 649 | }
|
---|
| 650 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
[c62eee6] | 651 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
| 652 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 653 | cam_moved = true;
|
---|
| 654 | }
|
---|
| 655 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
| 656 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 657 | cam_moved = true;
|
---|
| 658 | }
|
---|
| 659 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
| 660 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 661 | cam_moved = true;
|
---|
| 662 | }
|
---|
| 663 | if (cam_moved) {
|
---|
[c62eee6] | 664 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 665 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[267c4c5] | 666 | view_mat = R*T;
|
---|
[7ee66ea] | 667 |
|
---|
[cffca4d] | 668 | glUseProgram(color_sp);
|
---|
[267c4c5] | 669 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 670 |
|
---|
[cffca4d] | 671 | glUseProgram(texture_sp);
|
---|
[7ee66ea] | 672 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[267c4c5] | 673 |
|
---|
[7ee66ea] | 674 | cam_moved = false;
|
---|
| 675 | }
|
---|
[644a2e4] | 676 | }
|
---|
| 677 |
|
---|
[c1ca5b5] | 678 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
| 679 | ImGui::DestroyContext();
|
---|
| 680 |
|
---|
| 681 | glfwDestroyWindow(window);
|
---|
[5272b6b] | 682 | glfwTerminate();
|
---|
[c1ca5b5] | 683 |
|
---|
[5272b6b] | 684 | return 0;
|
---|
| 685 | }
|
---|
[ec4456b] | 686 |
|
---|
| 687 | GLuint loadShader(GLenum type, string file) {
|
---|
| 688 | cout << "Loading shader from file " << file << endl;
|
---|
| 689 |
|
---|
| 690 | ifstream shaderFile(file);
|
---|
| 691 | GLuint shaderId = 0;
|
---|
| 692 |
|
---|
| 693 | if (shaderFile.is_open()) {
|
---|
| 694 | string line, shaderString;
|
---|
| 695 |
|
---|
| 696 | while(getline(shaderFile, line)) {
|
---|
| 697 | shaderString += line + "\n";
|
---|
| 698 | }
|
---|
| 699 | shaderFile.close();
|
---|
| 700 | const char* shaderCString = shaderString.c_str();
|
---|
| 701 |
|
---|
| 702 | shaderId = glCreateShader(type);
|
---|
| 703 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 704 | glCompileShader(shaderId);
|
---|
| 705 |
|
---|
| 706 | cout << "Loaded successfully" << endl;
|
---|
| 707 | } else {
|
---|
[e856d62] | 708 | cout << "Failed to load the file" << endl;
|
---|
[ec4456b] | 709 | }
|
---|
| 710 |
|
---|
| 711 | return shaderId;
|
---|
| 712 | }
|
---|
[485424b] | 713 |
|
---|
| 714 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 715 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 716 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 717 |
|
---|
| 718 | GLuint shader_program = glCreateProgram();
|
---|
| 719 | glAttachShader(shader_program, vs);
|
---|
| 720 | glAttachShader(shader_program, fs);
|
---|
| 721 |
|
---|
| 722 | glLinkProgram(shader_program);
|
---|
| 723 |
|
---|
| 724 | return shader_program;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 728 | int n;
|
---|
[e856d62] | 729 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
[485424b] | 730 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
[e856d62] | 731 |
|
---|
| 732 | int width_in_bytes = *x * 4;
|
---|
| 733 | unsigned char *top = NULL;
|
---|
| 734 | unsigned char *bottom = NULL;
|
---|
| 735 | unsigned char temp = 0;
|
---|
| 736 | int half_height = *y / 2;
|
---|
| 737 |
|
---|
| 738 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
| 739 | for (int row = 0; row < half_height; row++) {
|
---|
| 740 | top = image_data + row * width_in_bytes;
|
---|
| 741 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
| 742 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
| 743 | temp = *top;
|
---|
| 744 | *top = *bottom;
|
---|
| 745 | *bottom = temp;
|
---|
| 746 | top++;
|
---|
| 747 | bottom++;
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[485424b] | 751 | if (!image_data) {
|
---|
| 752 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
| 753 | }
|
---|
[e856d62] | 754 |
|
---|
| 755 | // Not Power-of-2 check
|
---|
| 756 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
| 757 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
| 758 | }
|
---|
| 759 |
|
---|
[485424b] | 760 | return image_data;
|
---|
| 761 | }
|
---|
[33a9664] | 762 |
|
---|
[d9f99b2] | 763 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
[5c9d193] | 764 | // LINE EQUATION: P = O + Dt
|
---|
[b73cb3b] | 765 | // O = cam
|
---|
[5c9d193] | 766 | // D = ray_world
|
---|
| 767 |
|
---|
[b73cb3b] | 768 | // PLANE EQUATION: P dot n + d = 0
|
---|
| 769 | // n is the normal vector
|
---|
| 770 | // d is the offset from the origin
|
---|
[5c9d193] | 771 |
|
---|
| 772 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
[d9f99b2] | 773 | vec3 v1 = points[1] - points[0];
|
---|
| 774 | vec3 v2 = points[2] - points[0];
|
---|
[5c9d193] | 775 |
|
---|
| 776 | 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] | 777 |
|
---|
| 778 | print4DVector("Full world ray", world_ray);
|
---|
[5c9d193] | 779 |
|
---|
| 780 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
| 781 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
| 782 |
|
---|
[b73cb3b] | 783 | local_ray = local_ray - local_cam;
|
---|
[5c9d193] | 784 |
|
---|
[d9f99b2] | 785 | float d = -glm::dot(points[0], normal);
|
---|
[5c9d193] | 786 | cout << "d: " << d << endl;
|
---|
| 787 |
|
---|
| 788 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
| 789 | cout << "t: " << t << endl;
|
---|
| 790 |
|
---|
| 791 | vec3 intersection = local_cam + t*local_ray;
|
---|
| 792 | printVector("Intersection", intersection);
|
---|
| 793 |
|
---|
[d9f99b2] | 794 | if (insideTriangle(intersection, points)) {
|
---|
[e82692b] | 795 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
| 796 | return true;
|
---|
| 797 | } else {
|
---|
| 798 | return false;
|
---|
| 799 | }
|
---|
[5c9d193] | 800 | }
|
---|
| 801 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
[d9f99b2] | 802 | vec3 v21 = triangle_points[1] - triangle_points[0];
|
---|
| 803 | vec3 v31 = triangle_points[2] - triangle_points[0];
|
---|
| 804 | vec3 pv1 = p - triangle_points[0];
|
---|
[33a9664] | 805 |
|
---|
| 806 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 807 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 808 |
|
---|
| 809 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 810 | }
|
---|
[d12d003] | 811 |
|
---|
| 812 | void printVector(string label, vec3 v) {
|
---|
| 813 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
| 814 | }
|
---|
[b73cb3b] | 815 |
|
---|
| 816 | void print4DVector(string label, vec4 v) {
|
---|
| 817 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
| 818 | }
|
---|
[c1ca5b5] | 819 |
|
---|
[cffca4d] | 820 | // The easiest thing here seems to be to set all the colors we want in a CPU array once per frame,
|
---|
| 821 | // them copy it over to the GPU and make one draw call
|
---|
| 822 | // This method also easily allows us to use any colors we want for each shape.
|
---|
| 823 | // Try to compare the frame times for the current method and the new one
|
---|
| 824 | //
|
---|
| 825 | // Alternatively, I have one large color buffer that has selected and unselected colors
|
---|
| 826 | // Then, when I know which object is selected, I can use glVertexAttribPointer to decide
|
---|
| 827 | // whether to use the selected or unselected color for it
|
---|
| 828 |
|
---|
| 829 | // I'll have to think of the best way to do something similar when using
|
---|
| 830 | // one draw call. Probably, in that case, I'll use one draw call for all unselectable objects
|
---|
| 831 | // and use the approach mentioned above for all selectable objects.
|
---|
| 832 | // I can have one colors vbo for unselectable objects and another for selected+unselected colors
|
---|
| 833 | // of selectable objects
|
---|
| 834 |
|
---|
| 835 | // For both colored and textured objects, using a single draw call will only work for objects
|
---|
| 836 | // that don't change state (i.e. don't change colors or switch from colored to textured).
|
---|
| 837 |
|
---|
| 838 | // This means I'll probably have one call for static colored objects, one call for static textured objects,
|
---|
| 839 | // a loop of calls for dynamic currently colored objects, and a loop of calls for dynamic currently textured objects.
|
---|
| 840 | // This will increase if I add new shaders since I'll need either one new call or one new loop of calls per shader
|
---|
| 841 |
|
---|
[93462c6] | 842 | void renderScene(vector<SceneObject>& objects,
|
---|
[cffca4d] | 843 | GLuint color_sp, GLuint texture_sp,
|
---|
[93462c6] | 844 | GLuint vao1, GLuint vao2,
|
---|
| 845 | GLuint shader1_mat_loc, GLuint shader2_mat_loc,
|
---|
| 846 | GLuint points_vbo, GLuint normals_vbo,
|
---|
| 847 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
| 848 | SceneObject* selectedObject) {
|
---|
| 849 |
|
---|
[cffca4d] | 850 | vector<int> colored_objs, selected_objs, textured_objs, static_colored_objs, static_textured_objs;
|
---|
[93462c6] | 851 |
|
---|
[cffca4d] | 852 | // group scene objects by shader and vbo
|
---|
[93462c6] | 853 | for (unsigned int i = 0; i < objects.size(); i++) {
|
---|
[cffca4d] | 854 | if (selectedObject == &objects[i]) {
|
---|
| 855 | selected_objs.push_back(i);
|
---|
| 856 | } else if (objects[i].shader_program == color_sp) {
|
---|
| 857 | colored_objs.push_back(i);
|
---|
| 858 | } else if (objects[i].shader_program == texture_sp) {
|
---|
| 859 | textured_objs.push_back(i);
|
---|
[93462c6] | 860 | }
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | vector<int>::iterator it;
|
---|
| 864 |
|
---|
[cffca4d] | 865 | glUseProgram(color_sp);
|
---|
[93462c6] | 866 | glBindVertexArray(vao1);
|
---|
| 867 |
|
---|
[cffca4d] | 868 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 869 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 870 |
|
---|
| 871 | for (it = colored_objs.begin(); it != colored_objs.end(); it++) {
|
---|
[93462c6] | 872 | glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
| 873 |
|
---|
[cffca4d] | 874 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
| 875 | }
|
---|
[93462c6] | 876 |
|
---|
[cffca4d] | 877 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 878 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[93462c6] | 879 |
|
---|
[cffca4d] | 880 | for (it = selected_objs.begin(); it != selected_objs.end(); it++) {
|
---|
| 881 | glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
[93462c6] | 882 |
|
---|
[cffca4d] | 883 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
[93462c6] | 884 | }
|
---|
| 885 |
|
---|
[cffca4d] | 886 | glUseProgram(texture_sp);
|
---|
[93462c6] | 887 | glBindVertexArray(vao2);
|
---|
| 888 |
|
---|
[cffca4d] | 889 | for (it = textured_objs.begin(); it != textured_objs.end(); it++) {
|
---|
[93462c6] | 890 | glUniformMatrix4fv(shader2_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
| 891 |
|
---|
[cffca4d] | 892 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
[93462c6] | 893 | }
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | void renderSceneGui() {
|
---|
[c1ca5b5] | 897 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 898 |
|
---|
| 899 | // 1. Show a simple window.
|
---|
| 900 | // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
|
---|
[5b3462b] | 901 | /*
|
---|
[c1ca5b5] | 902 | {
|
---|
| 903 | static float f = 0.0f;
|
---|
| 904 | static int counter = 0;
|
---|
| 905 | ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
|
---|
| 906 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
---|
| 907 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
---|
| 908 |
|
---|
| 909 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
|
---|
| 910 | ImGui::Checkbox("Another Window", &show_another_window);
|
---|
| 911 |
|
---|
| 912 | if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
|
---|
| 913 | counter++;
|
---|
| 914 | ImGui::SameLine();
|
---|
| 915 | ImGui::Text("counter = %d", counter);
|
---|
| 916 |
|
---|
| 917 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
---|
| 918 | }
|
---|
[5b3462b] | 919 | */
|
---|
[c1ca5b5] | 920 |
|
---|
[5b3462b] | 921 | {
|
---|
| 922 | ImGui::SetNextWindowSize(ImVec2(85, 22), ImGuiCond_Once);
|
---|
| 923 | ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
|
---|
[f0cc877] | 924 | ImGui::Begin("WndStats", NULL,
|
---|
| 925 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 926 | ImGuiWindowFlags_NoResize |
|
---|
| 927 | ImGuiWindowFlags_NoMove);
|
---|
[5b3462b] | 928 | ImGui::Text("Score: ???");
|
---|
[c1ca5b5] | 929 | ImGui::End();
|
---|
| 930 | }
|
---|
| 931 |
|
---|
[5b3462b] | 932 | {
|
---|
| 933 | ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
|
---|
| 934 | ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
|
---|
[f0cc877] | 935 | ImGui::Begin("WndMenubar", NULL,
|
---|
| 936 | ImGuiWindowFlags_NoTitleBar |
|
---|
[5b3462b] | 937 | ImGuiWindowFlags_NoResize |
|
---|
| 938 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 939 | ImGui::InvisibleButton("", ImVec2(155, 18));
|
---|
[5b3462b] | 940 | ImGui::SameLine();
|
---|
[93462c6] | 941 | if (ImGui::Button("Main Menu")) {
|
---|
| 942 | events.push(EVENT_GO_TO_MAIN_MENU);
|
---|
[5b3462b] | 943 | }
|
---|
| 944 | ImGui::End();
|
---|
[c1ca5b5] | 945 | }
|
---|
| 946 |
|
---|
[93462c6] | 947 | ImGui::Render();
|
---|
| 948 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | void renderMainMenu() {
|
---|
| 952 | }
|
---|
| 953 |
|
---|
| 954 | void renderMainMenuGui() {
|
---|
| 955 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 956 |
|
---|
[f0cc877] | 957 | {
|
---|
| 958 | int padding = 4;
|
---|
| 959 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
[93462c6] | 960 | ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once);
|
---|
[f0cc877] | 961 | ImGui::Begin("WndMain", NULL,
|
---|
| 962 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 963 | ImGuiWindowFlags_NoResize |
|
---|
| 964 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 965 |
|
---|
| 966 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
| 967 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
| 968 | ImGui::SameLine();
|
---|
| 969 | if (ImGui::Button("New Game")) {
|
---|
| 970 | events.push(EVENT_GO_TO_GAME);
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
| 974 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
| 975 | ImGui::SameLine();
|
---|
| 976 | if (ImGui::Button("Quit")) {
|
---|
| 977 | events.push(EVENT_QUIT);
|
---|
| 978 | }
|
---|
| 979 |
|
---|
[f0cc877] | 980 | ImGui::End();
|
---|
| 981 | }
|
---|
| 982 |
|
---|
[c1ca5b5] | 983 | ImGui::Render();
|
---|
| 984 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 985 | }
|
---|