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