[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;
|
---|
[e165b85] | 397 | GLsizeiptr ubo_buffer_size = 0;
|
---|
[07ed460] | 398 |
|
---|
| 399 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 400 | points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
|
---|
| 401 | textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
[e165b85] | 402 | ubo_buffer_size += 16 * sizeof(GLfloat);
|
---|
[07ed460] | 403 | }
|
---|
[19c9338] | 404 |
|
---|
[8b7cfcf] | 405 | GLuint points_vbo = 0;
|
---|
| 406 | glGenBuffers(1, &points_vbo);
|
---|
| 407 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[07ed460] | 408 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
[05e43cf] | 409 |
|
---|
[07ed460] | 410 | offset = 0;
|
---|
| 411 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 412 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]);
|
---|
| 413 | offset += obj_it->points.size() * sizeof(GLfloat);
|
---|
| 414 | }
|
---|
[516668e] | 415 |
|
---|
[8b7cfcf] | 416 | GLuint colors_vbo = 0;
|
---|
| 417 | glGenBuffers(1, &colors_vbo);
|
---|
| 418 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[07ed460] | 419 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 420 |
|
---|
| 421 | offset = 0;
|
---|
| 422 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 423 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]);
|
---|
| 424 | offset += obj_it->colors.size() * sizeof(GLfloat);
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | GLuint selected_colors_vbo = 0;
|
---|
| 428 | glGenBuffers(1, &selected_colors_vbo);
|
---|
| 429 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 430 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 431 |
|
---|
| 432 | offset = 0;
|
---|
| 433 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 434 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]);
|
---|
| 435 | offset += obj_it->selected_colors.size() * sizeof(GLfloat);
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | GLuint texcoords_vbo = 0;
|
---|
| 439 | glGenBuffers(1, &texcoords_vbo);
|
---|
| 440 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 441 | glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 442 |
|
---|
| 443 | offset = 0;
|
---|
| 444 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 445 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]);
|
---|
| 446 | offset += obj_it->texcoords.size() * sizeof(GLfloat);
|
---|
| 447 | }
|
---|
[8b7cfcf] | 448 |
|
---|
[9dd2eb7] | 449 | GLuint normals_vbo = 0;
|
---|
| 450 | glGenBuffers(1, &normals_vbo);
|
---|
| 451 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 452 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 453 |
|
---|
| 454 | offset = 0;
|
---|
| 455 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 456 | glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->normals.size() * sizeof(GLfloat), &obj_it->normals[0]);
|
---|
| 457 | offset += obj_it->normals.size() * sizeof(GLfloat);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[e165b85] | 460 | /*
|
---|
| 461 | GLuint ubo = 0;
|
---|
| 462 | glGenBuffers(1, &ubo);
|
---|
| 463 |
|
---|
| 464 | //glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
| 465 | //glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size/2, NULL, GL_DYNAMIC_DRAW);
|
---|
| 466 |
|
---|
| 467 | offset = 0;
|
---|
| 468 | for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
|
---|
| 469 | //glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(obj_it->model_mat), value_ptr(obj_it->model_mat));
|
---|
| 470 | //glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(obj_it->model_mat), value_ptr(objects[0].model_mat));
|
---|
| 471 | offset += 16 * sizeof(GLfloat);
|
---|
| 472 | }
|
---|
| 473 | //glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
---|
| 474 | */
|
---|
| 475 |
|
---|
[644a2e4] | 476 | GLuint vao = 0;
|
---|
[516668e] | 477 | glGenVertexArrays(1, &vao);
|
---|
| 478 | glBindVertexArray(vao);
|
---|
| 479 |
|
---|
[8b7cfcf] | 480 | glEnableVertexAttribArray(0);
|
---|
| 481 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 482 | glEnableVertexAttribArray(2);
|
---|
[644a2e4] | 483 |
|
---|
[cffca4d] | 484 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 485 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 486 |
|
---|
| 487 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 488 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 489 |
|
---|
[485424b] | 490 | GLuint vao2 = 0;
|
---|
| 491 | glGenVertexArrays(1, &vao2);
|
---|
| 492 | glBindVertexArray(vao2);
|
---|
[644a2e4] | 493 |
|
---|
[485424b] | 494 | glEnableVertexAttribArray(0);
|
---|
| 495 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 496 | glEnableVertexAttribArray(2);
|
---|
[8b7cfcf] | 497 |
|
---|
[cffca4d] | 498 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 499 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[1a530df] | 500 |
|
---|
[cffca4d] | 501 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 502 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 503 |
|
---|
| 504 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 505 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[644a2e4] | 506 |
|
---|
[93baa0e] | 507 | float speed = 1.0f;
|
---|
| 508 | float last_position = 0.0f;
|
---|
| 509 |
|
---|
[7ee66ea] | 510 | float cam_speed = 1.0f;
|
---|
[201e2f8] | 511 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[7ee66ea] | 512 |
|
---|
[b73cb3b] | 513 | // glm::lookAt can create the view matrix
|
---|
| 514 | // glm::perspective can create the projection matrix
|
---|
| 515 |
|
---|
| 516 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
[64a70f4] | 517 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[7ee66ea] | 518 |
|
---|
[c62eee6] | 519 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 520 | mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[c62eee6] | 521 | view_mat = R*T;
|
---|
[7ee66ea] | 522 |
|
---|
| 523 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 524 | float aspect = (float)width / (float)height;
|
---|
| 525 |
|
---|
[d12d003] | 526 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 527 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 528 | float Sy = NEAR_CLIP / range;
|
---|
| 529 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 530 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[7ee66ea] | 531 |
|
---|
[c62eee6] | 532 | float proj_arr[] = {
|
---|
[7ee66ea] | 533 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 534 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 535 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 536 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 537 | };
|
---|
[c62eee6] | 538 | proj_mat = make_mat4(proj_arr);
|
---|
[7ee66ea] | 539 |
|
---|
[cffca4d] | 540 | GLuint model_test_loc = glGetUniformLocation(color_sp, "model");
|
---|
| 541 | GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
|
---|
| 542 | GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
|
---|
[e165b85] | 543 | GLuint ub_index = glGetUniformBlockIndex(color_sp, "shader_data");
|
---|
| 544 | cout << "UBO index: " << ub_index << endl;
|
---|
[7ee66ea] | 545 |
|
---|
[cffca4d] | 546 | GLuint model_mat_loc = glGetUniformLocation(texture_sp, "model");
|
---|
| 547 | GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
|
---|
| 548 | GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
|
---|
[19c9338] | 549 |
|
---|
[cffca4d] | 550 | glUseProgram(color_sp);
|
---|
[19c9338] | 551 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 552 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[485424b] | 553 |
|
---|
[e165b85] | 554 | //glUniformBlockBinding(color_sp, ub_index, 0);
|
---|
| 555 |
|
---|
[cffca4d] | 556 | glUseProgram(texture_sp);
|
---|
[19c9338] | 557 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 558 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[7ee66ea] | 559 |
|
---|
| 560 | bool cam_moved = false;
|
---|
| 561 |
|
---|
[046ce72] | 562 | int frame_count = 0;
|
---|
[f70ab75] | 563 | double elapsed_seconds_fps = 0.0f;
|
---|
[93baa0e] | 564 | double previous_seconds = glfwGetTime();
|
---|
[046ce72] | 565 |
|
---|
[9dd2eb7] | 566 | // This draws wireframes. Useful for seeing separate faces and occluded objects.
|
---|
| 567 | //glPolygonMode(GL_FRONT, GL_LINE);
|
---|
| 568 |
|
---|
[1c81bf0] | 569 | // disable vsync to see real framerate
|
---|
| 570 | //glfwSwapInterval(0);
|
---|
| 571 |
|
---|
[93462c6] | 572 | State curState = STATE_MAIN_MENU;
|
---|
| 573 |
|
---|
[5b3462b] | 574 | while (!glfwWindowShouldClose(window) && isRunning) {
|
---|
[93baa0e] | 575 | double current_seconds = glfwGetTime();
|
---|
| 576 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 577 | previous_seconds = current_seconds;
|
---|
| 578 |
|
---|
[046ce72] | 579 | elapsed_seconds_fps += elapsed_seconds;
|
---|
| 580 | if (elapsed_seconds_fps > 0.25f) {
|
---|
| 581 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
| 582 | cout << "FPS: " << fps << endl;
|
---|
| 583 |
|
---|
| 584 | frame_count = 0;
|
---|
| 585 | elapsed_seconds_fps = 0.0f;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | frame_count++;
|
---|
| 589 |
|
---|
[93baa0e] | 590 | if (fabs(last_position) > 1.0f) {
|
---|
| 591 | speed = -speed;
|
---|
| 592 | }
|
---|
| 593 |
|
---|
[baa5848] | 594 | // Handle events (Ideally, move all event-handling code
|
---|
| 595 | // before the render code)
|
---|
| 596 |
|
---|
| 597 | clickedObject = NULL;
|
---|
| 598 | glfwPollEvents();
|
---|
| 599 |
|
---|
[93462c6] | 600 | while (!events.empty()) {
|
---|
| 601 | switch (events.front()) {
|
---|
| 602 | case EVENT_GO_TO_MAIN_MENU:
|
---|
| 603 | curState = STATE_MAIN_MENU;
|
---|
| 604 | break;
|
---|
| 605 | case EVENT_GO_TO_GAME:
|
---|
| 606 | curState = STATE_GAME;
|
---|
| 607 | break;
|
---|
| 608 | case EVENT_QUIT:
|
---|
| 609 | isRunning = false;
|
---|
| 610 | break;
|
---|
| 611 | }
|
---|
| 612 | events.pop();
|
---|
[147ac6d] | 613 | }
|
---|
[93462c6] | 614 |
|
---|
| 615 | if (curState == STATE_GAME) {
|
---|
| 616 | if (clickedObject == &objects[0]) {
|
---|
| 617 | selectedObject = &objects[0];
|
---|
| 618 | }
|
---|
| 619 | if (clickedObject == &objects[1]) {
|
---|
| 620 | selectedObject = &objects[1];
|
---|
| 621 | }
|
---|
[baa5848] | 622 | }
|
---|
[33a9664] | 623 |
|
---|
[7ee66ea] | 624 | /*
|
---|
[93baa0e] | 625 | model[12] = last_position + speed*elapsed_seconds;
|
---|
| 626 | last_position = model[12];
|
---|
[7ee66ea] | 627 | */
|
---|
[93baa0e] | 628 |
|
---|
| 629 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[485424b] | 630 |
|
---|
[93462c6] | 631 | switch (curState) {
|
---|
| 632 | case STATE_MAIN_MENU:
|
---|
| 633 | renderMainMenu();
|
---|
| 634 | renderMainMenuGui();
|
---|
| 635 | break;
|
---|
| 636 | case STATE_GAME:
|
---|
| 637 | renderScene(objects,
|
---|
[cffca4d] | 638 | color_sp, texture_sp,
|
---|
[93462c6] | 639 | vao, vao2,
|
---|
| 640 | model_test_loc, model_mat_loc,
|
---|
| 641 | points_vbo, normals_vbo,
|
---|
| 642 | colors_vbo, texcoords_vbo, selected_colors_vbo,
|
---|
| 643 | selectedObject);
|
---|
| 644 | renderSceneGui();
|
---|
| 645 | break;
|
---|
[baa5848] | 646 | }
|
---|
[df652d5] | 647 |
|
---|
[644a2e4] | 648 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 649 |
|
---|
| 650 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 651 | glfwSetWindowShouldClose(window, 1);
|
---|
| 652 | }
|
---|
[7ee66ea] | 653 |
|
---|
| 654 | float dist = cam_speed * elapsed_seconds;
|
---|
| 655 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
[c62eee6] | 656 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
| 657 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 658 | cam_moved = true;
|
---|
| 659 | }
|
---|
| 660 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
[c62eee6] | 661 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
| 662 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
[7ee66ea] | 663 | cam_moved = true;
|
---|
| 664 | }
|
---|
| 665 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
[c62eee6] | 666 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
| 667 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 668 | cam_moved = true;
|
---|
| 669 | }
|
---|
| 670 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
[c62eee6] | 671 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
| 672 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
[7ee66ea] | 673 | cam_moved = true;
|
---|
| 674 | }
|
---|
| 675 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
| 676 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 677 | cam_moved = true;
|
---|
| 678 | }
|
---|
| 679 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
| 680 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 681 | cam_moved = true;
|
---|
| 682 | }
|
---|
| 683 | if (cam_moved) {
|
---|
[c62eee6] | 684 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 685 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[267c4c5] | 686 | view_mat = R*T;
|
---|
[7ee66ea] | 687 |
|
---|
[cffca4d] | 688 | glUseProgram(color_sp);
|
---|
[267c4c5] | 689 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 690 |
|
---|
[cffca4d] | 691 | glUseProgram(texture_sp);
|
---|
[7ee66ea] | 692 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[267c4c5] | 693 |
|
---|
[7ee66ea] | 694 | cam_moved = false;
|
---|
| 695 | }
|
---|
[644a2e4] | 696 | }
|
---|
| 697 |
|
---|
[c1ca5b5] | 698 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
| 699 | ImGui::DestroyContext();
|
---|
| 700 |
|
---|
| 701 | glfwDestroyWindow(window);
|
---|
[5272b6b] | 702 | glfwTerminate();
|
---|
[c1ca5b5] | 703 |
|
---|
[5272b6b] | 704 | return 0;
|
---|
| 705 | }
|
---|
[ec4456b] | 706 |
|
---|
| 707 | GLuint loadShader(GLenum type, string file) {
|
---|
| 708 | cout << "Loading shader from file " << file << endl;
|
---|
| 709 |
|
---|
| 710 | ifstream shaderFile(file);
|
---|
| 711 | GLuint shaderId = 0;
|
---|
| 712 |
|
---|
| 713 | if (shaderFile.is_open()) {
|
---|
| 714 | string line, shaderString;
|
---|
| 715 |
|
---|
| 716 | while(getline(shaderFile, line)) {
|
---|
| 717 | shaderString += line + "\n";
|
---|
| 718 | }
|
---|
| 719 | shaderFile.close();
|
---|
| 720 | const char* shaderCString = shaderString.c_str();
|
---|
| 721 |
|
---|
| 722 | shaderId = glCreateShader(type);
|
---|
| 723 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 724 | glCompileShader(shaderId);
|
---|
| 725 |
|
---|
| 726 | cout << "Loaded successfully" << endl;
|
---|
| 727 | } else {
|
---|
[e856d62] | 728 | cout << "Failed to load the file" << endl;
|
---|
[ec4456b] | 729 | }
|
---|
| 730 |
|
---|
| 731 | return shaderId;
|
---|
| 732 | }
|
---|
[485424b] | 733 |
|
---|
| 734 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 735 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 736 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 737 |
|
---|
| 738 | GLuint shader_program = glCreateProgram();
|
---|
| 739 | glAttachShader(shader_program, vs);
|
---|
| 740 | glAttachShader(shader_program, fs);
|
---|
| 741 |
|
---|
| 742 | glLinkProgram(shader_program);
|
---|
| 743 |
|
---|
| 744 | return shader_program;
|
---|
| 745 | }
|
---|
| 746 |
|
---|
| 747 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 748 | int n;
|
---|
[e856d62] | 749 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
[485424b] | 750 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
[e856d62] | 751 |
|
---|
| 752 | int width_in_bytes = *x * 4;
|
---|
| 753 | unsigned char *top = NULL;
|
---|
| 754 | unsigned char *bottom = NULL;
|
---|
| 755 | unsigned char temp = 0;
|
---|
| 756 | int half_height = *y / 2;
|
---|
| 757 |
|
---|
| 758 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
| 759 | for (int row = 0; row < half_height; row++) {
|
---|
| 760 | top = image_data + row * width_in_bytes;
|
---|
| 761 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
| 762 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
| 763 | temp = *top;
|
---|
| 764 | *top = *bottom;
|
---|
| 765 | *bottom = temp;
|
---|
| 766 | top++;
|
---|
| 767 | bottom++;
|
---|
| 768 | }
|
---|
| 769 | }
|
---|
| 770 |
|
---|
[485424b] | 771 | if (!image_data) {
|
---|
| 772 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
| 773 | }
|
---|
[e856d62] | 774 |
|
---|
| 775 | // Not Power-of-2 check
|
---|
| 776 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
| 777 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[485424b] | 780 | return image_data;
|
---|
| 781 | }
|
---|
[33a9664] | 782 |
|
---|
[d9f99b2] | 783 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
[5c9d193] | 784 | // LINE EQUATION: P = O + Dt
|
---|
[b73cb3b] | 785 | // O = cam
|
---|
[5c9d193] | 786 | // D = ray_world
|
---|
| 787 |
|
---|
[b73cb3b] | 788 | // PLANE EQUATION: P dot n + d = 0
|
---|
| 789 | // n is the normal vector
|
---|
| 790 | // d is the offset from the origin
|
---|
[5c9d193] | 791 |
|
---|
| 792 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
[d9f99b2] | 793 | vec3 v1 = points[1] - points[0];
|
---|
| 794 | vec3 v2 = points[2] - points[0];
|
---|
[5c9d193] | 795 |
|
---|
| 796 | 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] | 797 |
|
---|
| 798 | print4DVector("Full world ray", world_ray);
|
---|
[5c9d193] | 799 |
|
---|
| 800 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
| 801 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
| 802 |
|
---|
[b73cb3b] | 803 | local_ray = local_ray - local_cam;
|
---|
[5c9d193] | 804 |
|
---|
[d9f99b2] | 805 | float d = -glm::dot(points[0], normal);
|
---|
[5c9d193] | 806 | cout << "d: " << d << endl;
|
---|
| 807 |
|
---|
| 808 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
| 809 | cout << "t: " << t << endl;
|
---|
| 810 |
|
---|
| 811 | vec3 intersection = local_cam + t*local_ray;
|
---|
| 812 | printVector("Intersection", intersection);
|
---|
| 813 |
|
---|
[d9f99b2] | 814 | if (insideTriangle(intersection, points)) {
|
---|
[e82692b] | 815 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
| 816 | return true;
|
---|
| 817 | } else {
|
---|
| 818 | return false;
|
---|
| 819 | }
|
---|
[5c9d193] | 820 | }
|
---|
| 821 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
[d9f99b2] | 822 | vec3 v21 = triangle_points[1] - triangle_points[0];
|
---|
| 823 | vec3 v31 = triangle_points[2] - triangle_points[0];
|
---|
| 824 | vec3 pv1 = p - triangle_points[0];
|
---|
[33a9664] | 825 |
|
---|
| 826 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 827 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 828 |
|
---|
| 829 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 830 | }
|
---|
[d12d003] | 831 |
|
---|
| 832 | void printVector(string label, vec3 v) {
|
---|
| 833 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
| 834 | }
|
---|
[b73cb3b] | 835 |
|
---|
| 836 | void print4DVector(string label, vec4 v) {
|
---|
| 837 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
| 838 | }
|
---|
[c1ca5b5] | 839 |
|
---|
[cffca4d] | 840 | // The easiest thing here seems to be to set all the colors we want in a CPU array once per frame,
|
---|
| 841 | // them copy it over to the GPU and make one draw call
|
---|
| 842 | // This method also easily allows us to use any colors we want for each shape.
|
---|
| 843 | // Try to compare the frame times for the current method and the new one
|
---|
| 844 | //
|
---|
| 845 | // Alternatively, I have one large color buffer that has selected and unselected colors
|
---|
| 846 | // Then, when I know which object is selected, I can use glVertexAttribPointer to decide
|
---|
| 847 | // whether to use the selected or unselected color for it
|
---|
| 848 |
|
---|
| 849 | // I'll have to think of the best way to do something similar when using
|
---|
| 850 | // one draw call. Probably, in that case, I'll use one draw call for all unselectable objects
|
---|
| 851 | // and use the approach mentioned above for all selectable objects.
|
---|
| 852 | // I can have one colors vbo for unselectable objects and another for selected+unselected colors
|
---|
| 853 | // of selectable objects
|
---|
| 854 |
|
---|
| 855 | // For both colored and textured objects, using a single draw call will only work for objects
|
---|
| 856 | // that don't change state (i.e. don't change colors or switch from colored to textured).
|
---|
| 857 |
|
---|
| 858 | // This means I'll probably have one call for static colored objects, one call for static textured objects,
|
---|
| 859 | // a loop of calls for dynamic currently colored objects, and a loop of calls for dynamic currently textured objects.
|
---|
| 860 | // This will increase if I add new shaders since I'll need either one new call or one new loop of calls per shader
|
---|
| 861 |
|
---|
[93462c6] | 862 | void renderScene(vector<SceneObject>& objects,
|
---|
[cffca4d] | 863 | GLuint color_sp, GLuint texture_sp,
|
---|
[93462c6] | 864 | GLuint vao1, GLuint vao2,
|
---|
| 865 | GLuint shader1_mat_loc, GLuint shader2_mat_loc,
|
---|
| 866 | GLuint points_vbo, GLuint normals_vbo,
|
---|
| 867 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
| 868 | SceneObject* selectedObject) {
|
---|
| 869 |
|
---|
[cffca4d] | 870 | vector<int> colored_objs, selected_objs, textured_objs, static_colored_objs, static_textured_objs;
|
---|
[93462c6] | 871 |
|
---|
[cffca4d] | 872 | // group scene objects by shader and vbo
|
---|
[93462c6] | 873 | for (unsigned int i = 0; i < objects.size(); i++) {
|
---|
[cffca4d] | 874 | if (selectedObject == &objects[i]) {
|
---|
| 875 | selected_objs.push_back(i);
|
---|
| 876 | } else if (objects[i].shader_program == color_sp) {
|
---|
| 877 | colored_objs.push_back(i);
|
---|
| 878 | } else if (objects[i].shader_program == texture_sp) {
|
---|
| 879 | textured_objs.push_back(i);
|
---|
[93462c6] | 880 | }
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 | vector<int>::iterator it;
|
---|
| 884 |
|
---|
[cffca4d] | 885 | glUseProgram(color_sp);
|
---|
[93462c6] | 886 | glBindVertexArray(vao1);
|
---|
| 887 |
|
---|
[cffca4d] | 888 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 889 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 890 |
|
---|
| 891 | for (it = colored_objs.begin(); it != colored_objs.end(); it++) {
|
---|
[93462c6] | 892 | glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
| 893 |
|
---|
[cffca4d] | 894 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
| 895 | }
|
---|
[93462c6] | 896 |
|
---|
[cffca4d] | 897 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 898 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[93462c6] | 899 |
|
---|
[cffca4d] | 900 | for (it = selected_objs.begin(); it != selected_objs.end(); it++) {
|
---|
| 901 | glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
[93462c6] | 902 |
|
---|
[cffca4d] | 903 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
[93462c6] | 904 | }
|
---|
| 905 |
|
---|
[cffca4d] | 906 | glUseProgram(texture_sp);
|
---|
[93462c6] | 907 | glBindVertexArray(vao2);
|
---|
| 908 |
|
---|
[cffca4d] | 909 | for (it = textured_objs.begin(); it != textured_objs.end(); it++) {
|
---|
[93462c6] | 910 | glUniformMatrix4fv(shader2_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
|
---|
| 911 |
|
---|
[cffca4d] | 912 | glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
|
---|
[93462c6] | 913 | }
|
---|
| 914 | }
|
---|
| 915 |
|
---|
| 916 | void renderSceneGui() {
|
---|
[c1ca5b5] | 917 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 918 |
|
---|
| 919 | // 1. Show a simple window.
|
---|
| 920 | // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
|
---|
[5b3462b] | 921 | /*
|
---|
[c1ca5b5] | 922 | {
|
---|
| 923 | static float f = 0.0f;
|
---|
| 924 | static int counter = 0;
|
---|
| 925 | ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
|
---|
| 926 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
---|
| 927 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
---|
| 928 |
|
---|
| 929 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
|
---|
| 930 | ImGui::Checkbox("Another Window", &show_another_window);
|
---|
| 931 |
|
---|
| 932 | if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
|
---|
| 933 | counter++;
|
---|
| 934 | ImGui::SameLine();
|
---|
| 935 | ImGui::Text("counter = %d", counter);
|
---|
| 936 |
|
---|
| 937 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
---|
| 938 | }
|
---|
[5b3462b] | 939 | */
|
---|
[c1ca5b5] | 940 |
|
---|
[5b3462b] | 941 | {
|
---|
| 942 | ImGui::SetNextWindowSize(ImVec2(85, 22), ImGuiCond_Once);
|
---|
| 943 | ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
|
---|
[f0cc877] | 944 | ImGui::Begin("WndStats", NULL,
|
---|
| 945 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 946 | ImGuiWindowFlags_NoResize |
|
---|
| 947 | ImGuiWindowFlags_NoMove);
|
---|
[5b3462b] | 948 | ImGui::Text("Score: ???");
|
---|
[c1ca5b5] | 949 | ImGui::End();
|
---|
| 950 | }
|
---|
| 951 |
|
---|
[5b3462b] | 952 | {
|
---|
| 953 | ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
|
---|
| 954 | ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
|
---|
[f0cc877] | 955 | ImGui::Begin("WndMenubar", NULL,
|
---|
| 956 | ImGuiWindowFlags_NoTitleBar |
|
---|
[5b3462b] | 957 | ImGuiWindowFlags_NoResize |
|
---|
| 958 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 959 | ImGui::InvisibleButton("", ImVec2(155, 18));
|
---|
[5b3462b] | 960 | ImGui::SameLine();
|
---|
[93462c6] | 961 | if (ImGui::Button("Main Menu")) {
|
---|
| 962 | events.push(EVENT_GO_TO_MAIN_MENU);
|
---|
[5b3462b] | 963 | }
|
---|
| 964 | ImGui::End();
|
---|
[c1ca5b5] | 965 | }
|
---|
| 966 |
|
---|
[93462c6] | 967 | ImGui::Render();
|
---|
| 968 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 969 | }
|
---|
| 970 |
|
---|
| 971 | void renderMainMenu() {
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | void renderMainMenuGui() {
|
---|
| 975 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 976 |
|
---|
[f0cc877] | 977 | {
|
---|
| 978 | int padding = 4;
|
---|
| 979 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
[93462c6] | 980 | ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once);
|
---|
[f0cc877] | 981 | ImGui::Begin("WndMain", NULL,
|
---|
| 982 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 983 | ImGuiWindowFlags_NoResize |
|
---|
| 984 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 985 |
|
---|
| 986 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
| 987 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
| 988 | ImGui::SameLine();
|
---|
| 989 | if (ImGui::Button("New Game")) {
|
---|
| 990 | events.push(EVENT_GO_TO_GAME);
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
| 994 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
| 995 | ImGui::SameLine();
|
---|
| 996 | if (ImGui::Button("Quit")) {
|
---|
| 997 | events.push(EVENT_QUIT);
|
---|
| 998 | }
|
---|
| 999 |
|
---|
[f0cc877] | 1000 | ImGui::End();
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
[c1ca5b5] | 1003 | ImGui::Render();
|
---|
| 1004 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 1005 | }
|
---|