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