[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>
|
---|
[0d5c100] | 34 | #include <map>
|
---|
[22b2c37] | 35 |
|
---|
[5272b6b] | 36 | using namespace std;
|
---|
[7ee66ea] | 37 | using namespace glm;
|
---|
| 38 |
|
---|
[df652d5] | 39 | struct SceneObject {
|
---|
[d9f99b2] | 40 | unsigned int id;
|
---|
[5c403fe] | 41 | mat4 model_mat, model_base, model_transform;
|
---|
[baa5848] | 42 | GLuint shader_program;
|
---|
[05e43cf] | 43 | unsigned int num_points;
|
---|
[cffca4d] | 44 | GLint vertex_vbo_offset;
|
---|
[5c403fe] | 45 | GLint ubo_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 |
|
---|
[f7d35da] | 64 | #define NUM_KEYS (512)
|
---|
| 65 | #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 (maybe make this a const instead)
|
---|
| 66 |
|
---|
| 67 | const int KEY_STATE_UNCHANGED = -1;
|
---|
[485424b] | 68 | const bool FULLSCREEN = false;
|
---|
[f7d35da] | 69 | const bool SHOW_FPS = false;
|
---|
| 70 | const bool DISABLE_VSYNC = false; // disable vsync to see real framerate
|
---|
| 71 | unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
|
---|
| 72 |
|
---|
| 73 | int key_state[NUM_KEYS];
|
---|
| 74 | bool key_pressed[NUM_KEYS];
|
---|
[14ff67c] | 75 |
|
---|
[c62eee6] | 76 | int width = 640;
|
---|
| 77 | int height = 480;
|
---|
| 78 |
|
---|
[c1ca5b5] | 79 | double fps;
|
---|
| 80 |
|
---|
[c62eee6] | 81 | vec3 cam_pos;
|
---|
| 82 |
|
---|
| 83 | mat4 view_mat;
|
---|
| 84 | mat4 proj_mat;
|
---|
[5272b6b] | 85 |
|
---|
[df652d5] | 86 | vector<SceneObject> objects;
|
---|
[93462c6] | 87 | queue<Event> events;
|
---|
[df652d5] | 88 |
|
---|
[147ac6d] | 89 | SceneObject* clickedObject = NULL;
|
---|
[0d5c100] | 90 | SceneObject* selectedObject = NULL;
|
---|
[147ac6d] | 91 |
|
---|
[c1ca5b5] | 92 | float NEAR_CLIP = 0.1f;
|
---|
| 93 | float FAR_CLIP = 100.0f;
|
---|
| 94 |
|
---|
[5b3462b] | 95 | // Should really have some array or struct of UI-related variables
|
---|
| 96 | bool isRunning = true;
|
---|
| 97 |
|
---|
[c1ca5b5] | 98 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
---|
[046ce72] | 99 |
|
---|
[4f3262f] | 100 | void glfw_error_callback(int error, const char* description);
|
---|
| 101 |
|
---|
| 102 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
|
---|
[f7d35da] | 103 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
---|
[4f3262f] | 104 |
|
---|
[d9f99b2] | 105 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
[5c9d193] | 106 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
[33a9664] | 107 |
|
---|
[ec4456b] | 108 | GLuint loadShader(GLenum type, string file);
|
---|
[485424b] | 109 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
| 110 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
[ec4456b] | 111 |
|
---|
[d12d003] | 112 | void printVector(string label, vec3 v);
|
---|
[b73cb3b] | 113 | void print4DVector(string label, vec4 v);
|
---|
[d12d003] | 114 |
|
---|
[f9a242b] | 115 | void addObjectToScene(SceneObject& obj);
|
---|
[0d5c100] | 116 | void populateBuffers(vector<SceneObject>& objects,
|
---|
| 117 | GLuint* points_vbo,
|
---|
| 118 | GLuint* colors_vbo,
|
---|
| 119 | GLuint* selected_colors_vbo,
|
---|
| 120 | GLuint* texcoords_vbo,
|
---|
| 121 | GLuint* normals_vbo,
|
---|
| 122 | GLuint* ubo,
|
---|
| 123 | GLuint* model_mat_idx_vbo,
|
---|
| 124 | map<GLuint, unsigned int>& shaderCounts,
|
---|
| 125 | map<GLuint, unsigned int>& curShaderBase);
|
---|
[f9a242b] | 126 |
|
---|
[5c403fe] | 127 | void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
|
---|
| 128 |
|
---|
[93462c6] | 129 | void renderMainMenu();
|
---|
| 130 | void renderMainMenuGui();
|
---|
| 131 |
|
---|
| 132 | void renderScene(vector<SceneObject>& objects,
|
---|
[cffca4d] | 133 | GLuint color_sp, GLuint texture_sp,
|
---|
[93462c6] | 134 | GLuint vao1, GLuint vao2,
|
---|
| 135 | GLuint points_vbo, GLuint normals_vbo,
|
---|
| 136 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
[0d5c100] | 137 | SceneObject* selectedObject,
|
---|
| 138 | map<GLuint, unsigned int>& shaderCounts,
|
---|
| 139 | map<GLuint, unsigned int>& curShaderBase);
|
---|
[93462c6] | 140 | void renderSceneGui();
|
---|
[d12d003] | 141 |
|
---|
[c1ca5b5] | 142 | int main(int argc, char* argv[]) {
|
---|
[5272b6b] | 143 | cout << "New OpenGL Game" << endl;
|
---|
| 144 |
|
---|
[ec4456b] | 145 | if (!restart_gl_log()) {}
|
---|
| 146 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 147 |
|
---|
[ec4456b] | 148 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 149 | if (!glfwInit()) {
|
---|
| 150 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 151 | return 1;
|
---|
[be246ad] | 152 | }
|
---|
| 153 |
|
---|
| 154 | #ifdef __APPLE__
|
---|
| 155 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 156 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 157 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 158 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 159 | #endif
|
---|
[5272b6b] | 160 |
|
---|
[ec4456b] | 161 | glfwWindowHint(GLFW_SAMPLES, 4);
|
---|
| 162 |
|
---|
| 163 | GLFWwindow* window = NULL;
|
---|
[e856d62] | 164 | GLFWmonitor* mon = NULL;
|
---|
[ec4456b] | 165 |
|
---|
| 166 | if (FULLSCREEN) {
|
---|
[e856d62] | 167 | mon = glfwGetPrimaryMonitor();
|
---|
[ec4456b] | 168 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 169 |
|
---|
| 170 | width = vmode->width;
|
---|
| 171 | height = vmode->height;
|
---|
[e856d62] | 172 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
[ec4456b] | 173 | }
|
---|
[e856d62] | 174 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
[ec4456b] | 175 |
|
---|
[5272b6b] | 176 | if (!window) {
|
---|
| 177 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 178 | glfwTerminate();
|
---|
| 179 | return 1;
|
---|
| 180 | }
|
---|
[c62eee6] | 181 |
|
---|
[644a2e4] | 182 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 183 | glewExperimental = GL_TRUE;
|
---|
| 184 | glewInit();
|
---|
| 185 |
|
---|
[14ff67c] | 186 | /*
|
---|
| 187 | * RENDERING ALGORITHM NOTES:
|
---|
| 188 | *
|
---|
| 189 | * Basically, I need to split my objects into groups, so that each group fits into
|
---|
| 190 | * GL_MAX_UNIFORM_BLOCK_SIZE. I need to have an offset and a size for each group.
|
---|
| 191 | * Getting the offset is straitforward. The size may as well be GL_MAX_UNIFORM_BLOCK_SIZE
|
---|
| 192 | * for each group, since it seems that smaller sizes just round up to the nearest GL_MAX_UNIFORM_BLOCK_SIZE
|
---|
| 193 | *
|
---|
| 194 | * I'll need to have a loop inside my render loop that calls glBindBufferRange(GL_UNIFORM_BUFFER, ...
|
---|
| 195 | * for every 1024 objects and then draws all those objects with one glDraw call.
|
---|
| 196 | *
|
---|
[0d5c100] | 197 | * Since I currently have very few objects, I'll wait to implement this until I have
|
---|
| 198 | * a reasonable number of objects always using the same shader.
|
---|
[14ff67c] | 199 | */
|
---|
| 200 |
|
---|
| 201 | GLint UNIFORM_BUFFER_OFFSET_ALIGNMENT, MAX_UNIFORM_BLOCK_SIZE;
|
---|
| 202 | glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UNIFORM_BUFFER_OFFSET_ALIGNMENT);
|
---|
| 203 | glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 204 |
|
---|
| 205 | MAX_UNIFORMS = MAX_UNIFORM_BLOCK_SIZE / sizeof(mat4);
|
---|
| 206 |
|
---|
| 207 | cout << "UNIFORM_BUFFER_OFFSET_ALIGNMENT: " << UNIFORM_BUFFER_OFFSET_ALIGNMENT << endl;
|
---|
| 208 | cout << "MAX_UNIFORMS: " << MAX_UNIFORMS << endl;
|
---|
| 209 |
|
---|
[c1ca5b5] | 210 | // Setup Dear ImGui binding
|
---|
| 211 | IMGUI_CHECKVERSION();
|
---|
| 212 | ImGui::CreateContext();
|
---|
| 213 | ImGuiIO& io = ImGui::GetIO(); (void)io;
|
---|
| 214 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
---|
| 215 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
---|
| 216 | ImGui_ImplGlfwGL3_Init(window, true);
|
---|
| 217 |
|
---|
| 218 | // Setup style
|
---|
| 219 | ImGui::StyleColorsDark();
|
---|
| 220 | //ImGui::StyleColorsClassic();
|
---|
| 221 |
|
---|
| 222 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
[f7d35da] | 223 | glfwSetKeyCallback(window, key_callback);
|
---|
[c1ca5b5] | 224 |
|
---|
[5272b6b] | 225 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 226 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 227 | printf("Renderer: %s\n", renderer);
|
---|
| 228 | printf("OpenGL version supported %s\n", version);
|
---|
[93baa0e] | 229 |
|
---|
[5272b6b] | 230 | glEnable(GL_DEPTH_TEST);
|
---|
| 231 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 232 |
|
---|
[93baa0e] | 233 | glEnable(GL_CULL_FACE);
|
---|
| 234 | // glCullFace(GL_BACK);
|
---|
| 235 | // glFrontFace(GL_CW);
|
---|
| 236 |
|
---|
[485424b] | 237 | int x, y;
|
---|
| 238 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
| 239 | if (texImage) {
|
---|
| 240 | cout << "Yay, I loaded an image!" << endl;
|
---|
| 241 | cout << x << endl;
|
---|
| 242 | cout << y << endl;
|
---|
[e856d62] | 243 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
[485424b] | 244 | }
|
---|
| 245 |
|
---|
| 246 | GLuint tex = 0;
|
---|
| 247 | glGenTextures(1, &tex);
|
---|
| 248 | glActiveTexture(GL_TEXTURE0);
|
---|
| 249 | glBindTexture(GL_TEXTURE_2D, tex);
|
---|
| 250 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 251 |
|
---|
| 252 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 253 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 254 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 255 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 256 |
|
---|
[0d5c100] | 257 | /* RENDERING ALGORITHM
|
---|
| 258 | *
|
---|
| 259 | * Create a separate vbo for each of the following things:
|
---|
| 260 | * - points
|
---|
| 261 | * - colors
|
---|
| 262 | * - texture coordinates
|
---|
| 263 | * - selected colors
|
---|
| 264 | * - normals
|
---|
| 265 | * - indices into a ubo that stores a model matrix for each object
|
---|
| 266 | *
|
---|
| 267 | * Also, make a model matrix ubo, the entirety of which will be passed to the vertex shader.
|
---|
| 268 | * The vbo containing the correct index into the ubo (mentioned above) will be used to select
|
---|
| 269 | * the right model matrix for each point. The index in the vbo will be the saem for all points
|
---|
| 270 | * of any given object.
|
---|
| 271 | *
|
---|
| 272 | * There will be two shader programs for now, one for draing colored objects, and another for
|
---|
| 273 | * drawing textured ones. The points, normals, and model mat ubo indices will be passed to both
|
---|
| 274 | * shaders, while the colors vbo will only be passed to the colors shader, and the texcoords vbo
|
---|
| 275 | * only to the texture shader.
|
---|
| 276 | *
|
---|
| 277 | * Right now, the currently selected object is drawn using one color (specified in the selected
|
---|
| 278 | * colors vbo) regardless of whether it is normally rendering using colors or a texture. The selected
|
---|
| 279 | * object is rendering by binding the selected colors vbo in place of the colors vbo and using the colors
|
---|
| 280 | * shader. Then, the selected object is redrawn along with all other objects, but the depth buffer test
|
---|
| 281 | * prevents the unselected version of the object from appearing on the screen. This lets me render all the
|
---|
| 282 | * objects that use a particular shader using one glDrawArrays() call.
|
---|
| 283 | */
|
---|
[cffca4d] | 284 |
|
---|
| 285 | GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag");
|
---|
| 286 | GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
| 287 |
|
---|
[f9a242b] | 288 | SceneObject obj;
|
---|
[07ed460] | 289 | mat4 T_model, R_model;
|
---|
| 290 |
|
---|
| 291 | // triangle
|
---|
[f9a242b] | 292 | obj = SceneObject();
|
---|
| 293 | obj.shader_program = color_sp;
|
---|
| 294 | obj.points = {
|
---|
[d12d003] | 295 | 0.0f, 0.5f, 0.0f,
|
---|
| 296 | -0.5f, -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.0f, 0.5f, 0.0f,
|
---|
[516668e] | 301 | };
|
---|
[f9a242b] | 302 | obj.colors = {
|
---|
[07ed460] | 303 | 1.0f, 0.0f, 0.0f,
|
---|
| 304 | 0.0f, 0.0f, 1.0f,
|
---|
| 305 | 0.0f, 1.0f, 0.0f,
|
---|
| 306 | 0.0f, 1.0f, 0.0f,
|
---|
| 307 | 0.0f, 0.0f, 1.0f,
|
---|
| 308 | 1.0f, 0.0f, 0.0f,
|
---|
[93baa0e] | 309 | };
|
---|
[f9a242b] | 310 | obj.texcoords = {
|
---|
[07ed460] | 311 | 1.0f, 1.0f,
|
---|
| 312 | 0.0f, 1.0f,
|
---|
| 313 | 0.0f, 0.0f,
|
---|
| 314 | 1.0f, 1.0f,
|
---|
| 315 | 0.0f, 0.0f,
|
---|
| 316 | 1.0f, 0.0f
|
---|
[33a9664] | 317 | };
|
---|
[f9a242b] | 318 | obj.selected_colors = {
|
---|
[07ed460] | 319 | 0.0f, 1.0f, 0.0f,
|
---|
| 320 | 0.0f, 1.0f, 0.0f,
|
---|
| 321 | 0.0f, 1.0f, 0.0f,
|
---|
| 322 | 0.0f, 1.0f, 0.0f,
|
---|
| 323 | 0.0f, 1.0f, 0.0f,
|
---|
| 324 | 0.0f, 1.0f, 0.0f,
|
---|
| 325 | };
|
---|
| 326 |
|
---|
| 327 | T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
|
---|
| 328 | R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[5c403fe] | 329 | obj.model_base = T_model*R_model;
|
---|
[f9a242b] | 330 |
|
---|
| 331 | addObjectToScene(obj);
|
---|
[33a9664] | 332 |
|
---|
[07ed460] | 333 | // square
|
---|
[f9a242b] | 334 | obj = SceneObject();
|
---|
| 335 | obj.shader_program = texture_sp;
|
---|
| 336 | obj.points = {
|
---|
[b73cb3b] | 337 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 338 | -0.5f, 0.5f, 0.0f,
|
---|
| 339 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 340 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 341 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 342 | 0.5f, -0.5f, 0.0f,
|
---|
[64a70f4] | 343 | };
|
---|
[f9a242b] | 344 | obj.colors = {
|
---|
[07ed460] | 345 | 1.0f, 0.0f, 0.0f,
|
---|
| 346 | 0.0f, 0.0f, 1.0f,
|
---|
| 347 | 0.0f, 1.0f, 0.0f,
|
---|
| 348 | 0.0f, 1.0f, 0.0f,
|
---|
| 349 | 0.0f, 0.0f, 1.0f,
|
---|
| 350 | 1.0f, 0.0f, 0.0f,
|
---|
[485424b] | 351 | };
|
---|
[f9a242b] | 352 | obj.texcoords = {
|
---|
[64a70f4] | 353 | 1.0f, 1.0f,
|
---|
| 354 | 0.0f, 1.0f,
|
---|
[07ed460] | 355 | 0.0f, 0.0f,
|
---|
| 356 | 1.0f, 1.0f,
|
---|
| 357 | 0.0f, 0.0f,
|
---|
| 358 | 1.0f, 0.0f
|
---|
[485424b] | 359 | };
|
---|
[f9a242b] | 360 | obj.selected_colors = {
|
---|
[9f4986b] | 361 | 0.0f, 0.6f, 0.9f,
|
---|
| 362 | 0.0f, 0.6f, 0.9f,
|
---|
| 363 | 0.0f, 0.6f, 0.9f,
|
---|
| 364 | 0.0f, 0.6f, 0.9f,
|
---|
| 365 | 0.0f, 0.6f, 0.9f,
|
---|
| 366 | 0.0f, 0.6f, 0.9f,
|
---|
[19c9338] | 367 | };
|
---|
[df652d5] | 368 |
|
---|
[b73cb3b] | 369 | T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
|
---|
| 370 | R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[5c403fe] | 371 | obj.model_base = T_model*R_model;
|
---|
[f9a242b] | 372 |
|
---|
| 373 | addObjectToScene(obj);
|
---|
| 374 |
|
---|
| 375 | // player ship
|
---|
| 376 | obj = SceneObject();
|
---|
| 377 | obj.shader_program = color_sp;
|
---|
| 378 | obj.points = {
|
---|
| 379 | 0.0f, 0.5f, 0.0f,
|
---|
| 380 | -0.5f, -0.5f, 0.0f,
|
---|
| 381 | 0.5f, -0.5f, 0.0f,
|
---|
| 382 | 0.5f, -0.5f, 0.0f,
|
---|
| 383 | -0.5f, -0.5f, 0.0f,
|
---|
| 384 | 0.0f, 0.5f, 0.0f,
|
---|
| 385 | };
|
---|
| 386 | obj.colors = {
|
---|
| 387 | 0.0f, 0.0f, 0.3f,
|
---|
| 388 | 0.0f, 0.0f, 0.3f,
|
---|
| 389 | 0.0f, 0.0f, 0.3f,
|
---|
| 390 | 0.0f, 0.0f, 0.3f,
|
---|
| 391 | 0.0f, 0.0f, 0.3f,
|
---|
| 392 | 0.0f, 0.0f, 0.3f,
|
---|
| 393 | };
|
---|
| 394 | obj.texcoords = {
|
---|
| 395 | 1.0f, 1.0f,
|
---|
| 396 | 0.0f, 1.0f,
|
---|
| 397 | 0.0f, 0.0f,
|
---|
| 398 | 1.0f, 1.0f,
|
---|
| 399 | 0.0f, 0.0f,
|
---|
[f7d35da] | 400 | 1.0f, 0.0f,
|
---|
[f9a242b] | 401 | };
|
---|
| 402 | obj.selected_colors = {
|
---|
| 403 | 0.0f, 1.0f, 0.0f,
|
---|
| 404 | 0.0f, 1.0f, 0.0f,
|
---|
| 405 | 0.0f, 1.0f, 0.0f,
|
---|
| 406 | 0.0f, 1.0f, 0.0f,
|
---|
| 407 | 0.0f, 1.0f, 0.0f,
|
---|
| 408 | 0.0f, 1.0f, 0.0f,
|
---|
| 409 | };
|
---|
| 410 |
|
---|
[f7d35da] | 411 | T_model = translate(mat4(), vec3(0.0f, -0.9f, 0.0f));
|
---|
[f9a242b] | 412 | R_model = rotate(mat4(), -1.0f, vec3(1.0f, 0.0f, 0.0f));
|
---|
[5c403fe] | 413 | obj.model_base = T_model; //T_model * R_model;
|
---|
[f9a242b] | 414 |
|
---|
| 415 | addObjectToScene(obj);
|
---|
[df652d5] | 416 |
|
---|
[07ed460] | 417 | vector<SceneObject>::iterator obj_it;
|
---|
| 418 | GLsizeiptr offset;
|
---|
[19c9338] | 419 |
|
---|
[0d5c100] | 420 | GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo,
|
---|
| 421 | normals_vbo, ubo, model_mat_idx_vbo;
|
---|
[e165b85] | 422 |
|
---|
[0d5c100] | 423 | map<GLuint, unsigned int> shaderCounts, curShaderBase;
|
---|
[e165b85] | 424 |
|
---|
[0d5c100] | 425 | populateBuffers(objects,
|
---|
| 426 | &points_vbo,
|
---|
| 427 | &colors_vbo,
|
---|
| 428 | &selected_colors_vbo,
|
---|
| 429 | &texcoords_vbo,
|
---|
| 430 | &normals_vbo,
|
---|
| 431 | &ubo,
|
---|
| 432 | &model_mat_idx_vbo,
|
---|
| 433 | shaderCounts,
|
---|
| 434 | curShaderBase);
|
---|
[e165b85] | 435 |
|
---|
[644a2e4] | 436 | GLuint vao = 0;
|
---|
[516668e] | 437 | glGenVertexArrays(1, &vao);
|
---|
| 438 | glBindVertexArray(vao);
|
---|
| 439 |
|
---|
[8b7cfcf] | 440 | glEnableVertexAttribArray(0);
|
---|
| 441 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 442 | glEnableVertexAttribArray(2);
|
---|
[14ff67c] | 443 | glEnableVertexAttribArray(3);
|
---|
[644a2e4] | 444 |
|
---|
[cffca4d] | 445 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 446 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 447 |
|
---|
| 448 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 449 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 450 |
|
---|
[14ff67c] | 451 | glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
|
---|
| 452 | glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
|
---|
| 453 |
|
---|
[485424b] | 454 | GLuint vao2 = 0;
|
---|
| 455 | glGenVertexArrays(1, &vao2);
|
---|
| 456 | glBindVertexArray(vao2);
|
---|
[644a2e4] | 457 |
|
---|
[485424b] | 458 | glEnableVertexAttribArray(0);
|
---|
| 459 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 460 | glEnableVertexAttribArray(2);
|
---|
[14ff67c] | 461 | glEnableVertexAttribArray(3);
|
---|
[8b7cfcf] | 462 |
|
---|
[cffca4d] | 463 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 464 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[1a530df] | 465 |
|
---|
[cffca4d] | 466 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 467 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 468 |
|
---|
| 469 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 470 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[644a2e4] | 471 |
|
---|
[14ff67c] | 472 | glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
|
---|
| 473 | glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
|
---|
| 474 |
|
---|
[7ee66ea] | 475 | float cam_speed = 1.0f;
|
---|
[201e2f8] | 476 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[7ee66ea] | 477 |
|
---|
[b73cb3b] | 478 | // glm::lookAt can create the view matrix
|
---|
| 479 | // glm::perspective can create the projection matrix
|
---|
| 480 |
|
---|
| 481 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
[64a70f4] | 482 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[7ee66ea] | 483 |
|
---|
[c62eee6] | 484 | mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[5c403fe] | 485 | mat4 R = mat4();
|
---|
[c62eee6] | 486 | view_mat = R*T;
|
---|
[7ee66ea] | 487 |
|
---|
| 488 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 489 | float aspect = (float)width / (float)height;
|
---|
| 490 |
|
---|
[d12d003] | 491 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 492 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 493 | float Sy = NEAR_CLIP / range;
|
---|
| 494 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 495 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[7ee66ea] | 496 |
|
---|
[c62eee6] | 497 | float proj_arr[] = {
|
---|
[7ee66ea] | 498 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 499 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 500 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 501 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 502 | };
|
---|
[c62eee6] | 503 | proj_mat = make_mat4(proj_arr);
|
---|
[7ee66ea] | 504 |
|
---|
[14ff67c] | 505 | GLuint ub_binding_point = 0;
|
---|
| 506 |
|
---|
[cffca4d] | 507 | GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
|
---|
| 508 | GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
|
---|
[14ff67c] | 509 | GLuint color_sp_ub_index = glGetUniformBlockIndex(color_sp, "models");
|
---|
[7ee66ea] | 510 |
|
---|
[cffca4d] | 511 | GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
|
---|
| 512 | GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
|
---|
[14ff67c] | 513 | GLuint texture_sp_ub_index = glGetUniformBlockIndex(texture_sp, "models");
|
---|
[19c9338] | 514 |
|
---|
[cffca4d] | 515 | glUseProgram(color_sp);
|
---|
[19c9338] | 516 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 517 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[485424b] | 518 |
|
---|
[14ff67c] | 519 | glUniformBlockBinding(color_sp, color_sp_ub_index, ub_binding_point);
|
---|
| 520 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
[e165b85] | 521 |
|
---|
[cffca4d] | 522 | glUseProgram(texture_sp);
|
---|
[19c9338] | 523 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 524 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[7ee66ea] | 525 |
|
---|
[14ff67c] | 526 | glUniformBlockBinding(texture_sp, texture_sp_ub_index, ub_binding_point);
|
---|
| 527 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 528 |
|
---|
[7ee66ea] | 529 | bool cam_moved = false;
|
---|
| 530 |
|
---|
[046ce72] | 531 | int frame_count = 0;
|
---|
[f70ab75] | 532 | double elapsed_seconds_fps = 0.0f;
|
---|
[93baa0e] | 533 | double previous_seconds = glfwGetTime();
|
---|
[046ce72] | 534 |
|
---|
[9dd2eb7] | 535 | // This draws wireframes. Useful for seeing separate faces and occluded objects.
|
---|
| 536 | //glPolygonMode(GL_FRONT, GL_LINE);
|
---|
| 537 |
|
---|
[14ff67c] | 538 | if (DISABLE_VSYNC && SHOW_FPS) {
|
---|
| 539 | glfwSwapInterval(0);
|
---|
| 540 | }
|
---|
[1c81bf0] | 541 |
|
---|
[93462c6] | 542 | State curState = STATE_MAIN_MENU;
|
---|
| 543 |
|
---|
[5b3462b] | 544 | while (!glfwWindowShouldClose(window) && isRunning) {
|
---|
[93baa0e] | 545 | double current_seconds = glfwGetTime();
|
---|
| 546 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 547 | previous_seconds = current_seconds;
|
---|
| 548 |
|
---|
[14ff67c] | 549 | if (SHOW_FPS) {
|
---|
| 550 | elapsed_seconds_fps += elapsed_seconds;
|
---|
| 551 | if (elapsed_seconds_fps > 0.25f) {
|
---|
| 552 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
| 553 | cout << "FPS: " << fps << endl;
|
---|
[046ce72] | 554 |
|
---|
[14ff67c] | 555 | frame_count = 0;
|
---|
| 556 | elapsed_seconds_fps = 0.0f;
|
---|
| 557 | }
|
---|
[046ce72] | 558 |
|
---|
[14ff67c] | 559 | frame_count++;
|
---|
| 560 | }
|
---|
[046ce72] | 561 |
|
---|
[f7d35da] | 562 | // Handle events
|
---|
[baa5848] | 563 |
|
---|
| 564 | clickedObject = NULL;
|
---|
[f7d35da] | 565 |
|
---|
| 566 | // reset the all key states to KEY_STATE_UNCHANGED (something the GLFW key callback can never return)
|
---|
| 567 | // so that GLFW_PRESS and GLFW_RELEASE are only detected once
|
---|
| 568 | // TODO: Change this if we ever need to act on GLFW_REPEAT (which is when a key is held down continuously)
|
---|
| 569 | fill(key_state, key_state + NUM_KEYS, KEY_STATE_UNCHANGED);
|
---|
| 570 |
|
---|
[baa5848] | 571 | glfwPollEvents();
|
---|
| 572 |
|
---|
[93462c6] | 573 | while (!events.empty()) {
|
---|
| 574 | switch (events.front()) {
|
---|
| 575 | case EVENT_GO_TO_MAIN_MENU:
|
---|
| 576 | curState = STATE_MAIN_MENU;
|
---|
| 577 | break;
|
---|
| 578 | case EVENT_GO_TO_GAME:
|
---|
| 579 | curState = STATE_GAME;
|
---|
| 580 | break;
|
---|
| 581 | case EVENT_QUIT:
|
---|
| 582 | isRunning = false;
|
---|
| 583 | break;
|
---|
| 584 | }
|
---|
| 585 | events.pop();
|
---|
[147ac6d] | 586 | }
|
---|
[93462c6] | 587 |
|
---|
| 588 | if (curState == STATE_GAME) {
|
---|
| 589 | if (clickedObject == &objects[0]) {
|
---|
| 590 | selectedObject = &objects[0];
|
---|
| 591 | }
|
---|
| 592 | if (clickedObject == &objects[1]) {
|
---|
| 593 | selectedObject = &objects[1];
|
---|
| 594 | }
|
---|
[f7d35da] | 595 |
|
---|
| 596 | /*
|
---|
| 597 | if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
|
---|
[5c403fe] | 598 | transformObject(objects[1], translate(mat4(), vec3(0.3f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 599 | }
|
---|
| 600 | if (key_pressed[GLFW_KEY_RIGHT]) {
|
---|
| 601 | transformObject(objects[2], translate(mat4(), vec3(0.01f, 0.0f, 0.0f)), ubo);
|
---|
| 602 | }
|
---|
| 603 | if (key_pressed[GLFW_KEY_LEFT]) {
|
---|
| 604 | transformObject(objects[2], translate(mat4(), vec3(-0.01f, 0.0f, 0.0f)), ubo);
|
---|
| 605 | }
|
---|
| 606 | */
|
---|
[baa5848] | 607 | }
|
---|
[33a9664] | 608 |
|
---|
[f7d35da] | 609 | // Render scene
|
---|
[93baa0e] | 610 |
|
---|
| 611 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[485424b] | 612 |
|
---|
[93462c6] | 613 | switch (curState) {
|
---|
| 614 | case STATE_MAIN_MENU:
|
---|
| 615 | renderMainMenu();
|
---|
| 616 | renderMainMenuGui();
|
---|
| 617 | break;
|
---|
| 618 | case STATE_GAME:
|
---|
| 619 | renderScene(objects,
|
---|
[cffca4d] | 620 | color_sp, texture_sp,
|
---|
[93462c6] | 621 | vao, vao2,
|
---|
| 622 | points_vbo, normals_vbo,
|
---|
| 623 | colors_vbo, texcoords_vbo, selected_colors_vbo,
|
---|
[0d5c100] | 624 | selectedObject,
|
---|
| 625 | shaderCounts, curShaderBase);
|
---|
[93462c6] | 626 | renderSceneGui();
|
---|
| 627 | break;
|
---|
[baa5848] | 628 | }
|
---|
[df652d5] | 629 |
|
---|
[644a2e4] | 630 | glfwSwapBuffers(window);
|
---|
[ec4456b] | 631 |
|
---|
| 632 | if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
|
---|
| 633 | glfwSetWindowShouldClose(window, 1);
|
---|
| 634 | }
|
---|
[7ee66ea] | 635 |
|
---|
| 636 | float dist = cam_speed * elapsed_seconds;
|
---|
| 637 | if (glfwGetKey(window, GLFW_KEY_A)) {
|
---|
[c62eee6] | 638 | cam_pos.x -= cos(cam_yaw)*dist;
|
---|
| 639 | cam_pos.z += sin(cam_yaw)*dist;
|
---|
[f7d35da] | 640 |
|
---|
[7ee66ea] | 641 | cam_moved = true;
|
---|
| 642 | }
|
---|
| 643 | if (glfwGetKey(window, GLFW_KEY_D)) {
|
---|
[c62eee6] | 644 | cam_pos.x += cos(cam_yaw)*dist;
|
---|
| 645 | cam_pos.z -= sin(cam_yaw)*dist;
|
---|
[f7d35da] | 646 |
|
---|
[7ee66ea] | 647 | cam_moved = true;
|
---|
| 648 | }
|
---|
| 649 | if (glfwGetKey(window, GLFW_KEY_W)) {
|
---|
[c62eee6] | 650 | cam_pos.x -= sin(cam_yaw)*dist;
|
---|
| 651 | cam_pos.z -= cos(cam_yaw)*dist;
|
---|
[f7d35da] | 652 |
|
---|
[7ee66ea] | 653 | cam_moved = true;
|
---|
| 654 | }
|
---|
| 655 | if (glfwGetKey(window, GLFW_KEY_S)) {
|
---|
[c62eee6] | 656 | cam_pos.x += sin(cam_yaw)*dist;
|
---|
| 657 | cam_pos.z += cos(cam_yaw)*dist;
|
---|
[f7d35da] | 658 |
|
---|
[7ee66ea] | 659 | cam_moved = true;
|
---|
| 660 | }
|
---|
| 661 | if (glfwGetKey(window, GLFW_KEY_LEFT)) {
|
---|
| 662 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 663 | cam_moved = true;
|
---|
| 664 | }
|
---|
| 665 | if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
|
---|
| 666 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 667 | cam_moved = true;
|
---|
| 668 | }
|
---|
| 669 | if (cam_moved) {
|
---|
[c62eee6] | 670 | T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[7ee66ea] | 671 | R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
[f7d35da] | 672 |
|
---|
[267c4c5] | 673 | view_mat = R*T;
|
---|
[7ee66ea] | 674 |
|
---|
[cffca4d] | 675 | glUseProgram(color_sp);
|
---|
[267c4c5] | 676 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 677 |
|
---|
[cffca4d] | 678 | glUseProgram(texture_sp);
|
---|
[7ee66ea] | 679 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[267c4c5] | 680 |
|
---|
[7ee66ea] | 681 | cam_moved = false;
|
---|
| 682 | }
|
---|
[644a2e4] | 683 | }
|
---|
| 684 |
|
---|
[c1ca5b5] | 685 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
| 686 | ImGui::DestroyContext();
|
---|
| 687 |
|
---|
| 688 | glfwDestroyWindow(window);
|
---|
[5272b6b] | 689 | glfwTerminate();
|
---|
[c1ca5b5] | 690 |
|
---|
[5272b6b] | 691 | return 0;
|
---|
| 692 | }
|
---|
[ec4456b] | 693 |
|
---|
[4f3262f] | 694 | void glfw_error_callback(int error, const char* description) {
|
---|
| 695 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
| 699 | double mouse_x, mouse_y;
|
---|
| 700 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 701 |
|
---|
| 702 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 703 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
| 704 | selectedObject = NULL;
|
---|
| 705 |
|
---|
| 706 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
| 707 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
| 708 |
|
---|
| 709 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 710 |
|
---|
| 711 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
| 712 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
| 713 | ray_eye = vec4(ray_eye.xy(), -1.0f, 1.0f);
|
---|
| 714 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
| 715 |
|
---|
| 716 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
| 717 |
|
---|
| 718 | vec4 click_point;
|
---|
| 719 | 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
|
---|
| 720 | SceneObject* closest_object = NULL;
|
---|
| 721 |
|
---|
| 722 | for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
| 723 | for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {
|
---|
[0d5c100] | 724 | if (faceClicked(
|
---|
| 725 | {
|
---|
| 726 | vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),
|
---|
| 727 | vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),
|
---|
| 728 | vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
|
---|
[4f3262f] | 729 | },
|
---|
[0d5c100] | 730 | &*it, ray_world, cam_pos_temp, click_point
|
---|
| 731 | )) {
|
---|
[4f3262f] | 732 | click_point = view_mat * click_point;
|
---|
| 733 |
|
---|
| 734 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
| 735 | closest_point = click_point.xyz();
|
---|
[0d5c100] | 736 | closest_object = &*it;
|
---|
[4f3262f] | 737 | }
|
---|
| 738 | }
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | if (closest_object == NULL) {
|
---|
| 743 | cout << "No object was clicked" << endl;
|
---|
[f7d35da] | 744 | } else {
|
---|
[4f3262f] | 745 | clickedObject = closest_object;
|
---|
| 746 | cout << "Clicked object: " << clickedObject->id << endl;
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[f7d35da] | 751 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
---|
| 752 | key_state[key] = action;
|
---|
| 753 |
|
---|
| 754 | // should be true for GLFW_PRESS and GLFW_REPEAT
|
---|
| 755 | key_pressed[key] = (action != GLFW_RELEASE);
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 |
|
---|
[ec4456b] | 759 | GLuint loadShader(GLenum type, string file) {
|
---|
| 760 | cout << "Loading shader from file " << file << endl;
|
---|
| 761 |
|
---|
| 762 | ifstream shaderFile(file);
|
---|
| 763 | GLuint shaderId = 0;
|
---|
| 764 |
|
---|
| 765 | if (shaderFile.is_open()) {
|
---|
| 766 | string line, shaderString;
|
---|
| 767 |
|
---|
| 768 | while(getline(shaderFile, line)) {
|
---|
| 769 | shaderString += line + "\n";
|
---|
| 770 | }
|
---|
| 771 | shaderFile.close();
|
---|
| 772 | const char* shaderCString = shaderString.c_str();
|
---|
| 773 |
|
---|
| 774 | shaderId = glCreateShader(type);
|
---|
| 775 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 776 | glCompileShader(shaderId);
|
---|
| 777 |
|
---|
| 778 | cout << "Loaded successfully" << endl;
|
---|
| 779 | } else {
|
---|
[e856d62] | 780 | cout << "Failed to load the file" << endl;
|
---|
[ec4456b] | 781 | }
|
---|
| 782 |
|
---|
| 783 | return shaderId;
|
---|
| 784 | }
|
---|
[485424b] | 785 |
|
---|
| 786 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 787 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 788 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 789 |
|
---|
| 790 | GLuint shader_program = glCreateProgram();
|
---|
| 791 | glAttachShader(shader_program, vs);
|
---|
| 792 | glAttachShader(shader_program, fs);
|
---|
| 793 |
|
---|
| 794 | glLinkProgram(shader_program);
|
---|
| 795 |
|
---|
| 796 | return shader_program;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 800 | int n;
|
---|
[e856d62] | 801 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
[485424b] | 802 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
[e856d62] | 803 |
|
---|
| 804 | int width_in_bytes = *x * 4;
|
---|
| 805 | unsigned char *top = NULL;
|
---|
| 806 | unsigned char *bottom = NULL;
|
---|
| 807 | unsigned char temp = 0;
|
---|
| 808 | int half_height = *y / 2;
|
---|
| 809 |
|
---|
| 810 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
| 811 | for (int row = 0; row < half_height; row++) {
|
---|
| 812 | top = image_data + row * width_in_bytes;
|
---|
| 813 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
| 814 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
| 815 | temp = *top;
|
---|
| 816 | *top = *bottom;
|
---|
| 817 | *bottom = temp;
|
---|
| 818 | top++;
|
---|
| 819 | bottom++;
|
---|
| 820 | }
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[485424b] | 823 | if (!image_data) {
|
---|
| 824 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
| 825 | }
|
---|
[e856d62] | 826 |
|
---|
| 827 | // Not Power-of-2 check
|
---|
| 828 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
| 829 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
| 830 | }
|
---|
| 831 |
|
---|
[485424b] | 832 | return image_data;
|
---|
| 833 | }
|
---|
[33a9664] | 834 |
|
---|
[d9f99b2] | 835 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
[5c9d193] | 836 | // LINE EQUATION: P = O + Dt
|
---|
[b73cb3b] | 837 | // O = cam
|
---|
[5c9d193] | 838 | // D = ray_world
|
---|
| 839 |
|
---|
[b73cb3b] | 840 | // PLANE EQUATION: P dot n + d = 0
|
---|
| 841 | // n is the normal vector
|
---|
| 842 | // d is the offset from the origin
|
---|
[5c9d193] | 843 |
|
---|
| 844 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
[d9f99b2] | 845 | vec3 v1 = points[1] - points[0];
|
---|
| 846 | vec3 v2 = points[2] - points[0];
|
---|
[5c9d193] | 847 |
|
---|
| 848 | 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] | 849 |
|
---|
[5c9d193] | 850 | vec3 local_ray = (inverse(obj->model_mat) * world_ray).xyz();
|
---|
| 851 | vec3 local_cam = (inverse(obj->model_mat) * cam).xyz();
|
---|
| 852 |
|
---|
[b73cb3b] | 853 | local_ray = local_ray - local_cam;
|
---|
[5c9d193] | 854 |
|
---|
[d9f99b2] | 855 | float d = -glm::dot(points[0], normal);
|
---|
[5c9d193] | 856 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
| 857 |
|
---|
| 858 | vec3 intersection = local_cam + t*local_ray;
|
---|
| 859 |
|
---|
[d9f99b2] | 860 | if (insideTriangle(intersection, points)) {
|
---|
[e82692b] | 861 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
| 862 | return true;
|
---|
| 863 | } else {
|
---|
| 864 | return false;
|
---|
| 865 | }
|
---|
[5c9d193] | 866 | }
|
---|
[f7d35da] | 867 |
|
---|
[5c9d193] | 868 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
[d9f99b2] | 869 | vec3 v21 = triangle_points[1] - triangle_points[0];
|
---|
| 870 | vec3 v31 = triangle_points[2] - triangle_points[0];
|
---|
| 871 | vec3 pv1 = p - triangle_points[0];
|
---|
[33a9664] | 872 |
|
---|
| 873 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 874 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 875 |
|
---|
| 876 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 877 | }
|
---|
[d12d003] | 878 |
|
---|
| 879 | void printVector(string label, vec3 v) {
|
---|
| 880 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
| 881 | }
|
---|
[b73cb3b] | 882 |
|
---|
| 883 | void print4DVector(string label, vec4 v) {
|
---|
| 884 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
| 885 | }
|
---|
[c1ca5b5] | 886 |
|
---|
[0d5c100] | 887 | void addObjectToScene(SceneObject& obj) {
|
---|
| 888 | obj.id = objects.size(); // currently unused
|
---|
| 889 | obj.num_points = obj.points.size() / 3;
|
---|
[5c403fe] | 890 | obj.model_transform = mat4();
|
---|
[0d5c100] | 891 |
|
---|
| 892 | obj.normals.reserve(obj.points.size());
|
---|
| 893 | for (int i = 0; i < obj.points.size(); i += 9) {
|
---|
| 894 | vec3 point1 = vec3(obj.points[i], obj.points[i + 1], obj.points[i + 2]);
|
---|
| 895 | vec3 point2 = vec3(obj.points[i + 3], obj.points[i + 4], obj.points[i + 5]);
|
---|
| 896 | vec3 point3 = vec3(obj.points[i + 6], obj.points[i + 7], obj.points[i + 8]);
|
---|
[cffca4d] | 897 |
|
---|
[0d5c100] | 898 | vec3 normal = normalize(cross(point2 - point1, point3 - point1));
|
---|
[cffca4d] | 899 |
|
---|
[0d5c100] | 900 | // Add the same normal for all 3 points
|
---|
| 901 | for (int j = 0; j < 3; j++) {
|
---|
| 902 | obj.normals.push_back(normal.x);
|
---|
| 903 | obj.normals.push_back(normal.y);
|
---|
| 904 | obj.normals.push_back(normal.z);
|
---|
| 905 | }
|
---|
| 906 | }
|
---|
[cffca4d] | 907 |
|
---|
[0d5c100] | 908 | objects.push_back(obj);
|
---|
| 909 | }
|
---|
[cffca4d] | 910 |
|
---|
[0d5c100] | 911 | void populateBuffers(vector<SceneObject>& objects,
|
---|
| 912 | GLuint* points_vbo,
|
---|
| 913 | GLuint* colors_vbo,
|
---|
| 914 | GLuint* selected_colors_vbo,
|
---|
| 915 | GLuint* texcoords_vbo,
|
---|
| 916 | GLuint* normals_vbo,
|
---|
| 917 | GLuint* ubo,
|
---|
| 918 | GLuint* model_mat_idx_vbo,
|
---|
| 919 | map<GLuint, unsigned int>& shaderCounts,
|
---|
| 920 | map<GLuint, unsigned int>& curShaderBase) {
|
---|
| 921 | GLsizeiptr points_buffer_size = 0;
|
---|
| 922 | GLsizeiptr textures_buffer_size = 0;
|
---|
| 923 | GLsizeiptr ubo_buffer_size = 0;
|
---|
| 924 | GLsizeiptr model_mat_idx_buffer_size = 0;
|
---|
| 925 |
|
---|
| 926 | map<GLuint, unsigned int> curShaderOffset;
|
---|
[93462c6] | 927 |
|
---|
[0d5c100] | 928 | map<GLuint, unsigned int> shaderUboCounts;
|
---|
| 929 | map<GLuint, unsigned int> curShaderUboBase;
|
---|
| 930 | map<GLuint, unsigned int> curShaderUboOffset;
|
---|
[93462c6] | 931 |
|
---|
[0d5c100] | 932 | vector<SceneObject>::iterator it;
|
---|
| 933 |
|
---|
| 934 | /* Find all shaders that need to be used and the number of objects and
|
---|
| 935 | * number of points for each shader. Construct a map from shader id to count
|
---|
| 936 | * of points being drawn using that shader (for thw model matrix ubo, we
|
---|
| 937 | * need object counts instead). These will be used to get offsets into the
|
---|
| 938 | * vertex buffer for each shader.
|
---|
| 939 | */
|
---|
| 940 | for (it = objects.begin(); it != objects.end(); it++) {
|
---|
| 941 | points_buffer_size += it->points.size() * sizeof(GLfloat);
|
---|
| 942 | textures_buffer_size += it->texcoords.size() * sizeof(GLfloat);
|
---|
| 943 | ubo_buffer_size += 16 * sizeof(GLfloat);
|
---|
| 944 | model_mat_idx_buffer_size += it->num_points * sizeof(GLuint);
|
---|
| 945 |
|
---|
| 946 | if (shaderCounts.count(it->shader_program) == 0) {
|
---|
| 947 | shaderCounts[it->shader_program] = it->num_points;
|
---|
| 948 | shaderUboCounts[it->shader_program] = 1;
|
---|
| 949 | } else {
|
---|
| 950 | shaderCounts[it->shader_program] += it->num_points;
|
---|
| 951 | shaderUboCounts[it->shader_program]++;
|
---|
[e3ca955] | 952 | }
|
---|
[0d5c100] | 953 | }
|
---|
| 954 |
|
---|
| 955 | map<GLuint, unsigned int>::iterator shaderIt;
|
---|
| 956 | unsigned int lastShaderCount = 0;
|
---|
| 957 | unsigned int lastShaderUboCount = 0;
|
---|
| 958 |
|
---|
| 959 | /*
|
---|
| 960 | * The counts calculated above can be used to get the starting offset of
|
---|
| 961 | * each shader in the vertex buffer. Create a map of base offsets to mark
|
---|
| 962 | * where the data for the first object using a given shader begins. Also,
|
---|
| 963 | * create a map of current offsets to mark where to copy data for the next
|
---|
| 964 | * object being added.
|
---|
| 965 | */
|
---|
| 966 | cout << "Shader counts:" << endl;
|
---|
| 967 | for (shaderIt = shaderCounts.begin(); shaderIt != shaderCounts.end(); shaderIt++) {
|
---|
| 968 | curShaderOffset[shaderIt->first] = 0;
|
---|
| 969 | curShaderUboOffset[shaderIt->first] = 0;
|
---|
| 970 |
|
---|
| 971 | curShaderBase[shaderIt->first] = lastShaderCount;
|
---|
| 972 | lastShaderCount += shaderCounts[shaderIt->first];
|
---|
| 973 |
|
---|
| 974 | curShaderUboBase[shaderIt->first] = lastShaderUboCount;
|
---|
| 975 | lastShaderUboCount += shaderUboCounts[shaderIt->first];
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | // Initialize all the buffers using the counts calculated above
|
---|
| 979 |
|
---|
| 980 | *points_vbo = 0;
|
---|
| 981 | glGenBuffers(1, points_vbo);
|
---|
| 982 | glBindBuffer(GL_ARRAY_BUFFER, *points_vbo);
|
---|
| 983 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 984 |
|
---|
| 985 | *colors_vbo = 0;
|
---|
| 986 | glGenBuffers(1, colors_vbo);
|
---|
| 987 | glBindBuffer(GL_ARRAY_BUFFER, *colors_vbo);
|
---|
| 988 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 989 |
|
---|
| 990 | *selected_colors_vbo = 0;
|
---|
| 991 | glGenBuffers(1, selected_colors_vbo);
|
---|
| 992 | glBindBuffer(GL_ARRAY_BUFFER, *selected_colors_vbo);
|
---|
| 993 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 994 |
|
---|
| 995 | *texcoords_vbo = 0;
|
---|
| 996 | glGenBuffers(1, texcoords_vbo);
|
---|
| 997 | glBindBuffer(GL_ARRAY_BUFFER, *texcoords_vbo);
|
---|
| 998 | glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 999 |
|
---|
| 1000 | *normals_vbo = 0;
|
---|
| 1001 | glGenBuffers(1, normals_vbo);
|
---|
| 1002 | glBindBuffer(GL_ARRAY_BUFFER, *normals_vbo);
|
---|
| 1003 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1004 |
|
---|
| 1005 | *ubo = 0;
|
---|
| 1006 | glGenBuffers(1, ubo);
|
---|
| 1007 | glBindBuffer(GL_UNIFORM_BUFFER, *ubo);
|
---|
| 1008 | glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1009 |
|
---|
| 1010 | *model_mat_idx_vbo = 0;
|
---|
| 1011 | glGenBuffers(1, model_mat_idx_vbo);
|
---|
| 1012 | glBindBuffer(GL_ARRAY_BUFFER, *model_mat_idx_vbo);
|
---|
| 1013 | glBufferData(GL_ARRAY_BUFFER, model_mat_idx_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1014 |
|
---|
| 1015 | for (it = objects.begin(); it != objects.end(); it++) {
|
---|
| 1016 | it->vertex_vbo_offset = curShaderBase[it->shader_program] + curShaderOffset[it->shader_program];
|
---|
[5c403fe] | 1017 | it->ubo_offset = curShaderUboBase[it->shader_program] + curShaderUboOffset[it->shader_program];
|
---|
[0d5c100] | 1018 |
|
---|
| 1019 | glBindBuffer(GL_ARRAY_BUFFER, *points_vbo);
|
---|
| 1020 | glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->points.size() * sizeof(GLfloat), &it->points[0]);
|
---|
| 1021 |
|
---|
| 1022 | glBindBuffer(GL_ARRAY_BUFFER, *colors_vbo);
|
---|
| 1023 | glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->colors.size() * sizeof(GLfloat), &it->colors[0]);
|
---|
| 1024 |
|
---|
| 1025 | glBindBuffer(GL_ARRAY_BUFFER, *selected_colors_vbo);
|
---|
| 1026 | glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->selected_colors.size() * sizeof(GLfloat), &it->selected_colors[0]);
|
---|
| 1027 |
|
---|
| 1028 | glBindBuffer(GL_ARRAY_BUFFER, *texcoords_vbo);
|
---|
| 1029 | glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 2, it->texcoords.size() * sizeof(GLfloat), &it->texcoords[0]);
|
---|
| 1030 |
|
---|
| 1031 | glBindBuffer(GL_ARRAY_BUFFER, *normals_vbo);
|
---|
| 1032 | glBufferSubData(GL_ARRAY_BUFFER, it->vertex_vbo_offset * sizeof(GLfloat) * 3, it->normals.size() * sizeof(GLfloat), &it->normals[0]);
|
---|
| 1033 |
|
---|
| 1034 | glBindBuffer(GL_ARRAY_BUFFER, *model_mat_idx_vbo);
|
---|
| 1035 | for (int i = 0; i < it->num_points; i++) {
|
---|
[5c403fe] | 1036 | glBufferSubData(GL_ARRAY_BUFFER, (it->vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &it->ubo_offset);
|
---|
[93462c6] | 1037 | }
|
---|
[0d5c100] | 1038 |
|
---|
| 1039 | curShaderOffset[it->shader_program] += it->num_points;
|
---|
| 1040 |
|
---|
[5c403fe] | 1041 | it->model_mat = it->model_base * it->model_transform;
|
---|
[0d5c100] | 1042 | glBindBuffer(GL_UNIFORM_BUFFER, *ubo);
|
---|
[5c403fe] | 1043 | glBufferSubData(GL_UNIFORM_BUFFER, it->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(it->model_mat));
|
---|
[0d5c100] | 1044 |
|
---|
| 1045 | curShaderUboOffset[it->shader_program]++;
|
---|
[93462c6] | 1046 | }
|
---|
[0d5c100] | 1047 | }
|
---|
[93462c6] | 1048 |
|
---|
[5c403fe] | 1049 | void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) {
|
---|
| 1050 | obj.model_transform = obj.model_transform * transform;
|
---|
| 1051 | obj.model_mat = obj.model_transform * obj.model_base;
|
---|
| 1052 |
|
---|
| 1053 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
| 1054 | glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
[0d5c100] | 1057 | void renderScene(vector<SceneObject>& objects,
|
---|
| 1058 | GLuint color_sp, GLuint texture_sp,
|
---|
| 1059 | GLuint vao1, GLuint vao2,
|
---|
| 1060 | GLuint points_vbo, GLuint normals_vbo,
|
---|
| 1061 | GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
|
---|
| 1062 | SceneObject* selectedObject,
|
---|
| 1063 | map<GLuint, unsigned int>& shaderCounts,
|
---|
| 1064 | map<GLuint, unsigned int>& curShaderBase) {
|
---|
[93462c6] | 1065 |
|
---|
[cffca4d] | 1066 | glUseProgram(color_sp);
|
---|
[93462c6] | 1067 | glBindVertexArray(vao1);
|
---|
| 1068 |
|
---|
[0d5c100] | 1069 | if (selectedObject != NULL) {
|
---|
| 1070 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 1071 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[cffca4d] | 1072 |
|
---|
[0d5c100] | 1073 | glDrawArrays(GL_TRIANGLES, selectedObject->vertex_vbo_offset, selectedObject->num_points);
|
---|
[cffca4d] | 1074 | }
|
---|
[93462c6] | 1075 |
|
---|
[e3ca955] | 1076 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[cffca4d] | 1077 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[93462c6] | 1078 |
|
---|
[0d5c100] | 1079 | glDrawArrays(GL_TRIANGLES, curShaderBase[color_sp], shaderCounts[color_sp]);
|
---|
[93462c6] | 1080 |
|
---|
[cffca4d] | 1081 | glUseProgram(texture_sp);
|
---|
[93462c6] | 1082 | glBindVertexArray(vao2);
|
---|
| 1083 |
|
---|
[0d5c100] | 1084 | glDrawArrays(GL_TRIANGLES, curShaderBase[texture_sp], shaderCounts[texture_sp]);
|
---|
[93462c6] | 1085 | }
|
---|
| 1086 |
|
---|
| 1087 | void renderSceneGui() {
|
---|
[c1ca5b5] | 1088 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 1089 |
|
---|
| 1090 | // 1. Show a simple window.
|
---|
| 1091 | // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
|
---|
[5b3462b] | 1092 | /*
|
---|
[c1ca5b5] | 1093 | {
|
---|
| 1094 | static float f = 0.0f;
|
---|
| 1095 | static int counter = 0;
|
---|
| 1096 | ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
|
---|
| 1097 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
---|
| 1098 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
---|
| 1099 |
|
---|
| 1100 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
|
---|
| 1101 | ImGui::Checkbox("Another Window", &show_another_window);
|
---|
| 1102 |
|
---|
| 1103 | if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
|
---|
| 1104 | counter++;
|
---|
| 1105 | ImGui::SameLine();
|
---|
| 1106 | ImGui::Text("counter = %d", counter);
|
---|
| 1107 |
|
---|
| 1108 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
---|
| 1109 | }
|
---|
[5b3462b] | 1110 | */
|
---|
[c1ca5b5] | 1111 |
|
---|
[5b3462b] | 1112 | {
|
---|
| 1113 | ImGui::SetNextWindowSize(ImVec2(85, 22), ImGuiCond_Once);
|
---|
| 1114 | ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
|
---|
[f0cc877] | 1115 | ImGui::Begin("WndStats", NULL,
|
---|
| 1116 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 1117 | ImGuiWindowFlags_NoResize |
|
---|
| 1118 | ImGuiWindowFlags_NoMove);
|
---|
[5b3462b] | 1119 | ImGui::Text("Score: ???");
|
---|
[c1ca5b5] | 1120 | ImGui::End();
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
[5b3462b] | 1123 | {
|
---|
| 1124 | ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
|
---|
| 1125 | ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
|
---|
[f0cc877] | 1126 | ImGui::Begin("WndMenubar", NULL,
|
---|
| 1127 | ImGuiWindowFlags_NoTitleBar |
|
---|
[5b3462b] | 1128 | ImGuiWindowFlags_NoResize |
|
---|
| 1129 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 1130 | ImGui::InvisibleButton("", ImVec2(155, 18));
|
---|
[5b3462b] | 1131 | ImGui::SameLine();
|
---|
[93462c6] | 1132 | if (ImGui::Button("Main Menu")) {
|
---|
| 1133 | events.push(EVENT_GO_TO_MAIN_MENU);
|
---|
[5b3462b] | 1134 | }
|
---|
| 1135 | ImGui::End();
|
---|
[c1ca5b5] | 1136 | }
|
---|
| 1137 |
|
---|
[93462c6] | 1138 | ImGui::Render();
|
---|
| 1139 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
| 1142 | void renderMainMenu() {
|
---|
| 1143 | }
|
---|
| 1144 |
|
---|
| 1145 | void renderMainMenuGui() {
|
---|
| 1146 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 1147 |
|
---|
[f0cc877] | 1148 | {
|
---|
| 1149 | int padding = 4;
|
---|
| 1150 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
[93462c6] | 1151 | ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once);
|
---|
[f0cc877] | 1152 | ImGui::Begin("WndMain", NULL,
|
---|
| 1153 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 1154 | ImGuiWindowFlags_NoResize |
|
---|
| 1155 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 1156 |
|
---|
| 1157 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
| 1158 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
| 1159 | ImGui::SameLine();
|
---|
| 1160 | if (ImGui::Button("New Game")) {
|
---|
| 1161 | events.push(EVENT_GO_TO_GAME);
|
---|
| 1162 | }
|
---|
| 1163 |
|
---|
| 1164 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
| 1165 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
| 1166 | ImGui::SameLine();
|
---|
| 1167 | if (ImGui::Button("Quit")) {
|
---|
| 1168 | events.push(EVENT_QUIT);
|
---|
| 1169 | }
|
---|
| 1170 |
|
---|
[f0cc877] | 1171 | ImGui::End();
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
[c1ca5b5] | 1174 | ImGui::Render();
|
---|
| 1175 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 1176 | }
|
---|