[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
|
---|
[5c9d193] | 10 |
|
---|
[c62eee6] | 11 | #include <glm/mat4x4.hpp>
|
---|
[7ee66ea] | 12 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 13 | #include <glm/gtc/type_ptr.hpp>
|
---|
| 14 |
|
---|
[c1ca5b5] | 15 | #include "IMGUI/imgui.h"
|
---|
| 16 | #include "imgui_impl_glfw_gl3.h"
|
---|
| 17 |
|
---|
[5272b6b] | 18 | #include <GL/glew.h>
|
---|
| 19 | #include <GLFW/glfw3.h>
|
---|
| 20 |
|
---|
[22b2c37] | 21 | #include <cstdio>
|
---|
[5527206] | 22 | #include <cstdlib>
|
---|
| 23 | #include <ctime>
|
---|
[22b2c37] | 24 | #include <iostream>
|
---|
[ec4456b] | 25 | #include <fstream>
|
---|
[93baa0e] | 26 | #include <cmath>
|
---|
[1099b95] | 27 | #include <string>
|
---|
[19c9338] | 28 | #include <array>
|
---|
[df652d5] | 29 | #include <vector>
|
---|
[93462c6] | 30 | #include <queue>
|
---|
[0d5c100] | 31 | #include <map>
|
---|
[22b2c37] | 32 |
|
---|
[5272b6b] | 33 | using namespace std;
|
---|
[7ee66ea] | 34 | using namespace glm;
|
---|
| 35 |
|
---|
[92b1e90] | 36 | /* LASERS TODO:
|
---|
| 37 | * -Allow lasers that face any direction
|
---|
| 38 | * -Make the lasers rotate to always face the camera
|
---|
| 39 | * -Use textures to draw lasers
|
---|
| 40 | * -The textures should be grayscale and have transparency
|
---|
| 41 | * -The laser shader should take an input color to blend with the texture to give the lasers color
|
---|
[9f9f9a7] | 42 | * -DONE
|
---|
[92b1e90] | 43 | * -The lasers should be SceneObjects and drawn like all other objects
|
---|
[6877ef3] | 44 | * -DONE
|
---|
[92b1e90] | 45 | * -Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so
|
---|
[9f9f9a7] | 46 | *
|
---|
| 47 | * NOTE: Asteroid movement currently depends on framerate, fix this in a generic/reusable way
|
---|
[92b1e90] | 48 | */
|
---|
| 49 |
|
---|
| 50 | enum State {
|
---|
| 51 | STATE_MAIN_MENU,
|
---|
| 52 | STATE_GAME,
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | enum Event {
|
---|
| 56 | EVENT_GO_TO_MAIN_MENU,
|
---|
| 57 | EVENT_GO_TO_GAME,
|
---|
| 58 | EVENT_QUIT,
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | enum ObjectType {
|
---|
| 62 | TYPE_SHIP,
|
---|
| 63 | TYPE_ASTEROID,
|
---|
| 64 | TYPE_LASER,
|
---|
| 65 | };
|
---|
| 66 |
|
---|
[df652d5] | 67 | struct SceneObject {
|
---|
[d9f99b2] | 68 | unsigned int id;
|
---|
[92b1e90] | 69 | ObjectType type;
|
---|
[95595de] | 70 |
|
---|
| 71 | // Currently, model_transform should only have translate, and rotation and scale need to be done in model_base since
|
---|
| 72 | // they need to be done when the object is at the origin. I should change this to have separate scale, rotate, and translate
|
---|
| 73 | // matrices for each object that can be updated independently and then applied to the object in that order.
|
---|
[5c403fe] | 74 | mat4 model_mat, model_base, model_transform;
|
---|
[95595de] | 75 | mat4 translate_mat; // beginning of doing what's mentioned above
|
---|
[baa5848] | 76 | GLuint shader_program;
|
---|
[05e43cf] | 77 | unsigned int num_points;
|
---|
[c3c3158] | 78 | GLuint vertex_vbo_offset;
|
---|
| 79 | GLuint ubo_offset;
|
---|
[07ed460] | 80 | vector<GLfloat> points;
|
---|
| 81 | vector<GLfloat> colors;
|
---|
| 82 | vector<GLfloat> texcoords;
|
---|
[9dd2eb7] | 83 | vector<GLfloat> normals;
|
---|
[07ed460] | 84 | vector<GLfloat> selected_colors;
|
---|
[c3c3158] | 85 | bool deleted;
|
---|
[3d06b4e] | 86 | vec3 bounding_center;
|
---|
| 87 | GLfloat bounding_radius;
|
---|
[c3c3158] | 88 | };
|
---|
| 89 |
|
---|
| 90 | struct BufferInfo {
|
---|
| 91 | unsigned int vbo_base;
|
---|
| 92 | unsigned int vbo_offset;
|
---|
| 93 | unsigned int vbo_capacity;
|
---|
| 94 | unsigned int ubo_base;
|
---|
| 95 | unsigned int ubo_offset;
|
---|
| 96 | unsigned int ubo_capacity;
|
---|
[df652d5] | 97 | };
|
---|
| 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 |
|
---|
[c3c3158] | 114 | void addObjectToSceneDuringInit(SceneObject& obj);
|
---|
| 115 | void addObjectToScene(SceneObject& obj, map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 116 | GLuint points_vbo,
|
---|
| 117 | GLuint colors_vbo,
|
---|
| 118 | GLuint selected_colors_vbo,
|
---|
| 119 | GLuint texcoords_vbo,
|
---|
| 120 | GLuint normals_vbo,
|
---|
| 121 | GLuint ubo,
|
---|
[6877ef3] | 122 | GLuint model_mat_idx_vbo);
|
---|
[95595de] | 123 | void removeObjectFromScene(SceneObject& obj, GLuint ubo);
|
---|
[c3c3158] | 124 |
|
---|
[3d06b4e] | 125 | void calculateObjectBoundingBox(SceneObject& obj);
|
---|
| 126 |
|
---|
[92b1e90] | 127 | void addLaserToScene(SceneObject& obj, vec3 start, vec3 end, vec3 color, GLfloat width);
|
---|
[b155f13] | 128 |
|
---|
[c3c3158] | 129 | void initializeBuffers(
|
---|
| 130 | GLuint* points_vbo,
|
---|
| 131 | GLuint* colors_vbo,
|
---|
| 132 | GLuint* selected_colors_vbo,
|
---|
| 133 | GLuint* texcoords_vbo,
|
---|
| 134 | GLuint* normals_vbo,
|
---|
| 135 | GLuint* ubo,
|
---|
[6877ef3] | 136 | GLuint* model_mat_idx_vbo);
|
---|
[c3c3158] | 137 |
|
---|
[0d5c100] | 138 | void populateBuffers(vector<SceneObject>& objects,
|
---|
[c3c3158] | 139 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 140 | GLuint points_vbo,
|
---|
| 141 | GLuint colors_vbo,
|
---|
| 142 | GLuint selected_colors_vbo,
|
---|
| 143 | GLuint texcoords_vbo,
|
---|
| 144 | GLuint normals_vbo,
|
---|
| 145 | GLuint ubo,
|
---|
[6877ef3] | 146 | GLuint model_mat_idx_vbo);
|
---|
[c3c3158] | 147 |
|
---|
| 148 | void copyObjectDataToBuffers(SceneObject& obj,
|
---|
| 149 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 150 | GLuint points_vbo,
|
---|
| 151 | GLuint colors_vbo,
|
---|
| 152 | GLuint selected_colors_vbo,
|
---|
| 153 | GLuint texcoords_vbo,
|
---|
| 154 | GLuint normals_vbo,
|
---|
| 155 | GLuint ubo,
|
---|
[6877ef3] | 156 | GLuint model_mat_idx_vbo);
|
---|
[f9a242b] | 157 |
|
---|
[5c403fe] | 158 | void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
|
---|
| 159 |
|
---|
[93462c6] | 160 | void renderMainMenu();
|
---|
| 161 | void renderMainMenuGui();
|
---|
| 162 |
|
---|
[92b1e90] | 163 | void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 164 | GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
|
---|
[6877ef3] | 165 | GLuint color_vao, GLuint texture_vao,
|
---|
[b155f13] | 166 | GLuint colors_vbo, GLuint selected_colors_vbo,
|
---|
[92b1e90] | 167 | SceneObject* selectedObject);
|
---|
[93462c6] | 168 | void renderSceneGui();
|
---|
[d12d003] | 169 |
|
---|
[c3c3158] | 170 | void spawnAsteroid(vec3 pos, GLuint shader,
|
---|
| 171 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 172 | GLuint points_vbo,
|
---|
| 173 | GLuint colors_vbo,
|
---|
| 174 | GLuint selected_colors_vbo,
|
---|
| 175 | GLuint texcoords_vbo,
|
---|
| 176 | GLuint normals_vbo,
|
---|
| 177 | GLuint ubo,
|
---|
[6877ef3] | 178 | GLuint model_mat_idx_vbo);
|
---|
[cf2d1e5] | 179 |
|
---|
[5527206] | 180 | float getRandomNum(float low, float high);
|
---|
| 181 |
|
---|
| 182 | #define NUM_KEYS (512)
|
---|
| 183 | #define ONE_DEG_IN_RAD ((2.0f * M_PI) / 360.0f) // 0.017444444 (maybe make this a const instead)
|
---|
| 184 |
|
---|
| 185 | const int KEY_STATE_UNCHANGED = -1;
|
---|
| 186 | const bool FULLSCREEN = false;
|
---|
| 187 | const bool SHOW_FPS = false;
|
---|
| 188 | const bool DISABLE_VSYNC = false; // disable vsync to see real framerate
|
---|
| 189 | unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
|
---|
| 190 |
|
---|
| 191 | int key_state[NUM_KEYS];
|
---|
| 192 | bool key_pressed[NUM_KEYS];
|
---|
| 193 |
|
---|
| 194 | int width = 640;
|
---|
| 195 | int height = 480;
|
---|
| 196 |
|
---|
| 197 | double fps;
|
---|
| 198 |
|
---|
| 199 | vec3 cam_pos;
|
---|
| 200 |
|
---|
| 201 | mat4 view_mat;
|
---|
| 202 | mat4 proj_mat;
|
---|
| 203 |
|
---|
| 204 | // TODO: Consider using a list instead since it will make element deletion more efficient
|
---|
| 205 | vector<SceneObject> objects;
|
---|
| 206 | queue<Event> events;
|
---|
| 207 |
|
---|
| 208 | SceneObject* clickedObject = NULL;
|
---|
| 209 | SceneObject* selectedObject = NULL;
|
---|
| 210 |
|
---|
| 211 | float NEAR_CLIP = 0.1f;
|
---|
| 212 | float FAR_CLIP = 100.0f;
|
---|
| 213 |
|
---|
[95595de] | 214 | // TODO: Should really have some array or struct of UI-related variables
|
---|
[5527206] | 215 | bool isRunning = true;
|
---|
| 216 |
|
---|
| 217 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
---|
| 218 |
|
---|
[c1ca5b5] | 219 | int main(int argc, char* argv[]) {
|
---|
[5272b6b] | 220 | cout << "New OpenGL Game" << endl;
|
---|
| 221 |
|
---|
[ec4456b] | 222 | if (!restart_gl_log()) {}
|
---|
| 223 | gl_log("starting GLFW\n%s\n", glfwGetVersionString());
|
---|
[22b2c37] | 224 |
|
---|
[ec4456b] | 225 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 226 | if (!glfwInit()) {
|
---|
| 227 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
| 228 | return 1;
|
---|
[be246ad] | 229 | }
|
---|
| 230 |
|
---|
| 231 | #ifdef __APPLE__
|
---|
| 232 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 233 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 234 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 235 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
| 236 | #endif
|
---|
[5272b6b] | 237 |
|
---|
[9f9f9a7] | 238 | glfwWindowHint(GLFW_SAMPLES, 16);
|
---|
[ec4456b] | 239 |
|
---|
| 240 | GLFWwindow* window = NULL;
|
---|
[e856d62] | 241 | GLFWmonitor* mon = NULL;
|
---|
[ec4456b] | 242 |
|
---|
| 243 | if (FULLSCREEN) {
|
---|
[e856d62] | 244 | mon = glfwGetPrimaryMonitor();
|
---|
[ec4456b] | 245 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 246 |
|
---|
| 247 | width = vmode->width;
|
---|
| 248 | height = vmode->height;
|
---|
[e856d62] | 249 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
[ec4456b] | 250 | }
|
---|
[e856d62] | 251 | window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
|
---|
[ec4456b] | 252 |
|
---|
[5272b6b] | 253 | if (!window) {
|
---|
| 254 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
| 255 | glfwTerminate();
|
---|
| 256 | return 1;
|
---|
| 257 | }
|
---|
[c62eee6] | 258 |
|
---|
[644a2e4] | 259 | glfwMakeContextCurrent(window);
|
---|
[5272b6b] | 260 | glewExperimental = GL_TRUE;
|
---|
| 261 | glewInit();
|
---|
| 262 |
|
---|
[5527206] | 263 | srand(time(0));
|
---|
| 264 |
|
---|
[14ff67c] | 265 | /*
|
---|
| 266 | * RENDERING ALGORITHM NOTES:
|
---|
| 267 | *
|
---|
| 268 | * Basically, I need to split my objects into groups, so that each group fits into
|
---|
| 269 | * GL_MAX_UNIFORM_BLOCK_SIZE. I need to have an offset and a size for each group.
|
---|
| 270 | * Getting the offset is straitforward. The size may as well be GL_MAX_UNIFORM_BLOCK_SIZE
|
---|
| 271 | * for each group, since it seems that smaller sizes just round up to the nearest GL_MAX_UNIFORM_BLOCK_SIZE
|
---|
| 272 | *
|
---|
| 273 | * I'll need to have a loop inside my render loop that calls glBindBufferRange(GL_UNIFORM_BUFFER, ...
|
---|
| 274 | * for every 1024 objects and then draws all those objects with one glDraw call.
|
---|
| 275 | *
|
---|
[0d5c100] | 276 | * Since I currently have very few objects, I'll wait to implement this until I have
|
---|
| 277 | * a reasonable number of objects always using the same shader.
|
---|
[14ff67c] | 278 | */
|
---|
| 279 |
|
---|
| 280 | GLint UNIFORM_BUFFER_OFFSET_ALIGNMENT, MAX_UNIFORM_BLOCK_SIZE;
|
---|
| 281 | glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UNIFORM_BUFFER_OFFSET_ALIGNMENT);
|
---|
| 282 | glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 283 |
|
---|
| 284 | MAX_UNIFORMS = MAX_UNIFORM_BLOCK_SIZE / sizeof(mat4);
|
---|
| 285 |
|
---|
| 286 | cout << "UNIFORM_BUFFER_OFFSET_ALIGNMENT: " << UNIFORM_BUFFER_OFFSET_ALIGNMENT << endl;
|
---|
| 287 | cout << "MAX_UNIFORMS: " << MAX_UNIFORMS << endl;
|
---|
| 288 |
|
---|
[c1ca5b5] | 289 | // Setup Dear ImGui binding
|
---|
| 290 | IMGUI_CHECKVERSION();
|
---|
| 291 | ImGui::CreateContext();
|
---|
| 292 | ImGuiIO& io = ImGui::GetIO(); (void)io;
|
---|
| 293 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
---|
| 294 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
---|
| 295 | ImGui_ImplGlfwGL3_Init(window, true);
|
---|
| 296 |
|
---|
| 297 | // Setup style
|
---|
| 298 | ImGui::StyleColorsDark();
|
---|
| 299 | //ImGui::StyleColorsClassic();
|
---|
| 300 |
|
---|
| 301 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
[f7d35da] | 302 | glfwSetKeyCallback(window, key_callback);
|
---|
[c1ca5b5] | 303 |
|
---|
[5272b6b] | 304 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 305 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
| 306 | printf("Renderer: %s\n", renderer);
|
---|
| 307 | printf("OpenGL version supported %s\n", version);
|
---|
[93baa0e] | 308 |
|
---|
[9f9f9a7] | 309 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 310 |
|
---|
[5272b6b] | 311 | glEnable(GL_DEPTH_TEST);
|
---|
| 312 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 313 |
|
---|
[93baa0e] | 314 | glEnable(GL_CULL_FACE);
|
---|
| 315 | // glCullFace(GL_BACK);
|
---|
| 316 | // glFrontFace(GL_CW);
|
---|
| 317 |
|
---|
[9f9f9a7] | 318 | /*
|
---|
[485424b] | 319 | int x, y;
|
---|
| 320 | unsigned char* texImage = loadImage("test.png", &x, &y);
|
---|
| 321 | if (texImage) {
|
---|
| 322 | cout << "Yay, I loaded an image!" << endl;
|
---|
| 323 | cout << x << endl;
|
---|
| 324 | cout << y << endl;
|
---|
[e856d62] | 325 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
[485424b] | 326 | }
|
---|
| 327 |
|
---|
[9f9f9a7] | 328 | GLuint testTex = 0;
|
---|
| 329 | glGenTextures(1, &testTex);
|
---|
| 330 | glActiveTexture(GL_TEXTURE0);
|
---|
| 331 | glBindTexture(GL_TEXTURE_2D, testTex);
|
---|
| 332 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 333 |
|
---|
| 334 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 335 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 336 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 337 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 338 | */
|
---|
| 339 |
|
---|
| 340 | int x, y;
|
---|
| 341 | unsigned char* texImage = loadImage("laser.png", &x, &y);
|
---|
| 342 | if (texImage) {
|
---|
| 343 | cout << "Laser texture loaded successfully!" << endl;
|
---|
| 344 | cout << x << endl;
|
---|
| 345 | cout << y << endl;
|
---|
| 346 | printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | GLuint laserTex = 0;
|
---|
| 350 | glGenTextures(1, &laserTex);
|
---|
[485424b] | 351 | glActiveTexture(GL_TEXTURE0);
|
---|
[9f9f9a7] | 352 | glBindTexture(GL_TEXTURE_2D, laserTex);
|
---|
[485424b] | 353 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 354 |
|
---|
| 355 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 356 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 357 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 358 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 359 |
|
---|
[0d5c100] | 360 | /* RENDERING ALGORITHM
|
---|
| 361 | *
|
---|
| 362 | * Create a separate vbo for each of the following things:
|
---|
| 363 | * - points
|
---|
| 364 | * - colors
|
---|
| 365 | * - texture coordinates
|
---|
| 366 | * - selected colors
|
---|
| 367 | * - normals
|
---|
| 368 | * - indices into a ubo that stores a model matrix for each object
|
---|
| 369 | *
|
---|
| 370 | * Also, make a model matrix ubo, the entirety of which will be passed to the vertex shader.
|
---|
| 371 | * The vbo containing the correct index into the ubo (mentioned above) will be used to select
|
---|
| 372 | * the right model matrix for each point. The index in the vbo will be the saem for all points
|
---|
| 373 | * of any given object.
|
---|
| 374 | *
|
---|
| 375 | * There will be two shader programs for now, one for draing colored objects, and another for
|
---|
| 376 | * drawing textured ones. The points, normals, and model mat ubo indices will be passed to both
|
---|
| 377 | * shaders, while the colors vbo will only be passed to the colors shader, and the texcoords vbo
|
---|
| 378 | * only to the texture shader.
|
---|
| 379 | *
|
---|
| 380 | * Right now, the currently selected object is drawn using one color (specified in the selected
|
---|
| 381 | * colors vbo) regardless of whether it is normally rendering using colors or a texture. The selected
|
---|
| 382 | * object is rendering by binding the selected colors vbo in place of the colors vbo and using the colors
|
---|
| 383 | * shader. Then, the selected object is redrawn along with all other objects, but the depth buffer test
|
---|
| 384 | * prevents the unselected version of the object from appearing on the screen. This lets me render all the
|
---|
| 385 | * objects that use a particular shader using one glDrawArrays() call.
|
---|
| 386 | */
|
---|
[cffca4d] | 387 |
|
---|
[c3c3158] | 388 | map<GLuint, BufferInfo> shaderBufferInfo;
|
---|
| 389 |
|
---|
[cffca4d] | 390 | GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag");
|
---|
| 391 | GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
|
---|
[b155f13] | 392 | GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");
|
---|
[cffca4d] | 393 |
|
---|
[c3c3158] | 394 | shaderBufferInfo[color_sp] = BufferInfo();
|
---|
| 395 | shaderBufferInfo[texture_sp] = BufferInfo();
|
---|
[b155f13] | 396 | shaderBufferInfo[laser_sp] = BufferInfo();
|
---|
[c3c3158] | 397 |
|
---|
[f9a242b] | 398 | SceneObject obj;
|
---|
[07ed460] | 399 | mat4 T_model, R_model;
|
---|
| 400 |
|
---|
[92b1e90] | 401 | // TODO: Confirm there's nothing I need from the commented out models and delete them
|
---|
| 402 | // (Check to make sure the textured square is drawn correctly)
|
---|
| 403 |
|
---|
[cf2d1e5] | 404 | /*
|
---|
[07ed460] | 405 | // triangle
|
---|
[f9a242b] | 406 | obj = SceneObject();
|
---|
| 407 | obj.shader_program = color_sp;
|
---|
| 408 | obj.points = {
|
---|
[d12d003] | 409 | 0.0f, 0.5f, 0.0f,
|
---|
| 410 | -0.5f, -0.5f, 0.0f,
|
---|
| 411 | 0.5f, -0.5f, 0.0f,
|
---|
| 412 | 0.5f, -0.5f, 0.0f,
|
---|
| 413 | -0.5f, -0.5f, 0.0f,
|
---|
| 414 | 0.0f, 0.5f, 0.0f,
|
---|
[516668e] | 415 | };
|
---|
[f9a242b] | 416 | obj.colors = {
|
---|
[07ed460] | 417 | 1.0f, 0.0f, 0.0f,
|
---|
| 418 | 0.0f, 0.0f, 1.0f,
|
---|
| 419 | 0.0f, 1.0f, 0.0f,
|
---|
| 420 | 0.0f, 1.0f, 0.0f,
|
---|
| 421 | 0.0f, 0.0f, 1.0f,
|
---|
| 422 | 1.0f, 0.0f, 0.0f,
|
---|
[93baa0e] | 423 | };
|
---|
[c94a699] | 424 | obj.texcoords = { 0.0f };
|
---|
[f9a242b] | 425 | obj.selected_colors = {
|
---|
[07ed460] | 426 | 0.0f, 1.0f, 0.0f,
|
---|
| 427 | 0.0f, 1.0f, 0.0f,
|
---|
| 428 | 0.0f, 1.0f, 0.0f,
|
---|
| 429 | 0.0f, 1.0f, 0.0f,
|
---|
| 430 | 0.0f, 1.0f, 0.0f,
|
---|
| 431 | 0.0f, 1.0f, 0.0f,
|
---|
| 432 | };
|
---|
| 433 |
|
---|
[dba67b2] | 434 | T_model = translate(mat4(1.0f), vec3(0.45f, -1.5f, 0.0f));
|
---|
| 435 | R_model = rotate(mat4(1.0f), 0.0f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[5c403fe] | 436 | obj.model_base = T_model*R_model;
|
---|
[f9a242b] | 437 |
|
---|
[c3c3158] | 438 | addObjectToSceneDuringInit(obj);
|
---|
[33a9664] | 439 |
|
---|
[07ed460] | 440 | // square
|
---|
[f9a242b] | 441 | obj = SceneObject();
|
---|
| 442 | obj.shader_program = texture_sp;
|
---|
| 443 | obj.points = {
|
---|
[b73cb3b] | 444 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 445 | -0.5f, 0.5f, 0.0f,
|
---|
| 446 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 447 | 0.5f, 0.5f, 0.0f,
|
---|
[d12d003] | 448 | -0.5f, -0.5f, 0.0f,
|
---|
[b73cb3b] | 449 | 0.5f, -0.5f, 0.0f,
|
---|
[64a70f4] | 450 | };
|
---|
[c94a699] | 451 | obj.colors = { 0.0f };
|
---|
[f9a242b] | 452 | obj.texcoords = {
|
---|
[64a70f4] | 453 | 1.0f, 1.0f,
|
---|
| 454 | 0.0f, 1.0f,
|
---|
[07ed460] | 455 | 0.0f, 0.0f,
|
---|
| 456 | 1.0f, 1.0f,
|
---|
| 457 | 0.0f, 0.0f,
|
---|
| 458 | 1.0f, 0.0f
|
---|
[485424b] | 459 | };
|
---|
[f9a242b] | 460 | obj.selected_colors = {
|
---|
[9f4986b] | 461 | 0.0f, 0.6f, 0.9f,
|
---|
| 462 | 0.0f, 0.6f, 0.9f,
|
---|
| 463 | 0.0f, 0.6f, 0.9f,
|
---|
| 464 | 0.0f, 0.6f, 0.9f,
|
---|
| 465 | 0.0f, 0.6f, 0.9f,
|
---|
| 466 | 0.0f, 0.6f, 0.9f,
|
---|
[19c9338] | 467 | };
|
---|
[df652d5] | 468 |
|
---|
[dba67b2] | 469 | T_model = translate(mat4(1.0f), vec3(-0.5f, -1.5f, -1.00f));
|
---|
| 470 | R_model = rotate(mat4(1.0f), 0.5f, vec3(0.0f, 1.0f, 0.0f));
|
---|
[5c403fe] | 471 | obj.model_base = T_model*R_model;
|
---|
[f9a242b] | 472 |
|
---|
[c3c3158] | 473 | addObjectToSceneDuringInit(obj);
|
---|
[cf2d1e5] | 474 | */
|
---|
[f9a242b] | 475 |
|
---|
| 476 | // player ship
|
---|
| 477 | obj = SceneObject();
|
---|
[92b1e90] | 478 | obj.type = TYPE_SHIP;
|
---|
[f9a242b] | 479 | obj.shader_program = color_sp;
|
---|
| 480 | obj.points = {
|
---|
[81f28c0] | 481 | //back
|
---|
[3d06b4e] | 482 | -0.5f, 0.3f, 0.0f,
|
---|
| 483 | -0.5f, 0.0f, 0.0f,
|
---|
| 484 | 0.5f, 0.0f, 0.0f,
|
---|
| 485 | -0.5f, 0.3f, 0.0f,
|
---|
| 486 | 0.5f, 0.0f, 0.0f,
|
---|
| 487 | 0.5f, 0.3f, 0.0f,
|
---|
[81f28c0] | 488 |
|
---|
| 489 | // left back
|
---|
[3d06b4e] | 490 | -0.5f, 0.3f, -2.0f,
|
---|
| 491 | -0.5f, 0.0f, -2.0f,
|
---|
| 492 | -0.5f, 0.0f, 0.0f,
|
---|
| 493 | -0.5f, 0.3f, -2.0f,
|
---|
| 494 | -0.5f, 0.0f, 0.0f,
|
---|
| 495 | -0.5f, 0.3f, 0.0f,
|
---|
[81f28c0] | 496 |
|
---|
| 497 | // right back
|
---|
[3d06b4e] | 498 | 0.5f, 0.3f, 0.0f,
|
---|
| 499 | 0.5f, 0.0f, 0.0f,
|
---|
| 500 | 0.5f, 0.0f, -2.0f,
|
---|
| 501 | 0.5f, 0.3f, 0.0f,
|
---|
| 502 | 0.5f, 0.0f, -2.0f,
|
---|
| 503 | 0.5f, 0.3f, -2.0f,
|
---|
[81f28c0] | 504 |
|
---|
| 505 | // left mid
|
---|
[3d06b4e] | 506 | -0.25f, 0.3f, -3.0f,
|
---|
| 507 | -0.25f, 0.0f, -3.0f,
|
---|
| 508 | -0.5f, 0.0f, -2.0f,
|
---|
| 509 | -0.25f, 0.3f, -3.0f,
|
---|
| 510 | -0.5f, 0.0f, -2.0f,
|
---|
| 511 | -0.5f, 0.3f, -2.0f,
|
---|
[81f28c0] | 512 |
|
---|
| 513 | // right mid
|
---|
[3d06b4e] | 514 | 0.5f, 0.3f, -2.0f,
|
---|
| 515 | 0.5f, 0.0f, -2.0f,
|
---|
| 516 | 0.25f, 0.0f, -3.0f,
|
---|
| 517 | 0.5f, 0.3f, -2.0f,
|
---|
| 518 | 0.25f, 0.0f, -3.0f,
|
---|
| 519 | 0.25f, 0.3f, -3.0f,
|
---|
[81f28c0] | 520 |
|
---|
| 521 | // left front
|
---|
[3d06b4e] | 522 | 0.0f, 0.0f, -3.5f,
|
---|
| 523 | -0.25f, 0.0f, -3.0f,
|
---|
| 524 | -0.25f, 0.3f, -3.0f,
|
---|
[81f28c0] | 525 |
|
---|
| 526 | // right front
|
---|
[3d06b4e] | 527 | 0.25f, 0.3f, -3.0f,
|
---|
| 528 | 0.25f, 0.0f, -3.0f,
|
---|
| 529 | 0.0f, 0.0f, -3.5f,
|
---|
[81f28c0] | 530 |
|
---|
| 531 | // top back
|
---|
[3d06b4e] | 532 | -0.5f, 0.3f, -2.0f,
|
---|
| 533 | -0.5f, 0.3f, 0.0f,
|
---|
| 534 | 0.5f, 0.3f, 0.0f,
|
---|
| 535 | -0.5f, 0.3f, -2.0f,
|
---|
| 536 | 0.5f, 0.3f, 0.0f,
|
---|
| 537 | 0.5f, 0.3f, -2.0f,
|
---|
[81f28c0] | 538 |
|
---|
[20e0020] | 539 | // bottom back
|
---|
[3d06b4e] | 540 | -0.5f, 0.0f, 0.0f,
|
---|
| 541 | -0.5f, 0.0f, -2.0f,
|
---|
| 542 | 0.5f, 0.0f, 0.0f,
|
---|
| 543 | 0.5f, 0.0f, 0.0f,
|
---|
| 544 | -0.5f, 0.0f, -2.0f,
|
---|
| 545 | 0.5f, 0.0f, -2.0f,
|
---|
[20e0020] | 546 |
|
---|
| 547 | // top mid
|
---|
[3d06b4e] | 548 | -0.25f, 0.3f, -3.0f,
|
---|
| 549 | -0.5f, 0.3f, -2.0f,
|
---|
| 550 | 0.5f, 0.3f, -2.0f,
|
---|
| 551 | -0.25f, 0.3f, -3.0f,
|
---|
| 552 | 0.5f, 0.3f, -2.0f,
|
---|
| 553 | 0.25f, 0.3f, -3.0f,
|
---|
[20e0020] | 554 |
|
---|
| 555 | // bottom mid
|
---|
[3d06b4e] | 556 | -0.5f, 0.0f, -2.0f,
|
---|
| 557 | -0.25f, 0.0f, -3.0f,
|
---|
| 558 | 0.5f, 0.0f, -2.0f,
|
---|
| 559 | 0.5f, 0.0f, -2.0f,
|
---|
| 560 | -0.25f, 0.0f, -3.0f,
|
---|
| 561 | 0.25f, 0.0f, -3.0f,
|
---|
[20e0020] | 562 |
|
---|
| 563 | // top front
|
---|
[3d06b4e] | 564 | -0.25f, 0.3f, -3.0f,
|
---|
| 565 | 0.25f, 0.3f, -3.0f,
|
---|
| 566 | 0.0f, 0.0f, -3.5f,
|
---|
[20e0020] | 567 |
|
---|
| 568 | // bottom front
|
---|
[3d06b4e] | 569 | 0.25f, 0.0f, -3.0f,
|
---|
| 570 | -0.25f, 0.0f, -3.0f,
|
---|
| 571 | 0.0f, 0.0f, -3.5f,
|
---|
[20e0020] | 572 |
|
---|
| 573 | // left wing start back
|
---|
[3d06b4e] | 574 | -1.5f, 0.3f, 0.0f,
|
---|
| 575 | -1.5f, 0.0f, 0.0f,
|
---|
| 576 | -0.5f, 0.0f, 0.0f,
|
---|
| 577 | -1.5f, 0.3f, 0.0f,
|
---|
| 578 | -0.5f, 0.0f, 0.0f,
|
---|
| 579 | -0.5f, 0.3f, 0.0f,
|
---|
[20e0020] | 580 |
|
---|
| 581 | // left wing start top
|
---|
[3d06b4e] | 582 | -0.5f, 0.3f, -0.3f,
|
---|
| 583 | -1.3f, 0.3f, -0.3f,
|
---|
| 584 | -1.5f, 0.3f, 0.0f,
|
---|
| 585 | -0.5f, 0.3f, -0.3f,
|
---|
| 586 | -1.5f, 0.3f, 0.0f,
|
---|
| 587 | -0.5f, 0.3f, 0.0f,
|
---|
[20e0020] | 588 |
|
---|
| 589 | // left wing start front
|
---|
[3d06b4e] | 590 | -0.5f, 0.3f, -0.3f,
|
---|
| 591 | -0.5f, 0.0f, -0.3f,
|
---|
| 592 | -1.3f, 0.0f, -0.3f,
|
---|
| 593 | -0.5f, 0.3f, -0.3f,
|
---|
| 594 | -1.3f, 0.0f, -0.3f,
|
---|
| 595 | -1.3f, 0.3f, -0.3f,
|
---|
[20e0020] | 596 |
|
---|
| 597 | // left wing start bottom
|
---|
[3d06b4e] | 598 | -0.5f, 0.0f, 0.0f,
|
---|
| 599 | -1.5f, 0.0f, 0.0f,
|
---|
| 600 | -1.3f, 0.0f, -0.3f,
|
---|
| 601 | -0.5f, 0.0f, 0.0f,
|
---|
| 602 | -1.3f, 0.0f, -0.3f,
|
---|
| 603 | -0.5f, 0.0f, -0.3f,
|
---|
[20e0020] | 604 |
|
---|
| 605 | // left wing end outside
|
---|
[3d06b4e] | 606 | -1.5f, 0.3f, 0.0f,
|
---|
| 607 | -2.2f, 0.15f, -0.8f,
|
---|
| 608 | -1.5f, 0.0f, 0.0f,
|
---|
[20e0020] | 609 |
|
---|
| 610 | // left wing end top
|
---|
[3d06b4e] | 611 | -1.3f, 0.3f, -0.3f,
|
---|
| 612 | -2.2f, 0.15f, -0.8f,
|
---|
| 613 | -1.5f, 0.3f, 0.0f,
|
---|
[20e0020] | 614 |
|
---|
| 615 | // left wing end front
|
---|
[3d06b4e] | 616 | -1.3f, 0.0f, -0.3f,
|
---|
| 617 | -2.2f, 0.15f, -0.8f,
|
---|
| 618 | -1.3f, 0.3f, -0.3f,
|
---|
[20e0020] | 619 |
|
---|
| 620 | // left wing end bottom
|
---|
[3d06b4e] | 621 | -1.5f, 0.0f, 0.0f,
|
---|
| 622 | -2.2f, 0.15f, -0.8f,
|
---|
| 623 | -1.3f, 0.0f, -0.3f,
|
---|
[20e0020] | 624 |
|
---|
| 625 | // right wing start back
|
---|
[3d06b4e] | 626 | 1.5f, 0.0f, 0.0f,
|
---|
| 627 | 1.5f, 0.3f, 0.0f,
|
---|
| 628 | 0.5f, 0.0f, 0.0f,
|
---|
| 629 | 0.5f, 0.0f, 0.0f,
|
---|
| 630 | 1.5f, 0.3f, 0.0f,
|
---|
| 631 | 0.5f, 0.3f, 0.0f,
|
---|
[20e0020] | 632 |
|
---|
| 633 | // right wing start top
|
---|
[3d06b4e] | 634 | 1.3f, 0.3f, -0.3f,
|
---|
| 635 | 0.5f, 0.3f, -0.3f,
|
---|
| 636 | 1.5f, 0.3f, 0.0f,
|
---|
| 637 | 1.5f, 0.3f, 0.0f,
|
---|
| 638 | 0.5f, 0.3f, -0.3f,
|
---|
| 639 | 0.5f, 0.3f, 0.0f,
|
---|
[20e0020] | 640 |
|
---|
| 641 | // right wing start front
|
---|
[3d06b4e] | 642 | 0.5f, 0.0f, -0.3f,
|
---|
| 643 | 0.5f, 0.3f, -0.3f,
|
---|
| 644 | 1.3f, 0.0f, -0.3f,
|
---|
| 645 | 1.3f, 0.0f, -0.3f,
|
---|
| 646 | 0.5f, 0.3f, -0.3f,
|
---|
| 647 | 1.3f, 0.3f, -0.3f,
|
---|
[20e0020] | 648 |
|
---|
| 649 | // right wing start bottom
|
---|
[3d06b4e] | 650 | 1.5f, 0.0f, 0.0f,
|
---|
| 651 | 0.5f, 0.0f, 0.0f,
|
---|
| 652 | 1.3f, 0.0f, -0.3f,
|
---|
| 653 | 1.3f, 0.0f, -0.3f,
|
---|
| 654 | 0.5f, 0.0f, 0.0f,
|
---|
| 655 | 0.5f, 0.0f, -0.3f,
|
---|
[20e0020] | 656 |
|
---|
| 657 | // right wing end outside
|
---|
[3d06b4e] | 658 | 2.2f, 0.15f, -0.8f,
|
---|
| 659 | 1.5f, 0.3f, 0.0f,
|
---|
| 660 | 1.5f, 0.0f, 0.0f,
|
---|
[20e0020] | 661 |
|
---|
| 662 | // right wing end top
|
---|
[3d06b4e] | 663 | 2.2f, 0.15f, -0.8f,
|
---|
| 664 | 1.3f, 0.3f, -0.3f,
|
---|
| 665 | 1.5f, 0.3f, 0.0f,
|
---|
[20e0020] | 666 |
|
---|
| 667 | // right wing end front
|
---|
[3d06b4e] | 668 | 2.2f, 0.15f, -0.8f,
|
---|
| 669 | 1.3f, 0.0f, -0.3f,
|
---|
| 670 | 1.3f, 0.3f, -0.3f,
|
---|
[20e0020] | 671 |
|
---|
| 672 | // right wing end bottom
|
---|
[3d06b4e] | 673 | 2.2f, 0.15f, -0.8f,
|
---|
| 674 | 1.5f, 0.0f, 0.0f,
|
---|
| 675 | 1.3f, 0.0f, -0.3f,
|
---|
[f9a242b] | 676 | };
|
---|
| 677 | obj.colors = {
|
---|
| 678 | 0.0f, 0.0f, 0.3f,
|
---|
| 679 | 0.0f, 0.0f, 0.3f,
|
---|
| 680 | 0.0f, 0.0f, 0.3f,
|
---|
| 681 | 0.0f, 0.0f, 0.3f,
|
---|
| 682 | 0.0f, 0.0f, 0.3f,
|
---|
| 683 | 0.0f, 0.0f, 0.3f,
|
---|
[81f28c0] | 684 |
|
---|
| 685 | 0.0f, 0.0f, 0.3f,
|
---|
| 686 | 0.0f, 0.0f, 0.3f,
|
---|
| 687 | 0.0f, 0.0f, 0.3f,
|
---|
| 688 | 0.0f, 0.0f, 0.3f,
|
---|
| 689 | 0.0f, 0.0f, 0.3f,
|
---|
| 690 | 0.0f, 0.0f, 0.3f,
|
---|
| 691 |
|
---|
| 692 | 0.0f, 0.0f, 0.3f,
|
---|
| 693 | 0.0f, 0.0f, 0.3f,
|
---|
| 694 | 0.0f, 0.0f, 0.3f,
|
---|
| 695 | 0.0f, 0.0f, 0.3f,
|
---|
| 696 | 0.0f, 0.0f, 0.3f,
|
---|
| 697 | 0.0f, 0.0f, 0.3f,
|
---|
| 698 |
|
---|
| 699 | 0.0f, 0.0f, 0.3f,
|
---|
| 700 | 0.0f, 0.0f, 0.3f,
|
---|
| 701 | 0.0f, 0.0f, 0.3f,
|
---|
| 702 | 0.0f, 0.0f, 0.3f,
|
---|
| 703 | 0.0f, 0.0f, 0.3f,
|
---|
| 704 | 0.0f, 0.0f, 0.3f,
|
---|
| 705 |
|
---|
| 706 | 0.0f, 0.0f, 0.3f,
|
---|
| 707 | 0.0f, 0.0f, 0.3f,
|
---|
| 708 | 0.0f, 0.0f, 0.3f,
|
---|
| 709 | 0.0f, 0.0f, 0.3f,
|
---|
| 710 | 0.0f, 0.0f, 0.3f,
|
---|
| 711 | 0.0f, 0.0f, 0.3f,
|
---|
| 712 |
|
---|
[cf2d1e5] | 713 | 0.0f, 0.0f, 1.0f,
|
---|
| 714 | 0.0f, 0.0f, 1.0f,
|
---|
| 715 | 0.0f, 0.0f, 1.0f,
|
---|
[81f28c0] | 716 |
|
---|
[cf2d1e5] | 717 | 0.0f, 0.0f, 1.0f,
|
---|
| 718 | 0.0f, 0.0f, 1.0f,
|
---|
| 719 | 0.0f, 0.0f, 1.0f,
|
---|
[81f28c0] | 720 |
|
---|
[cf2d1e5] | 721 | 0.0f, 0.0f, 1.0f,
|
---|
| 722 | 0.0f, 0.0f, 1.0f,
|
---|
| 723 | 0.0f, 0.0f, 1.0f,
|
---|
| 724 | 0.0f, 0.0f, 1.0f,
|
---|
| 725 | 0.0f, 0.0f, 1.0f,
|
---|
| 726 | 0.0f, 0.0f, 1.0f,
|
---|
[81f28c0] | 727 |
|
---|
[cf2d1e5] | 728 | 0.0f, 0.0f, 1.0f,
|
---|
| 729 | 0.0f, 0.0f, 1.0f,
|
---|
| 730 | 0.0f, 0.0f, 1.0f,
|
---|
| 731 | 0.0f, 0.0f, 1.0f,
|
---|
| 732 | 0.0f, 0.0f, 1.0f,
|
---|
| 733 | 0.0f, 0.0f, 1.0f,
|
---|
[81f28c0] | 734 |
|
---|
[cf2d1e5] | 735 | 0.0f, 0.0f, 1.0f,
|
---|
| 736 | 0.0f, 0.0f, 1.0f,
|
---|
| 737 | 0.0f, 0.0f, 1.0f,
|
---|
| 738 | 0.0f, 0.0f, 1.0f,
|
---|
| 739 | 0.0f, 0.0f, 1.0f,
|
---|
| 740 | 0.0f, 0.0f, 1.0f,
|
---|
[81f28c0] | 741 |
|
---|
[cf2d1e5] | 742 | 0.0f, 0.0f, 1.0f,
|
---|
| 743 | 0.0f, 0.0f, 1.0f,
|
---|
| 744 | 0.0f, 0.0f, 1.0f,
|
---|
| 745 | 0.0f, 0.0f, 1.0f,
|
---|
| 746 | 0.0f, 0.0f, 1.0f,
|
---|
| 747 | 0.0f, 0.0f, 1.0f,
|
---|
[81f28c0] | 748 |
|
---|
| 749 | 0.0f, 0.0f, 0.3f,
|
---|
| 750 | 0.0f, 0.0f, 0.3f,
|
---|
| 751 | 0.0f, 0.0f, 0.3f,
|
---|
[20e0020] | 752 |
|
---|
[81f28c0] | 753 | 0.0f, 0.0f, 0.3f,
|
---|
| 754 | 0.0f, 0.0f, 0.3f,
|
---|
| 755 | 0.0f, 0.0f, 0.3f,
|
---|
| 756 |
|
---|
| 757 | 0.0f, 0.0f, 0.3f,
|
---|
| 758 | 0.0f, 0.0f, 0.3f,
|
---|
| 759 | 0.0f, 0.0f, 0.3f,
|
---|
| 760 | 0.0f, 0.0f, 0.3f,
|
---|
| 761 | 0.0f, 0.0f, 0.3f,
|
---|
| 762 | 0.0f, 0.0f, 0.3f,
|
---|
| 763 |
|
---|
[20e0020] | 764 | 0.0f, 0.0f, 0.3f,
|
---|
| 765 | 0.0f, 0.0f, 0.3f,
|
---|
| 766 | 0.0f, 0.0f, 0.3f,
|
---|
| 767 | 0.0f, 0.0f, 0.3f,
|
---|
| 768 | 0.0f, 0.0f, 0.3f,
|
---|
| 769 | 0.0f, 0.0f, 0.3f,
|
---|
| 770 |
|
---|
| 771 | 0.0f, 0.0f, 0.3f,
|
---|
| 772 | 0.0f, 0.0f, 0.3f,
|
---|
| 773 | 0.0f, 0.0f, 0.3f,
|
---|
| 774 | 0.0f, 0.0f, 0.3f,
|
---|
| 775 | 0.0f, 0.0f, 0.3f,
|
---|
| 776 | 0.0f, 0.0f, 0.3f,
|
---|
| 777 |
|
---|
| 778 | 0.0f, 0.0f, 0.3f,
|
---|
| 779 | 0.0f, 0.0f, 0.3f,
|
---|
| 780 | 0.0f, 0.0f, 0.3f,
|
---|
| 781 | 0.0f, 0.0f, 0.3f,
|
---|
| 782 | 0.0f, 0.0f, 0.3f,
|
---|
| 783 | 0.0f, 0.0f, 0.3f,
|
---|
| 784 |
|
---|
| 785 | 0.0f, 0.0f, 0.3f,
|
---|
| 786 | 0.0f, 0.0f, 0.3f,
|
---|
| 787 | 0.0f, 0.0f, 0.3f,
|
---|
| 788 |
|
---|
| 789 | 0.0f, 0.0f, 0.3f,
|
---|
| 790 | 0.0f, 0.0f, 0.3f,
|
---|
| 791 | 0.0f, 0.0f, 0.3f,
|
---|
| 792 |
|
---|
| 793 | 0.0f, 0.0f, 0.3f,
|
---|
| 794 | 0.0f, 0.0f, 0.3f,
|
---|
| 795 | 0.0f, 0.0f, 0.3f,
|
---|
| 796 |
|
---|
| 797 | 0.0f, 0.0f, 0.3f,
|
---|
| 798 | 0.0f, 0.0f, 0.3f,
|
---|
| 799 | 0.0f, 0.0f, 0.3f,
|
---|
| 800 |
|
---|
| 801 | 0.0f, 0.0f, 0.3f,
|
---|
| 802 | 0.0f, 0.0f, 0.3f,
|
---|
| 803 | 0.0f, 0.0f, 0.3f,
|
---|
| 804 | 0.0f, 0.0f, 0.3f,
|
---|
| 805 | 0.0f, 0.0f, 0.3f,
|
---|
| 806 | 0.0f, 0.0f, 0.3f,
|
---|
| 807 |
|
---|
| 808 | 0.0f, 0.0f, 0.3f,
|
---|
| 809 | 0.0f, 0.0f, 0.3f,
|
---|
| 810 | 0.0f, 0.0f, 0.3f,
|
---|
| 811 | 0.0f, 0.0f, 0.3f,
|
---|
| 812 | 0.0f, 0.0f, 0.3f,
|
---|
| 813 | 0.0f, 0.0f, 0.3f,
|
---|
| 814 |
|
---|
| 815 | 0.0f, 0.0f, 0.3f,
|
---|
| 816 | 0.0f, 0.0f, 0.3f,
|
---|
| 817 | 0.0f, 0.0f, 0.3f,
|
---|
| 818 | 0.0f, 0.0f, 0.3f,
|
---|
| 819 | 0.0f, 0.0f, 0.3f,
|
---|
| 820 | 0.0f, 0.0f, 0.3f,
|
---|
| 821 |
|
---|
| 822 | 0.0f, 0.0f, 0.3f,
|
---|
| 823 | 0.0f, 0.0f, 0.3f,
|
---|
| 824 | 0.0f, 0.0f, 0.3f,
|
---|
| 825 | 0.0f, 0.0f, 0.3f,
|
---|
| 826 | 0.0f, 0.0f, 0.3f,
|
---|
| 827 | 0.0f, 0.0f, 0.3f,
|
---|
| 828 |
|
---|
| 829 | 0.0f, 0.0f, 0.3f,
|
---|
| 830 | 0.0f, 0.0f, 0.3f,
|
---|
| 831 | 0.0f, 0.0f, 0.3f,
|
---|
| 832 |
|
---|
[81f28c0] | 833 | 0.0f, 0.0f, 0.3f,
|
---|
| 834 | 0.0f, 0.0f, 0.3f,
|
---|
| 835 | 0.0f, 0.0f, 0.3f,
|
---|
| 836 |
|
---|
| 837 | 0.0f, 0.0f, 0.3f,
|
---|
| 838 | 0.0f, 0.0f, 0.3f,
|
---|
| 839 | 0.0f, 0.0f, 0.3f,
|
---|
| 840 |
|
---|
| 841 | 0.0f, 0.0f, 0.3f,
|
---|
| 842 | 0.0f, 0.0f, 0.3f,
|
---|
| 843 | 0.0f, 0.0f, 0.3f,
|
---|
[f9a242b] | 844 | };
|
---|
[cf2d1e5] | 845 | obj.texcoords = { 0.0f };
|
---|
| 846 | obj.selected_colors = { 0.0f };
|
---|
[f9a242b] | 847 |
|
---|
[b155f13] | 848 | T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f));
|
---|
[dba67b2] | 849 | R_model = rotate(mat4(1.0f), 20.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 0.0f, 0.0f));
|
---|
| 850 | R_model = mat4(1.0f);
|
---|
| 851 | obj.model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
|
---|
[f9a242b] | 852 |
|
---|
[95595de] | 853 | obj.translate_mat = T_model;
|
---|
| 854 |
|
---|
[c3c3158] | 855 | addObjectToSceneDuringInit(obj);
|
---|
[cf2d1e5] | 856 |
|
---|
[92b1e90] | 857 | obj = SceneObject();
|
---|
| 858 | obj.shader_program = laser_sp;
|
---|
| 859 |
|
---|
| 860 | addLaserToScene(obj, vec3(0.34f, -2.0f, 1.6f), vec3(0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f);
|
---|
| 861 |
|
---|
| 862 | obj = SceneObject();
|
---|
| 863 | obj.shader_program = laser_sp;
|
---|
| 864 |
|
---|
| 865 | addLaserToScene(obj, vec3(-0.34f, -2.0f, 1.6f), vec3(-0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f);
|
---|
| 866 |
|
---|
[07ed460] | 867 | vector<SceneObject>::iterator obj_it;
|
---|
[19c9338] | 868 |
|
---|
[0d5c100] | 869 | GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo,
|
---|
[6877ef3] | 870 | normals_vbo, ubo, model_mat_idx_vbo;
|
---|
[e165b85] | 871 |
|
---|
[c3c3158] | 872 | initializeBuffers(
|
---|
| 873 | &points_vbo,
|
---|
| 874 | &colors_vbo,
|
---|
| 875 | &selected_colors_vbo,
|
---|
| 876 | &texcoords_vbo,
|
---|
| 877 | &normals_vbo,
|
---|
| 878 | &ubo,
|
---|
[6877ef3] | 879 | &model_mat_idx_vbo);
|
---|
[e165b85] | 880 |
|
---|
[0d5c100] | 881 | populateBuffers(objects,
|
---|
[c3c3158] | 882 | shaderBufferInfo,
|
---|
| 883 | points_vbo,
|
---|
| 884 | colors_vbo,
|
---|
| 885 | selected_colors_vbo,
|
---|
| 886 | texcoords_vbo,
|
---|
| 887 | normals_vbo,
|
---|
| 888 | ubo,
|
---|
[6877ef3] | 889 | model_mat_idx_vbo);
|
---|
[b155f13] | 890 |
|
---|
[92b1e90] | 891 | GLuint color_vao = 0;
|
---|
| 892 | glGenVertexArrays(1, &color_vao);
|
---|
| 893 | glBindVertexArray(color_vao);
|
---|
[516668e] | 894 |
|
---|
[8b7cfcf] | 895 | glEnableVertexAttribArray(0);
|
---|
| 896 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 897 | glEnableVertexAttribArray(2);
|
---|
[14ff67c] | 898 | glEnableVertexAttribArray(3);
|
---|
[644a2e4] | 899 |
|
---|
[cffca4d] | 900 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 901 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 902 |
|
---|
| 903 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 904 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 905 |
|
---|
[14ff67c] | 906 | glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
|
---|
| 907 | glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
|
---|
| 908 |
|
---|
[92b1e90] | 909 | GLuint texture_vao = 0;
|
---|
| 910 | glGenVertexArrays(1, &texture_vao);
|
---|
| 911 | glBindVertexArray(texture_vao);
|
---|
[644a2e4] | 912 |
|
---|
[485424b] | 913 | glEnableVertexAttribArray(0);
|
---|
| 914 | glEnableVertexAttribArray(1);
|
---|
[9dd2eb7] | 915 | glEnableVertexAttribArray(2);
|
---|
[14ff67c] | 916 | glEnableVertexAttribArray(3);
|
---|
[8b7cfcf] | 917 |
|
---|
[cffca4d] | 918 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 919 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[1a530df] | 920 |
|
---|
[cffca4d] | 921 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 922 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
| 923 |
|
---|
| 924 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 925 | glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[644a2e4] | 926 |
|
---|
[14ff67c] | 927 | glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
|
---|
| 928 | glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
|
---|
| 929 |
|
---|
[7ee66ea] | 930 | float cam_speed = 1.0f;
|
---|
[201e2f8] | 931 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[809ce16] | 932 | float cam_pitch_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[7ee66ea] | 933 |
|
---|
[b73cb3b] | 934 | // glm::lookAt can create the view matrix
|
---|
| 935 | // glm::perspective can create the projection matrix
|
---|
| 936 |
|
---|
| 937 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
[81f28c0] | 938 | //cam_pos = vec3(-2.1f, -1.5f, -1.5f); // Good position for checking ship faces
|
---|
[64a70f4] | 939 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[8d5e67b] | 940 | float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
|
---|
[7ee66ea] | 941 |
|
---|
[dba67b2] | 942 | mat4 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
| 943 | mat4 yaw_mat = rotate(mat4(1.0f), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 944 | mat4 pitch_mat = rotate(mat4(1.0f), -cam_pitch, vec3(1.0f, 0.0f, 0.0f));
|
---|
[8d5e67b] | 945 | mat4 R = pitch_mat * yaw_mat;
|
---|
[c62eee6] | 946 | view_mat = R*T;
|
---|
[7ee66ea] | 947 |
|
---|
| 948 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
| 949 | float aspect = (float)width / (float)height;
|
---|
| 950 |
|
---|
[d12d003] | 951 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 952 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 953 | float Sy = NEAR_CLIP / range;
|
---|
| 954 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 955 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[7ee66ea] | 956 |
|
---|
[c62eee6] | 957 | float proj_arr[] = {
|
---|
[7ee66ea] | 958 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 959 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 960 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 961 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
| 962 | };
|
---|
[c62eee6] | 963 | proj_mat = make_mat4(proj_arr);
|
---|
[7ee66ea] | 964 |
|
---|
[14ff67c] | 965 | GLuint ub_binding_point = 0;
|
---|
| 966 |
|
---|
[cffca4d] | 967 | GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
|
---|
| 968 | GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
|
---|
[14ff67c] | 969 | GLuint color_sp_ub_index = glGetUniformBlockIndex(color_sp, "models");
|
---|
[7ee66ea] | 970 |
|
---|
[cffca4d] | 971 | GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
|
---|
| 972 | GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
|
---|
[14ff67c] | 973 | GLuint texture_sp_ub_index = glGetUniformBlockIndex(texture_sp, "models");
|
---|
[19c9338] | 974 |
|
---|
[b155f13] | 975 | GLuint laser_view_mat_loc = glGetUniformLocation(laser_sp, "view");
|
---|
| 976 | GLuint laser_proj_mat_loc = glGetUniformLocation(laser_sp, "proj");
|
---|
[9f9f9a7] | 977 | GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color");
|
---|
[b155f13] | 978 |
|
---|
[cffca4d] | 979 | glUseProgram(color_sp);
|
---|
[19c9338] | 980 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 981 | glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[485424b] | 982 |
|
---|
[14ff67c] | 983 | glUniformBlockBinding(color_sp, color_sp_ub_index, ub_binding_point);
|
---|
| 984 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
[e165b85] | 985 |
|
---|
[cffca4d] | 986 | glUseProgram(texture_sp);
|
---|
[19c9338] | 987 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[c62eee6] | 988 | glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[7ee66ea] | 989 |
|
---|
[14ff67c] | 990 | glUniformBlockBinding(texture_sp, texture_sp_ub_index, ub_binding_point);
|
---|
| 991 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 992 |
|
---|
[9f9f9a7] | 993 |
|
---|
[b155f13] | 994 | glUseProgram(laser_sp);
|
---|
| 995 | glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 996 | glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
|
---|
[9f9f9a7] | 997 | glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f);
|
---|
[b155f13] | 998 |
|
---|
[7ee66ea] | 999 | bool cam_moved = false;
|
---|
| 1000 |
|
---|
[046ce72] | 1001 | int frame_count = 0;
|
---|
[f70ab75] | 1002 | double elapsed_seconds_fps = 0.0f;
|
---|
[5527206] | 1003 | double elapsed_seconds_spawn = 0.0f;
|
---|
[93baa0e] | 1004 | double previous_seconds = glfwGetTime();
|
---|
[046ce72] | 1005 |
|
---|
[9dd2eb7] | 1006 | // This draws wireframes. Useful for seeing separate faces and occluded objects.
|
---|
| 1007 | //glPolygonMode(GL_FRONT, GL_LINE);
|
---|
| 1008 |
|
---|
[14ff67c] | 1009 | if (DISABLE_VSYNC && SHOW_FPS) {
|
---|
| 1010 | glfwSwapInterval(0);
|
---|
| 1011 | }
|
---|
[1c81bf0] | 1012 |
|
---|
[93462c6] | 1013 | State curState = STATE_MAIN_MENU;
|
---|
| 1014 |
|
---|
[5b3462b] | 1015 | while (!glfwWindowShouldClose(window) && isRunning) {
|
---|
[93baa0e] | 1016 | double current_seconds = glfwGetTime();
|
---|
| 1017 | double elapsed_seconds = current_seconds - previous_seconds;
|
---|
| 1018 | previous_seconds = current_seconds;
|
---|
| 1019 |
|
---|
[14ff67c] | 1020 | if (SHOW_FPS) {
|
---|
| 1021 | elapsed_seconds_fps += elapsed_seconds;
|
---|
| 1022 | if (elapsed_seconds_fps > 0.25f) {
|
---|
| 1023 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
| 1024 | cout << "FPS: " << fps << endl;
|
---|
[046ce72] | 1025 |
|
---|
[14ff67c] | 1026 | frame_count = 0;
|
---|
| 1027 | elapsed_seconds_fps = 0.0f;
|
---|
| 1028 | }
|
---|
[046ce72] | 1029 |
|
---|
[14ff67c] | 1030 | frame_count++;
|
---|
| 1031 | }
|
---|
[046ce72] | 1032 |
|
---|
[f7d35da] | 1033 | // Handle events
|
---|
[baa5848] | 1034 |
|
---|
| 1035 | clickedObject = NULL;
|
---|
[f7d35da] | 1036 |
|
---|
| 1037 | // reset the all key states to KEY_STATE_UNCHANGED (something the GLFW key callback can never return)
|
---|
| 1038 | // so that GLFW_PRESS and GLFW_RELEASE are only detected once
|
---|
[cf2d1e5] | 1039 | // TODO: Change this if we ever need to act on GLFW_REPEAT (which is when a key is held down
|
---|
| 1040 | // continuously for a period of time)
|
---|
[f7d35da] | 1041 | fill(key_state, key_state + NUM_KEYS, KEY_STATE_UNCHANGED);
|
---|
| 1042 |
|
---|
[baa5848] | 1043 | glfwPollEvents();
|
---|
| 1044 |
|
---|
[93462c6] | 1045 | while (!events.empty()) {
|
---|
| 1046 | switch (events.front()) {
|
---|
| 1047 | case EVENT_GO_TO_MAIN_MENU:
|
---|
| 1048 | curState = STATE_MAIN_MENU;
|
---|
| 1049 | break;
|
---|
| 1050 | case EVENT_GO_TO_GAME:
|
---|
| 1051 | curState = STATE_GAME;
|
---|
| 1052 | break;
|
---|
| 1053 | case EVENT_QUIT:
|
---|
| 1054 | isRunning = false;
|
---|
| 1055 | break;
|
---|
| 1056 | }
|
---|
| 1057 | events.pop();
|
---|
[147ac6d] | 1058 | }
|
---|
[93462c6] | 1059 |
|
---|
| 1060 | if (curState == STATE_GAME) {
|
---|
[95595de] | 1061 |
|
---|
| 1062 | elapsed_seconds_spawn += elapsed_seconds;
|
---|
| 1063 | if (elapsed_seconds_spawn > 0.5f) {
|
---|
| 1064 | spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp,
|
---|
| 1065 | shaderBufferInfo,
|
---|
| 1066 | points_vbo,
|
---|
| 1067 | colors_vbo,
|
---|
| 1068 | selected_colors_vbo,
|
---|
| 1069 | texcoords_vbo,
|
---|
| 1070 | normals_vbo,
|
---|
| 1071 | ubo,
|
---|
[6877ef3] | 1072 | model_mat_idx_vbo);
|
---|
[95595de] | 1073 |
|
---|
| 1074 | elapsed_seconds_spawn -= 0.5f;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
[cf2d1e5] | 1077 | /*
|
---|
[93462c6] | 1078 | if (clickedObject == &objects[0]) {
|
---|
| 1079 | selectedObject = &objects[0];
|
---|
| 1080 | }
|
---|
| 1081 | if (clickedObject == &objects[1]) {
|
---|
| 1082 | selectedObject = &objects[1];
|
---|
| 1083 | }
|
---|
[cf2d1e5] | 1084 | */
|
---|
[f7d35da] | 1085 |
|
---|
| 1086 | /*
|
---|
| 1087 | if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
|
---|
[dba67b2] | 1088 | transformObject(objects[1], translate(mat4(1.0f), vec3(0.3f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 1089 | }
|
---|
| 1090 | if (key_pressed[GLFW_KEY_RIGHT]) {
|
---|
[dba67b2] | 1091 | transformObject(objects[2], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 1092 | }
|
---|
| 1093 | if (key_pressed[GLFW_KEY_LEFT]) {
|
---|
[dba67b2] | 1094 | transformObject(objects[2], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 1095 | }
|
---|
| 1096 | */
|
---|
[cf2d1e5] | 1097 |
|
---|
| 1098 | if (key_pressed[GLFW_KEY_RIGHT]) {
|
---|
[dba67b2] | 1099 | transformObject(objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
|
---|
[cf2d1e5] | 1100 | }
|
---|
| 1101 | if (key_pressed[GLFW_KEY_LEFT]) {
|
---|
[dba67b2] | 1102 | transformObject(objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
|
---|
[cf2d1e5] | 1103 | }
|
---|
| 1104 |
|
---|
[92b1e90] | 1105 | // this code moves the asteroids
|
---|
| 1106 | for (int i = 0; i < objects.size(); i++) {
|
---|
| 1107 | if (objects[i].type == TYPE_ASTEROID && !objects[i].deleted) {
|
---|
[dba67b2] | 1108 | transformObject(objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo);
|
---|
[95595de] | 1109 |
|
---|
[ebaa95c] | 1110 | vec3 obj_center = vec3(view_mat * vec4(objects[i].bounding_center, 1.0f));
|
---|
| 1111 |
|
---|
| 1112 | if ((obj_center.z - objects[i].bounding_radius) > -NEAR_CLIP) {
|
---|
[95595de] | 1113 | removeObjectFromScene(objects[i], ubo);
|
---|
| 1114 | }
|
---|
[5527206] | 1115 | }
|
---|
[cf2d1e5] | 1116 | }
|
---|
[93baa0e] | 1117 |
|
---|
[c3c3158] | 1118 | if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
|
---|
[95595de] | 1119 | removeObjectFromScene(objects[0], ubo);
|
---|
[c3c3158] | 1120 | }
|
---|
[baa5848] | 1121 | }
|
---|
[df652d5] | 1122 |
|
---|
[c3c3158] | 1123 | if (key_state[GLFW_KEY_ESCAPE] == GLFW_PRESS) {
|
---|
[ec4456b] | 1124 | glfwSetWindowShouldClose(window, 1);
|
---|
| 1125 | }
|
---|
[7ee66ea] | 1126 |
|
---|
| 1127 | float dist = cam_speed * elapsed_seconds;
|
---|
[c3c3158] | 1128 | if (key_pressed[GLFW_KEY_A]) {
|
---|
[dba67b2] | 1129 | vec3 dir = vec3(inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f));
|
---|
[809ce16] | 1130 | cam_pos += dir * dist;
|
---|
[f7d35da] | 1131 |
|
---|
[7ee66ea] | 1132 | cam_moved = true;
|
---|
| 1133 | }
|
---|
[c3c3158] | 1134 | if (key_pressed[GLFW_KEY_D]) {
|
---|
[dba67b2] | 1135 | vec3 dir = vec3(inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f));
|
---|
[809ce16] | 1136 | cam_pos += dir * dist;
|
---|
[f7d35da] | 1137 |
|
---|
[7ee66ea] | 1138 | cam_moved = true;
|
---|
| 1139 | }
|
---|
[c3c3158] | 1140 | if (key_pressed[GLFW_KEY_W]) {
|
---|
[dba67b2] | 1141 | vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f));
|
---|
[809ce16] | 1142 | cam_pos += dir * dist;
|
---|
[f7d35da] | 1143 |
|
---|
[7ee66ea] | 1144 | cam_moved = true;
|
---|
| 1145 | }
|
---|
[c3c3158] | 1146 | if (key_pressed[GLFW_KEY_S]) {
|
---|
[dba67b2] | 1147 | vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f));
|
---|
[809ce16] | 1148 | cam_pos += dir * dist;
|
---|
[f7d35da] | 1149 |
|
---|
[7ee66ea] | 1150 | cam_moved = true;
|
---|
| 1151 | }
|
---|
[cf2d1e5] | 1152 | /*
|
---|
[c3c3158] | 1153 | if (key_pressed[GLFW_KEY_LEFT]) {
|
---|
| 1154 | cam_yaw += cam_yaw_speed * elapsed_seconds;
|
---|
| 1155 | cam_moved = true;
|
---|
[7ee66ea] | 1156 | }
|
---|
[c3c3158] | 1157 | if (key_pressed[GLFW_KEY_RIGHT]) {
|
---|
| 1158 | cam_yaw -= cam_yaw_speed * elapsed_seconds;
|
---|
| 1159 | cam_moved = true;
|
---|
[7ee66ea] | 1160 | }
|
---|
[c3c3158] | 1161 | if (key_pressed[GLFW_KEY_UP]) {
|
---|
| 1162 | cam_pitch += cam_pitch_speed * elapsed_seconds;
|
---|
| 1163 | cam_moved = true;
|
---|
[809ce16] | 1164 | }
|
---|
[c3c3158] | 1165 | if (key_pressed[GLFW_KEY_DOWN]) {
|
---|
| 1166 | cam_pitch -= cam_pitch_speed * elapsed_seconds;
|
---|
| 1167 | cam_moved = true;
|
---|
[809ce16] | 1168 | }
|
---|
[cf2d1e5] | 1169 | */
|
---|
[7ee66ea] | 1170 | if (cam_moved) {
|
---|
[dba67b2] | 1171 | T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[809ce16] | 1172 |
|
---|
[dba67b2] | 1173 | mat4 yaw_mat = rotate(mat4(1.0f), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 1174 | mat4 pitch_mat = rotate(mat4(1.0f), -cam_pitch, vec3(1.0f, 0.0f, 0.0f));
|
---|
[809ce16] | 1175 | R = pitch_mat * yaw_mat;
|
---|
[f7d35da] | 1176 |
|
---|
[c3c3158] | 1177 | view_mat = R * T;
|
---|
[7ee66ea] | 1178 |
|
---|
[20e0020] | 1179 | //printVector("cam pos", cam_pos);
|
---|
[809ce16] | 1180 |
|
---|
[cffca4d] | 1181 | glUseProgram(color_sp);
|
---|
[267c4c5] | 1182 | glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 1183 |
|
---|
[cffca4d] | 1184 | glUseProgram(texture_sp);
|
---|
[7ee66ea] | 1185 | glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
[267c4c5] | 1186 |
|
---|
[b155f13] | 1187 | glUseProgram(laser_sp);
|
---|
| 1188 | glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
|
---|
| 1189 |
|
---|
[7ee66ea] | 1190 | cam_moved = false;
|
---|
| 1191 | }
|
---|
[c3c3158] | 1192 |
|
---|
| 1193 | // Render scene
|
---|
| 1194 |
|
---|
| 1195 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1196 |
|
---|
| 1197 | switch (curState) {
|
---|
| 1198 | case STATE_MAIN_MENU:
|
---|
| 1199 | renderMainMenu();
|
---|
| 1200 | renderMainMenuGui();
|
---|
| 1201 | break;
|
---|
| 1202 | case STATE_GAME:
|
---|
[92b1e90] | 1203 | renderScene(shaderBufferInfo,
|
---|
| 1204 | color_sp, texture_sp, laser_sp,
|
---|
[6877ef3] | 1205 | color_vao, texture_vao,
|
---|
[b155f13] | 1206 | colors_vbo, selected_colors_vbo,
|
---|
[92b1e90] | 1207 | selectedObject);
|
---|
[c3c3158] | 1208 | renderSceneGui();
|
---|
| 1209 | break;
|
---|
| 1210 | }
|
---|
| 1211 |
|
---|
| 1212 | glfwSwapBuffers(window);
|
---|
[644a2e4] | 1213 | }
|
---|
| 1214 |
|
---|
[c1ca5b5] | 1215 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
| 1216 | ImGui::DestroyContext();
|
---|
| 1217 |
|
---|
| 1218 | glfwDestroyWindow(window);
|
---|
[5272b6b] | 1219 | glfwTerminate();
|
---|
[c1ca5b5] | 1220 |
|
---|
[5272b6b] | 1221 | return 0;
|
---|
| 1222 | }
|
---|
[ec4456b] | 1223 |
|
---|
[4f3262f] | 1224 | void glfw_error_callback(int error, const char* description) {
|
---|
| 1225 | gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
| 1228 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
| 1229 | double mouse_x, mouse_y;
|
---|
| 1230 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 1231 |
|
---|
| 1232 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 1233 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
| 1234 | selectedObject = NULL;
|
---|
| 1235 |
|
---|
| 1236 | float x = (2.0f*mouse_x) / width - 1.0f;
|
---|
| 1237 | float y = 1.0f - (2.0f*mouse_y) / height;
|
---|
| 1238 |
|
---|
| 1239 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 1240 |
|
---|
| 1241 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
| 1242 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
[dba67b2] | 1243 | ray_eye = vec4(vec2(ray_eye), -1.0f, 1.0f);
|
---|
[4f3262f] | 1244 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
| 1245 |
|
---|
| 1246 | vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
|
---|
| 1247 |
|
---|
| 1248 | vec4 click_point;
|
---|
| 1249 | 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
|
---|
| 1250 | SceneObject* closest_object = NULL;
|
---|
| 1251 |
|
---|
| 1252 | for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
[92b1e90] | 1253 | if (it->type == TYPE_LASER) continue;
|
---|
[4f3262f] | 1254 | for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {
|
---|
[0d5c100] | 1255 | if (faceClicked(
|
---|
| 1256 | {
|
---|
| 1257 | vec3(it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),
|
---|
| 1258 | vec3(it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),
|
---|
| 1259 | vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
|
---|
[4f3262f] | 1260 | },
|
---|
[0d5c100] | 1261 | &*it, ray_world, cam_pos_temp, click_point
|
---|
| 1262 | )) {
|
---|
[4f3262f] | 1263 | click_point = view_mat * click_point;
|
---|
| 1264 |
|
---|
| 1265 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
[dba67b2] | 1266 | closest_point = vec3(click_point);
|
---|
[0d5c100] | 1267 | closest_object = &*it;
|
---|
[4f3262f] | 1268 | }
|
---|
| 1269 | }
|
---|
| 1270 | }
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
| 1273 | if (closest_object == NULL) {
|
---|
| 1274 | cout << "No object was clicked" << endl;
|
---|
[f7d35da] | 1275 | } else {
|
---|
[4f3262f] | 1276 | clickedObject = closest_object;
|
---|
| 1277 | cout << "Clicked object: " << clickedObject->id << endl;
|
---|
| 1278 | }
|
---|
| 1279 | }
|
---|
| 1280 | }
|
---|
| 1281 |
|
---|
[f7d35da] | 1282 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
---|
| 1283 | key_state[key] = action;
|
---|
| 1284 |
|
---|
| 1285 | // should be true for GLFW_PRESS and GLFW_REPEAT
|
---|
| 1286 | key_pressed[key] = (action != GLFW_RELEASE);
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
| 1289 |
|
---|
[ec4456b] | 1290 | GLuint loadShader(GLenum type, string file) {
|
---|
| 1291 | cout << "Loading shader from file " << file << endl;
|
---|
| 1292 |
|
---|
| 1293 | ifstream shaderFile(file);
|
---|
| 1294 | GLuint shaderId = 0;
|
---|
| 1295 |
|
---|
| 1296 | if (shaderFile.is_open()) {
|
---|
| 1297 | string line, shaderString;
|
---|
| 1298 |
|
---|
| 1299 | while(getline(shaderFile, line)) {
|
---|
| 1300 | shaderString += line + "\n";
|
---|
| 1301 | }
|
---|
| 1302 | shaderFile.close();
|
---|
| 1303 | const char* shaderCString = shaderString.c_str();
|
---|
| 1304 |
|
---|
| 1305 | shaderId = glCreateShader(type);
|
---|
| 1306 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 1307 | glCompileShader(shaderId);
|
---|
| 1308 |
|
---|
| 1309 | cout << "Loaded successfully" << endl;
|
---|
| 1310 | } else {
|
---|
[e856d62] | 1311 | cout << "Failed to load the file" << endl;
|
---|
[ec4456b] | 1312 | }
|
---|
| 1313 |
|
---|
| 1314 | return shaderId;
|
---|
| 1315 | }
|
---|
[485424b] | 1316 |
|
---|
| 1317 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 1318 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 1319 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 1320 |
|
---|
| 1321 | GLuint shader_program = glCreateProgram();
|
---|
| 1322 | glAttachShader(shader_program, vs);
|
---|
| 1323 | glAttachShader(shader_program, fs);
|
---|
| 1324 |
|
---|
| 1325 | glLinkProgram(shader_program);
|
---|
| 1326 |
|
---|
| 1327 | return shader_program;
|
---|
| 1328 | }
|
---|
| 1329 |
|
---|
| 1330 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 1331 | int n;
|
---|
[e856d62] | 1332 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
[485424b] | 1333 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
[e856d62] | 1334 |
|
---|
| 1335 | int width_in_bytes = *x * 4;
|
---|
| 1336 | unsigned char *top = NULL;
|
---|
| 1337 | unsigned char *bottom = NULL;
|
---|
| 1338 | unsigned char temp = 0;
|
---|
| 1339 | int half_height = *y / 2;
|
---|
| 1340 |
|
---|
| 1341 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
| 1342 | for (int row = 0; row < half_height; row++) {
|
---|
| 1343 | top = image_data + row * width_in_bytes;
|
---|
| 1344 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
| 1345 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
| 1346 | temp = *top;
|
---|
| 1347 | *top = *bottom;
|
---|
| 1348 | *bottom = temp;
|
---|
| 1349 | top++;
|
---|
| 1350 | bottom++;
|
---|
| 1351 | }
|
---|
| 1352 | }
|
---|
| 1353 |
|
---|
[485424b] | 1354 | if (!image_data) {
|
---|
| 1355 | fprintf(stderr, "ERROR: could not load %s\n", file_name.c_str());
|
---|
| 1356 | }
|
---|
[e856d62] | 1357 |
|
---|
| 1358 | // Not Power-of-2 check
|
---|
| 1359 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
| 1360 | fprintf(stderr, "WARNING: texture %s is not power-of-2 dimensions\n", file_name.c_str());
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
[485424b] | 1363 | return image_data;
|
---|
| 1364 | }
|
---|
[33a9664] | 1365 |
|
---|
[d9f99b2] | 1366 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
[5c9d193] | 1367 | // LINE EQUATION: P = O + Dt
|
---|
[b73cb3b] | 1368 | // O = cam
|
---|
[5c9d193] | 1369 | // D = ray_world
|
---|
| 1370 |
|
---|
[b73cb3b] | 1371 | // PLANE EQUATION: P dot n + d = 0
|
---|
| 1372 | // n is the normal vector
|
---|
| 1373 | // d is the offset from the origin
|
---|
[5c9d193] | 1374 |
|
---|
| 1375 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
[d9f99b2] | 1376 | vec3 v1 = points[1] - points[0];
|
---|
| 1377 | vec3 v2 = points[2] - points[0];
|
---|
[5c9d193] | 1378 |
|
---|
| 1379 | 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] | 1380 |
|
---|
[dba67b2] | 1381 | vec3 local_ray = vec3(inverse(obj->model_mat) * world_ray);
|
---|
| 1382 | vec3 local_cam = vec3(inverse(obj->model_mat) * cam);
|
---|
[5c9d193] | 1383 |
|
---|
[b73cb3b] | 1384 | local_ray = local_ray - local_cam;
|
---|
[5c9d193] | 1385 |
|
---|
[d9f99b2] | 1386 | float d = -glm::dot(points[0], normal);
|
---|
[5c9d193] | 1387 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
| 1388 |
|
---|
| 1389 | vec3 intersection = local_cam + t*local_ray;
|
---|
| 1390 |
|
---|
[d9f99b2] | 1391 | if (insideTriangle(intersection, points)) {
|
---|
[e82692b] | 1392 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
| 1393 | return true;
|
---|
| 1394 | } else {
|
---|
| 1395 | return false;
|
---|
| 1396 | }
|
---|
[5c9d193] | 1397 | }
|
---|
[f7d35da] | 1398 |
|
---|
[5c9d193] | 1399 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
[d9f99b2] | 1400 | vec3 v21 = triangle_points[1] - triangle_points[0];
|
---|
| 1401 | vec3 v31 = triangle_points[2] - triangle_points[0];
|
---|
| 1402 | vec3 pv1 = p - triangle_points[0];
|
---|
[33a9664] | 1403 |
|
---|
| 1404 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 1405 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 1406 |
|
---|
| 1407 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 1408 | }
|
---|
[d12d003] | 1409 |
|
---|
| 1410 | void printVector(string label, vec3 v) {
|
---|
| 1411 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
|
---|
| 1412 | }
|
---|
[b73cb3b] | 1413 |
|
---|
| 1414 | void print4DVector(string label, vec4 v) {
|
---|
| 1415 | cout << label << " -> (" << v.x << "," << v.y << "," << v.z << "," << v.w << ")" << endl;
|
---|
| 1416 | }
|
---|
[c1ca5b5] | 1417 |
|
---|
[c3c3158] | 1418 | void addObjectToSceneDuringInit(SceneObject& obj) {
|
---|
[3d06b4e] | 1419 | // Each objects must have at least 3 points, so the size of
|
---|
| 1420 | // the points array must be a positive multiple of 9
|
---|
| 1421 | if (obj.points.size() == 0 || (obj.points.size() % 9) != 0) {
|
---|
| 1422 | return;
|
---|
| 1423 | }
|
---|
| 1424 |
|
---|
[0d5c100] | 1425 | obj.id = objects.size(); // currently unused
|
---|
| 1426 | obj.num_points = obj.points.size() / 3;
|
---|
[dba67b2] | 1427 | obj.model_transform = mat4(1.0f);
|
---|
[c3c3158] | 1428 | obj.deleted = false;
|
---|
[0d5c100] | 1429 |
|
---|
| 1430 | obj.normals.reserve(obj.points.size());
|
---|
| 1431 | for (int i = 0; i < obj.points.size(); i += 9) {
|
---|
| 1432 | vec3 point1 = vec3(obj.points[i], obj.points[i + 1], obj.points[i + 2]);
|
---|
| 1433 | vec3 point2 = vec3(obj.points[i + 3], obj.points[i + 4], obj.points[i + 5]);
|
---|
| 1434 | vec3 point3 = vec3(obj.points[i + 6], obj.points[i + 7], obj.points[i + 8]);
|
---|
[cffca4d] | 1435 |
|
---|
[0d5c100] | 1436 | vec3 normal = normalize(cross(point2 - point1, point3 - point1));
|
---|
[cffca4d] | 1437 |
|
---|
[0d5c100] | 1438 | // Add the same normal for all 3 points
|
---|
| 1439 | for (int j = 0; j < 3; j++) {
|
---|
| 1440 | obj.normals.push_back(normal.x);
|
---|
| 1441 | obj.normals.push_back(normal.y);
|
---|
| 1442 | obj.normals.push_back(normal.z);
|
---|
| 1443 | }
|
---|
| 1444 | }
|
---|
[cffca4d] | 1445 |
|
---|
[3d06b4e] | 1446 | calculateObjectBoundingBox(obj);
|
---|
| 1447 |
|
---|
[95595de] | 1448 | obj.bounding_center = vec3(obj.translate_mat * vec4(obj.bounding_center, 1.0f));
|
---|
| 1449 |
|
---|
[0d5c100] | 1450 | objects.push_back(obj);
|
---|
| 1451 | }
|
---|
[cffca4d] | 1452 |
|
---|
[c3c3158] | 1453 | void addObjectToScene(SceneObject& obj,
|
---|
| 1454 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 1455 | GLuint points_vbo,
|
---|
| 1456 | GLuint colors_vbo,
|
---|
| 1457 | GLuint selected_colors_vbo,
|
---|
| 1458 | GLuint texcoords_vbo,
|
---|
| 1459 | GLuint normals_vbo,
|
---|
| 1460 | GLuint ubo,
|
---|
[6877ef3] | 1461 | GLuint model_mat_idx_vbo) {
|
---|
[c3c3158] | 1462 | addObjectToSceneDuringInit(obj);
|
---|
| 1463 |
|
---|
| 1464 | BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
|
---|
| 1465 |
|
---|
[3d06b4e] | 1466 | // Check if the buffers aren't large enough to fit the new object and, if so, call
|
---|
| 1467 | // populateBuffers() to resize and repopupulate them
|
---|
[c3c3158] | 1468 | if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj.num_points) ||
|
---|
| 1469 | bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) {
|
---|
| 1470 | populateBuffers(objects, shaderBufferInfo,
|
---|
| 1471 | points_vbo,
|
---|
| 1472 | colors_vbo,
|
---|
| 1473 | selected_colors_vbo,
|
---|
| 1474 | texcoords_vbo,
|
---|
| 1475 | normals_vbo,
|
---|
| 1476 | ubo,
|
---|
[6877ef3] | 1477 | model_mat_idx_vbo);
|
---|
[c3c3158] | 1478 | } else {
|
---|
| 1479 | copyObjectDataToBuffers(objects.back(), shaderBufferInfo,
|
---|
| 1480 | points_vbo,
|
---|
| 1481 | colors_vbo,
|
---|
| 1482 | selected_colors_vbo,
|
---|
| 1483 | texcoords_vbo,
|
---|
| 1484 | normals_vbo,
|
---|
| 1485 | ubo,
|
---|
[6877ef3] | 1486 | model_mat_idx_vbo);
|
---|
[c3c3158] | 1487 | }
|
---|
| 1488 | }
|
---|
| 1489 |
|
---|
[95595de] | 1490 | void removeObjectFromScene(SceneObject& obj, GLuint ubo) {
|
---|
[c3c3158] | 1491 | if (!obj.deleted) {
|
---|
| 1492 | // Move the object outside the render bounds of the scene so it doesn't get rendered
|
---|
| 1493 | // TODO: Find a better way of hiding the object until the next time buffers are repopulated
|
---|
[dba67b2] | 1494 | transformObject(obj, translate(mat4(1.0f), vec3(0.0f, 0.0f, FAR_CLIP * 1000.0f)), ubo);
|
---|
[c3c3158] | 1495 | obj.deleted = true;
|
---|
| 1496 | }
|
---|
| 1497 | }
|
---|
| 1498 |
|
---|
[3d06b4e] | 1499 | void calculateObjectBoundingBox(SceneObject& obj) {
|
---|
| 1500 | GLfloat min_x = obj.points[0];
|
---|
| 1501 | GLfloat max_x = obj.points[0];
|
---|
| 1502 | GLfloat min_y = obj.points[1];
|
---|
| 1503 | GLfloat max_y = obj.points[1];
|
---|
| 1504 | GLfloat min_z = obj.points[2];
|
---|
| 1505 | GLfloat max_z = obj.points[2];
|
---|
| 1506 |
|
---|
| 1507 | // start from the second point
|
---|
| 1508 | for (int i = 3; i < obj.points.size(); i += 3) {
|
---|
| 1509 | if (min_x > obj.points[i]) {
|
---|
| 1510 | min_x = obj.points[i];
|
---|
| 1511 | }
|
---|
| 1512 | else if (max_x < obj.points[i]) {
|
---|
| 1513 | max_x = obj.points[i];
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
| 1516 | if (min_y > obj.points[i + 1]) {
|
---|
| 1517 | min_y = obj.points[i + 1];
|
---|
| 1518 | }
|
---|
| 1519 | else if (max_y < obj.points[i + 1]) {
|
---|
| 1520 | max_y = obj.points[i + 1];
|
---|
| 1521 | }
|
---|
| 1522 |
|
---|
| 1523 | if (min_z > obj.points[i + 2]) {
|
---|
| 1524 | min_z = obj.points[i + 2];
|
---|
| 1525 | }
|
---|
| 1526 | else if (max_z < obj.points[i + 2]) {
|
---|
| 1527 | max_z = obj.points[i + 2];
|
---|
| 1528 | }
|
---|
| 1529 | }
|
---|
| 1530 |
|
---|
| 1531 | obj.bounding_center = vec3((min_x + max_x) / 2.0f, (min_y + max_y) / 2.0f, (min_z + max_z) / 2.0f);
|
---|
| 1532 |
|
---|
| 1533 | GLfloat radius_x = max_x - obj.bounding_center.x;
|
---|
| 1534 | GLfloat radius_y = max_y - obj.bounding_center.y;
|
---|
| 1535 | GLfloat radius_z = max_z - obj.bounding_center.z;
|
---|
| 1536 |
|
---|
[95595de] | 1537 | // This actually underestimates the radius. Might need to be fixed at some point.
|
---|
[3d06b4e] | 1538 | obj.bounding_radius = radius_x;
|
---|
| 1539 | if (obj.bounding_radius < radius_y)
|
---|
| 1540 | obj.bounding_radius = radius_y;
|
---|
| 1541 | if (obj.bounding_radius < radius_z)
|
---|
| 1542 | obj.bounding_radius = radius_z;
|
---|
| 1543 |
|
---|
| 1544 | for (int i = 0; i < obj.points.size(); i += 3) {
|
---|
| 1545 | obj.points[i] -= obj.bounding_center.x;
|
---|
| 1546 | obj.points[i + 1] -= obj.bounding_center.y;
|
---|
| 1547 | obj.points[i + 2] -= obj.bounding_center.z;
|
---|
| 1548 | }
|
---|
| 1549 |
|
---|
| 1550 | obj.bounding_center = vec3(0.0f, 0.0f, 0.0f);
|
---|
| 1551 | }
|
---|
| 1552 |
|
---|
[b155f13] | 1553 | // currently only works correctly for lasers oriented along the z axis
|
---|
[92b1e90] | 1554 | void addLaserToScene(SceneObject& obj, vec3 start, vec3 end, vec3 color, GLfloat width) {
|
---|
| 1555 | obj.id = objects.size(); // currently unused
|
---|
| 1556 | obj.type = TYPE_LASER;
|
---|
| 1557 | obj.deleted = false;
|
---|
[b155f13] | 1558 |
|
---|
| 1559 | // I really need to create a direction vector and add/subtract that from start and end
|
---|
| 1560 | // to get the right coords
|
---|
| 1561 | // Also need to multiply the width times a vector perpendicular to it
|
---|
| 1562 | vec3 dir = end - start;
|
---|
| 1563 |
|
---|
[92b1e90] | 1564 | obj.points = {
|
---|
[9f9f9a7] | 1565 | start.x + width / 2, start.y, start.z - width / 2,
|
---|
| 1566 | start.x - width / 2, start.y, start.z - width / 2,
|
---|
[b155f13] | 1567 | start.x - width / 2, start.y, start.z,
|
---|
[9f9f9a7] | 1568 | start.x + width / 2, start.y, start.z - width / 2,
|
---|
[b155f13] | 1569 | start.x - width / 2, start.y, start.z,
|
---|
| 1570 | start.x + width / 2, start.y, start.z,
|
---|
[9f9f9a7] | 1571 | end.x + width / 2, end.y, end.z + width / 2,
|
---|
| 1572 | end.x - width / 2, end.y, end.z + width / 2,
|
---|
| 1573 | start.x - width / 2, start.y, start.z - width / 2,
|
---|
| 1574 | end.x + width / 2, end.y, end.z + width / 2,
|
---|
| 1575 | start.x - width / 2, start.y, start.z - width / 2,
|
---|
| 1576 | start.x + width / 2, start.y, start.z - width / 2,
|
---|
[b155f13] | 1577 | end.x + width / 2, end.y, end.z,
|
---|
| 1578 | end.x - width / 2, end.y, end.z,
|
---|
[9f9f9a7] | 1579 | end.x - width / 2, end.y, end.z + width / 2,
|
---|
[b155f13] | 1580 | end.x + width / 2, end.y, end.z,
|
---|
[9f9f9a7] | 1581 | end.x - width / 2, end.y, end.z + width / 2,
|
---|
| 1582 | end.x + width / 2, end.y, end.z + width / 2,
|
---|
[b155f13] | 1583 | };
|
---|
| 1584 |
|
---|
| 1585 | vec3 end_color = vec3(1.0f, 0.0f, 0.0f); // temporary
|
---|
[92b1e90] | 1586 | obj.colors = {
|
---|
[b155f13] | 1587 | end_color.x, end_color.y, end_color.z,
|
---|
| 1588 | end_color.x, end_color.y, end_color.z,
|
---|
| 1589 | end_color.x, end_color.y, end_color.z,
|
---|
| 1590 | end_color.x, end_color.y, end_color.z,
|
---|
| 1591 | end_color.x, end_color.y, end_color.z,
|
---|
| 1592 | end_color.x, end_color.y, end_color.z,
|
---|
| 1593 | color.x, color.y, color.z,
|
---|
| 1594 | color.x, color.y, color.z,
|
---|
| 1595 | color.x, color.y, color.z,
|
---|
| 1596 | color.x, color.y, color.z,
|
---|
| 1597 | color.x, color.y, color.z,
|
---|
| 1598 | color.x, color.y, color.z,
|
---|
| 1599 | end_color.x, end_color.y, end_color.z,
|
---|
| 1600 | end_color.x, end_color.y, end_color.z,
|
---|
| 1601 | end_color.x, end_color.y, end_color.z,
|
---|
| 1602 | end_color.x, end_color.y, end_color.z,
|
---|
| 1603 | end_color.x, end_color.y, end_color.z,
|
---|
| 1604 | end_color.x, end_color.y, end_color.z,
|
---|
| 1605 | };
|
---|
| 1606 |
|
---|
[9f9f9a7] | 1607 | obj.texcoords = {
|
---|
| 1608 | 1.0f, 0.5f,
|
---|
| 1609 | 0.0f, 0.5f,
|
---|
| 1610 | 0.0f, 0.0f,
|
---|
| 1611 | 1.0f, 0.5f,
|
---|
| 1612 | 0.0f, 0.0f,
|
---|
| 1613 | 1.0f, 0.0f,
|
---|
| 1614 | 1.0f, 0.51f,
|
---|
| 1615 | 0.0f, 0.51f,
|
---|
| 1616 | 0.0f, 0.49f,
|
---|
| 1617 | 1.0f, 0.51f,
|
---|
| 1618 | 0.0f, 0.49f,
|
---|
| 1619 | 1.0f, 0.49f,
|
---|
| 1620 | 1.0f, 1.0f,
|
---|
| 1621 | 0.0f, 1.0f,
|
---|
| 1622 | 0.0f, 0.5f,
|
---|
| 1623 | 1.0f, 1.0f,
|
---|
| 1624 | 0.0f, 0.5f,
|
---|
| 1625 | 1.0f, 0.5f,
|
---|
| 1626 | };
|
---|
| 1627 |
|
---|
[92b1e90] | 1628 | obj.num_points = obj.points.size() / 3;
|
---|
[b155f13] | 1629 |
|
---|
[92b1e90] | 1630 | objects.push_back(obj);
|
---|
[b155f13] | 1631 | }
|
---|
| 1632 |
|
---|
[c3c3158] | 1633 | void initializeBuffers(
|
---|
| 1634 | GLuint* points_vbo,
|
---|
| 1635 | GLuint* colors_vbo,
|
---|
| 1636 | GLuint* selected_colors_vbo,
|
---|
| 1637 | GLuint* texcoords_vbo,
|
---|
| 1638 | GLuint* normals_vbo,
|
---|
| 1639 | GLuint* ubo,
|
---|
[6877ef3] | 1640 | GLuint* model_mat_idx_vbo) {
|
---|
[c3c3158] | 1641 | *points_vbo = 0;
|
---|
| 1642 | glGenBuffers(1, points_vbo);
|
---|
| 1643 |
|
---|
| 1644 | *colors_vbo = 0;
|
---|
| 1645 | glGenBuffers(1, colors_vbo);
|
---|
| 1646 |
|
---|
| 1647 | *selected_colors_vbo = 0;
|
---|
| 1648 | glGenBuffers(1, selected_colors_vbo);
|
---|
| 1649 |
|
---|
| 1650 | *texcoords_vbo = 0;
|
---|
| 1651 | glGenBuffers(1, texcoords_vbo);
|
---|
| 1652 |
|
---|
| 1653 | *normals_vbo = 0;
|
---|
| 1654 | glGenBuffers(1, normals_vbo);
|
---|
| 1655 |
|
---|
| 1656 | *ubo = 0;
|
---|
| 1657 | glGenBuffers(1, ubo);
|
---|
| 1658 |
|
---|
| 1659 | *model_mat_idx_vbo = 0;
|
---|
| 1660 | glGenBuffers(1, model_mat_idx_vbo);
|
---|
| 1661 | }
|
---|
| 1662 |
|
---|
[0d5c100] | 1663 | void populateBuffers(vector<SceneObject>& objects,
|
---|
[c3c3158] | 1664 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 1665 | GLuint points_vbo,
|
---|
| 1666 | GLuint colors_vbo,
|
---|
| 1667 | GLuint selected_colors_vbo,
|
---|
| 1668 | GLuint texcoords_vbo,
|
---|
| 1669 | GLuint normals_vbo,
|
---|
| 1670 | GLuint ubo,
|
---|
[6877ef3] | 1671 | GLuint model_mat_idx_vbo) {
|
---|
[0d5c100] | 1672 | GLsizeiptr points_buffer_size = 0;
|
---|
| 1673 | GLsizeiptr textures_buffer_size = 0;
|
---|
| 1674 | GLsizeiptr ubo_buffer_size = 0;
|
---|
| 1675 | GLsizeiptr model_mat_idx_buffer_size = 0;
|
---|
| 1676 |
|
---|
[c3c3158] | 1677 | map<GLuint, unsigned int> shaderCounts;
|
---|
[0d5c100] | 1678 | map<GLuint, unsigned int> shaderUboCounts;
|
---|
[93462c6] | 1679 |
|
---|
[0d5c100] | 1680 | vector<SceneObject>::iterator it;
|
---|
| 1681 |
|
---|
[92b1e90] | 1682 | /* Find all shaders that need to be used and the number of objects and
|
---|
[c3c3158] | 1683 | * number of points for each shader. Construct a map from shader id to count
|
---|
| 1684 | * of points being drawn using that shader (for thw model matrix ubo, we
|
---|
| 1685 | * need object counts instead). These will be used to get offsets into the
|
---|
| 1686 | * vertex buffer for each shader.
|
---|
| 1687 | */
|
---|
| 1688 | for (it = objects.begin(); it != objects.end();) {
|
---|
| 1689 | if (it->deleted) {
|
---|
| 1690 | it = objects.erase(it);
|
---|
[0d5c100] | 1691 | } else {
|
---|
[c94a699] | 1692 | points_buffer_size += it->num_points * sizeof(GLfloat) * 3;
|
---|
| 1693 | textures_buffer_size += it->num_points * sizeof(GLfloat) * 2;
|
---|
[c3c3158] | 1694 | ubo_buffer_size += 16 * sizeof(GLfloat);
|
---|
| 1695 | model_mat_idx_buffer_size += it->num_points * sizeof(GLuint);
|
---|
| 1696 |
|
---|
| 1697 | if (shaderCounts.count(it->shader_program) == 0) {
|
---|
| 1698 | shaderCounts[it->shader_program] = it->num_points;
|
---|
| 1699 | shaderUboCounts[it->shader_program] = 1;
|
---|
| 1700 | } else {
|
---|
| 1701 | shaderCounts[it->shader_program] += it->num_points;
|
---|
| 1702 | shaderUboCounts[it->shader_program]++;
|
---|
| 1703 | }
|
---|
| 1704 |
|
---|
| 1705 | it++;
|
---|
[e3ca955] | 1706 | }
|
---|
[0d5c100] | 1707 | }
|
---|
| 1708 |
|
---|
[c3c3158] | 1709 | // double the buffer sizes to leave room for new objects
|
---|
| 1710 | points_buffer_size *= 2;
|
---|
| 1711 | textures_buffer_size *= 2;
|
---|
| 1712 | ubo_buffer_size *= 2;
|
---|
| 1713 | model_mat_idx_buffer_size *= 2;
|
---|
| 1714 |
|
---|
[0d5c100] | 1715 | map<GLuint, unsigned int>::iterator shaderIt;
|
---|
| 1716 | unsigned int lastShaderCount = 0;
|
---|
| 1717 | unsigned int lastShaderUboCount = 0;
|
---|
| 1718 |
|
---|
| 1719 | /*
|
---|
[c3c3158] | 1720 | * The counts calculated above can be used to get the starting offset of
|
---|
| 1721 | * each shader in the vertex buffer. Create a map of base offsets to mark
|
---|
| 1722 | * where the data for the first object using a given shader begins. Also,
|
---|
| 1723 | * create a map of current offsets to mark where to copy data for the next
|
---|
| 1724 | * object being added.
|
---|
| 1725 | */
|
---|
[0d5c100] | 1726 | for (shaderIt = shaderCounts.begin(); shaderIt != shaderCounts.end(); shaderIt++) {
|
---|
[c3c3158] | 1727 | shaderBufferInfo[shaderIt->first].vbo_base = lastShaderCount * 2;
|
---|
| 1728 | shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2;
|
---|
[92b1e90] | 1729 |
|
---|
[c3c3158] | 1730 | cout << "shader: " << shaderIt->first << endl;
|
---|
| 1731 | cout << "point counts: " << shaderCounts[shaderIt->first] << endl;
|
---|
| 1732 | cout << "object counts: " << shaderUboCounts[shaderIt->first] << endl;
|
---|
| 1733 | cout << "vbo_base: " << shaderBufferInfo[shaderIt->first].vbo_base << endl;
|
---|
| 1734 | cout << "ubo_base: " << shaderBufferInfo[shaderIt->first].ubo_base << endl;
|
---|
[0d5c100] | 1735 |
|
---|
[c3c3158] | 1736 | shaderBufferInfo[shaderIt->first].vbo_offset = 0;
|
---|
| 1737 | shaderBufferInfo[shaderIt->first].ubo_offset = 0;
|
---|
| 1738 |
|
---|
| 1739 | shaderBufferInfo[shaderIt->first].vbo_capacity = shaderCounts[shaderIt->first] * 2;
|
---|
| 1740 | shaderBufferInfo[shaderIt->first].ubo_capacity = shaderUboCounts[shaderIt->first] * 2;
|
---|
[0d5c100] | 1741 |
|
---|
[c3c3158] | 1742 | lastShaderCount += shaderCounts[shaderIt->first];
|
---|
[0d5c100] | 1743 | lastShaderUboCount += shaderUboCounts[shaderIt->first];
|
---|
| 1744 | }
|
---|
| 1745 |
|
---|
[c3c3158] | 1746 | // Allocate all the buffers using the counts calculated above
|
---|
[0d5c100] | 1747 |
|
---|
[c3c3158] | 1748 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
[0d5c100] | 1749 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1750 |
|
---|
[c3c3158] | 1751 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[0d5c100] | 1752 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1753 |
|
---|
[c3c3158] | 1754 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
[0d5c100] | 1755 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1756 |
|
---|
[c3c3158] | 1757 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
[0d5c100] | 1758 | glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1759 |
|
---|
[c3c3158] | 1760 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
[0d5c100] | 1761 | glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1762 |
|
---|
[c3c3158] | 1763 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
[0d5c100] | 1764 | glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1765 |
|
---|
[c3c3158] | 1766 | glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
|
---|
[0d5c100] | 1767 | glBufferData(GL_ARRAY_BUFFER, model_mat_idx_buffer_size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 1768 |
|
---|
| 1769 | for (it = objects.begin(); it != objects.end(); it++) {
|
---|
[c3c3158] | 1770 | copyObjectDataToBuffers(*it, shaderBufferInfo,
|
---|
| 1771 | points_vbo,
|
---|
| 1772 | colors_vbo,
|
---|
| 1773 | selected_colors_vbo,
|
---|
| 1774 | texcoords_vbo,
|
---|
| 1775 | normals_vbo,
|
---|
| 1776 | ubo,
|
---|
[6877ef3] | 1777 | model_mat_idx_vbo);
|
---|
[c3c3158] | 1778 | }
|
---|
| 1779 | }
|
---|
[0d5c100] | 1780 |
|
---|
[c3c3158] | 1781 | void copyObjectDataToBuffers(SceneObject& obj,
|
---|
| 1782 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 1783 | GLuint points_vbo,
|
---|
| 1784 | GLuint colors_vbo,
|
---|
| 1785 | GLuint selected_colors_vbo,
|
---|
| 1786 | GLuint texcoords_vbo,
|
---|
| 1787 | GLuint normals_vbo,
|
---|
| 1788 | GLuint ubo,
|
---|
[6877ef3] | 1789 | GLuint model_mat_idx_vbo) {
|
---|
[c3c3158] | 1790 | BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
|
---|
[0d5c100] | 1791 |
|
---|
[c3c3158] | 1792 | obj.vertex_vbo_offset = bufferInfo->vbo_base + bufferInfo->vbo_offset;
|
---|
| 1793 | obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset;
|
---|
[0d5c100] | 1794 |
|
---|
[6877ef3] | 1795 | glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
|
---|
| 1796 | glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]);
|
---|
[0d5c100] | 1797 |
|
---|
[6877ef3] | 1798 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
| 1799 | glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
|
---|
[0d5c100] | 1800 |
|
---|
| 1801 |
|
---|
[6877ef3] | 1802 | if (obj.type != TYPE_LASER) {
|
---|
[92b1e90] | 1803 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 1804 | glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
|
---|
[9f9f9a7] | 1805 | }
|
---|
[0d5c100] | 1806 |
|
---|
[9f9f9a7] | 1807 | glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
|
---|
| 1808 | glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
|
---|
[0d5c100] | 1809 |
|
---|
[9f9f9a7] | 1810 | if (obj.type != TYPE_LASER) {
|
---|
[92b1e90] | 1811 | glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
|
---|
| 1812 | glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]);
|
---|
[c3c3158] | 1813 |
|
---|
[92b1e90] | 1814 | glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
|
---|
| 1815 | for (int i = 0; i < obj.num_points; i++) {
|
---|
| 1816 | glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset);
|
---|
| 1817 | }
|
---|
| 1818 |
|
---|
| 1819 | obj.model_mat = obj.model_transform * obj.model_base;
|
---|
| 1820 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
| 1821 | glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
|
---|
| 1822 | }
|
---|
[c3c3158] | 1823 |
|
---|
| 1824 | bufferInfo->vbo_offset += obj.num_points;
|
---|
| 1825 | bufferInfo->ubo_offset++;
|
---|
[0d5c100] | 1826 | }
|
---|
[93462c6] | 1827 |
|
---|
[5c403fe] | 1828 | void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) {
|
---|
[3d06b4e] | 1829 | obj.model_transform = transform * obj.model_transform;
|
---|
[5c403fe] | 1830 | obj.model_mat = obj.model_transform * obj.model_base;
|
---|
| 1831 |
|
---|
[95595de] | 1832 | obj.bounding_center = vec3(transform * vec4(obj.bounding_center, 1.0f));
|
---|
| 1833 |
|
---|
[5c403fe] | 1834 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
| 1835 | glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
|
---|
| 1836 | }
|
---|
| 1837 |
|
---|
[92b1e90] | 1838 | void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 1839 | GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
|
---|
[6877ef3] | 1840 | GLuint color_vao, GLuint texture_vao,
|
---|
[b155f13] | 1841 | GLuint colors_vbo, GLuint selected_colors_vbo,
|
---|
[92b1e90] | 1842 | SceneObject* selectedObject) {
|
---|
[93462c6] | 1843 |
|
---|
[cffca4d] | 1844 | glUseProgram(color_sp);
|
---|
[92b1e90] | 1845 | glBindVertexArray(color_vao);
|
---|
[93462c6] | 1846 |
|
---|
[0d5c100] | 1847 | if (selectedObject != NULL) {
|
---|
| 1848 | glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
|
---|
| 1849 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[cffca4d] | 1850 |
|
---|
[0d5c100] | 1851 | glDrawArrays(GL_TRIANGLES, selectedObject->vertex_vbo_offset, selectedObject->num_points);
|
---|
[cffca4d] | 1852 | }
|
---|
[93462c6] | 1853 |
|
---|
[e3ca955] | 1854 | glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
|
---|
[cffca4d] | 1855 | glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
---|
[93462c6] | 1856 |
|
---|
[c3c3158] | 1857 | glDrawArrays(GL_TRIANGLES, shaderBufferInfo[color_sp].vbo_base, shaderBufferInfo[color_sp].vbo_offset);
|
---|
[93462c6] | 1858 |
|
---|
[cffca4d] | 1859 | glUseProgram(texture_sp);
|
---|
[92b1e90] | 1860 | glBindVertexArray(texture_vao);
|
---|
[93462c6] | 1861 |
|
---|
[c3c3158] | 1862 | glDrawArrays(GL_TRIANGLES, shaderBufferInfo[texture_sp].vbo_base, shaderBufferInfo[texture_sp].vbo_offset);
|
---|
[93462c6] | 1863 |
|
---|
[9f9f9a7] | 1864 | glEnable(GL_BLEND);
|
---|
| 1865 |
|
---|
[92b1e90] | 1866 | glUseProgram(laser_sp);
|
---|
[9f9f9a7] | 1867 | glBindVertexArray(texture_vao);
|
---|
[b155f13] | 1868 |
|
---|
[92b1e90] | 1869 | glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
|
---|
[9f9f9a7] | 1870 |
|
---|
| 1871 | glDisable(GL_BLEND);
|
---|
[b155f13] | 1872 | }
|
---|
| 1873 |
|
---|
[93462c6] | 1874 | void renderSceneGui() {
|
---|
[c1ca5b5] | 1875 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 1876 |
|
---|
| 1877 | // 1. Show a simple window.
|
---|
| 1878 | // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
|
---|
[5b3462b] | 1879 | /*
|
---|
[c1ca5b5] | 1880 | {
|
---|
| 1881 | static float f = 0.0f;
|
---|
| 1882 | static int counter = 0;
|
---|
| 1883 | ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
|
---|
| 1884 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
---|
| 1885 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
---|
| 1886 |
|
---|
| 1887 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
|
---|
| 1888 | ImGui::Checkbox("Another Window", &show_another_window);
|
---|
| 1889 |
|
---|
| 1890 | if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
|
---|
| 1891 | counter++;
|
---|
| 1892 | ImGui::SameLine();
|
---|
| 1893 | ImGui::Text("counter = %d", counter);
|
---|
| 1894 |
|
---|
| 1895 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
---|
| 1896 | }
|
---|
[5b3462b] | 1897 | */
|
---|
[c1ca5b5] | 1898 |
|
---|
[5b3462b] | 1899 | {
|
---|
| 1900 | ImGui::SetNextWindowSize(ImVec2(85, 22), ImGuiCond_Once);
|
---|
| 1901 | ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
|
---|
[f0cc877] | 1902 | ImGui::Begin("WndStats", NULL,
|
---|
| 1903 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 1904 | ImGuiWindowFlags_NoResize |
|
---|
| 1905 | ImGuiWindowFlags_NoMove);
|
---|
[5b3462b] | 1906 | ImGui::Text("Score: ???");
|
---|
[c1ca5b5] | 1907 | ImGui::End();
|
---|
| 1908 | }
|
---|
| 1909 |
|
---|
[5b3462b] | 1910 | {
|
---|
| 1911 | ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
|
---|
| 1912 | ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
|
---|
[f0cc877] | 1913 | ImGui::Begin("WndMenubar", NULL,
|
---|
| 1914 | ImGuiWindowFlags_NoTitleBar |
|
---|
[5b3462b] | 1915 | ImGuiWindowFlags_NoResize |
|
---|
| 1916 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 1917 | ImGui::InvisibleButton("", ImVec2(155, 18));
|
---|
[5b3462b] | 1918 | ImGui::SameLine();
|
---|
[93462c6] | 1919 | if (ImGui::Button("Main Menu")) {
|
---|
| 1920 | events.push(EVENT_GO_TO_MAIN_MENU);
|
---|
[5b3462b] | 1921 | }
|
---|
| 1922 | ImGui::End();
|
---|
[c1ca5b5] | 1923 | }
|
---|
| 1924 |
|
---|
[93462c6] | 1925 | ImGui::Render();
|
---|
| 1926 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 1927 | }
|
---|
| 1928 |
|
---|
| 1929 | void renderMainMenu() {
|
---|
| 1930 | }
|
---|
| 1931 |
|
---|
| 1932 | void renderMainMenuGui() {
|
---|
| 1933 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 1934 |
|
---|
[f0cc877] | 1935 | {
|
---|
| 1936 | int padding = 4;
|
---|
| 1937 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
[93462c6] | 1938 | ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once);
|
---|
[f0cc877] | 1939 | ImGui::Begin("WndMain", NULL,
|
---|
| 1940 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 1941 | ImGuiWindowFlags_NoResize |
|
---|
| 1942 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 1943 |
|
---|
| 1944 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
| 1945 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
| 1946 | ImGui::SameLine();
|
---|
| 1947 | if (ImGui::Button("New Game")) {
|
---|
| 1948 | events.push(EVENT_GO_TO_GAME);
|
---|
| 1949 | }
|
---|
| 1950 |
|
---|
| 1951 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
| 1952 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
| 1953 | ImGui::SameLine();
|
---|
| 1954 | if (ImGui::Button("Quit")) {
|
---|
| 1955 | events.push(EVENT_QUIT);
|
---|
| 1956 | }
|
---|
| 1957 |
|
---|
[f0cc877] | 1958 | ImGui::End();
|
---|
| 1959 | }
|
---|
| 1960 |
|
---|
[c1ca5b5] | 1961 | ImGui::Render();
|
---|
| 1962 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 1963 | }
|
---|
[cf2d1e5] | 1964 |
|
---|
[c3c3158] | 1965 | void spawnAsteroid(vec3 pos, GLuint shader,
|
---|
| 1966 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
| 1967 | GLuint points_vbo,
|
---|
| 1968 | GLuint colors_vbo,
|
---|
| 1969 | GLuint selected_colors_vbo,
|
---|
| 1970 | GLuint texcoords_vbo,
|
---|
| 1971 | GLuint normals_vbo,
|
---|
| 1972 | GLuint ubo,
|
---|
[6877ef3] | 1973 | GLuint model_mat_idx_vbo) {
|
---|
[cf2d1e5] | 1974 | SceneObject obj = SceneObject();
|
---|
[92b1e90] | 1975 | obj.type = TYPE_ASTEROID;
|
---|
[cf2d1e5] | 1976 | obj.shader_program = shader;
|
---|
| 1977 |
|
---|
| 1978 | obj.points = {
|
---|
| 1979 | // front
|
---|
| 1980 | 1.0f, 1.0f, 1.0f,
|
---|
| 1981 | -1.0f, 1.0f, 1.0f,
|
---|
| 1982 | -1.0f, -1.0f, 1.0f,
|
---|
| 1983 | 1.0f, 1.0f, 1.0f,
|
---|
| 1984 | -1.0f, -1.0f, 1.0f,
|
---|
| 1985 | 1.0f, -1.0f, 1.0f,
|
---|
| 1986 |
|
---|
| 1987 | // top
|
---|
| 1988 | 1.0f, 1.0f, -1.0f,
|
---|
| 1989 | -1.0f, 1.0f, -1.0f,
|
---|
| 1990 | -1.0f, 1.0f, 1.0f,
|
---|
| 1991 | 1.0f, 1.0f, -1.0f,
|
---|
| 1992 | -1.0f, 1.0f, 1.0f,
|
---|
| 1993 | 1.0f, 1.0f, 1.0f,
|
---|
| 1994 |
|
---|
| 1995 | // bottom
|
---|
| 1996 | 1.0f, -1.0f, 1.0f,
|
---|
| 1997 | -1.0f, -1.0f, 1.0f,
|
---|
| 1998 | -1.0f, -1.0f, -1.0f,
|
---|
| 1999 | 1.0f, -1.0f, 1.0f,
|
---|
| 2000 | -1.0f, -1.0f, -1.0f,
|
---|
| 2001 | 1.0f, -1.0f, -1.0f,
|
---|
| 2002 |
|
---|
| 2003 | // back
|
---|
| 2004 | 1.0f, 1.0f, -1.0f,
|
---|
| 2005 | -1.0f, -1.0f, -1.0f,
|
---|
| 2006 | -1.0f, 1.0f, -1.0f,
|
---|
| 2007 | 1.0f, 1.0f, -1.0f,
|
---|
| 2008 | 1.0f, -1.0f, -1.0f,
|
---|
| 2009 | -1.0f, -1.0f, -1.0f,
|
---|
| 2010 |
|
---|
| 2011 | // right
|
---|
| 2012 | 1.0f, 1.0f, -1.0f,
|
---|
| 2013 | 1.0f, 1.0f, 1.0f,
|
---|
| 2014 | 1.0f, -1.0f, 1.0f,
|
---|
| 2015 | 1.0f, 1.0f, -1.0f,
|
---|
| 2016 | 1.0f, -1.0f, 1.0f,
|
---|
| 2017 | 1.0f, -1.0f, -1.0f,
|
---|
| 2018 |
|
---|
| 2019 | // left
|
---|
| 2020 | -1.0f, 1.0f, 1.0f,
|
---|
| 2021 | -1.0f, 1.0f, -1.0f,
|
---|
| 2022 | -1.0f, -1.0f, -1.0f,
|
---|
| 2023 | -1.0f, 1.0f, 1.0f,
|
---|
| 2024 | -1.0f, -1.0f, -1.0f,
|
---|
| 2025 | -1.0f, -1.0f, 1.0f,
|
---|
| 2026 | };
|
---|
| 2027 | obj.colors = {
|
---|
| 2028 | // front
|
---|
| 2029 | 0.8f, 0.0f, 0.0f,
|
---|
| 2030 | 0.8f, 0.0f, 0.0f,
|
---|
| 2031 | 0.8f, 0.0f, 0.0f,
|
---|
| 2032 | 0.8f, 0.0f, 0.0f,
|
---|
| 2033 | 0.8f, 0.0f, 0.0f,
|
---|
| 2034 | 0.8f, 0.0f, 0.0f,
|
---|
| 2035 |
|
---|
| 2036 | // top
|
---|
| 2037 | 0.8f, 0.0f, 0.0f,
|
---|
| 2038 | 0.8f, 0.0f, 0.0f,
|
---|
| 2039 | 0.8f, 0.0f, 0.0f,
|
---|
| 2040 | 0.8f, 0.0f, 0.0f,
|
---|
| 2041 | 0.8f, 0.0f, 0.0f,
|
---|
| 2042 | 0.8f, 0.0f, 0.0f,
|
---|
| 2043 |
|
---|
| 2044 | // bottom
|
---|
| 2045 | 0.8f, 0.0f, 0.0f,
|
---|
| 2046 | 0.8f, 0.0f, 0.0f,
|
---|
| 2047 | 0.8f, 0.0f, 0.0f,
|
---|
| 2048 | 0.8f, 0.0f, 0.0f,
|
---|
| 2049 | 0.8f, 0.0f, 0.0f,
|
---|
| 2050 | 0.8f, 0.0f, 0.0f,
|
---|
| 2051 |
|
---|
| 2052 | // back
|
---|
| 2053 | 0.8f, 0.0f, 0.0f,
|
---|
| 2054 | 0.8f, 0.0f, 0.0f,
|
---|
| 2055 | 0.8f, 0.0f, 0.0f,
|
---|
| 2056 | 0.8f, 0.0f, 0.0f,
|
---|
| 2057 | 0.8f, 0.0f, 0.0f,
|
---|
| 2058 | 0.8f, 0.0f, 0.0f,
|
---|
| 2059 |
|
---|
| 2060 | // right
|
---|
| 2061 | 0.8f, 0.0f, 0.0f,
|
---|
| 2062 | 0.8f, 0.0f, 0.0f,
|
---|
| 2063 | 0.8f, 0.0f, 0.0f,
|
---|
| 2064 | 0.8f, 0.0f, 0.0f,
|
---|
| 2065 | 0.8f, 0.0f, 0.0f,
|
---|
| 2066 | 0.8f, 0.0f, 0.0f,
|
---|
| 2067 |
|
---|
| 2068 | // left
|
---|
| 2069 | 0.8f, 0.0f, 0.0f,
|
---|
| 2070 | 0.8f, 0.0f, 0.0f,
|
---|
| 2071 | 0.8f, 0.0f, 0.0f,
|
---|
| 2072 | 0.8f, 0.0f, 0.0f,
|
---|
| 2073 | 0.8f, 0.0f, 0.0f,
|
---|
| 2074 | 0.8f, 0.0f, 0.0f,
|
---|
| 2075 | };
|
---|
| 2076 | obj.texcoords = { 0.0f };
|
---|
| 2077 | obj.selected_colors = { 0.0f };
|
---|
| 2078 |
|
---|
[dba67b2] | 2079 | mat4 T = translate(mat4(1.0f), pos);
|
---|
| 2080 | mat4 R = rotate(mat4(1.0f), 60.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 1.0f, -1.0f));
|
---|
| 2081 | obj.model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
|
---|
[cf2d1e5] | 2082 |
|
---|
[95595de] | 2083 | obj.translate_mat = T;
|
---|
| 2084 |
|
---|
[c3c3158] | 2085 | addObjectToScene(obj, shaderBufferInfo,
|
---|
| 2086 | points_vbo,
|
---|
| 2087 | colors_vbo,
|
---|
| 2088 | selected_colors_vbo,
|
---|
| 2089 | texcoords_vbo,
|
---|
| 2090 | normals_vbo,
|
---|
| 2091 | ubo,
|
---|
[6877ef3] | 2092 | model_mat_idx_vbo);
|
---|
[cf2d1e5] | 2093 | }
|
---|
[5527206] | 2094 |
|
---|
| 2095 | float getRandomNum(float low, float high) {
|
---|
| 2096 | return low + ((float)rand()/RAND_MAX) * (high-low);
|
---|
| 2097 | }
|
---|