[88ebdc8] | 1 | #define STB_IMAGE_IMPLEMENTATION
|
---|
[485424b] | 2 | #include "stb_image.h"
|
---|
| 3 |
|
---|
[1099b95] | 4 | #define _USE_MATH_DEFINES
|
---|
[5c9d193] | 5 |
|
---|
[c62eee6] | 6 | #include <glm/mat4x4.hpp>
|
---|
[7ee66ea] | 7 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 8 | #include <glm/gtc/type_ptr.hpp>
|
---|
| 9 |
|
---|
[c1ca5b5] | 10 | #include "IMGUI/imgui.h"
|
---|
| 11 | #include "imgui_impl_glfw_gl3.h"
|
---|
| 12 |
|
---|
[5272b6b] | 13 | #include <GL/glew.h>
|
---|
| 14 | #include <GLFW/glfw3.h>
|
---|
| 15 |
|
---|
[22b2c37] | 16 | #include <cstdio>
|
---|
[5527206] | 17 | #include <cstdlib>
|
---|
| 18 | #include <ctime>
|
---|
[22b2c37] | 19 | #include <iostream>
|
---|
[ec4456b] | 20 | #include <fstream>
|
---|
[1f3d32b] | 21 | #include <sstream>
|
---|
[93baa0e] | 22 | #include <cmath>
|
---|
[1099b95] | 23 | #include <string>
|
---|
[19c9338] | 24 | #include <array>
|
---|
[df652d5] | 25 | #include <vector>
|
---|
[93462c6] | 26 | #include <queue>
|
---|
[0d5c100] | 27 | #include <map>
|
---|
[22b2c37] | 28 |
|
---|
[1fcca9e] | 29 | #include "logger.hpp"
|
---|
[203ab1b] | 30 | #include "utils.hpp"
|
---|
[7e10667] | 31 |
|
---|
[301d0d4] | 32 | #include "compiler.hpp"
|
---|
[5529ab5] | 33 | #include "crash-logger.hpp"
|
---|
[d9b6a1c] | 34 |
|
---|
[5272b6b] | 35 | using namespace std;
|
---|
[7ee66ea] | 36 | using namespace glm;
|
---|
| 37 |
|
---|
[92b1e90] | 38 | enum State {
|
---|
| 39 | STATE_MAIN_MENU,
|
---|
| 40 | STATE_GAME,
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | enum Event {
|
---|
| 44 | EVENT_GO_TO_MAIN_MENU,
|
---|
| 45 | EVENT_GO_TO_GAME,
|
---|
| 46 | EVENT_QUIT,
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | enum ObjectType {
|
---|
| 50 | TYPE_SHIP,
|
---|
| 51 | TYPE_ASTEROID,
|
---|
| 52 | TYPE_LASER,
|
---|
[646f3f2] | 53 | TYPE_EXPLOSION,
|
---|
[92b1e90] | 54 | };
|
---|
| 55 |
|
---|
[a0eb547] | 56 | enum AttribType {
|
---|
| 57 | ATTRIB_UNIFORM,
|
---|
| 58 | ATTRIB_OBJECT_VARYING,
|
---|
| 59 | ATTRIB_POINT_VARYING,
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[49db5fc] | 62 | // Add more types as I need them
|
---|
| 63 | enum UniformType {
|
---|
| 64 | UNIFORM_NONE,
|
---|
| 65 | UNIFORM_MATRIX_4F,
|
---|
| 66 | UNIFORM_1F,
|
---|
| 67 | UNIFORM_3F,
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[c4c205e] | 70 | enum UIValueType {
|
---|
| 71 | UIVALUE_INT,
|
---|
| 72 | UIVALUE_DOUBLE,
|
---|
| 73 | };
|
---|
| 74 |
|
---|
[df652d5] | 75 | struct SceneObject {
|
---|
[d9f99b2] | 76 | unsigned int id;
|
---|
[92b1e90] | 77 | ObjectType type;
|
---|
[7e10667] | 78 | bool deleted;
|
---|
[95595de] | 79 |
|
---|
| 80 | // Currently, model_transform should only have translate, and rotation and scale need to be done in model_base since
|
---|
| 81 | // they need to be done when the object is at the origin. I should change this to have separate scale, rotate, and translate
|
---|
| 82 | // matrices for each object that can be updated independently and then applied to the object in that order.
|
---|
[4c7cd57] | 83 | // TODO: Actually, to make this as generic as possible, each object should have a matrix stack to support,
|
---|
| 84 | // for instance, applying a rotate, then a translate, then another rotate. Think about and implement the best approach.
|
---|
[5c403fe] | 85 | mat4 model_mat, model_base, model_transform;
|
---|
[95595de] | 86 | mat4 translate_mat; // beginning of doing what's mentioned above
|
---|
[05e43cf] | 87 | unsigned int num_points;
|
---|
[c3c3158] | 88 | GLuint vertex_vbo_offset;
|
---|
| 89 | GLuint ubo_offset;
|
---|
[07ed460] | 90 | vector<GLfloat> points;
|
---|
| 91 | vector<GLfloat> colors;
|
---|
| 92 | vector<GLfloat> texcoords;
|
---|
[9dd2eb7] | 93 | vector<GLfloat> normals;
|
---|
[3d06b4e] | 94 | vec3 bounding_center;
|
---|
| 95 | GLfloat bounding_radius;
|
---|
[c3c3158] | 96 | };
|
---|
| 97 |
|
---|
[1f3d32b] | 98 | struct Asteroid : SceneObject {
|
---|
[0e0f851] | 99 | float hp;
|
---|
[1f3d32b] | 100 | };
|
---|
| 101 |
|
---|
| 102 | struct Laser : SceneObject {
|
---|
| 103 | Asteroid* targetAsteroid;
|
---|
| 104 | };
|
---|
| 105 |
|
---|
[dc19a39] | 106 | struct ParticleEffect : SceneObject {
|
---|
| 107 | vector<GLfloat> particleVelocities;
|
---|
| 108 | vector<GLfloat> particleTimes;
|
---|
[7e10667] | 109 | GLfloat startTime;
|
---|
| 110 | GLfloat duration;
|
---|
[dc19a39] | 111 | };
|
---|
| 112 |
|
---|
[1f3d32b] | 113 | struct EffectOverTime {
|
---|
[0e0f851] | 114 | float& effectedValue;
|
---|
| 115 | float startValue;
|
---|
[1f3d32b] | 116 | double startTime;
|
---|
[0e0f851] | 117 | float changePerSecond;
|
---|
[1f3d32b] | 118 | bool deleted;
|
---|
| 119 | SceneObject* effectedObject;
|
---|
| 120 |
|
---|
[39ac76d] | 121 | // TODO: Why not just use an initializer list for all the instance variables
|
---|
[b220f78] | 122 | // TODO: Maybe pass in startTime instead of calling glfwGetTime() here
|
---|
[0e0f851] | 123 | EffectOverTime(float& effectedValue, float changePerSecond, SceneObject* object)
|
---|
| 124 | : effectedValue(effectedValue), changePerSecond(changePerSecond), effectedObject(object) {
|
---|
[1f3d32b] | 125 | startValue = effectedValue;
|
---|
| 126 | startTime = glfwGetTime();
|
---|
| 127 | deleted = false;
|
---|
| 128 | }
|
---|
| 129 | };
|
---|
| 130 |
|
---|
[c3c3158] | 131 | struct BufferInfo {
|
---|
| 132 | unsigned int ubo_base;
|
---|
| 133 | unsigned int ubo_offset;
|
---|
| 134 | unsigned int ubo_capacity;
|
---|
[df652d5] | 135 | };
|
---|
| 136 |
|
---|
[a0eb547] | 137 | struct AttribInfo {
|
---|
| 138 | AttribType attribType;
|
---|
| 139 | GLuint index;
|
---|
| 140 | GLint size;
|
---|
| 141 | GLenum type;
|
---|
[49db5fc] | 142 | UniformType uniType;
|
---|
| 143 | GLuint buffer; // For uniforms, this is the uniform location
|
---|
[a0eb547] | 144 | size_t fieldOffset;
|
---|
[49db5fc] | 145 | GLfloat* data; // pointer to data source for uniform attributes
|
---|
[a0eb547] | 146 | };
|
---|
| 147 |
|
---|
[7a55b49] | 148 | struct ShaderModelGroup {
|
---|
| 149 | GLuint shaderProgram;
|
---|
| 150 | GLuint vao;
|
---|
[a0eb547] | 151 | map<string, AttribInfo> attribs;
|
---|
[7a55b49] | 152 | unsigned int numPoints;
|
---|
[a0eb547] | 153 | unsigned int vboCapacity;
|
---|
[7a55b49] | 154 | };
|
---|
| 155 |
|
---|
[c4c205e] | 156 | struct UIValue {
|
---|
| 157 | UIValueType type;
|
---|
| 158 | string label;
|
---|
| 159 | void* value;
|
---|
| 160 |
|
---|
| 161 | UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
|
---|
| 162 | };
|
---|
| 163 |
|
---|
[4f3262f] | 164 | void glfw_error_callback(int error, const char* description);
|
---|
| 165 |
|
---|
| 166 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
|
---|
[f7d35da] | 167 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
---|
[5b02676] | 168 | /*** START OF REFACTORED CODE ***/
|
---|
[e6bc0f4] | 169 | void window_size_callback(GLFWwindow* window, int width, int height);
|
---|
[5b02676] | 170 | /*** END OF REFACTORED CODE ***/
|
---|
[4f3262f] | 171 |
|
---|
[0e0f851] | 172 | void APIENTRY debugGlCallback(
|
---|
| 173 | GLenum source,
|
---|
| 174 | GLenum type,
|
---|
| 175 | GLuint id,
|
---|
| 176 | GLenum severity,
|
---|
| 177 | GLsizei length,
|
---|
| 178 | const GLchar* message,
|
---|
| 179 | const void* userParam
|
---|
| 180 | );
|
---|
| 181 |
|
---|
[d9f99b2] | 182 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
|
---|
[5c9d193] | 183 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points);
|
---|
[33a9664] | 184 |
|
---|
[ec4456b] | 185 | GLuint loadShader(GLenum type, string file);
|
---|
[485424b] | 186 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
|
---|
| 187 | unsigned char* loadImage(string file_name, int* x, int* y);
|
---|
[ec4456b] | 188 |
|
---|
[1f3d32b] | 189 | void initObject(SceneObject* obj);
|
---|
| 190 | void addObjectToScene(SceneObject* obj,
|
---|
[8316333] | 191 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
[0414306] | 192 | map<ObjectType, ShaderModelGroup>& modelGroups,
|
---|
[f97e638] | 193 | GLuint ubo);
|
---|
[95595de] | 194 | void removeObjectFromScene(SceneObject& obj, GLuint ubo);
|
---|
[c3c3158] | 195 |
|
---|
[7a55b49] | 196 | ShaderModelGroup createModelGroup(GLuint shaderProgram);
|
---|
| 197 | void removeModelFromGroup(ShaderModelGroup& modelGroup, SceneObject& model);
|
---|
| 198 | void addModelToGroup(ShaderModelGroup& modelGroup, SceneObject& model);
|
---|
| 199 |
|
---|
[a0eb547] | 200 | void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, GLenum type, size_t fieldOffset);
|
---|
[49db5fc] | 201 | void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, UniformType type, GLfloat* data);
|
---|
[a0eb547] | 202 | void initModelGroupAttribs(ShaderModelGroup& modelGroup);
|
---|
[49db5fc] | 203 | void bindUniformData(AttribInfo& attrib);
|
---|
[b220f78] | 204 | void bindUniformData(AttribInfo& attrib, GLfloat* data);
|
---|
[a0eb547] | 205 |
|
---|
| 206 | size_t GLsizeof(GLenum);
|
---|
| 207 | GLvoid* getVectorAttribPtr(SceneObject& obj, size_t attribOffset);
|
---|
| 208 | GLvoid* getScalarAttribPtr(SceneObject& obj, size_t attribOffset);
|
---|
| 209 |
|
---|
[1f3d32b] | 210 | void calculateObjectBoundingBox(SceneObject* obj);
|
---|
[b155f13] | 211 |
|
---|
[1f3d32b] | 212 | void populateBuffers(vector<SceneObject*>& objects,
|
---|
[c3c3158] | 213 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
[0414306] | 214 | map<ObjectType, ShaderModelGroup>& modelGroups,
|
---|
[f97e638] | 215 | GLuint ubo);
|
---|
[c3c3158] | 216 |
|
---|
| 217 | void copyObjectDataToBuffers(SceneObject& obj,
|
---|
| 218 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
[0414306] | 219 | map<ObjectType, ShaderModelGroup>& modelGroups,
|
---|
[49db5fc] | 220 | GLuint ubo);
|
---|
[f9a242b] | 221 |
|
---|
[5c403fe] | 222 | void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
|
---|
| 223 |
|
---|
[646f3f2] | 224 | // TODO: instead of using these methods, create constructors for these
|
---|
[dd9771c] | 225 | SceneObject* createShip();
|
---|
| 226 | Asteroid* createAsteroid(vec3 pos);
|
---|
| 227 | Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width);
|
---|
[7e10667] | 228 | ParticleEffect* createExplosion(mat4 model_mat);
|
---|
[1f3d32b] | 229 |
|
---|
| 230 | void translateLaser(Laser* laser, const vec3& translation, GLuint ubo);
|
---|
[a0eb547] | 231 | void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp);
|
---|
[e9347b4] | 232 | bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection);
|
---|
[612d1f6] | 233 |
|
---|
[93462c6] | 234 | void renderMainMenu();
|
---|
| 235 | void renderMainMenuGui();
|
---|
| 236 |
|
---|
[7e10667] | 237 | void renderScene(map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo);
|
---|
[c4c205e] | 238 | void renderSceneGui(map<string, vector<UIValue>> valueLists);
|
---|
| 239 |
|
---|
[a9d191a] | 240 | void initGuiValueLists(map<string, vector<UIValue>> valueLists);
|
---|
[c4c205e] | 241 | void renderGuiValueList(vector<UIValue>& values);
|
---|
[d12d003] | 242 |
|
---|
[5527206] | 243 | #define NUM_KEYS (512)
|
---|
| 244 | #define ONE_DEG_IN_RAD ((2.0f * M_PI) / 360.0f) // 0.017444444 (maybe make this a const instead)
|
---|
[8fbd34f] | 245 | #define TARGET_FPS 60.0f
|
---|
[5527206] | 246 |
|
---|
| 247 | const int KEY_STATE_UNCHANGED = -1;
|
---|
| 248 | const bool FULLSCREEN = false;
|
---|
[db06984] | 249 | const int EXPLOSION_PARTICLE_COUNT = 300;
|
---|
[5527206] | 250 | unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
|
---|
| 251 |
|
---|
| 252 | int key_state[NUM_KEYS];
|
---|
[fabed35] | 253 | bool key_down[NUM_KEYS];
|
---|
[5527206] | 254 |
|
---|
[e6bc0f4] | 255 | int windowWidth = 640;
|
---|
| 256 | int windowHeight = 480;
|
---|
[5527206] | 257 |
|
---|
| 258 | vec3 cam_pos;
|
---|
| 259 |
|
---|
| 260 | mat4 view_mat;
|
---|
| 261 | mat4 proj_mat;
|
---|
| 262 |
|
---|
[1f3d32b] | 263 | vector<SceneObject*> objects;
|
---|
[5527206] | 264 | queue<Event> events;
|
---|
[1f3d32b] | 265 | vector<EffectOverTime*> effects;
|
---|
[5527206] | 266 |
|
---|
| 267 | SceneObject* clickedObject = NULL;
|
---|
| 268 | SceneObject* selectedObject = NULL;
|
---|
| 269 |
|
---|
| 270 | float NEAR_CLIP = 0.1f;
|
---|
| 271 | float FAR_CLIP = 100.0f;
|
---|
| 272 |
|
---|
[95595de] | 273 | // TODO: Should really have some array or struct of UI-related variables
|
---|
[5527206] | 274 | bool isRunning = true;
|
---|
| 275 |
|
---|
| 276 | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
---|
| 277 |
|
---|
[1f3d32b] | 278 | Laser* leftLaser = NULL;
|
---|
| 279 | EffectOverTime* leftLaserEffect = NULL;
|
---|
| 280 |
|
---|
| 281 | Laser* rightLaser = NULL;
|
---|
| 282 | EffectOverTime* rightLaserEffect = NULL;
|
---|
[fabed35] | 283 |
|
---|
[a9d191a] | 284 | map<string, vector<UIValue>> valueLists;
|
---|
| 285 |
|
---|
[3effd81] | 286 | /*
|
---|
[e9347b4] | 287 | * TODO: Asteroid and ship movement currently depend on framerate, fix this in a generic/reusable way
|
---|
| 288 | * Disabling vsync is a great way to test this
|
---|
[3effd81] | 289 | */
|
---|
| 290 |
|
---|
[cabdd5c] | 291 | /*** START OF REFACTORED CODE ***/
|
---|
[d9b6a1c] | 292 | // Helps to test logging during crashes
|
---|
[b373466] | 293 | void badFunc() {
|
---|
[d9b6a1c] | 294 | int* test = NULL;
|
---|
[6abfd07] | 295 |
|
---|
[d9b6a1c] | 296 | *test = 1;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[6abfd07] | 299 | int __main(int argc, char* argv[]);
|
---|
[d9b6a1c] | 300 |
|
---|
[c1ca5b5] | 301 | int main(int argc, char* argv[]) {
|
---|
[6abfd07] | 302 | CrashLogger logger(__main, argc, argv);
|
---|
[d9b6a1c] | 303 |
|
---|
| 304 | exit(0);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | int __main(int argc, char* argv[]) {
|
---|
[5272b6b] | 308 | cout << "New OpenGL Game" << endl;
|
---|
| 309 |
|
---|
[bae0911] | 310 | restart_gl_log();
|
---|
| 311 | gl_log("starting GLFW\n%s", glfwGetVersionString());
|
---|
[22b2c37] | 312 |
|
---|
[98f06d9] | 313 | open_log();
|
---|
| 314 | get_log() << "starting GLFW" << endl;
|
---|
| 315 | get_log() << glfwGetVersionString() << endl;
|
---|
| 316 |
|
---|
[ec4456b] | 317 | glfwSetErrorCallback(glfw_error_callback);
|
---|
[5272b6b] | 318 | if (!glfwInit()) {
|
---|
[bae0911] | 319 | gl_log_err("ERROR: could not start GLFW3");
|
---|
[98f06d9] | 320 | cerr << "ERROR: could not start GLFW3" << endl;
|
---|
| 321 | get_log() << "ERROR: could not start GLFW3" << endl;
|
---|
[5272b6b] | 322 | return 1;
|
---|
[be246ad] | 323 | }
|
---|
| 324 |
|
---|
[d9b6a1c] | 325 | #ifdef MAC
|
---|
[be246ad] | 326 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
---|
| 327 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
| 328 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
---|
| 329 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
---|
[446e55d] | 330 | #else
|
---|
| 331 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
---|
| 332 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
---|
[be246ad] | 333 | #endif
|
---|
[5272b6b] | 334 |
|
---|
[ec4456b] | 335 | GLFWwindow* window = NULL;
|
---|
[e856d62] | 336 | GLFWmonitor* mon = NULL;
|
---|
[ec4456b] | 337 |
|
---|
[0e0f851] | 338 | glfwWindowHint(GLFW_SAMPLES, 16);
|
---|
| 339 | glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
|
---|
| 340 |
|
---|
[ec4456b] | 341 | if (FULLSCREEN) {
|
---|
[e856d62] | 342 | mon = glfwGetPrimaryMonitor();
|
---|
[ec4456b] | 343 | const GLFWvidmode* vmode = glfwGetVideoMode(mon);
|
---|
| 344 |
|
---|
[e6bc0f4] | 345 | windowWidth = vmode->width;
|
---|
| 346 | windowHeight = vmode->height;
|
---|
[e856d62] | 347 | cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
|
---|
[ec4456b] | 348 | }
|
---|
[e6bc0f4] | 349 | window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL);
|
---|
[ec4456b] | 350 |
|
---|
[5272b6b] | 351 | if (!window) {
|
---|
[bae0911] | 352 | gl_log_err("ERROR: could not open window with GLFW3");
|
---|
[98f06d9] | 353 | cerr << "ERROR: could not open window with GLFW3" << endl;
|
---|
| 354 | get_log() << "ERROR: could not open window with GLFW3" << endl;
|
---|
[5272b6b] | 355 | glfwTerminate();
|
---|
| 356 | return 1;
|
---|
| 357 | }
|
---|
[c62eee6] | 358 |
|
---|
[644a2e4] | 359 | glfwMakeContextCurrent(window);
|
---|
[e6bc0f4] | 360 | glViewport(0, 0, windowWidth, windowHeight);
|
---|
| 361 |
|
---|
[5272b6b] | 362 | glewExperimental = GL_TRUE;
|
---|
| 363 | glewInit();
|
---|
| 364 |
|
---|
[0e0f851] | 365 | if (GLEW_KHR_debug) {
|
---|
| 366 | cout << "FOUND GLEW debug extension" << endl;
|
---|
| 367 | glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
---|
| 368 | glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, nullptr);
|
---|
| 369 | cout << "Bound debug callback" << endl;
|
---|
[446e55d] | 370 | } else {
|
---|
[e6bc0f4] | 371 | cout << "OpenGL debug message callback is not supported" << endl;
|
---|
[0e0f851] | 372 | }
|
---|
[cabdd5c] | 373 | /*** END OF REFACTORED CODE ***/
|
---|
[0e0f851] | 374 |
|
---|
[5527206] | 375 | srand(time(0));
|
---|
| 376 |
|
---|
[14ff67c] | 377 | /*
|
---|
| 378 | * RENDERING ALGORITHM NOTES:
|
---|
| 379 | *
|
---|
| 380 | * Basically, I need to split my objects into groups, so that each group fits into
|
---|
| 381 | * GL_MAX_UNIFORM_BLOCK_SIZE. I need to have an offset and a size for each group.
|
---|
| 382 | * Getting the offset is straitforward. The size may as well be GL_MAX_UNIFORM_BLOCK_SIZE
|
---|
| 383 | * for each group, since it seems that smaller sizes just round up to the nearest GL_MAX_UNIFORM_BLOCK_SIZE
|
---|
| 384 | *
|
---|
| 385 | * I'll need to have a loop inside my render loop that calls glBindBufferRange(GL_UNIFORM_BUFFER, ...
|
---|
| 386 | * for every 1024 objects and then draws all those objects with one glDraw call.
|
---|
| 387 | *
|
---|
[0d5c100] | 388 | * Since I currently have very few objects, I'll wait to implement this until I have
|
---|
| 389 | * a reasonable number of objects always using the same shader.
|
---|
[14ff67c] | 390 | */
|
---|
| 391 |
|
---|
| 392 | GLint UNIFORM_BUFFER_OFFSET_ALIGNMENT, MAX_UNIFORM_BLOCK_SIZE;
|
---|
| 393 | glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UNIFORM_BUFFER_OFFSET_ALIGNMENT);
|
---|
| 394 | glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 395 |
|
---|
| 396 | MAX_UNIFORMS = MAX_UNIFORM_BLOCK_SIZE / sizeof(mat4);
|
---|
| 397 |
|
---|
| 398 | cout << "UNIFORM_BUFFER_OFFSET_ALIGNMENT: " << UNIFORM_BUFFER_OFFSET_ALIGNMENT << endl;
|
---|
| 399 | cout << "MAX_UNIFORMS: " << MAX_UNIFORMS << endl;
|
---|
| 400 |
|
---|
[f133da0] | 401 | /*** START OF REFACTORED CODE ***/
|
---|
[c1ca5b5] | 402 | // Setup Dear ImGui binding
|
---|
| 403 | IMGUI_CHECKVERSION();
|
---|
| 404 | ImGui::CreateContext();
|
---|
| 405 | ImGuiIO& io = ImGui::GetIO(); (void)io;
|
---|
| 406 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
---|
| 407 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
---|
| 408 | ImGui_ImplGlfwGL3_Init(window, true);
|
---|
| 409 |
|
---|
| 410 | // Setup style
|
---|
| 411 | ImGui::StyleColorsDark();
|
---|
| 412 | //ImGui::StyleColorsClassic();
|
---|
| 413 |
|
---|
| 414 | glfwSetMouseButtonCallback(window, mouse_button_callback);
|
---|
[f7d35da] | 415 | glfwSetKeyCallback(window, key_callback);
|
---|
[e6bc0f4] | 416 | glfwSetWindowSizeCallback(window, window_size_callback);
|
---|
[4d84c72] | 417 | /*** END OF REFACTORED CODE ***/
|
---|
[e6bc0f4] | 418 |
|
---|
[5272b6b] | 419 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
| 420 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
[0e0f851] | 421 | cout << "Renderer: " << renderer << endl;
|
---|
[bae0911] | 422 | cout << "Supported OpenGL version: " << version << endl;
|
---|
| 423 |
|
---|
| 424 | gl_log("Renderer: %s", renderer);
|
---|
| 425 | gl_log("Supported OpenGL version: %s", version);
|
---|
[93baa0e] | 426 |
|
---|
[98f06d9] | 427 | get_log() << "Renderer: " << renderer << endl;
|
---|
| 428 | get_log() << "Supported OpenGL version: " << version << endl;
|
---|
| 429 |
|
---|
[9f9f9a7] | 430 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 431 |
|
---|
[5272b6b] | 432 | glEnable(GL_DEPTH_TEST);
|
---|
| 433 | glDepthFunc(GL_LESS);
|
---|
[516668e] | 434 |
|
---|
[93baa0e] | 435 | glEnable(GL_CULL_FACE);
|
---|
| 436 | // glCullFace(GL_BACK);
|
---|
| 437 | // glFrontFace(GL_CW);
|
---|
| 438 |
|
---|
[9f9f9a7] | 439 | int x, y;
|
---|
| 440 | unsigned char* texImage = loadImage("laser.png", &x, &y);
|
---|
| 441 | if (texImage) {
|
---|
| 442 | cout << "Laser texture loaded successfully!" << endl;
|
---|
[155a7cf] | 443 | cout << x << ", " << y << endl;
|
---|
| 444 | cout << "first 4 bytes are: " << texImage[0] << " " << texImage[1] << " " << texImage[2] << " " << texImage[3] << endl;
|
---|
[9f9f9a7] | 445 | }
|
---|
| 446 |
|
---|
| 447 | GLuint laserTex = 0;
|
---|
| 448 | glGenTextures(1, &laserTex);
|
---|
[485424b] | 449 | glActiveTexture(GL_TEXTURE0);
|
---|
[9f9f9a7] | 450 | glBindTexture(GL_TEXTURE_2D, laserTex);
|
---|
[485424b] | 451 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
|
---|
| 452 |
|
---|
| 453 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 454 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 455 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 456 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 457 |
|
---|
[0d5c100] | 458 | /* RENDERING ALGORITHM
|
---|
| 459 | *
|
---|
| 460 | * Create a separate vbo for each of the following things:
|
---|
| 461 | * - points
|
---|
| 462 | * - colors
|
---|
| 463 | * - texture coordinates
|
---|
| 464 | * - selected colors
|
---|
| 465 | * - normals
|
---|
| 466 | * - indices into a ubo that stores a model matrix for each object
|
---|
| 467 | *
|
---|
| 468 | * Also, make a model matrix ubo, the entirety of which will be passed to the vertex shader.
|
---|
| 469 | * The vbo containing the correct index into the ubo (mentioned above) will be used to select
|
---|
| 470 | * the right model matrix for each point. The index in the vbo will be the saem for all points
|
---|
| 471 | * of any given object.
|
---|
| 472 | *
|
---|
| 473 | * Right now, the currently selected object is drawn using one color (specified in the selected
|
---|
| 474 | * colors vbo) regardless of whether it is normally rendering using colors or a texture. The selected
|
---|
| 475 | * object is rendering by binding the selected colors vbo in place of the colors vbo and using the colors
|
---|
| 476 | * shader. Then, the selected object is redrawn along with all other objects, but the depth buffer test
|
---|
| 477 | * prevents the unselected version of the object from appearing on the screen. This lets me render all the
|
---|
| 478 | * objects that use a particular shader using one glDrawArrays() call.
|
---|
| 479 | */
|
---|
[cffca4d] | 480 |
|
---|
[49db5fc] | 481 | GLfloat laserColor[3] = {0.2f, 1.0f, 0.2f};
|
---|
[b220f78] | 482 | GLfloat curTime, prevTime, elapsedTime;
|
---|
[49db5fc] | 483 |
|
---|
[f97e638] | 484 | GLuint ubo = 0;
|
---|
| 485 | glGenBuffers(1, &ubo);
|
---|
[81f28c0] | 486 |
|
---|
[a0eb547] | 487 | map<GLuint, BufferInfo> shaderBufferInfo;
|
---|
| 488 | map<ObjectType, ShaderModelGroup> modelGroups;
|
---|
| 489 |
|
---|
[5b02676] | 490 | /*** START OF REFACTORED CODE ***/
|
---|
[a0eb547] | 491 | modelGroups[TYPE_SHIP] = createModelGroup(
|
---|
[4d84c72] | 492 | loadShaderProgram("gl-shaders/ship.vert", "gl-shaders/ship.frag"));
|
---|
[a0eb547] | 493 | shaderBufferInfo[modelGroups[TYPE_SHIP].shaderProgram] = BufferInfo(); // temporary
|
---|
[0e0f851] | 494 |
|
---|
[a0eb547] | 495 | defineModelGroupAttrib(modelGroups[TYPE_SHIP], "vertex_position", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 496 | 3, GL_FLOAT, offset_of(&SceneObject::points));
|
---|
[a0eb547] | 497 | defineModelGroupAttrib(modelGroups[TYPE_SHIP], "vertex_color", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 498 | 3, GL_FLOAT, offset_of(&SceneObject::colors));
|
---|
[a0eb547] | 499 | defineModelGroupAttrib(modelGroups[TYPE_SHIP], "vertex_normal", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 500 | 3, GL_FLOAT, offset_of(&SceneObject::normals));
|
---|
[a0eb547] | 501 | defineModelGroupAttrib(modelGroups[TYPE_SHIP], "ubo_index", ATTRIB_OBJECT_VARYING,
|
---|
[7e10667] | 502 | 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
|
---|
[5b02676] | 503 | /*** END OF REFACTORED CODE ***/
|
---|
[81f28c0] | 504 |
|
---|
[49db5fc] | 505 | defineModelGroupUniform(modelGroups[TYPE_SHIP], "view", ATTRIB_UNIFORM,
|
---|
| 506 | 1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
|
---|
| 507 | defineModelGroupUniform(modelGroups[TYPE_SHIP], "proj", ATTRIB_UNIFORM,
|
---|
| 508 | 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
|
---|
| 509 |
|
---|
[a0eb547] | 510 | initModelGroupAttribs(modelGroups[TYPE_SHIP]);
|
---|
[20e0020] | 511 |
|
---|
[5b02676] | 512 | /*** START OF REFACTORED CODE ***/
|
---|
[a0eb547] | 513 | modelGroups[TYPE_ASTEROID] = createModelGroup(
|
---|
[4d84c72] | 514 | loadShaderProgram("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag"));
|
---|
[a0eb547] | 515 | shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary
|
---|
[0e0f851] | 516 |
|
---|
[a0eb547] | 517 | defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "vertex_position", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 518 | 3, GL_FLOAT, offset_of(&SceneObject::points));
|
---|
[a0eb547] | 519 | defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "vertex_color", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 520 | 3, GL_FLOAT, offset_of(&SceneObject::colors));
|
---|
[a0eb547] | 521 | defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "vertex_normal", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 522 | 3, GL_FLOAT, offset_of(&SceneObject::normals));
|
---|
[a0eb547] | 523 | defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "ubo_index", ATTRIB_OBJECT_VARYING,
|
---|
[7e10667] | 524 | 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
|
---|
[5b02676] | 525 | /*** END OF REFACTORED CODE ***/
|
---|
[a0eb547] | 526 |
|
---|
[49db5fc] | 527 | defineModelGroupUniform(modelGroups[TYPE_ASTEROID], "view", ATTRIB_UNIFORM,
|
---|
| 528 | 1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
|
---|
| 529 | defineModelGroupUniform(modelGroups[TYPE_ASTEROID], "proj", ATTRIB_UNIFORM,
|
---|
| 530 | 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
|
---|
| 531 |
|
---|
[a0eb547] | 532 | initModelGroupAttribs(modelGroups[TYPE_ASTEROID]);
|
---|
[0e0f851] | 533 |
|
---|
[5b02676] | 534 | /*** START OF REFACTORED CODE ***/
|
---|
[a0eb547] | 535 | modelGroups[TYPE_LASER] = createModelGroup(
|
---|
[4d84c72] | 536 | loadShaderProgram("gl-shaders/laser.vert", "gl-shaders/laser.frag"));
|
---|
[a0eb547] | 537 | shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram] = BufferInfo(); // temporary
|
---|
[0e0f851] | 538 |
|
---|
[a0eb547] | 539 | defineModelGroupAttrib(modelGroups[TYPE_LASER], "vertex_position", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 540 | 3, GL_FLOAT, offset_of(&SceneObject::points));
|
---|
[a0eb547] | 541 | defineModelGroupAttrib(modelGroups[TYPE_LASER], "vt", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 542 | 2, GL_FLOAT, offset_of(&SceneObject::texcoords));
|
---|
[a0eb547] | 543 | defineModelGroupAttrib(modelGroups[TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING,
|
---|
[7e10667] | 544 | 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
|
---|
[5b02676] | 545 | /*** END OF REFACTORED CODE ***/
|
---|
[20e0020] | 546 |
|
---|
[49db5fc] | 547 | defineModelGroupUniform(modelGroups[TYPE_LASER], "view", ATTRIB_UNIFORM,
|
---|
| 548 | 1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
|
---|
| 549 | defineModelGroupUniform(modelGroups[TYPE_LASER], "proj", ATTRIB_UNIFORM,
|
---|
| 550 | 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
|
---|
| 551 | defineModelGroupUniform(modelGroups[TYPE_LASER], "laser_color", ATTRIB_UNIFORM,
|
---|
| 552 | 1, UNIFORM_3F, laserColor);
|
---|
| 553 |
|
---|
[a0eb547] | 554 | initModelGroupAttribs(modelGroups[TYPE_LASER]);
|
---|
[20e0020] | 555 |
|
---|
[5b02676] | 556 | /*** START OF REFACTORED CODE ***/
|
---|
[a0eb547] | 557 | modelGroups[TYPE_EXPLOSION] = createModelGroup(
|
---|
[4d84c72] | 558 | loadShaderProgram("gl-shaders/explosion.vert", "gl-shaders/explosion.frag"));
|
---|
[a0eb547] | 559 | shaderBufferInfo[modelGroups[TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary
|
---|
| 560 |
|
---|
[b220f78] | 561 | defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "v_i", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 562 | 3, GL_FLOAT, offset_of(&ParticleEffect::particleVelocities));
|
---|
[b220f78] | 563 | defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "start_time", ATTRIB_POINT_VARYING,
|
---|
[7e10667] | 564 | 1, GL_FLOAT, offset_of(&ParticleEffect::particleTimes));
|
---|
[dc19a39] | 565 | defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "ubo_index", ATTRIB_OBJECT_VARYING,
|
---|
[7e10667] | 566 | 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
|
---|
[5b02676] | 567 | /*** END OF REFACTORED CODE ***/
|
---|
[b220f78] | 568 |
|
---|
| 569 | defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM,
|
---|
| 570 | 1, UNIFORM_1F, &curTime);
|
---|
| 571 | defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "view", ATTRIB_UNIFORM,
|
---|
| 572 | 1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
|
---|
| 573 | defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "proj", ATTRIB_UNIFORM,
|
---|
| 574 | 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
|
---|
| 575 |
|
---|
[b05e2b5] | 576 | initModelGroupAttribs(modelGroups[TYPE_EXPLOSION]);
|
---|
[de53394] | 577 |
|
---|
[a0eb547] | 578 | cam_pos = vec3(0.0f, 0.0f, 2.0f);
|
---|
| 579 | float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
|
---|
| 580 | float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
|
---|
| 581 |
|
---|
| 582 | // player ship
|
---|
[a926b79] | 583 | objects.push_back(createShip());
|
---|
[a0eb547] | 584 |
|
---|
[7e10667] | 585 | populateBuffers(objects, shaderBufferInfo, modelGroups, ubo);
|
---|
[20e0020] | 586 |
|
---|
[1f3d32b] | 587 | float cam_speed = 1.0f;
|
---|
| 588 | float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
| 589 | float cam_pitch_speed = 60.0f*ONE_DEG_IN_RAD;
|
---|
[20e0020] | 590 |
|
---|
[1f3d32b] | 591 | // glm::lookAt can create the view matrix
|
---|
| 592 | // glm::perspective can create the projection matrix
|
---|
[20e0020] | 593 |
|
---|
[1f3d32b] | 594 | mat4 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
| 595 | mat4 yaw_mat = rotate(mat4(1.0f), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 596 | mat4 pitch_mat = rotate(mat4(1.0f), -cam_pitch, vec3(1.0f, 0.0f, 0.0f));
|
---|
| 597 | mat4 R = pitch_mat * yaw_mat;
|
---|
| 598 | view_mat = R*T;
|
---|
[20e0020] | 599 |
|
---|
[1f3d32b] | 600 | // TODO: Create a function to construct the projection matrix
|
---|
| 601 | // (Maybe I should just use glm::perspective, after making sure it matches what I have now)
|
---|
| 602 | float fov = 67.0f * ONE_DEG_IN_RAD;
|
---|
[e6bc0f4] | 603 | float aspect = (float)windowWidth / (float)windowHeight;
|
---|
[20e0020] | 604 |
|
---|
[1f3d32b] | 605 | float range = tan(fov * 0.5f) * NEAR_CLIP;
|
---|
| 606 | float Sx = NEAR_CLIP / (range * aspect);
|
---|
| 607 | float Sy = NEAR_CLIP / range;
|
---|
| 608 | float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
| 609 | float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
|
---|
[20e0020] | 610 |
|
---|
[1f3d32b] | 611 | float proj_arr[] = {
|
---|
| 612 | Sx, 0.0f, 0.0f, 0.0f,
|
---|
| 613 | 0.0f, Sy, 0.0f, 0.0f,
|
---|
| 614 | 0.0f, 0.0f, Sz, -1.0f,
|
---|
| 615 | 0.0f, 0.0f, Pz, 0.0f,
|
---|
[f9a242b] | 616 | };
|
---|
[1f3d32b] | 617 | proj_mat = make_mat4(proj_arr);
|
---|
[81f28c0] | 618 |
|
---|
[c5fb958] | 619 | /* TODO: Fix the UBO binding code based on the following forum post (in order to support multiple ubos):
|
---|
[7e10667] | 620 | (Also, I bookmarked a great explanation of this under )
|
---|
[c5fb958] | 621 |
|
---|
[c55614a] | 622 | CHECK MY OpenGL BOOKMARK CALLED "Learn OpenGL: Advanced GLSL"
|
---|
| 623 |
|
---|
[c5fb958] | 624 | No, you're misunderstanding how this works. UBO binding works exactly like texture object binding.
|
---|
| 625 |
|
---|
| 626 | The OpenGL context has a number of slots for binding UBOs. There are GL_MAX_UNIFORM_BUFFER_BINDINGS number of
|
---|
| 627 | slots for UBO binding.
|
---|
| 628 |
|
---|
| 629 | Uniform Blocks in a program can be set to use one of the slots in the context. You do this by first querying
|
---|
| 630 | the block index using the block name (glGetUniformBlockIndex). This is similar to how you need to use
|
---|
| 631 | glGetUniformLocation in order to set a uniform's value with glUniform. Block indices, like uniform locations,
|
---|
| 632 | are specific to a program.
|
---|
| 633 |
|
---|
| 634 | Once you have the block index, you use glUniformBlockBinding to set that specific program to use a particular
|
---|
| 635 | uniform buffer slot in the context.
|
---|
| 636 |
|
---|
| 637 | Let's say you have a global UBO that you want to use for every program. To make using it easier, you want to
|
---|
| 638 | bind it just once.
|
---|
| 639 |
|
---|
| 640 | So first, you pick a uniform buffer slot in the context, one that always will refer to this UBO. Let's say
|
---|
| 641 | you pick slot 8.
|
---|
| 642 |
|
---|
| 643 | When you build a program object that may use this global uniform buffer, what you do is quite simple. First,
|
---|
| 644 | after linking the program, call glGetUniformBlockIndex(program, "NameOfGlobalUniformBlock"). If you get back
|
---|
| 645 | GL_INVALID_INDEX, then you know that the global uniform block isn't used in that program. Otherwise you get
|
---|
| 646 | back a block index.
|
---|
| 647 |
|
---|
| 648 | If you got a valid block index, then you call glUniformBlockBinding(program, uniformBlockIndex, 8). Remember
|
---|
| 649 | that 8 is the uniform buffer context slot that we selected earlier. This causes this particular program to
|
---|
| 650 | use uniform buffer slot #8 to find the buffer for "NameOfGlobalUniformBlock".
|
---|
| 651 |
|
---|
| 652 | Finally, to set the actual buffer in the context, call glBindBufferRange(GL_UNIFORM_BUFFER, 8,
|
---|
| 653 | bufferObjectName, offset, size);
|
---|
| 654 | */
|
---|
[fe5e3ca] | 655 |
|
---|
[1f3d32b] | 656 | GLuint ub_binding_point = 0;
|
---|
[81f28c0] | 657 |
|
---|
[4c7cd57] | 658 | GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_SHIP].shaderProgram, "models");
|
---|
[0e0f851] | 659 |
|
---|
[0414306] | 660 | GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_ASTEROID].shaderProgram, "models");
|
---|
[81f28c0] | 661 |
|
---|
[b62c109] | 662 | GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models");
|
---|
[81f28c0] | 663 |
|
---|
[0414306] | 664 | GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_EXPLOSION].shaderProgram, "models");
|
---|
[db06984] | 665 |
|
---|
[81f28c0] | 666 |
|
---|
[4c7cd57] | 667 | glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
|
---|
[49db5fc] | 668 | bindUniformData(modelGroups[TYPE_SHIP].attribs["view"]);
|
---|
| 669 | bindUniformData(modelGroups[TYPE_SHIP].attribs["proj"]);
|
---|
[485424b] | 670 |
|
---|
[4c7cd57] | 671 | glUniformBlockBinding(modelGroups[TYPE_SHIP].shaderProgram, ship_sp_models_ub_index, ub_binding_point);
|
---|
[14ff67c] | 672 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
[e165b85] | 673 |
|
---|
[fd6f465] | 674 |
|
---|
[0414306] | 675 | glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram);
|
---|
[49db5fc] | 676 | bindUniformData(modelGroups[TYPE_ASTEROID].attribs["view"]);
|
---|
| 677 | bindUniformData(modelGroups[TYPE_ASTEROID].attribs["proj"]);
|
---|
[0e0f851] | 678 |
|
---|
[0414306] | 679 | glUniformBlockBinding(modelGroups[TYPE_ASTEROID].shaderProgram, asteroid_sp_models_ub_index, ub_binding_point);
|
---|
[0e0f851] | 680 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 681 |
|
---|
| 682 |
|
---|
[49db5fc] | 683 | // may want to do initialization for basic_texture uniform here too
|
---|
| 684 | // Right now, I think I'm getting away without getting that uniform location because I'm only
|
---|
| 685 | // using one texture, so setting it to GL_TEXTURE0 once works
|
---|
[b62c109] | 686 | glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
|
---|
[49db5fc] | 687 | bindUniformData(modelGroups[TYPE_LASER].attribs["view"]);
|
---|
| 688 | bindUniformData(modelGroups[TYPE_LASER].attribs["proj"]);
|
---|
| 689 | bindUniformData(modelGroups[TYPE_LASER].attribs["laser_color"]);
|
---|
[b155f13] | 690 |
|
---|
[b62c109] | 691 | glUniformBlockBinding(modelGroups[TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point);
|
---|
[fd6f465] | 692 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 693 |
|
---|
| 694 |
|
---|
[0414306] | 695 | glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
|
---|
[7e10667] | 696 | bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["view"]);
|
---|
| 697 | bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["proj"]);
|
---|
| 698 |
|
---|
[0414306] | 699 | glUniformBlockBinding(modelGroups[TYPE_EXPLOSION].shaderProgram, explosion_sp_models_ub_index, ub_binding_point);
|
---|
[646f3f2] | 700 | glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
|
---|
| 701 |
|
---|
| 702 |
|
---|
[c4c205e] | 703 | double fps;
|
---|
| 704 | unsigned int score = 0;
|
---|
| 705 |
|
---|
[7ee66ea] | 706 | bool cam_moved = false;
|
---|
| 707 |
|
---|
[046ce72] | 708 | int frame_count = 0;
|
---|
[f70ab75] | 709 | double elapsed_seconds_fps = 0.0f;
|
---|
[5527206] | 710 | double elapsed_seconds_spawn = 0.0f;
|
---|
[b220f78] | 711 |
|
---|
| 712 | prevTime = glfwGetTime();
|
---|
[046ce72] | 713 |
|
---|
[9dd2eb7] | 714 | // This draws wireframes. Useful for seeing separate faces and occluded objects.
|
---|
| 715 | //glPolygonMode(GL_FRONT, GL_LINE);
|
---|
| 716 |
|
---|
[1f3d32b] | 717 | // disable vsync to see real framerate
|
---|
| 718 | //glfwSwapInterval(0);
|
---|
[1c81bf0] | 719 |
|
---|
[93462c6] | 720 | State curState = STATE_MAIN_MENU;
|
---|
| 721 |
|
---|
[a9d191a] | 722 | initGuiValueLists(valueLists);
|
---|
[c4c205e] | 723 |
|
---|
| 724 | valueLists["stats value list"].push_back(UIValue(UIVALUE_INT, "Score", &score));
|
---|
| 725 | valueLists["stats value list"].push_back(UIValue(UIVALUE_DOUBLE, "FPS", &fps));
|
---|
| 726 |
|
---|
[5b3462b] | 727 | while (!glfwWindowShouldClose(window) && isRunning) {
|
---|
[b220f78] | 728 | curTime = glfwGetTime();
|
---|
| 729 | elapsedTime = curTime - prevTime;
|
---|
[8fbd34f] | 730 |
|
---|
| 731 | // temporary code to get around vsync issue in OSX Sierra
|
---|
[b220f78] | 732 | if (elapsedTime < (1.0f / TARGET_FPS)) {
|
---|
[4ece3bf] | 733 | continue;
|
---|
[8fbd34f] | 734 | }
|
---|
| 735 |
|
---|
[b220f78] | 736 | prevTime = curTime;
|
---|
[93baa0e] | 737 |
|
---|
[b220f78] | 738 | elapsed_seconds_fps += elapsedTime;
|
---|
[1f3d32b] | 739 | if (elapsed_seconds_fps > 0.25f) {
|
---|
| 740 | fps = (double)frame_count / elapsed_seconds_fps;
|
---|
[046ce72] | 741 |
|
---|
[1f3d32b] | 742 | frame_count = 0;
|
---|
| 743 | elapsed_seconds_fps = 0.0f;
|
---|
[14ff67c] | 744 | }
|
---|
[046ce72] | 745 |
|
---|
[1f3d32b] | 746 | frame_count++;
|
---|
| 747 |
|
---|
[f7d35da] | 748 | // Handle events
|
---|
[baa5848] | 749 |
|
---|
| 750 | clickedObject = NULL;
|
---|
[f7d35da] | 751 |
|
---|
| 752 | // reset the all key states to KEY_STATE_UNCHANGED (something the GLFW key callback can never return)
|
---|
| 753 | // so that GLFW_PRESS and GLFW_RELEASE are only detected once
|
---|
[cf2d1e5] | 754 | // TODO: Change this if we ever need to act on GLFW_REPEAT (which is when a key is held down
|
---|
| 755 | // continuously for a period of time)
|
---|
[f7d35da] | 756 | fill(key_state, key_state + NUM_KEYS, KEY_STATE_UNCHANGED);
|
---|
| 757 |
|
---|
[baa5848] | 758 | glfwPollEvents();
|
---|
| 759 |
|
---|
[93462c6] | 760 | while (!events.empty()) {
|
---|
| 761 | switch (events.front()) {
|
---|
| 762 | case EVENT_GO_TO_MAIN_MENU:
|
---|
| 763 | curState = STATE_MAIN_MENU;
|
---|
| 764 | break;
|
---|
| 765 | case EVENT_GO_TO_GAME:
|
---|
| 766 | curState = STATE_GAME;
|
---|
| 767 | break;
|
---|
| 768 | case EVENT_QUIT:
|
---|
| 769 | isRunning = false;
|
---|
| 770 | break;
|
---|
| 771 | }
|
---|
| 772 | events.pop();
|
---|
[147ac6d] | 773 | }
|
---|
[93462c6] | 774 |
|
---|
| 775 | if (curState == STATE_GAME) {
|
---|
[95595de] | 776 |
|
---|
[b220f78] | 777 | elapsed_seconds_spawn += elapsedTime;
|
---|
[95595de] | 778 | if (elapsed_seconds_spawn > 0.5f) {
|
---|
[dd9771c] | 779 | SceneObject* obj = createAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)));
|
---|
[f97e638] | 780 | addObjectToScene(obj, shaderBufferInfo, modelGroups, ubo);
|
---|
[95595de] | 781 |
|
---|
| 782 | elapsed_seconds_spawn -= 0.5f;
|
---|
| 783 | }
|
---|
| 784 |
|
---|
[cf2d1e5] | 785 | /*
|
---|
[93462c6] | 786 | if (clickedObject == &objects[0]) {
|
---|
| 787 | selectedObject = &objects[0];
|
---|
| 788 | }
|
---|
| 789 | if (clickedObject == &objects[1]) {
|
---|
| 790 | selectedObject = &objects[1];
|
---|
| 791 | }
|
---|
[cf2d1e5] | 792 | */
|
---|
[f7d35da] | 793 |
|
---|
| 794 | /*
|
---|
| 795 | if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
|
---|
[dba67b2] | 796 | transformObject(objects[1], translate(mat4(1.0f), vec3(0.3f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 797 | }
|
---|
[fabed35] | 798 | if (key_down[GLFW_KEY_RIGHT]) {
|
---|
[dba67b2] | 799 | transformObject(objects[2], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 800 | }
|
---|
[fabed35] | 801 | if (key_down[GLFW_KEY_LEFT]) {
|
---|
[dba67b2] | 802 | transformObject(objects[2], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
|
---|
[f7d35da] | 803 | }
|
---|
| 804 | */
|
---|
[cf2d1e5] | 805 |
|
---|
[fabed35] | 806 | if (key_down[GLFW_KEY_RIGHT]) {
|
---|
[1f3d32b] | 807 | transformObject(*objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);
|
---|
[fabed35] | 808 |
|
---|
[1f3d32b] | 809 | if (leftLaser != NULL && !leftLaser->deleted) {
|
---|
| 810 | translateLaser(leftLaser, vec3(0.01f, 0.0f, 0.0f), ubo);
|
---|
[fabed35] | 811 | }
|
---|
[1f3d32b] | 812 | if (rightLaser != NULL && !rightLaser->deleted) {
|
---|
| 813 | translateLaser(rightLaser, vec3(0.01f, 0.0f, 0.0f), ubo);
|
---|
[fabed35] | 814 | }
|
---|
[cf2d1e5] | 815 | }
|
---|
[fabed35] | 816 | if (key_down[GLFW_KEY_LEFT]) {
|
---|
[1f3d32b] | 817 | transformObject(*objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);
|
---|
[fabed35] | 818 |
|
---|
[1f3d32b] | 819 | if (leftLaser != NULL && !leftLaser->deleted) {
|
---|
| 820 | translateLaser(leftLaser, vec3(-0.01f, 0.0f, 0.0f), ubo);
|
---|
[fabed35] | 821 | }
|
---|
[1f3d32b] | 822 | if (rightLaser != NULL && !rightLaser->deleted) {
|
---|
| 823 | translateLaser(rightLaser, vec3(-0.01f, 0.0f, 0.0f), ubo);
|
---|
[fabed35] | 824 | }
|
---|
[8316333] | 825 | }
|
---|
[fabed35] | 826 |
|
---|
| 827 | if (key_state[GLFW_KEY_Z] == GLFW_PRESS) {
|
---|
[1f3d32b] | 828 | vec3 offset(objects[0]->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
[8316333] | 829 |
|
---|
[dd9771c] | 830 | leftLaser = createLaser(
|
---|
| 831 | vec3(-0.21f, -1.19f, 1.76f)+offset,
|
---|
| 832 | vec3(-0.21f, -1.19f, -3.0f)+offset,
|
---|
| 833 | vec3(0.0f, 1.0f, 0.0f), 0.03f);
|
---|
[f97e638] | 834 | addObjectToScene(leftLaser, shaderBufferInfo, modelGroups, ubo);
|
---|
[fabed35] | 835 | } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) {
|
---|
[1f3d32b] | 836 | removeObjectFromScene(*leftLaser, ubo);
|
---|
[8316333] | 837 | }
|
---|
[fabed35] | 838 |
|
---|
| 839 | if (key_state[GLFW_KEY_X] == GLFW_PRESS) {
|
---|
[1f3d32b] | 840 | vec3 offset(objects[0]->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
[8316333] | 841 |
|
---|
[dd9771c] | 842 | rightLaser = createLaser(
|
---|
| 843 | vec3(0.21f, -1.19f, 1.76f) + offset,
|
---|
| 844 | vec3(0.21f, -1.19f, -3.0f) + offset,
|
---|
| 845 | vec3(0.0f, 1.0f, 0.0f), 0.03f);
|
---|
[f97e638] | 846 | addObjectToScene(rightLaser, shaderBufferInfo, modelGroups, ubo);
|
---|
[fabed35] | 847 | } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) {
|
---|
[1f3d32b] | 848 | removeObjectFromScene(*rightLaser, ubo);
|
---|
[cf2d1e5] | 849 | }
|
---|
| 850 |
|
---|
[92b1e90] | 851 | // this code moves the asteroids
|
---|
[8e8aed6] | 852 | for (unsigned int i = 0; i < objects.size(); i++) {
|
---|
[7e10667] | 853 | if (!objects[i]->deleted) {
|
---|
| 854 | if (objects[i]->type == TYPE_ASTEROID) {
|
---|
| 855 | transformObject(*objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo);
|
---|
| 856 |
|
---|
| 857 | vec3 obj_center = vec3(view_mat * vec4(objects[i]->bounding_center, 1.0f));
|
---|
| 858 |
|
---|
| 859 | if ((obj_center.z - objects[i]->bounding_radius) > -NEAR_CLIP) {
|
---|
| 860 | removeObjectFromScene(*objects[i], ubo);
|
---|
| 861 | }
|
---|
| 862 | if (((Asteroid*)objects[i])->hp <= 0) {
|
---|
| 863 | // TODO: Optimize this so I don't recalculate the camera rotation every time
|
---|
| 864 | float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
|
---|
| 865 | mat4 pitch_mat = rotate(mat4(1.0f), cam_pitch, vec3(1.0f, 0.0f, 0.0f));
|
---|
| 866 | mat4 model_mat = translate(mat4(1.0f), objects[i]->bounding_center) * pitch_mat;
|
---|
| 867 |
|
---|
| 868 | removeObjectFromScene(*objects[i], ubo);
|
---|
| 869 | score++;
|
---|
| 870 |
|
---|
| 871 | addObjectToScene(createExplosion(model_mat), shaderBufferInfo, modelGroups, ubo);
|
---|
| 872 | }
|
---|
| 873 | } else if (objects[i]->type == TYPE_EXPLOSION) {
|
---|
| 874 | ParticleEffect* explosion = (ParticleEffect*)objects[i];
|
---|
| 875 | if (glfwGetTime() >= explosion->startTime + explosion->duration) {
|
---|
| 876 | removeObjectFromScene(*objects[i], ubo);
|
---|
| 877 | }
|
---|
[2b0214c] | 878 | }
|
---|
[5527206] | 879 | }
|
---|
[cf2d1e5] | 880 | }
|
---|
[93baa0e] | 881 |
|
---|
[1f3d32b] | 882 | if (leftLaser != NULL && !leftLaser->deleted) {
|
---|
[a0eb547] | 883 | updateLaserTarget(leftLaser, objects, modelGroups[TYPE_LASER], modelGroups[TYPE_ASTEROID].shaderProgram);
|
---|
[1f3d32b] | 884 | }
|
---|
| 885 | if (rightLaser != NULL && !rightLaser->deleted) {
|
---|
[a0eb547] | 886 | updateLaserTarget(rightLaser, objects, modelGroups[TYPE_LASER], modelGroups[TYPE_ASTEROID].shaderProgram);
|
---|
[1f3d32b] | 887 | }
|
---|
| 888 | }
|
---|
| 889 |
|
---|
| 890 | for (vector<EffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) {
|
---|
| 891 | if ((*it)->deleted || (*it)->effectedObject->deleted) {
|
---|
| 892 | delete *it;
|
---|
| 893 | it = effects.erase(it);
|
---|
| 894 | } else {
|
---|
| 895 | EffectOverTime* eot = *it;
|
---|
[b220f78] | 896 | eot->effectedValue = eot->startValue + (curTime - eot->startTime) * eot->changePerSecond;
|
---|
[1f3d32b] | 897 |
|
---|
| 898 | it++;
|
---|
[c3c3158] | 899 | }
|
---|
[baa5848] | 900 | }
|
---|
[df652d5] | 901 |
|
---|
[c3c3158] | 902 | if (key_state[GLFW_KEY_ESCAPE] == GLFW_PRESS) {
|
---|
[ec4456b] | 903 | glfwSetWindowShouldClose(window, 1);
|
---|
| 904 | }
|
---|
[7ee66ea] | 905 |
|
---|
[b220f78] | 906 | float dist = cam_speed * elapsedTime;
|
---|
[fabed35] | 907 | if (key_down[GLFW_KEY_A]) {
|
---|
[dba67b2] | 908 | vec3 dir = vec3(inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f));
|
---|
[809ce16] | 909 | cam_pos += dir * dist;
|
---|
[f7d35da] | 910 |
|
---|
[7ee66ea] | 911 | cam_moved = true;
|
---|
| 912 | }
|
---|
[fabed35] | 913 | if (key_down[GLFW_KEY_D]) {
|
---|
[dba67b2] | 914 | vec3 dir = vec3(inverse(R) * vec4(1.0f, 0.0f, 0.0f, 1.0f));
|
---|
[809ce16] | 915 | cam_pos += dir * dist;
|
---|
[f7d35da] | 916 |
|
---|
[7ee66ea] | 917 | cam_moved = true;
|
---|
| 918 | }
|
---|
[fabed35] | 919 | if (key_down[GLFW_KEY_W]) {
|
---|
[dba67b2] | 920 | vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, -1.0f, 1.0f));
|
---|
[809ce16] | 921 | cam_pos += dir * dist;
|
---|
[f7d35da] | 922 |
|
---|
[7ee66ea] | 923 | cam_moved = true;
|
---|
| 924 | }
|
---|
[fabed35] | 925 | if (key_down[GLFW_KEY_S]) {
|
---|
[dba67b2] | 926 | vec3 dir = vec3(inverse(R) * vec4(0.0f, 0.0f, 1.0f, 1.0f));
|
---|
[809ce16] | 927 | cam_pos += dir * dist;
|
---|
[f7d35da] | 928 |
|
---|
[7ee66ea] | 929 | cam_moved = true;
|
---|
| 930 | }
|
---|
[cf2d1e5] | 931 | /*
|
---|
[fabed35] | 932 | if (key_down[GLFW_KEY_LEFT]) {
|
---|
[b220f78] | 933 | cam_yaw += cam_yaw_speed * elapsedTime;
|
---|
[c3c3158] | 934 | cam_moved = true;
|
---|
[7ee66ea] | 935 | }
|
---|
[fabed35] | 936 | if (key_down[GLFW_KEY_RIGHT]) {
|
---|
[b220f78] | 937 | cam_yaw -= cam_yaw_speed * elapsedTime;
|
---|
[c3c3158] | 938 | cam_moved = true;
|
---|
[7ee66ea] | 939 | }
|
---|
[fabed35] | 940 | if (key_down[GLFW_KEY_UP]) {
|
---|
[b220f78] | 941 | cam_pitch += cam_pitch_speed * elapsedTime;
|
---|
[c3c3158] | 942 | cam_moved = true;
|
---|
[809ce16] | 943 | }
|
---|
[fabed35] | 944 | if (key_down[GLFW_KEY_DOWN]) {
|
---|
[b220f78] | 945 | cam_pitch -= cam_pitch_speed * elapsedTime;
|
---|
[c3c3158] | 946 | cam_moved = true;
|
---|
[809ce16] | 947 | }
|
---|
[cf2d1e5] | 948 | */
|
---|
[1f3d32b] | 949 | if (cam_moved && false) { // disable camera movement
|
---|
[dba67b2] | 950 | T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
|
---|
[809ce16] | 951 |
|
---|
[dba67b2] | 952 | mat4 yaw_mat = rotate(mat4(1.0f), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
|
---|
| 953 | mat4 pitch_mat = rotate(mat4(1.0f), -cam_pitch, vec3(1.0f, 0.0f, 0.0f));
|
---|
[809ce16] | 954 | R = pitch_mat * yaw_mat;
|
---|
[f7d35da] | 955 |
|
---|
[c3c3158] | 956 | view_mat = R * T;
|
---|
[7ee66ea] | 957 |
|
---|
[4c7cd57] | 958 | glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
|
---|
[49db5fc] | 959 | bindUniformData(modelGroups[TYPE_SHIP].attribs["view"]);
|
---|
[267c4c5] | 960 |
|
---|
[b62c109] | 961 | glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
|
---|
[49db5fc] | 962 | bindUniformData(modelGroups[TYPE_LASER].attribs["view"]);
|
---|
[b155f13] | 963 |
|
---|
[7ee66ea] | 964 | cam_moved = false;
|
---|
| 965 | }
|
---|
[c3c3158] | 966 |
|
---|
[0414306] | 967 | glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
|
---|
[b220f78] | 968 | bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["cur_time"]);
|
---|
[db06984] | 969 |
|
---|
[cabdd5c] | 970 | /*** START OF REFACTORED CODE ***/
|
---|
[c3c3158] | 971 | // Render scene
|
---|
| 972 |
|
---|
| 973 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[cabdd5c] | 974 | /*** END OF REFACTORED CODE ***/
|
---|
[e6bc0f4] | 975 |
|
---|
[c3c3158] | 976 | switch (curState) {
|
---|
| 977 | case STATE_MAIN_MENU:
|
---|
| 978 | renderMainMenu();
|
---|
| 979 | renderMainMenuGui();
|
---|
| 980 | break;
|
---|
| 981 | case STATE_GAME:
|
---|
[7e10667] | 982 | renderScene(modelGroups, ubo);
|
---|
[c4c205e] | 983 | renderSceneGui(valueLists);
|
---|
[c3c3158] | 984 | break;
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | glfwSwapBuffers(window);
|
---|
[644a2e4] | 988 | }
|
---|
| 989 |
|
---|
[c1ca5b5] | 990 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
| 991 | ImGui::DestroyContext();
|
---|
| 992 |
|
---|
| 993 | glfwDestroyWindow(window);
|
---|
[5272b6b] | 994 | glfwTerminate();
|
---|
[c1ca5b5] | 995 |
|
---|
[98f06d9] | 996 | close_log();
|
---|
| 997 |
|
---|
[1f3d32b] | 998 | // free memory
|
---|
| 999 |
|
---|
| 1000 | for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
| 1001 | delete *it;
|
---|
| 1002 | }
|
---|
[d9b6a1c] | 1003 |
|
---|
[6abfd07] | 1004 | return 0;
|
---|
[5272b6b] | 1005 | }
|
---|
[ec4456b] | 1006 |
|
---|
[4f3262f] | 1007 | void glfw_error_callback(int error, const char* description) {
|
---|
[bae0911] | 1008 | gl_log_err("GLFW ERROR: code %i msg: %s", error, description);
|
---|
[98f06d9] | 1009 | cerr << "GLFW ERROR: code " << error << " msg: " << description << endl;
|
---|
| 1010 | get_log() << "GLFW ERROR: code " << error << " msg: " << description << endl;
|
---|
[4f3262f] | 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
---|
| 1014 | double mouse_x, mouse_y;
|
---|
| 1015 | glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
---|
| 1016 |
|
---|
| 1017 | if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
|
---|
| 1018 | cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
|
---|
| 1019 | selectedObject = NULL;
|
---|
| 1020 |
|
---|
[e6bc0f4] | 1021 | float x = (2.0f*mouse_x) / windowWidth - 1.0f;
|
---|
| 1022 | float y = 1.0f - (2.0f*mouse_y) / windowHeight;
|
---|
[4f3262f] | 1023 |
|
---|
| 1024 | cout << "x: " << x << ", y: " << y << endl;
|
---|
| 1025 |
|
---|
| 1026 | vec4 ray_clip = vec4(x, y, -1.0f, 1.0f);
|
---|
| 1027 | vec4 ray_eye = inverse(proj_mat) * ray_clip;
|
---|
[dba67b2] | 1028 | ray_eye = vec4(vec2(ray_eye), -1.0f, 1.0f);
|
---|
[4f3262f] | 1029 | vec4 ray_world = inverse(view_mat) * ray_eye;
|
---|
| 1030 |
|
---|
| 1031 | vec4 click_point;
|
---|
| 1032 | 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
|
---|
| 1033 | SceneObject* closest_object = NULL;
|
---|
| 1034 |
|
---|
[1f3d32b] | 1035 | for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
| 1036 | if ((*it)->type == TYPE_LASER) continue;
|
---|
| 1037 | for (unsigned int p_idx = 0; p_idx < (*it)->points.size(); p_idx += 9) {
|
---|
[0d5c100] | 1038 | if (faceClicked(
|
---|
| 1039 | {
|
---|
[1f3d32b] | 1040 | vec3((*it)->points[p_idx], (*it)->points[p_idx + 1], (*it)->points[p_idx + 2]),
|
---|
| 1041 | vec3((*it)->points[p_idx + 3], (*it)->points[p_idx + 4], (*it)->points[p_idx + 5]),
|
---|
| 1042 | vec3((*it)->points[p_idx + 6], (*it)->points[p_idx + 7], (*it)->points[p_idx + 8]),
|
---|
[4f3262f] | 1043 | },
|
---|
[1f3d32b] | 1044 | *it, ray_world, vec4(cam_pos, 1.0f), click_point
|
---|
[0d5c100] | 1045 | )) {
|
---|
[4f3262f] | 1046 | click_point = view_mat * click_point;
|
---|
| 1047 |
|
---|
| 1048 | if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) {
|
---|
[dba67b2] | 1049 | closest_point = vec3(click_point);
|
---|
[1f3d32b] | 1050 | closest_object = *it;
|
---|
[4f3262f] | 1051 | }
|
---|
| 1052 | }
|
---|
| 1053 | }
|
---|
| 1054 | }
|
---|
| 1055 |
|
---|
| 1056 | if (closest_object == NULL) {
|
---|
| 1057 | cout << "No object was clicked" << endl;
|
---|
[f7d35da] | 1058 | } else {
|
---|
[4f3262f] | 1059 | clickedObject = closest_object;
|
---|
| 1060 | cout << "Clicked object: " << clickedObject->id << endl;
|
---|
| 1061 | }
|
---|
| 1062 | }
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
[f7d35da] | 1065 | void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
---|
| 1066 | key_state[key] = action;
|
---|
| 1067 |
|
---|
| 1068 | // should be true for GLFW_PRESS and GLFW_REPEAT
|
---|
[fabed35] | 1069 | key_down[key] = (action != GLFW_RELEASE);
|
---|
[f7d35da] | 1070 | }
|
---|
| 1071 |
|
---|
[5b02676] | 1072 | /*** START OF REFACTORED CODE ***/
|
---|
[e6bc0f4] | 1073 | void window_size_callback(GLFWwindow* window, int width, int height) {
|
---|
| 1074 | cout << "Window resized to (" << width << ", " << height << ")" << endl;
|
---|
| 1075 |
|
---|
| 1076 | windowWidth = width;
|
---|
| 1077 | windowHeight = height;
|
---|
| 1078 |
|
---|
| 1079 | // TODO: Ideally, remove the window title bar when the window is maximized
|
---|
| 1080 | // Check https://github.com/glfw/glfw/issues/778
|
---|
| 1081 |
|
---|
| 1082 | // This requires glfw3.3. I think I have to upgrade
|
---|
| 1083 | // Doesn't seem to be needed in OSX and also causes a segfault there
|
---|
| 1084 | //glfwSetWindowAttrib(window, GLFW_DECORATED, GLFW_FALSE);
|
---|
| 1085 | }
|
---|
[5b02676] | 1086 | /*** END OF REFACTORED CODE ***/
|
---|
[e6bc0f4] | 1087 |
|
---|
[0e0f851] | 1088 | void APIENTRY debugGlCallback(
|
---|
| 1089 | GLenum source,
|
---|
| 1090 | GLenum type,
|
---|
| 1091 | GLuint id,
|
---|
| 1092 | GLenum severity,
|
---|
| 1093 | GLsizei length,
|
---|
| 1094 | const GLchar* message,
|
---|
| 1095 | const void* userParam
|
---|
| 1096 | ) {
|
---|
| 1097 | string strMessage(message);
|
---|
| 1098 |
|
---|
| 1099 | // TODO: Use C++ strings directly
|
---|
| 1100 | char source_str[2048];
|
---|
| 1101 | char type_str[2048];
|
---|
| 1102 | char severity_str[2048];
|
---|
| 1103 |
|
---|
| 1104 | switch (source) {
|
---|
[a0eb547] | 1105 | case 0x8246:
|
---|
| 1106 | strcpy(source_str, "API");
|
---|
| 1107 | break;
|
---|
| 1108 | case 0x8247:
|
---|
| 1109 | strcpy(source_str, "WINDOW_SYSTEM");
|
---|
| 1110 | break;
|
---|
| 1111 | case 0x8248:
|
---|
| 1112 | strcpy(source_str, "SHADER_COMPILER");
|
---|
| 1113 | break;
|
---|
| 1114 | case 0x8249:
|
---|
| 1115 | strcpy(source_str, "THIRD_PARTY");
|
---|
| 1116 | break;
|
---|
| 1117 | case 0x824A:
|
---|
| 1118 | strcpy(source_str, "APPLICATION");
|
---|
| 1119 | break;
|
---|
| 1120 | case 0x824B:
|
---|
| 1121 | strcpy(source_str, "OTHER");
|
---|
| 1122 | break;
|
---|
| 1123 | default:
|
---|
| 1124 | strcpy(source_str, "undefined");
|
---|
| 1125 | break;
|
---|
[0e0f851] | 1126 | }
|
---|
| 1127 |
|
---|
| 1128 | switch (type) {
|
---|
[a0eb547] | 1129 | case 0x824C:
|
---|
| 1130 | strcpy(type_str, "ERROR");
|
---|
| 1131 | break;
|
---|
| 1132 | case 0x824D:
|
---|
| 1133 | strcpy(type_str, "DEPRECATED_BEHAVIOR");
|
---|
| 1134 | break;
|
---|
| 1135 | case 0x824E:
|
---|
| 1136 | strcpy(type_str, "UNDEFINED_BEHAVIOR");
|
---|
| 1137 | break;
|
---|
| 1138 | case 0x824F:
|
---|
| 1139 | strcpy(type_str, "PORTABILITY");
|
---|
| 1140 | break;
|
---|
| 1141 | case 0x8250:
|
---|
| 1142 | strcpy(type_str, "PERFORMANCE");
|
---|
| 1143 | break;
|
---|
| 1144 | case 0x8251:
|
---|
| 1145 | strcpy(type_str, "OTHER");
|
---|
| 1146 | break;
|
---|
| 1147 | case 0x8268:
|
---|
| 1148 | strcpy(type_str, "MARKER");
|
---|
| 1149 | break;
|
---|
| 1150 | case 0x8269:
|
---|
| 1151 | strcpy(type_str, "PUSH_GROUP");
|
---|
| 1152 | break;
|
---|
| 1153 | case 0x826A:
|
---|
| 1154 | strcpy(type_str, "POP_GROUP");
|
---|
| 1155 | break;
|
---|
| 1156 | default:
|
---|
| 1157 | strcpy(type_str, "undefined");
|
---|
| 1158 | break;
|
---|
[0e0f851] | 1159 | }
|
---|
| 1160 | switch (severity) {
|
---|
[a0eb547] | 1161 | case 0x9146:
|
---|
| 1162 | strcpy(severity_str, "HIGH");
|
---|
| 1163 | break;
|
---|
| 1164 | case 0x9147:
|
---|
| 1165 | strcpy(severity_str, "MEDIUM");
|
---|
| 1166 | break;
|
---|
| 1167 | case 0x9148:
|
---|
| 1168 | strcpy(severity_str, "LOW");
|
---|
| 1169 | break;
|
---|
| 1170 | case 0x826B:
|
---|
| 1171 | strcpy(severity_str, "NOTIFICATION");
|
---|
| 1172 | break;
|
---|
| 1173 | default:
|
---|
| 1174 | strcpy(severity_str, "undefined");
|
---|
| 1175 | break;
|
---|
[0e0f851] | 1176 | }
|
---|
| 1177 |
|
---|
| 1178 | if (string(severity_str) != "NOTIFICATION") {
|
---|
| 1179 | cout << "OpenGL Error!!!" << endl;
|
---|
| 1180 | cout << "Source: " << string(source_str) << endl;
|
---|
| 1181 | cout << "Type: " << string(type_str) << endl;
|
---|
| 1182 | cout << "Severity: " << string(severity_str) << endl;
|
---|
| 1183 | cout << strMessage << endl;
|
---|
| 1184 | }
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
[f7d35da] | 1187 |
|
---|
[ec4456b] | 1188 | GLuint loadShader(GLenum type, string file) {
|
---|
| 1189 | cout << "Loading shader from file " << file << endl;
|
---|
| 1190 |
|
---|
| 1191 | ifstream shaderFile(file);
|
---|
| 1192 | GLuint shaderId = 0;
|
---|
| 1193 |
|
---|
| 1194 | if (shaderFile.is_open()) {
|
---|
| 1195 | string line, shaderString;
|
---|
| 1196 |
|
---|
| 1197 | while(getline(shaderFile, line)) {
|
---|
| 1198 | shaderString += line + "\n";
|
---|
| 1199 | }
|
---|
| 1200 | shaderFile.close();
|
---|
| 1201 | const char* shaderCString = shaderString.c_str();
|
---|
| 1202 |
|
---|
| 1203 | shaderId = glCreateShader(type);
|
---|
| 1204 | glShaderSource(shaderId, 1, &shaderCString, NULL);
|
---|
| 1205 | glCompileShader(shaderId);
|
---|
| 1206 |
|
---|
| 1207 | cout << "Loaded successfully" << endl;
|
---|
| 1208 | } else {
|
---|
[e856d62] | 1209 | cout << "Failed to load the file" << endl;
|
---|
[ec4456b] | 1210 | }
|
---|
| 1211 |
|
---|
| 1212 | return shaderId;
|
---|
| 1213 | }
|
---|
[485424b] | 1214 |
|
---|
| 1215 | GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath) {
|
---|
| 1216 | GLuint vs = loadShader(GL_VERTEX_SHADER, vertexShaderPath);
|
---|
| 1217 | GLuint fs = loadShader(GL_FRAGMENT_SHADER, fragmentShaderPath);
|
---|
| 1218 |
|
---|
| 1219 | GLuint shader_program = glCreateProgram();
|
---|
| 1220 | glAttachShader(shader_program, vs);
|
---|
| 1221 | glAttachShader(shader_program, fs);
|
---|
| 1222 |
|
---|
| 1223 | glLinkProgram(shader_program);
|
---|
| 1224 |
|
---|
| 1225 | return shader_program;
|
---|
| 1226 | }
|
---|
| 1227 |
|
---|
| 1228 | unsigned char* loadImage(string file_name, int* x, int* y) {
|
---|
| 1229 | int n;
|
---|
[e856d62] | 1230 | int force_channels = 4; // This forces RGBA (4 bytes per pixel)
|
---|
[485424b] | 1231 | unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
|
---|
[e856d62] | 1232 |
|
---|
| 1233 | int width_in_bytes = *x * 4;
|
---|
| 1234 | unsigned char *top = NULL;
|
---|
| 1235 | unsigned char *bottom = NULL;
|
---|
| 1236 | unsigned char temp = 0;
|
---|
| 1237 | int half_height = *y / 2;
|
---|
| 1238 |
|
---|
| 1239 | // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
|
---|
| 1240 | for (int row = 0; row < half_height; row++) {
|
---|
| 1241 | top = image_data + row * width_in_bytes;
|
---|
| 1242 | bottom = image_data + (*y - row - 1) * width_in_bytes;
|
---|
| 1243 | for (int col = 0; col < width_in_bytes; col++) {
|
---|
| 1244 | temp = *top;
|
---|
| 1245 | *top = *bottom;
|
---|
| 1246 | *bottom = temp;
|
---|
| 1247 | top++;
|
---|
| 1248 | bottom++;
|
---|
| 1249 | }
|
---|
| 1250 | }
|
---|
| 1251 |
|
---|
[485424b] | 1252 | if (!image_data) {
|
---|
[bae0911] | 1253 | gl_log_err("ERROR: could not load %s", file_name.c_str());
|
---|
[98f06d9] | 1254 | cerr << "ERROR: could not load " << file_name << endl;
|
---|
| 1255 | get_log() << "ERROR: could not load " << file_name << endl;
|
---|
[485424b] | 1256 | }
|
---|
[e856d62] | 1257 |
|
---|
| 1258 | // Not Power-of-2 check
|
---|
| 1259 | if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
|
---|
[bae0911] | 1260 | gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str());
|
---|
[98f06d9] | 1261 | cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
|
---|
| 1262 | get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
|
---|
[e856d62] | 1263 | }
|
---|
| 1264 |
|
---|
[485424b] | 1265 | return image_data;
|
---|
| 1266 | }
|
---|
[33a9664] | 1267 |
|
---|
[d9f99b2] | 1268 | bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) {
|
---|
[5c9d193] | 1269 | // LINE EQUATION: P = O + Dt
|
---|
[b73cb3b] | 1270 | // O = cam
|
---|
[5c9d193] | 1271 | // D = ray_world
|
---|
| 1272 |
|
---|
[b73cb3b] | 1273 | // PLANE EQUATION: P dot n + d = 0
|
---|
| 1274 | // n is the normal vector
|
---|
| 1275 | // d is the offset from the origin
|
---|
[5c9d193] | 1276 |
|
---|
| 1277 | // Take the cross-product of two vectors on the plane to get the normal
|
---|
[d9f99b2] | 1278 | vec3 v1 = points[1] - points[0];
|
---|
| 1279 | vec3 v2 = points[2] - points[0];
|
---|
[5c9d193] | 1280 |
|
---|
[1f3d32b] | 1281 | 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);
|
---|
| 1282 |
|
---|
| 1283 | vec3 local_ray = vec3(inverse(obj->model_mat) * world_ray);
|
---|
| 1284 | vec3 local_cam = vec3(inverse(obj->model_mat) * cam);
|
---|
| 1285 |
|
---|
| 1286 | local_ray = local_ray - local_cam;
|
---|
| 1287 |
|
---|
| 1288 | float d = -glm::dot(points[0], normal);
|
---|
| 1289 | float t = -(glm::dot(local_cam, normal) + d) / glm::dot(local_ray, normal);
|
---|
| 1290 |
|
---|
| 1291 | vec3 intersection = local_cam + t*local_ray;
|
---|
| 1292 |
|
---|
| 1293 | if (insideTriangle(intersection, points)) {
|
---|
| 1294 | click_point = obj->model_mat * vec4(intersection, 1.0f);
|
---|
| 1295 | return true;
|
---|
| 1296 | } else {
|
---|
| 1297 | return false;
|
---|
| 1298 | }
|
---|
| 1299 | }
|
---|
| 1300 |
|
---|
| 1301 | bool insideTriangle(vec3 p, array<vec3, 3> triangle_points) {
|
---|
| 1302 | vec3 v21 = triangle_points[1] - triangle_points[0];
|
---|
| 1303 | vec3 v31 = triangle_points[2] - triangle_points[0];
|
---|
| 1304 | vec3 pv1 = p - triangle_points[0];
|
---|
| 1305 |
|
---|
| 1306 | float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
|
---|
| 1307 | float x = (pv1.x-y*v31.x) / v21.x;
|
---|
| 1308 |
|
---|
| 1309 | return x > 0.0f && y > 0.0f && x+y < 1.0f;
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
[7e10667] | 1312 | // TODO: Pass a reference, not a pointer
|
---|
| 1313 | void initObject(SceneObject* obj) {
|
---|
[1f3d32b] | 1314 | obj->id = objects.size(); // currently unused
|
---|
| 1315 | obj->num_points = obj->points.size() / 3;
|
---|
| 1316 | obj->model_transform = mat4(1.0f);
|
---|
| 1317 | obj->deleted = false;
|
---|
| 1318 |
|
---|
| 1319 | obj->normals.reserve(obj->points.size());
|
---|
[8e8aed6] | 1320 | for (unsigned int i = 0; i < obj->points.size(); i += 9) {
|
---|
[1f3d32b] | 1321 | vec3 point1 = vec3(obj->points[i], obj->points[i + 1], obj->points[i + 2]);
|
---|
| 1322 | vec3 point2 = vec3(obj->points[i + 3], obj->points[i + 4], obj->points[i + 5]);
|
---|
| 1323 | vec3 point3 = vec3(obj->points[i + 6], obj->points[i + 7], obj->points[i + 8]);
|
---|
| 1324 |
|
---|
| 1325 | vec3 normal = normalize(cross(point2 - point1, point3 - point1));
|
---|
| 1326 |
|
---|
| 1327 | // Add the same normal for all 3 points
|
---|
| 1328 | for (int j = 0; j < 3; j++) {
|
---|
| 1329 | obj->normals.push_back(normal.x);
|
---|
| 1330 | obj->normals.push_back(normal.y);
|
---|
| 1331 | obj->normals.push_back(normal.z);
|
---|
| 1332 | }
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
[646f3f2] | 1335 | if (obj->type == TYPE_SHIP || obj->type == TYPE_ASTEROID) {
|
---|
[1f3d32b] | 1336 | calculateObjectBoundingBox(obj);
|
---|
| 1337 |
|
---|
| 1338 | obj->bounding_center = vec3(obj->translate_mat * vec4(obj->bounding_center, 1.0f));
|
---|
| 1339 | }
|
---|
| 1340 | }
|
---|
| 1341 |
|
---|
[7e10667] | 1342 | // TODO: Check if I can pass in a reference to obj instead (do this for all other functions as well)
|
---|
[1f3d32b] | 1343 | void addObjectToScene(SceneObject* obj,
|
---|
| 1344 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
[0414306] | 1345 | map<ObjectType, ShaderModelGroup>& modelGroups,
|
---|
[f97e638] | 1346 | GLuint ubo) {
|
---|
[1f3d32b] | 1347 | objects.push_back(obj);
|
---|
| 1348 |
|
---|
[dd9771c] | 1349 | BufferInfo* bufferInfo = &shaderBufferInfo[modelGroups[obj->type].shaderProgram];
|
---|
[1f3d32b] | 1350 |
|
---|
| 1351 | // Check if the buffers aren't large enough to fit the new object and, if so, call
|
---|
| 1352 | // populateBuffers() to resize and repopupulate them
|
---|
[7e10667] | 1353 | if ((modelGroups[obj->type].vboCapacity < (modelGroups[obj->type].numPoints + obj->num_points) ||
|
---|
| 1354 | bufferInfo->ubo_capacity <= bufferInfo->ubo_offset)) {
|
---|
[1f3d32b] | 1355 |
|
---|
| 1356 | if (leftLaser != NULL && leftLaser->deleted) {
|
---|
| 1357 | leftLaser = NULL;
|
---|
| 1358 | }
|
---|
| 1359 | if (rightLaser != NULL && rightLaser->deleted) {
|
---|
| 1360 | rightLaser = NULL;
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
[f97e638] | 1363 | populateBuffers(objects, shaderBufferInfo, modelGroups, ubo);
|
---|
[1f3d32b] | 1364 | } else {
|
---|
[49db5fc] | 1365 | copyObjectDataToBuffers(*objects.back(), shaderBufferInfo, modelGroups, ubo);
|
---|
[1f3d32b] | 1366 | }
|
---|
| 1367 | }
|
---|
| 1368 |
|
---|
| 1369 | void removeObjectFromScene(SceneObject& obj, GLuint ubo) {
|
---|
| 1370 | if (!obj.deleted) {
|
---|
| 1371 | // Move the object outside the render bounds of the scene so it doesn't get rendered
|
---|
| 1372 | // TODO: Find a better way of hiding the object until the next time buffers are repopulated
|
---|
| 1373 | transformObject(obj, translate(mat4(1.0f), vec3(0.0f, 0.0f, FAR_CLIP * 1000.0f)), ubo);
|
---|
| 1374 | obj.deleted = true;
|
---|
| 1375 | }
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
[7e10667] | 1378 | // TODO: Pass a reference, not a pointer
|
---|
[1f3d32b] | 1379 | void calculateObjectBoundingBox(SceneObject* obj) {
|
---|
| 1380 | GLfloat min_x = obj->points[0];
|
---|
| 1381 | GLfloat max_x = obj->points[0];
|
---|
| 1382 | GLfloat min_y = obj->points[1];
|
---|
| 1383 | GLfloat max_y = obj->points[1];
|
---|
| 1384 | GLfloat min_z = obj->points[2];
|
---|
| 1385 | GLfloat max_z = obj->points[2];
|
---|
| 1386 |
|
---|
| 1387 | // start from the second point
|
---|
[8e8aed6] | 1388 | for (unsigned int i = 3; i < obj->points.size(); i += 3) {
|
---|
[1f3d32b] | 1389 | if (min_x > obj->points[i]) {
|
---|
| 1390 | min_x = obj->points[i];
|
---|
| 1391 | }
|
---|
| 1392 | else if (max_x < obj->points[i]) {
|
---|
| 1393 | max_x = obj->points[i];
|
---|
| 1394 | }
|
---|
| 1395 |
|
---|
| 1396 | if (min_y > obj->points[i + 1]) {
|
---|
| 1397 | min_y = obj->points[i + 1];
|
---|
| 1398 | }
|
---|
| 1399 | else if (max_y < obj->points[i + 1]) {
|
---|
| 1400 | max_y = obj->points[i + 1];
|
---|
| 1401 | }
|
---|
| 1402 |
|
---|
| 1403 | if (min_z > obj->points[i + 2]) {
|
---|
| 1404 | min_z = obj->points[i + 2];
|
---|
| 1405 | }
|
---|
| 1406 | else if (max_z < obj->points[i + 2]) {
|
---|
| 1407 | max_z = obj->points[i + 2];
|
---|
| 1408 | }
|
---|
| 1409 | }
|
---|
| 1410 |
|
---|
| 1411 | obj->bounding_center = vec3((min_x + max_x) / 2.0f, (min_y + max_y) / 2.0f, (min_z + max_z) / 2.0f);
|
---|
| 1412 |
|
---|
| 1413 | GLfloat radius_x = max_x - obj->bounding_center.x;
|
---|
| 1414 | GLfloat radius_y = max_y - obj->bounding_center.y;
|
---|
| 1415 | GLfloat radius_z = max_z - obj->bounding_center.z;
|
---|
| 1416 |
|
---|
| 1417 | // TODO: This actually underestimates the radius. Might need to be fixed at some point.
|
---|
| 1418 | // TODO: Does not take into account any scaling in the model matrix
|
---|
| 1419 | obj->bounding_radius = radius_x;
|
---|
| 1420 | if (obj->bounding_radius < radius_y)
|
---|
| 1421 | obj->bounding_radius = radius_y;
|
---|
| 1422 | if (obj->bounding_radius < radius_z)
|
---|
| 1423 | obj->bounding_radius = radius_z;
|
---|
| 1424 |
|
---|
[8e8aed6] | 1425 | for (unsigned int i = 0; i < obj->points.size(); i += 3) {
|
---|
[1f3d32b] | 1426 | obj->points[i] -= obj->bounding_center.x;
|
---|
| 1427 | obj->points[i + 1] -= obj->bounding_center.y;
|
---|
| 1428 | obj->points[i + 2] -= obj->bounding_center.z;
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
| 1431 | obj->bounding_center = vec3(0.0f, 0.0f, 0.0f);
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
[dd9771c] | 1434 | SceneObject* createShip() {
|
---|
[1f3d32b] | 1435 | SceneObject* ship = new SceneObject();
|
---|
| 1436 |
|
---|
| 1437 | ship->type = TYPE_SHIP;
|
---|
| 1438 |
|
---|
| 1439 | ship->points = {
|
---|
| 1440 | //back
|
---|
| 1441 | -0.5f, 0.3f, 0.0f,
|
---|
| 1442 | -0.5f, 0.0f, 0.0f,
|
---|
| 1443 | 0.5f, 0.0f, 0.0f,
|
---|
| 1444 | -0.5f, 0.3f, 0.0f,
|
---|
| 1445 | 0.5f, 0.0f, 0.0f,
|
---|
| 1446 | 0.5f, 0.3f, 0.0f,
|
---|
| 1447 |
|
---|
| 1448 | // left back
|
---|
| 1449 | -0.5f, 0.3f, -2.0f,
|
---|
| 1450 | -0.5f, 0.0f, -2.0f,
|
---|
| 1451 | -0.5f, 0.0f, 0.0f,
|
---|
| 1452 | -0.5f, 0.3f, -2.0f,
|
---|
| 1453 | -0.5f, 0.0f, 0.0f,
|
---|
| 1454 | -0.5f, 0.3f, 0.0f,
|
---|
| 1455 |
|
---|
| 1456 | // right back
|
---|
| 1457 | 0.5f, 0.3f, 0.0f,
|
---|
| 1458 | 0.5f, 0.0f, 0.0f,
|
---|
| 1459 | 0.5f, 0.0f, -2.0f,
|
---|
| 1460 | 0.5f, 0.3f, 0.0f,
|
---|
| 1461 | 0.5f, 0.0f, -2.0f,
|
---|
| 1462 | 0.5f, 0.3f, -2.0f,
|
---|
| 1463 |
|
---|
| 1464 | // left mid
|
---|
| 1465 | -0.25f, 0.3f, -3.0f,
|
---|
| 1466 | -0.25f, 0.0f, -3.0f,
|
---|
| 1467 | -0.5f, 0.0f, -2.0f,
|
---|
| 1468 | -0.25f, 0.3f, -3.0f,
|
---|
| 1469 | -0.5f, 0.0f, -2.0f,
|
---|
| 1470 | -0.5f, 0.3f, -2.0f,
|
---|
| 1471 |
|
---|
| 1472 | // right mid
|
---|
| 1473 | 0.5f, 0.3f, -2.0f,
|
---|
| 1474 | 0.5f, 0.0f, -2.0f,
|
---|
| 1475 | 0.25f, 0.0f, -3.0f,
|
---|
| 1476 | 0.5f, 0.3f, -2.0f,
|
---|
| 1477 | 0.25f, 0.0f, -3.0f,
|
---|
| 1478 | 0.25f, 0.3f, -3.0f,
|
---|
| 1479 |
|
---|
| 1480 | // left front
|
---|
| 1481 | 0.0f, 0.0f, -3.5f,
|
---|
| 1482 | -0.25f, 0.0f, -3.0f,
|
---|
| 1483 | -0.25f, 0.3f, -3.0f,
|
---|
| 1484 |
|
---|
| 1485 | // right front
|
---|
| 1486 | 0.25f, 0.3f, -3.0f,
|
---|
| 1487 | 0.25f, 0.0f, -3.0f,
|
---|
| 1488 | 0.0f, 0.0f, -3.5f,
|
---|
| 1489 |
|
---|
| 1490 | // top back
|
---|
| 1491 | -0.5f, 0.3f, -2.0f,
|
---|
| 1492 | -0.5f, 0.3f, 0.0f,
|
---|
| 1493 | 0.5f, 0.3f, 0.0f,
|
---|
| 1494 | -0.5f, 0.3f, -2.0f,
|
---|
| 1495 | 0.5f, 0.3f, 0.0f,
|
---|
| 1496 | 0.5f, 0.3f, -2.0f,
|
---|
| 1497 |
|
---|
| 1498 | // bottom back
|
---|
| 1499 | -0.5f, 0.0f, 0.0f,
|
---|
| 1500 | -0.5f, 0.0f, -2.0f,
|
---|
| 1501 | 0.5f, 0.0f, 0.0f,
|
---|
| 1502 | 0.5f, 0.0f, 0.0f,
|
---|
| 1503 | -0.5f, 0.0f, -2.0f,
|
---|
| 1504 | 0.5f, 0.0f, -2.0f,
|
---|
| 1505 |
|
---|
| 1506 | // top mid
|
---|
| 1507 | -0.25f, 0.3f, -3.0f,
|
---|
| 1508 | -0.5f, 0.3f, -2.0f,
|
---|
| 1509 | 0.5f, 0.3f, -2.0f,
|
---|
| 1510 | -0.25f, 0.3f, -3.0f,
|
---|
| 1511 | 0.5f, 0.3f, -2.0f,
|
---|
| 1512 | 0.25f, 0.3f, -3.0f,
|
---|
| 1513 |
|
---|
| 1514 | // bottom mid
|
---|
| 1515 | -0.5f, 0.0f, -2.0f,
|
---|
| 1516 | -0.25f, 0.0f, -3.0f,
|
---|
| 1517 | 0.5f, 0.0f, -2.0f,
|
---|
| 1518 | 0.5f, 0.0f, -2.0f,
|
---|
| 1519 | -0.25f, 0.0f, -3.0f,
|
---|
| 1520 | 0.25f, 0.0f, -3.0f,
|
---|
| 1521 |
|
---|
| 1522 | // top front
|
---|
| 1523 | -0.25f, 0.3f, -3.0f,
|
---|
| 1524 | 0.25f, 0.3f, -3.0f,
|
---|
| 1525 | 0.0f, 0.0f, -3.5f,
|
---|
| 1526 |
|
---|
| 1527 | // bottom front
|
---|
| 1528 | 0.25f, 0.0f, -3.0f,
|
---|
| 1529 | -0.25f, 0.0f, -3.0f,
|
---|
| 1530 | 0.0f, 0.0f, -3.5f,
|
---|
| 1531 |
|
---|
| 1532 | // left wing start back
|
---|
| 1533 | -1.5f, 0.3f, 0.0f,
|
---|
| 1534 | -1.5f, 0.0f, 0.0f,
|
---|
| 1535 | -0.5f, 0.0f, 0.0f,
|
---|
| 1536 | -1.5f, 0.3f, 0.0f,
|
---|
| 1537 | -0.5f, 0.0f, 0.0f,
|
---|
| 1538 | -0.5f, 0.3f, 0.0f,
|
---|
| 1539 |
|
---|
| 1540 | // left wing start top
|
---|
| 1541 | -0.5f, 0.3f, -0.3f,
|
---|
| 1542 | -1.3f, 0.3f, -0.3f,
|
---|
| 1543 | -1.5f, 0.3f, 0.0f,
|
---|
| 1544 | -0.5f, 0.3f, -0.3f,
|
---|
| 1545 | -1.5f, 0.3f, 0.0f,
|
---|
| 1546 | -0.5f, 0.3f, 0.0f,
|
---|
| 1547 |
|
---|
| 1548 | // left wing start front
|
---|
| 1549 | -0.5f, 0.3f, -0.3f,
|
---|
| 1550 | -0.5f, 0.0f, -0.3f,
|
---|
| 1551 | -1.3f, 0.0f, -0.3f,
|
---|
| 1552 | -0.5f, 0.3f, -0.3f,
|
---|
| 1553 | -1.3f, 0.0f, -0.3f,
|
---|
| 1554 | -1.3f, 0.3f, -0.3f,
|
---|
| 1555 |
|
---|
| 1556 | // left wing start bottom
|
---|
| 1557 | -0.5f, 0.0f, 0.0f,
|
---|
| 1558 | -1.5f, 0.0f, 0.0f,
|
---|
| 1559 | -1.3f, 0.0f, -0.3f,
|
---|
| 1560 | -0.5f, 0.0f, 0.0f,
|
---|
| 1561 | -1.3f, 0.0f, -0.3f,
|
---|
| 1562 | -0.5f, 0.0f, -0.3f,
|
---|
| 1563 |
|
---|
| 1564 | // left wing end outside
|
---|
| 1565 | -1.5f, 0.3f, 0.0f,
|
---|
| 1566 | -2.2f, 0.15f, -0.8f,
|
---|
| 1567 | -1.5f, 0.0f, 0.0f,
|
---|
| 1568 |
|
---|
| 1569 | // left wing end top
|
---|
| 1570 | -1.3f, 0.3f, -0.3f,
|
---|
| 1571 | -2.2f, 0.15f, -0.8f,
|
---|
| 1572 | -1.5f, 0.3f, 0.0f,
|
---|
| 1573 |
|
---|
| 1574 | // left wing end front
|
---|
| 1575 | -1.3f, 0.0f, -0.3f,
|
---|
| 1576 | -2.2f, 0.15f, -0.8f,
|
---|
| 1577 | -1.3f, 0.3f, -0.3f,
|
---|
| 1578 |
|
---|
| 1579 | // left wing end bottom
|
---|
| 1580 | -1.5f, 0.0f, 0.0f,
|
---|
| 1581 | -2.2f, 0.15f, -0.8f,
|
---|
| 1582 | -1.3f, 0.0f, -0.3f,
|
---|
| 1583 |
|
---|
| 1584 | // right wing start back
|
---|
| 1585 | 1.5f, 0.0f, 0.0f,
|
---|
| 1586 | 1.5f, 0.3f, 0.0f,
|
---|
| 1587 | 0.5f, 0.0f, 0.0f,
|
---|
| 1588 | 0.5f, 0.0f, 0.0f,
|
---|
| 1589 | 1.5f, 0.3f, 0.0f,
|
---|
| 1590 | 0.5f, 0.3f, 0.0f,
|
---|
| 1591 |
|
---|
| 1592 | // right wing start top
|
---|
| 1593 | 1.3f, 0.3f, -0.3f,
|
---|
| 1594 | 0.5f, 0.3f, -0.3f,
|
---|
| 1595 | 1.5f, 0.3f, 0.0f,
|
---|
| 1596 | 1.5f, 0.3f, 0.0f,
|
---|
| 1597 | 0.5f, 0.3f, -0.3f,
|
---|
| 1598 | 0.5f, 0.3f, 0.0f,
|
---|
| 1599 |
|
---|
| 1600 | // right wing start front
|
---|
| 1601 | 0.5f, 0.0f, -0.3f,
|
---|
| 1602 | 0.5f, 0.3f, -0.3f,
|
---|
| 1603 | 1.3f, 0.0f, -0.3f,
|
---|
| 1604 | 1.3f, 0.0f, -0.3f,
|
---|
| 1605 | 0.5f, 0.3f, -0.3f,
|
---|
| 1606 | 1.3f, 0.3f, -0.3f,
|
---|
| 1607 |
|
---|
| 1608 | // right wing start bottom
|
---|
| 1609 | 1.5f, 0.0f, 0.0f,
|
---|
| 1610 | 0.5f, 0.0f, 0.0f,
|
---|
| 1611 | 1.3f, 0.0f, -0.3f,
|
---|
| 1612 | 1.3f, 0.0f, -0.3f,
|
---|
| 1613 | 0.5f, 0.0f, 0.0f,
|
---|
| 1614 | 0.5f, 0.0f, -0.3f,
|
---|
| 1615 |
|
---|
| 1616 | // right wing end outside
|
---|
| 1617 | 2.2f, 0.15f, -0.8f,
|
---|
| 1618 | 1.5f, 0.3f, 0.0f,
|
---|
| 1619 | 1.5f, 0.0f, 0.0f,
|
---|
| 1620 |
|
---|
| 1621 | // right wing end top
|
---|
| 1622 | 2.2f, 0.15f, -0.8f,
|
---|
| 1623 | 1.3f, 0.3f, -0.3f,
|
---|
| 1624 | 1.5f, 0.3f, 0.0f,
|
---|
| 1625 |
|
---|
| 1626 | // right wing end front
|
---|
| 1627 | 2.2f, 0.15f, -0.8f,
|
---|
| 1628 | 1.3f, 0.0f, -0.3f,
|
---|
| 1629 | 1.3f, 0.3f, -0.3f,
|
---|
| 1630 |
|
---|
| 1631 | // right wing end bottom
|
---|
| 1632 | 2.2f, 0.15f, -0.8f,
|
---|
| 1633 | 1.5f, 0.0f, 0.0f,
|
---|
| 1634 | 1.3f, 0.0f, -0.3f,
|
---|
| 1635 | };
|
---|
| 1636 | ship->colors = {
|
---|
| 1637 | 0.0f, 0.0f, 0.3f,
|
---|
| 1638 | 0.0f, 0.0f, 0.3f,
|
---|
| 1639 | 0.0f, 0.0f, 0.3f,
|
---|
| 1640 | 0.0f, 0.0f, 0.3f,
|
---|
| 1641 | 0.0f, 0.0f, 0.3f,
|
---|
| 1642 | 0.0f, 0.0f, 0.3f,
|
---|
| 1643 |
|
---|
| 1644 | 0.0f, 0.0f, 0.3f,
|
---|
| 1645 | 0.0f, 0.0f, 0.3f,
|
---|
| 1646 | 0.0f, 0.0f, 0.3f,
|
---|
| 1647 | 0.0f, 0.0f, 0.3f,
|
---|
| 1648 | 0.0f, 0.0f, 0.3f,
|
---|
| 1649 | 0.0f, 0.0f, 0.3f,
|
---|
[b73cb3b] | 1650 |
|
---|
[1f3d32b] | 1651 | 0.0f, 0.0f, 0.3f,
|
---|
| 1652 | 0.0f, 0.0f, 0.3f,
|
---|
| 1653 | 0.0f, 0.0f, 0.3f,
|
---|
| 1654 | 0.0f, 0.0f, 0.3f,
|
---|
| 1655 | 0.0f, 0.0f, 0.3f,
|
---|
| 1656 | 0.0f, 0.0f, 0.3f,
|
---|
[5c9d193] | 1657 |
|
---|
[1f3d32b] | 1658 | 0.0f, 0.0f, 0.3f,
|
---|
| 1659 | 0.0f, 0.0f, 0.3f,
|
---|
| 1660 | 0.0f, 0.0f, 0.3f,
|
---|
| 1661 | 0.0f, 0.0f, 0.3f,
|
---|
| 1662 | 0.0f, 0.0f, 0.3f,
|
---|
| 1663 | 0.0f, 0.0f, 0.3f,
|
---|
[5c9d193] | 1664 |
|
---|
[1f3d32b] | 1665 | 0.0f, 0.0f, 0.3f,
|
---|
| 1666 | 0.0f, 0.0f, 0.3f,
|
---|
| 1667 | 0.0f, 0.0f, 0.3f,
|
---|
| 1668 | 0.0f, 0.0f, 0.3f,
|
---|
| 1669 | 0.0f, 0.0f, 0.3f,
|
---|
| 1670 | 0.0f, 0.0f, 0.3f,
|
---|
[5c9d193] | 1671 |
|
---|
[1f3d32b] | 1672 | 0.0f, 0.0f, 1.0f,
|
---|
| 1673 | 0.0f, 0.0f, 1.0f,
|
---|
| 1674 | 0.0f, 0.0f, 1.0f,
|
---|
[5c9d193] | 1675 |
|
---|
[1f3d32b] | 1676 | 0.0f, 0.0f, 1.0f,
|
---|
| 1677 | 0.0f, 0.0f, 1.0f,
|
---|
| 1678 | 0.0f, 0.0f, 1.0f,
|
---|
[f7d35da] | 1679 |
|
---|
[1f3d32b] | 1680 | 0.0f, 0.0f, 1.0f,
|
---|
| 1681 | 0.0f, 0.0f, 1.0f,
|
---|
| 1682 | 0.0f, 0.0f, 1.0f,
|
---|
| 1683 | 0.0f, 0.0f, 1.0f,
|
---|
| 1684 | 0.0f, 0.0f, 1.0f,
|
---|
| 1685 | 0.0f, 0.0f, 1.0f,
|
---|
[33a9664] | 1686 |
|
---|
[1f3d32b] | 1687 | 0.0f, 0.0f, 1.0f,
|
---|
| 1688 | 0.0f, 0.0f, 1.0f,
|
---|
| 1689 | 0.0f, 0.0f, 1.0f,
|
---|
| 1690 | 0.0f, 0.0f, 1.0f,
|
---|
| 1691 | 0.0f, 0.0f, 1.0f,
|
---|
| 1692 | 0.0f, 0.0f, 1.0f,
|
---|
[33a9664] | 1693 |
|
---|
[1f3d32b] | 1694 | 0.0f, 0.0f, 1.0f,
|
---|
| 1695 | 0.0f, 0.0f, 1.0f,
|
---|
| 1696 | 0.0f, 0.0f, 1.0f,
|
---|
| 1697 | 0.0f, 0.0f, 1.0f,
|
---|
| 1698 | 0.0f, 0.0f, 1.0f,
|
---|
| 1699 | 0.0f, 0.0f, 1.0f,
|
---|
[d12d003] | 1700 |
|
---|
[1f3d32b] | 1701 | 0.0f, 0.0f, 1.0f,
|
---|
| 1702 | 0.0f, 0.0f, 1.0f,
|
---|
| 1703 | 0.0f, 0.0f, 1.0f,
|
---|
| 1704 | 0.0f, 0.0f, 1.0f,
|
---|
| 1705 | 0.0f, 0.0f, 1.0f,
|
---|
| 1706 | 0.0f, 0.0f, 1.0f,
|
---|
[b73cb3b] | 1707 |
|
---|
[1f3d32b] | 1708 | 0.0f, 0.0f, 0.3f,
|
---|
| 1709 | 0.0f, 0.0f, 0.3f,
|
---|
| 1710 | 0.0f, 0.0f, 0.3f,
|
---|
[c1ca5b5] | 1711 |
|
---|
[1f3d32b] | 1712 | 0.0f, 0.0f, 0.3f,
|
---|
| 1713 | 0.0f, 0.0f, 0.3f,
|
---|
| 1714 | 0.0f, 0.0f, 0.3f,
|
---|
[3d06b4e] | 1715 |
|
---|
[1f3d32b] | 1716 | 0.0f, 0.0f, 0.3f,
|
---|
| 1717 | 0.0f, 0.0f, 0.3f,
|
---|
| 1718 | 0.0f, 0.0f, 0.3f,
|
---|
| 1719 | 0.0f, 0.0f, 0.3f,
|
---|
| 1720 | 0.0f, 0.0f, 0.3f,
|
---|
| 1721 | 0.0f, 0.0f, 0.3f,
|
---|
[0d5c100] | 1722 |
|
---|
[1f3d32b] | 1723 | 0.0f, 0.0f, 0.3f,
|
---|
| 1724 | 0.0f, 0.0f, 0.3f,
|
---|
| 1725 | 0.0f, 0.0f, 0.3f,
|
---|
| 1726 | 0.0f, 0.0f, 0.3f,
|
---|
| 1727 | 0.0f, 0.0f, 0.3f,
|
---|
| 1728 | 0.0f, 0.0f, 0.3f,
|
---|
[cffca4d] | 1729 |
|
---|
[1f3d32b] | 1730 | 0.0f, 0.0f, 0.3f,
|
---|
| 1731 | 0.0f, 0.0f, 0.3f,
|
---|
| 1732 | 0.0f, 0.0f, 0.3f,
|
---|
| 1733 | 0.0f, 0.0f, 0.3f,
|
---|
| 1734 | 0.0f, 0.0f, 0.3f,
|
---|
| 1735 | 0.0f, 0.0f, 0.3f,
|
---|
[cffca4d] | 1736 |
|
---|
[1f3d32b] | 1737 | 0.0f, 0.0f, 0.3f,
|
---|
| 1738 | 0.0f, 0.0f, 0.3f,
|
---|
| 1739 | 0.0f, 0.0f, 0.3f,
|
---|
| 1740 | 0.0f, 0.0f, 0.3f,
|
---|
| 1741 | 0.0f, 0.0f, 0.3f,
|
---|
| 1742 | 0.0f, 0.0f, 0.3f,
|
---|
[cffca4d] | 1743 |
|
---|
[1f3d32b] | 1744 | 0.0f, 0.0f, 0.3f,
|
---|
| 1745 | 0.0f, 0.0f, 0.3f,
|
---|
| 1746 | 0.0f, 0.0f, 0.3f,
|
---|
[95595de] | 1747 |
|
---|
[1f3d32b] | 1748 | 0.0f, 0.0f, 0.3f,
|
---|
| 1749 | 0.0f, 0.0f, 0.3f,
|
---|
| 1750 | 0.0f, 0.0f, 0.3f,
|
---|
[cffca4d] | 1751 |
|
---|
[1f3d32b] | 1752 | 0.0f, 0.0f, 0.3f,
|
---|
| 1753 | 0.0f, 0.0f, 0.3f,
|
---|
| 1754 | 0.0f, 0.0f, 0.3f,
|
---|
[c3c3158] | 1755 |
|
---|
[1f3d32b] | 1756 | 0.0f, 0.0f, 0.3f,
|
---|
| 1757 | 0.0f, 0.0f, 0.3f,
|
---|
| 1758 | 0.0f, 0.0f, 0.3f,
|
---|
[c3c3158] | 1759 |
|
---|
[1f3d32b] | 1760 | 0.0f, 0.0f, 0.3f,
|
---|
| 1761 | 0.0f, 0.0f, 0.3f,
|
---|
| 1762 | 0.0f, 0.0f, 0.3f,
|
---|
| 1763 | 0.0f, 0.0f, 0.3f,
|
---|
| 1764 | 0.0f, 0.0f, 0.3f,
|
---|
| 1765 | 0.0f, 0.0f, 0.3f,
|
---|
[fabed35] | 1766 |
|
---|
[1f3d32b] | 1767 | 0.0f, 0.0f, 0.3f,
|
---|
| 1768 | 0.0f, 0.0f, 0.3f,
|
---|
| 1769 | 0.0f, 0.0f, 0.3f,
|
---|
| 1770 | 0.0f, 0.0f, 0.3f,
|
---|
| 1771 | 0.0f, 0.0f, 0.3f,
|
---|
| 1772 | 0.0f, 0.0f, 0.3f,
|
---|
[fabed35] | 1773 |
|
---|
[1f3d32b] | 1774 | 0.0f, 0.0f, 0.3f,
|
---|
| 1775 | 0.0f, 0.0f, 0.3f,
|
---|
| 1776 | 0.0f, 0.0f, 0.3f,
|
---|
| 1777 | 0.0f, 0.0f, 0.3f,
|
---|
| 1778 | 0.0f, 0.0f, 0.3f,
|
---|
| 1779 | 0.0f, 0.0f, 0.3f,
|
---|
[c3c3158] | 1780 |
|
---|
[1f3d32b] | 1781 | 0.0f, 0.0f, 0.3f,
|
---|
| 1782 | 0.0f, 0.0f, 0.3f,
|
---|
| 1783 | 0.0f, 0.0f, 0.3f,
|
---|
| 1784 | 0.0f, 0.0f, 0.3f,
|
---|
| 1785 | 0.0f, 0.0f, 0.3f,
|
---|
| 1786 | 0.0f, 0.0f, 0.3f,
|
---|
[c3c3158] | 1787 |
|
---|
[1f3d32b] | 1788 | 0.0f, 0.0f, 0.3f,
|
---|
| 1789 | 0.0f, 0.0f, 0.3f,
|
---|
| 1790 | 0.0f, 0.0f, 0.3f,
|
---|
[3d06b4e] | 1791 |
|
---|
[1f3d32b] | 1792 | 0.0f, 0.0f, 0.3f,
|
---|
| 1793 | 0.0f, 0.0f, 0.3f,
|
---|
| 1794 | 0.0f, 0.0f, 0.3f,
|
---|
[3d06b4e] | 1795 |
|
---|
[1f3d32b] | 1796 | 0.0f, 0.0f, 0.3f,
|
---|
| 1797 | 0.0f, 0.0f, 0.3f,
|
---|
| 1798 | 0.0f, 0.0f, 0.3f,
|
---|
[3d06b4e] | 1799 |
|
---|
[1f3d32b] | 1800 | 0.0f, 0.0f, 0.3f,
|
---|
| 1801 | 0.0f, 0.0f, 0.3f,
|
---|
| 1802 | 0.0f, 0.0f, 0.3f,
|
---|
| 1803 | };
|
---|
| 1804 | ship->texcoords = { 0.0f };
|
---|
[3d06b4e] | 1805 |
|
---|
[1f3d32b] | 1806 | mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f));
|
---|
| 1807 | mat4 R_model(1.0f);
|
---|
| 1808 | ship->model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
|
---|
[3d06b4e] | 1809 |
|
---|
[1f3d32b] | 1810 | ship->translate_mat = T_model;
|
---|
[3d06b4e] | 1811 |
|
---|
[1f3d32b] | 1812 | initObject(ship);
|
---|
[3d06b4e] | 1813 |
|
---|
[1f3d32b] | 1814 | return ship;
|
---|
[3d06b4e] | 1815 | }
|
---|
| 1816 |
|
---|
[3effd81] | 1817 | /* LASER RENDERING/POSITIONING ALGORITHM
|
---|
| 1818 | * -Draw a thin rectangle for the laser beam, using the specified width and endpoints
|
---|
| 1819 | * -Texture the beam with a grayscale partially transparent image
|
---|
| 1820 | * -In the shader, blend the image with a color to support lasers of different colors
|
---|
| 1821 | *
|
---|
| 1822 | * The flat part of the textured rectangle needs to always face the camera, so the laser's width is constant
|
---|
| 1823 | * This is done as follows:
|
---|
| 1824 | * -Determine the length of the laser based on the start and end points
|
---|
| 1825 | * -Draw a rectangle along the z-axis and rotated upwards along the y-axis, with the correct final length and width
|
---|
| 1826 | * -Rotate the beam around the z-axis by the correct angle, sot that in its final position, the flat part faces the camera
|
---|
| 1827 | * -Rotate the beam along the x-axis and then along the y-axis and then translate it to put it into its final position
|
---|
| 1828 | */
|
---|
[8316333] | 1829 | // TODO: Make the color parameter have an effect
|
---|
[dd9771c] | 1830 | Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width) {
|
---|
[1f3d32b] | 1831 | Laser* obj = new Laser();
|
---|
| 1832 | obj->type = TYPE_LASER;
|
---|
| 1833 | obj->targetAsteroid = NULL;
|
---|
[b155f13] | 1834 |
|
---|
[3effd81] | 1835 | vec3 ray = end - start;
|
---|
| 1836 | float length = glm::length(ray);
|
---|
[b155f13] | 1837 |
|
---|
[1f3d32b] | 1838 | obj->points = {
|
---|
[fd6f465] | 1839 | width / 2, 0.0f, -width / 2,
|
---|
| 1840 | -width / 2, 0.0f, -width / 2,
|
---|
| 1841 | -width / 2, 0.0f, 0.0f,
|
---|
| 1842 | width / 2, 0.0f, -width / 2,
|
---|
| 1843 | -width / 2, 0.0f, 0.0f,
|
---|
| 1844 | width / 2, 0.0f, 0.0f,
|
---|
| 1845 | width / 2, 0.0f, -length + width / 2,
|
---|
| 1846 | -width / 2, 0.0f, -length + width / 2,
|
---|
| 1847 | -width / 2, 0.0f, -width / 2,
|
---|
| 1848 | width / 2, 0.0f, -length + width / 2,
|
---|
| 1849 | -width / 2, 0.0f, -width / 2,
|
---|
| 1850 | width / 2, 0.0f, -width / 2,
|
---|
| 1851 | width / 2, 0.0f, -length,
|
---|
| 1852 | -width / 2, 0.0f, -length,
|
---|
| 1853 | -width / 2, 0.0f, -length + width / 2,
|
---|
| 1854 | width / 2, 0.0f, -length,
|
---|
| 1855 | -width / 2, 0.0f, -length + width / 2,
|
---|
| 1856 | width / 2, 0.0f, -length + width / 2,
|
---|
[b155f13] | 1857 | };
|
---|
| 1858 |
|
---|
[1f3d32b] | 1859 | obj->texcoords = {
|
---|
[9f9f9a7] | 1860 | 1.0f, 0.5f,
|
---|
| 1861 | 0.0f, 0.5f,
|
---|
| 1862 | 0.0f, 0.0f,
|
---|
| 1863 | 1.0f, 0.5f,
|
---|
| 1864 | 0.0f, 0.0f,
|
---|
| 1865 | 1.0f, 0.0f,
|
---|
| 1866 | 1.0f, 0.51f,
|
---|
| 1867 | 0.0f, 0.51f,
|
---|
| 1868 | 0.0f, 0.49f,
|
---|
| 1869 | 1.0f, 0.51f,
|
---|
| 1870 | 0.0f, 0.49f,
|
---|
| 1871 | 1.0f, 0.49f,
|
---|
| 1872 | 1.0f, 1.0f,
|
---|
| 1873 | 0.0f, 1.0f,
|
---|
| 1874 | 0.0f, 0.5f,
|
---|
| 1875 | 1.0f, 1.0f,
|
---|
| 1876 | 0.0f, 0.5f,
|
---|
| 1877 | 1.0f, 0.5f,
|
---|
| 1878 | };
|
---|
| 1879 |
|
---|
[3effd81] | 1880 | float xAxisRotation = asin(ray.y / length);
|
---|
| 1881 | float yAxisRotation = atan2(-ray.x, -ray.z);
|
---|
| 1882 |
|
---|
| 1883 | vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) *
|
---|
| 1884 | rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) *
|
---|
| 1885 | vec4(0.0f, 1.0f, 0.0f, 1.0f));
|
---|
| 1886 |
|
---|
| 1887 | // To project point P onto line AB:
|
---|
| 1888 | // projection = A + dot(AP,AB) / dot(AB,AB) * AB
|
---|
| 1889 | vec3 projOnLaser = start + glm::dot(cam_pos-start, ray) / (length*length) * ray;
|
---|
| 1890 | vec3 laserToCam = cam_pos - projOnLaser;
|
---|
| 1891 |
|
---|
| 1892 | float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam));
|
---|
| 1893 |
|
---|
[1f3d32b] | 1894 | obj->model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f));
|
---|
[b155f13] | 1895 |
|
---|
[8316333] | 1896 | initObject(obj);
|
---|
| 1897 |
|
---|
[1f3d32b] | 1898 | obj->model_transform = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj->model_transform;
|
---|
| 1899 | obj->model_transform = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj->model_transform;
|
---|
| 1900 | obj->model_transform = translate(mat4(1.0f), start) * obj->model_transform;
|
---|
[612d1f6] | 1901 |
|
---|
[8316333] | 1902 | return obj;
|
---|
[b155f13] | 1903 | }
|
---|
| 1904 |
|
---|
[7a55b49] | 1905 | ShaderModelGroup createModelGroup(GLuint shaderProgram) {
|
---|
| 1906 | ShaderModelGroup smg;
|
---|
| 1907 |
|
---|
| 1908 | smg.shaderProgram = shaderProgram;
|
---|
[0414306] | 1909 | glGenVertexArrays(1, &smg.vao);
|
---|
| 1910 | smg.numPoints = 0;
|
---|
[7a55b49] | 1911 |
|
---|
| 1912 | return smg;
|
---|
| 1913 | }
|
---|
| 1914 |
|
---|
[a926b79] | 1915 | // TODO: Add the code to resize the buffers here
|
---|
| 1916 | // addObjectToScene and removeObjectFromScene pretty much already do this.
|
---|
| 1917 | // However, when addObjectToScene resizes the buffers, it resizes them for all object types
|
---|
| 1918 | // It would be more efficient to only resize them for the object type in question
|
---|
| 1919 |
|
---|
[7a55b49] | 1920 | void removeModelFromGroup(ShaderModelGroup& modelGroup, SceneObject& model) {
|
---|
| 1921 | // TODO: Implement
|
---|
| 1922 | }
|
---|
| 1923 |
|
---|
| 1924 | void addModelToGroup(ShaderModelGroup& modelGroup, SceneObject& model) {
|
---|
| 1925 | // TODO: Implement
|
---|
| 1926 | }
|
---|
| 1927 |
|
---|
[a0eb547] | 1928 | void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType,
|
---|
| 1929 | GLint size, GLenum type, size_t fieldOffset) {
|
---|
| 1930 | if (type != GL_FLOAT && type != GL_UNSIGNED_INT) {
|
---|
| 1931 | cout << "Unknown shader program attribute type: " << type << endl;
|
---|
| 1932 | return;
|
---|
| 1933 | }
|
---|
| 1934 |
|
---|
| 1935 | AttribInfo attribInfo;
|
---|
| 1936 |
|
---|
| 1937 | attribInfo.attribType = attribType;
|
---|
| 1938 | attribInfo.index = modelGroup.attribs.size();
|
---|
| 1939 | attribInfo.size = size;
|
---|
| 1940 | attribInfo.type = type;
|
---|
| 1941 | attribInfo.fieldOffset = fieldOffset;
|
---|
| 1942 |
|
---|
| 1943 | modelGroup.attribs[name] = attribInfo;
|
---|
| 1944 | }
|
---|
| 1945 |
|
---|
[49db5fc] | 1946 | void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType,
|
---|
| 1947 | GLint size, UniformType type, GLfloat* data) {
|
---|
| 1948 | AttribInfo attribInfo;
|
---|
| 1949 |
|
---|
| 1950 | attribInfo.attribType = attribType;
|
---|
| 1951 | attribInfo.size = size;
|
---|
| 1952 | attribInfo.uniType = type;
|
---|
| 1953 | attribInfo.data = data;
|
---|
| 1954 |
|
---|
| 1955 | modelGroup.attribs[name] = attribInfo;
|
---|
| 1956 | }
|
---|
| 1957 |
|
---|
[a0eb547] | 1958 | void initModelGroupAttribs(ShaderModelGroup& modelGroup) {
|
---|
[5b02676] | 1959 | /*** START OF REFACTORED CODE ***/
|
---|
[a0eb547] | 1960 | glBindVertexArray(modelGroup.vao);
|
---|
| 1961 |
|
---|
| 1962 | map<string, AttribInfo>::iterator it;
|
---|
| 1963 | for (it = modelGroup.attribs.begin(); it != modelGroup.attribs.end(); it++) {
|
---|
[5b02676] | 1964 | /*** END OF REFACTORED CODE ***/
|
---|
[49db5fc] | 1965 | if (it->second.attribType == ATTRIB_UNIFORM) {
|
---|
| 1966 | it->second.buffer = glGetUniformLocation(modelGroup.shaderProgram, it->first.c_str());
|
---|
[5b02676] | 1967 | /*** START OF REFACTORED CODE ***/
|
---|
[49db5fc] | 1968 | } else {
|
---|
| 1969 | glEnableVertexAttribArray(it->second.index);
|
---|
[a0eb547] | 1970 |
|
---|
[49db5fc] | 1971 | glGenBuffers(1, &it->second.buffer);
|
---|
| 1972 | glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer);
|
---|
[a0eb547] | 1973 |
|
---|
[49db5fc] | 1974 | switch (it->second.type) {
|
---|
| 1975 | case GL_FLOAT: {
|
---|
| 1976 | glVertexAttribPointer(it->second.index, it->second.size, it->second.type, GL_FALSE, 0, NULL);
|
---|
| 1977 | break;
|
---|
| 1978 | }
|
---|
| 1979 | case GL_UNSIGNED_INT: {
|
---|
| 1980 | glVertexAttribIPointer(it->second.index, it->second.size, it->second.type, 0, NULL);
|
---|
| 1981 | break;
|
---|
| 1982 | }
|
---|
[a0eb547] | 1983 | }
|
---|
| 1984 | }
|
---|
| 1985 | }
|
---|
[5b02676] | 1986 | /*** START OF REFACTORED CODE ***/
|
---|
[a0eb547] | 1987 | }
|
---|
| 1988 |
|
---|
[49db5fc] | 1989 | void bindUniformData(AttribInfo& attrib) {
|
---|
| 1990 | switch(attrib.uniType) {
|
---|
| 1991 | case UNIFORM_MATRIX_4F:
|
---|
| 1992 | glUniformMatrix4fv(attrib.buffer, attrib.size, GL_FALSE, attrib.data);
|
---|
| 1993 | break;
|
---|
| 1994 | case UNIFORM_1F:
|
---|
[b220f78] | 1995 | glUniform1fv(attrib.buffer, attrib.size, attrib.data);
|
---|
[49db5fc] | 1996 | break;
|
---|
| 1997 | case UNIFORM_3F:
|
---|
| 1998 | glUniform3fv(attrib.buffer, attrib.size, attrib.data);
|
---|
| 1999 | break;
|
---|
[b220f78] | 2000 | case UNIFORM_NONE:
|
---|
| 2001 | break;
|
---|
| 2002 | }
|
---|
| 2003 | }
|
---|
| 2004 |
|
---|
| 2005 | void bindUniformData(AttribInfo& attrib, GLfloat *data) {
|
---|
| 2006 | switch(attrib.uniType) {
|
---|
| 2007 | case UNIFORM_MATRIX_4F:
|
---|
| 2008 | glUniformMatrix4fv(attrib.buffer, attrib.size, GL_FALSE, data);
|
---|
| 2009 | break;
|
---|
| 2010 | case UNIFORM_1F:
|
---|
| 2011 | glUniform1fv(attrib.buffer, attrib.size, data);
|
---|
| 2012 | break;
|
---|
| 2013 | case UNIFORM_3F:
|
---|
| 2014 | glUniform3fv(attrib.buffer, attrib.size, data);
|
---|
| 2015 | break;
|
---|
| 2016 | case UNIFORM_NONE:
|
---|
| 2017 | break;
|
---|
[49db5fc] | 2018 | }
|
---|
| 2019 | }
|
---|
| 2020 |
|
---|
[a0eb547] | 2021 | /* The purpose of this function is to replace the use of sizeof() when calling
|
---|
| 2022 | * function like glBufferSubData and using AttribInfo to get offsets and types
|
---|
| 2023 | * I need instead of hardcoding them. I can't save a type like GLfloat, but I cam
|
---|
| 2024 | * save GL_FLOAT and use this function to return sizeof(GLfloat) when GL_FLOAT is
|
---|
| 2025 | * passed.
|
---|
| 2026 | */
|
---|
| 2027 | size_t GLsizeof(GLenum type) {
|
---|
| 2028 | switch (type) {
|
---|
| 2029 | case GL_FLOAT:
|
---|
| 2030 | return sizeof(GLfloat);
|
---|
| 2031 | case GL_UNSIGNED_INT:
|
---|
| 2032 | return sizeof(GLuint);
|
---|
| 2033 | default:
|
---|
| 2034 | cout << "Uknown GL type passed to GLsizeof: " << type << endl;
|
---|
| 2035 | return 0;
|
---|
| 2036 | }
|
---|
| 2037 | }
|
---|
| 2038 |
|
---|
| 2039 | /* This function returns a reference to the first element of a given vector
|
---|
| 2040 | * attribute in obj. The vector is assumed to hold GLfloats. If the same thing
|
---|
| 2041 | * needs to be done later for vectors of other types, we could pass in a GLenum,
|
---|
| 2042 | * and do something similar to GLsizeof
|
---|
| 2043 | */
|
---|
| 2044 | GLvoid* getVectorAttribPtr(SceneObject& obj, size_t attribOffset) {
|
---|
| 2045 | return (GLvoid*)(&(*(vector<GLfloat>*)((size_t)&obj + attribOffset))[0]);
|
---|
| 2046 | }
|
---|
| 2047 |
|
---|
| 2048 | GLvoid* getScalarAttribPtr(SceneObject& obj, size_t attribOffset) {
|
---|
| 2049 | return (GLvoid*)((size_t)&obj + attribOffset);
|
---|
| 2050 | }
|
---|
| 2051 |
|
---|
[1f3d32b] | 2052 | void populateBuffers(vector<SceneObject*>& objects,
|
---|
[c3c3158] | 2053 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
[0414306] | 2054 | map<ObjectType, ShaderModelGroup>& modelGroups,
|
---|
[f97e638] | 2055 | GLuint ubo) {
|
---|
[0e0f851] | 2056 | GLsizeiptr num_points = 0;
|
---|
| 2057 | GLsizeiptr num_objects = 0;
|
---|
[0d5c100] | 2058 |
|
---|
[c3c3158] | 2059 | map<GLuint, unsigned int> shaderCounts;
|
---|
[0d5c100] | 2060 | map<GLuint, unsigned int> shaderUboCounts;
|
---|
[93462c6] | 2061 |
|
---|
[646f3f2] | 2062 | map<GLuint, BufferInfo>::iterator shaderIt;
|
---|
| 2063 |
|
---|
| 2064 | for (shaderIt = shaderBufferInfo.begin(); shaderIt != shaderBufferInfo.end(); shaderIt++) {
|
---|
| 2065 | shaderCounts[shaderIt->first] = 0;
|
---|
| 2066 | shaderUboCounts[shaderIt->first] = 0;
|
---|
| 2067 | }
|
---|
| 2068 |
|
---|
[1f3d32b] | 2069 | vector<SceneObject*>::iterator it;
|
---|
[0d5c100] | 2070 |
|
---|
[92b1e90] | 2071 | /* Find all shaders that need to be used and the number of objects and
|
---|
[c3c3158] | 2072 | * number of points for each shader. Construct a map from shader id to count
|
---|
| 2073 | * of points being drawn using that shader (for thw model matrix ubo, we
|
---|
| 2074 | * need object counts instead). These will be used to get offsets into the
|
---|
| 2075 | * vertex buffer for each shader.
|
---|
| 2076 | */
|
---|
[1f3d32b] | 2077 | for (it = objects.begin(); it != objects.end(); ) {
|
---|
| 2078 | if ((*it)->deleted) {
|
---|
| 2079 | delete *it;
|
---|
[c3c3158] | 2080 | it = objects.erase(it);
|
---|
[0d5c100] | 2081 | } else {
|
---|
[0e0f851] | 2082 | num_points += (*it)->num_points;
|
---|
| 2083 | num_objects++;
|
---|
[c3c3158] | 2084 |
|
---|
[dd9771c] | 2085 | shaderCounts[modelGroups[(*it)->type].shaderProgram] += (*it)->num_points;
|
---|
| 2086 | shaderUboCounts[modelGroups[(*it)->type].shaderProgram]++;
|
---|
[c3c3158] | 2087 |
|
---|
| 2088 | it++;
|
---|
[e3ca955] | 2089 | }
|
---|
[0d5c100] | 2090 | }
|
---|
| 2091 |
|
---|
[c3c3158] | 2092 | // double the buffer sizes to leave room for new objects
|
---|
[0e0f851] | 2093 | num_points *= 2;
|
---|
| 2094 | num_objects *= 2;
|
---|
[c3c3158] | 2095 |
|
---|
[646f3f2] | 2096 | map<GLuint, unsigned int>::iterator shaderCountIt;
|
---|
[0d5c100] | 2097 | unsigned int lastShaderCount = 0;
|
---|
| 2098 | unsigned int lastShaderUboCount = 0;
|
---|
| 2099 |
|
---|
| 2100 | /*
|
---|
[c3c3158] | 2101 | * The counts calculated above can be used to get the starting offset of
|
---|
| 2102 | * each shader in the vertex buffer. Create a map of base offsets to mark
|
---|
| 2103 | * where the data for the first object using a given shader begins. Also,
|
---|
| 2104 | * create a map of current offsets to mark where to copy data for the next
|
---|
| 2105 | * object being added.
|
---|
| 2106 | */
|
---|
[646f3f2] | 2107 | for (shaderCountIt = shaderCounts.begin(); shaderCountIt != shaderCounts.end(); shaderCountIt++) {
|
---|
[0e0f851] | 2108 | // When populating the buffers, leave as much empty space as space taken up by existing objects
|
---|
| 2109 | // to allow new objects to be added without immediately having to resize the buffers
|
---|
[646f3f2] | 2110 | shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2;
|
---|
[0d5c100] | 2111 |
|
---|
[646f3f2] | 2112 | shaderBufferInfo[shaderCountIt->first].ubo_offset = 0;
|
---|
[c3c3158] | 2113 |
|
---|
[646f3f2] | 2114 | shaderBufferInfo[shaderCountIt->first].ubo_capacity = shaderUboCounts[shaderCountIt->first] * 2;
|
---|
[0d5c100] | 2115 |
|
---|
[646f3f2] | 2116 | lastShaderCount += shaderCounts[shaderCountIt->first];
|
---|
| 2117 | lastShaderUboCount += shaderUboCounts[shaderCountIt->first];
|
---|
[0d5c100] | 2118 | }
|
---|
| 2119 |
|
---|
[4c7cd57] | 2120 | map<ObjectType, ShaderModelGroup>::iterator modelGroupIt;
|
---|
[a0eb547] | 2121 | ShaderModelGroup* smg;
|
---|
[4c7cd57] | 2122 | for (modelGroupIt = modelGroups.begin(); modelGroupIt != modelGroups.end(); modelGroupIt++) {
|
---|
[a0eb547] | 2123 | smg = &modelGroups[modelGroupIt->first];
|
---|
| 2124 |
|
---|
| 2125 | smg->numPoints = 0;
|
---|
| 2126 | smg->vboCapacity = shaderCounts[smg->shaderProgram] * 2;
|
---|
[a926b79] | 2127 |
|
---|
[c55614a] | 2128 | map<string, AttribInfo>::iterator attrIt;
|
---|
| 2129 | for (attrIt = smg->attribs.begin(); attrIt != smg->attribs.end(); attrIt++) {
|
---|
| 2130 | if (attrIt->second.attribType != ATTRIB_UNIFORM) {
|
---|
| 2131 | glBindBuffer(GL_ARRAY_BUFFER, attrIt->second.buffer);
|
---|
| 2132 | glBufferData(GL_ARRAY_BUFFER, smg->vboCapacity * GLsizeof(attrIt->second.type) * attrIt->second.size, NULL, GL_DYNAMIC_DRAW);
|
---|
| 2133 | }
|
---|
[a0eb547] | 2134 | }
|
---|
[4c7cd57] | 2135 | }
|
---|
[0414306] | 2136 |
|
---|
[f97e638] | 2137 | // Allocate the ubo using the counts calculated above
|
---|
[0d5c100] | 2138 |
|
---|
[c3c3158] | 2139 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
[0e0f851] | 2140 | glBufferData(GL_UNIFORM_BUFFER, num_objects * sizeof(mat4), NULL, GL_DYNAMIC_DRAW);
|
---|
[0d5c100] | 2141 |
|
---|
| 2142 | for (it = objects.begin(); it != objects.end(); it++) {
|
---|
[49db5fc] | 2143 | copyObjectDataToBuffers(**it, shaderBufferInfo, modelGroups, ubo);
|
---|
[c3c3158] | 2144 | }
|
---|
| 2145 | }
|
---|
[0d5c100] | 2146 |
|
---|
[c3c3158] | 2147 | void copyObjectDataToBuffers(SceneObject& obj,
|
---|
| 2148 | map<GLuint, BufferInfo>& shaderBufferInfo,
|
---|
[0414306] | 2149 | map<ObjectType, ShaderModelGroup>& modelGroups,
|
---|
[49db5fc] | 2150 | GLuint ubo) {
|
---|
[dd9771c] | 2151 | BufferInfo* bufferInfo = &shaderBufferInfo[modelGroups[obj.type].shaderProgram];
|
---|
[0d5c100] | 2152 |
|
---|
[dc19a39] | 2153 | obj.vertex_vbo_offset = modelGroups[obj.type].numPoints;
|
---|
[c3c3158] | 2154 | obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset;
|
---|
[0d5c100] | 2155 |
|
---|
[dc19a39] | 2156 | ShaderModelGroup& smg = modelGroups[obj.type];
|
---|
[0d5c100] | 2157 |
|
---|
[dc19a39] | 2158 | for (map<string, AttribInfo>::iterator it = smg.attribs.begin(); it != smg.attribs.end(); it++) {
|
---|
| 2159 | glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer);
|
---|
[fd6f465] | 2160 |
|
---|
[7e10667] | 2161 | switch (it->second.attribType) {
|
---|
| 2162 | case ATTRIB_POINT_VARYING:
|
---|
| 2163 | glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * GLsizeof(it->second.type) * it->second.size,
|
---|
| 2164 | obj.num_points * GLsizeof(it->second.type) * it->second.size, getVectorAttribPtr(obj, it->second.fieldOffset));
|
---|
| 2165 | break;
|
---|
| 2166 | case ATTRIB_OBJECT_VARYING:
|
---|
| 2167 | for (unsigned int i = 0; i < obj.num_points; i++) {
|
---|
| 2168 | glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * GLsizeof(it->second.type) * it->second.size,
|
---|
| 2169 | GLsizeof(it->second.type) * it->second.size, getScalarAttribPtr(obj, it->second.fieldOffset));
|
---|
| 2170 | }
|
---|
| 2171 | break;
|
---|
| 2172 | case ATTRIB_UNIFORM:
|
---|
| 2173 | break;
|
---|
[646f3f2] | 2174 | }
|
---|
[dc19a39] | 2175 | }
|
---|
[fd6f465] | 2176 |
|
---|
[7e10667] | 2177 | obj.model_mat = obj.model_transform * obj.model_base;
|
---|
[25b47d7] | 2178 |
|
---|
[dc19a39] | 2179 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
| 2180 | glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
|
---|
[646f3f2] | 2181 |
|
---|
[dc19a39] | 2182 | if (obj.type == TYPE_ASTEROID) {
|
---|
| 2183 | glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram);
|
---|
| 2184 |
|
---|
| 2185 | ostringstream oss;
|
---|
| 2186 | oss << "hp[" << obj.ubo_offset << "]";
|
---|
[7e10667] | 2187 | glUniform1f(glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, oss.str().c_str()), ((Asteroid&)obj).hp);
|
---|
| 2188 | } else if (obj.type == TYPE_EXPLOSION) {
|
---|
| 2189 | glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
|
---|
| 2190 |
|
---|
| 2191 | ostringstream oss;
|
---|
| 2192 | oss << "explosion_start_time[" << obj.ubo_offset << "]";
|
---|
| 2193 | glUniform1f(glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, oss.str().c_str()), ((ParticleEffect&)obj).startTime);
|
---|
[25b47d7] | 2194 | }
|
---|
[0e0f851] | 2195 |
|
---|
[b62c109] | 2196 | modelGroups[obj.type].numPoints += obj.num_points;
|
---|
[c3c3158] | 2197 | bufferInfo->ubo_offset++;
|
---|
[0d5c100] | 2198 | }
|
---|
[93462c6] | 2199 |
|
---|
[5c403fe] | 2200 | void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) {
|
---|
[fabed35] | 2201 | if (obj.deleted) return;
|
---|
| 2202 |
|
---|
[3d06b4e] | 2203 | obj.model_transform = transform * obj.model_transform;
|
---|
[5c403fe] | 2204 | obj.model_mat = obj.model_transform * obj.model_base;
|
---|
| 2205 |
|
---|
[95595de] | 2206 | obj.bounding_center = vec3(transform * vec4(obj.bounding_center, 1.0f));
|
---|
| 2207 |
|
---|
[5c403fe] | 2208 | glBindBuffer(GL_UNIFORM_BUFFER, ubo);
|
---|
| 2209 | glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
|
---|
| 2210 | }
|
---|
| 2211 |
|
---|
[1f3d32b] | 2212 | void translateLaser(Laser* laser, const vec3& translation, GLuint ubo) {
|
---|
[e9347b4] | 2213 | // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created,
|
---|
[612d1f6] | 2214 | // and then re-used here
|
---|
| 2215 |
|
---|
[1f3d32b] | 2216 | vec3 start = vec3(laser->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
| 2217 | vec3 end = vec3(laser->model_transform * vec4(0.0f, 0.0f, laser->points[38], 1.0f));
|
---|
[612d1f6] | 2218 |
|
---|
| 2219 | vec3 ray = end - start;
|
---|
| 2220 | float length = glm::length(ray);
|
---|
| 2221 |
|
---|
| 2222 | float xAxisRotation = asin(ray.y / length);
|
---|
| 2223 | float yAxisRotation = atan2(-ray.x, -ray.z);
|
---|
| 2224 |
|
---|
| 2225 | vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) *
|
---|
| 2226 | rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) *
|
---|
| 2227 | vec4(0.0f, 1.0f, 0.0f, 1.0f));
|
---|
| 2228 |
|
---|
| 2229 | // To project point P onto line AB:
|
---|
| 2230 | // projection = A + dot(AP,AB) / dot(AB,AB) * AB
|
---|
| 2231 | vec3 projOnLaser = start + glm::dot(cam_pos - start, ray) / (length*length) * ray;
|
---|
| 2232 | vec3 laserToCam = cam_pos - projOnLaser;
|
---|
| 2233 |
|
---|
| 2234 | float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam));
|
---|
| 2235 |
|
---|
[1f3d32b] | 2236 | laser->model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f));
|
---|
[612d1f6] | 2237 |
|
---|
[1f3d32b] | 2238 | transformObject(*laser, translate(mat4(1.0f), translation), ubo);
|
---|
[612d1f6] | 2239 | }
|
---|
| 2240 |
|
---|
[a0eb547] | 2241 | void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp) {
|
---|
[e9347b4] | 2242 | // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created,
|
---|
| 2243 | // and then re-used here
|
---|
| 2244 |
|
---|
[1f3d32b] | 2245 | vec3 start = vec3(laser->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
| 2246 | vec3 end = vec3(laser->model_transform * vec4(0.0f, 0.0f, laser->points[2] + laser->points[20], 1.0f));
|
---|
[e9347b4] | 2247 |
|
---|
| 2248 | vec3 intersection(0.0f), closestIntersection(0.0f);
|
---|
[1f3d32b] | 2249 | Asteroid* closestAsteroid = NULL;
|
---|
[e9347b4] | 2250 |
|
---|
[1f3d32b] | 2251 | for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
|
---|
| 2252 | if ((*it)->type == TYPE_ASTEROID && !(*it)->deleted && getLaserAndAsteroidIntersection(start, end, **it, intersection)) {
|
---|
[e9347b4] | 2253 | // TODO: Implement a more generic algorithm for testing the closest object by getting the distance between the points
|
---|
| 2254 | if (closestAsteroid == NULL || intersection.z > closestIntersection.z) {
|
---|
| 2255 | // TODO: At this point, find the real intersection of the laser with one of the asteroid's sides
|
---|
[1f3d32b] | 2256 | closestAsteroid = (Asteroid*)*it;
|
---|
[e9347b4] | 2257 | closestIntersection = intersection;
|
---|
| 2258 | }
|
---|
| 2259 | }
|
---|
| 2260 | }
|
---|
| 2261 |
|
---|
[1f3d32b] | 2262 | float width = laser->points[0] - laser->points[2];
|
---|
| 2263 |
|
---|
| 2264 | if (laser->targetAsteroid != closestAsteroid) {
|
---|
| 2265 | if (laser->targetAsteroid != NULL) {
|
---|
| 2266 | if (laser == leftLaser) {
|
---|
| 2267 | leftLaserEffect->deleted = true;
|
---|
| 2268 | } else if (laser == rightLaser) {
|
---|
| 2269 | rightLaserEffect->deleted = true;
|
---|
| 2270 | }
|
---|
| 2271 | }
|
---|
| 2272 |
|
---|
| 2273 | EffectOverTime* eot = NULL;
|
---|
[e9347b4] | 2274 |
|
---|
[1f3d32b] | 2275 | if (closestAsteroid != NULL) {
|
---|
[25b47d7] | 2276 | eot = new EffectOverTime(closestAsteroid->hp, -20.0f, closestAsteroid);
|
---|
[1f3d32b] | 2277 | effects.push_back(eot);
|
---|
| 2278 | }
|
---|
| 2279 |
|
---|
| 2280 | if (laser == leftLaser) {
|
---|
| 2281 | leftLaserEffect = eot;
|
---|
| 2282 | } else if (laser == rightLaser) {
|
---|
| 2283 | rightLaserEffect = eot;
|
---|
| 2284 | }
|
---|
| 2285 | }
|
---|
| 2286 | laser->targetAsteroid = closestAsteroid;
|
---|
| 2287 |
|
---|
| 2288 | float length = 5.24f; // I think this was to make sure the laser went past the end of the screen
|
---|
[e9347b4] | 2289 | if (closestAsteroid != NULL) {
|
---|
| 2290 | length = glm::length(closestIntersection - start);
|
---|
[0e0f851] | 2291 |
|
---|
[a0eb547] | 2292 | // TODO: Find a more generic way of updating the asteroid hp than in updateLaserTarget
|
---|
[0e0f851] | 2293 |
|
---|
| 2294 | glUseProgram(asteroid_sp);
|
---|
[25b47d7] | 2295 |
|
---|
| 2296 | ostringstream oss;
|
---|
| 2297 | oss << "hp[" << closestAsteroid->ubo_offset << "]";
|
---|
| 2298 | glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), closestAsteroid->hp);
|
---|
[e9347b4] | 2299 | }
|
---|
| 2300 |
|
---|
[1f3d32b] | 2301 | laser->points[20] = -length + width / 2;
|
---|
| 2302 | laser->points[23] = -length + width / 2;
|
---|
| 2303 | laser->points[29] = -length + width / 2;
|
---|
| 2304 | laser->points[38] = -length;
|
---|
| 2305 | laser->points[41] = -length;
|
---|
| 2306 | laser->points[44] = -length + width / 2;
|
---|
| 2307 | laser->points[47] = -length;
|
---|
| 2308 | laser->points[50] = -length + width / 2;
|
---|
| 2309 | laser->points[53] = -length + width / 2;
|
---|
[e9347b4] | 2310 |
|
---|
[a0eb547] | 2311 | AttribInfo* attrib = &laserSmg.attribs["vertex_position"];
|
---|
| 2312 | glBindBuffer(GL_ARRAY_BUFFER, attrib->buffer);
|
---|
| 2313 | glBufferSubData(GL_ARRAY_BUFFER, laser->vertex_vbo_offset * GLsizeof(attrib->type) * attrib->size,
|
---|
| 2314 | laser->num_points * GLsizeof(attrib->type) * attrib->size, getVectorAttribPtr(*laser, attrib->fieldOffset));
|
---|
[e9347b4] | 2315 | }
|
---|
| 2316 |
|
---|
| 2317 | bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection) {
|
---|
| 2318 | /*
|
---|
| 2319 | ### LINE EQUATIONS ###
|
---|
| 2320 | x = x1 + u * (x2 - x1)
|
---|
| 2321 | y = y1 + u * (y2 - y1)
|
---|
| 2322 | z = z1 + u * (z2 - z1)
|
---|
| 2323 |
|
---|
| 2324 | ### SPHERE EQUATION ###
|
---|
| 2325 | (x - x3)^2 + (y - y3)^2 + (z - z3)^2 = r^2
|
---|
| 2326 |
|
---|
| 2327 | ### QUADRATIC EQUATION TO SOLVE ###
|
---|
| 2328 | a*u^2 + b*u + c = 0
|
---|
| 2329 | WHERE THE CONSTANTS ARE
|
---|
| 2330 | a = (x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2
|
---|
| 2331 | b = 2*( (x2 - x1)*(x1 - x3) + (y2 - y1)*(y1 - y3) + (z2 - z1)*(z1 - z3) )
|
---|
| 2332 | c = x3^2 + y3^2 + z3^2 + x1^2 + y1^2 + z1^2 - 2(x3*x1 + y3*y1 + z3*z1) - r^2
|
---|
| 2333 |
|
---|
| 2334 | u = (-b +- sqrt(b^2 - 4*a*c)) / 2a
|
---|
| 2335 |
|
---|
| 2336 | If the value under the root is >= 0, we got an intersection
|
---|
| 2337 | If the value > 0, there are two solutions. Take the one closer to 0, since that's the
|
---|
| 2338 | one closer to the laser start point
|
---|
| 2339 | */
|
---|
| 2340 |
|
---|
| 2341 | vec3& center = asteroid.bounding_center;
|
---|
| 2342 |
|
---|
| 2343 | float a = pow(end.x-start.x, 2) + pow(end.y-start.y, 2) + pow(end.z-start.z, 2);
|
---|
| 2344 | 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));
|
---|
| 2345 | 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);
|
---|
| 2346 | float discriminant = pow(b, 2) - 4*a*c;
|
---|
| 2347 |
|
---|
| 2348 | if (discriminant >= 0.0f) {
|
---|
| 2349 | // In this case, the negative root will always give the point closer to the laser start point
|
---|
| 2350 | float u = (-b - sqrt(discriminant)) / (2 * a);
|
---|
| 2351 |
|
---|
| 2352 | // Check that the intersection is within the line segment corresponding to the laser
|
---|
| 2353 | if (0.0f <= u && u <= 1.0f) {
|
---|
| 2354 | intersection = start + u * (end - start);
|
---|
| 2355 | return true;
|
---|
| 2356 | }
|
---|
| 2357 | }
|
---|
| 2358 |
|
---|
| 2359 | return false;
|
---|
| 2360 | }
|
---|
| 2361 |
|
---|
[7e10667] | 2362 | void renderScene(map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) {
|
---|
[93462c6] | 2363 |
|
---|
[4c7cd57] | 2364 | glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
|
---|
| 2365 | glBindVertexArray(modelGroups[TYPE_SHIP].vao);
|
---|
[93462c6] | 2366 |
|
---|
[a926b79] | 2367 | glDrawArrays(GL_TRIANGLES, 0, modelGroups[TYPE_SHIP].numPoints);
|
---|
[93462c6] | 2368 |
|
---|
[0414306] | 2369 | glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram);
|
---|
| 2370 | glBindVertexArray(modelGroups[TYPE_ASTEROID].vao);
|
---|
[0e0f851] | 2371 |
|
---|
[14e6918] | 2372 | glDrawArrays(GL_TRIANGLES, 0, modelGroups[TYPE_ASTEROID].numPoints);
|
---|
[0e0f851] | 2373 |
|
---|
[9f9f9a7] | 2374 | glEnable(GL_BLEND);
|
---|
| 2375 |
|
---|
[b62c109] | 2376 | glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
|
---|
| 2377 | glBindVertexArray(modelGroups[TYPE_LASER].vao);
|
---|
[b155f13] | 2378 |
|
---|
[a9d191a] | 2379 | glDrawArrays(GL_TRIANGLES, 0, modelGroups[TYPE_LASER].numPoints);
|
---|
[9f9f9a7] | 2380 |
|
---|
[0414306] | 2381 | glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
|
---|
| 2382 | glBindVertexArray(modelGroups[TYPE_EXPLOSION].vao);
|
---|
[db06984] | 2383 |
|
---|
| 2384 | glEnable(GL_PROGRAM_POINT_SIZE);
|
---|
| 2385 |
|
---|
[0414306] | 2386 | glDrawArrays(GL_POINTS, 0, modelGroups[TYPE_EXPLOSION].numPoints);
|
---|
[db06984] | 2387 |
|
---|
| 2388 | glDisable(GL_PROGRAM_POINT_SIZE);
|
---|
| 2389 | glDisable(GL_BLEND);
|
---|
[b155f13] | 2390 | }
|
---|
| 2391 |
|
---|
[c4c205e] | 2392 | void renderSceneGui(map<string, vector<UIValue>> valueLists) {
|
---|
[c1ca5b5] | 2393 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 2394 |
|
---|
| 2395 | // 1. Show a simple window.
|
---|
| 2396 | // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
|
---|
[5b3462b] | 2397 | /*
|
---|
[c1ca5b5] | 2398 | {
|
---|
| 2399 | static float f = 0.0f;
|
---|
| 2400 | static int counter = 0;
|
---|
| 2401 | ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
|
---|
| 2402 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
---|
| 2403 | ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
---|
| 2404 |
|
---|
| 2405 | ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
|
---|
| 2406 | ImGui::Checkbox("Another Window", &show_another_window);
|
---|
| 2407 |
|
---|
| 2408 | if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
|
---|
| 2409 | counter++;
|
---|
| 2410 | ImGui::SameLine();
|
---|
| 2411 | ImGui::Text("counter = %d", counter);
|
---|
| 2412 |
|
---|
| 2413 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
---|
| 2414 | }
|
---|
[5b3462b] | 2415 | */
|
---|
[c1ca5b5] | 2416 |
|
---|
[5b3462b] | 2417 | {
|
---|
[1f3d32b] | 2418 | ImGui::SetNextWindowSize(ImVec2(95, 46), ImGuiCond_Once);
|
---|
[5b3462b] | 2419 | ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once);
|
---|
[f0cc877] | 2420 | ImGui::Begin("WndStats", NULL,
|
---|
| 2421 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 2422 | ImGuiWindowFlags_NoResize |
|
---|
| 2423 | ImGuiWindowFlags_NoMove);
|
---|
[c4c205e] | 2424 |
|
---|
| 2425 | renderGuiValueList(valueLists["stats value list"]);
|
---|
| 2426 |
|
---|
[c1ca5b5] | 2427 | ImGui::End();
|
---|
| 2428 | }
|
---|
| 2429 |
|
---|
[5b3462b] | 2430 | {
|
---|
| 2431 | ImGui::SetNextWindowSize(ImVec2(250, 35), ImGuiCond_Once);
|
---|
[c4c205e] | 2432 | ImGui::SetNextWindowPos(ImVec2(380, 10), ImGuiCond_Once);
|
---|
[f0cc877] | 2433 | ImGui::Begin("WndMenubar", NULL,
|
---|
| 2434 | ImGuiWindowFlags_NoTitleBar |
|
---|
[5b3462b] | 2435 | ImGuiWindowFlags_NoResize |
|
---|
| 2436 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 2437 | ImGui::InvisibleButton("", ImVec2(155, 18));
|
---|
[5b3462b] | 2438 | ImGui::SameLine();
|
---|
[93462c6] | 2439 | if (ImGui::Button("Main Menu")) {
|
---|
| 2440 | events.push(EVENT_GO_TO_MAIN_MENU);
|
---|
[5b3462b] | 2441 | }
|
---|
| 2442 | ImGui::End();
|
---|
[c1ca5b5] | 2443 | }
|
---|
| 2444 |
|
---|
[c4c205e] | 2445 | {
|
---|
| 2446 | ImGui::SetNextWindowSize(ImVec2(200, 200), ImGuiCond_Once);
|
---|
| 2447 | ImGui::SetNextWindowPos(ImVec2(430, 60), ImGuiCond_Once);
|
---|
| 2448 | ImGui::Begin("WndDebug", NULL,
|
---|
| 2449 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 2450 | ImGuiWindowFlags_NoResize |
|
---|
| 2451 | ImGuiWindowFlags_NoMove);
|
---|
| 2452 |
|
---|
| 2453 | renderGuiValueList(valueLists["debug value list"]);
|
---|
| 2454 |
|
---|
| 2455 | ImGui::End();
|
---|
| 2456 | }
|
---|
| 2457 |
|
---|
[93462c6] | 2458 | ImGui::Render();
|
---|
| 2459 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 2460 | }
|
---|
| 2461 |
|
---|
| 2462 | void renderMainMenu() {
|
---|
| 2463 | }
|
---|
| 2464 |
|
---|
[f133da0] | 2465 | /*** START OF REFACTORED CODE ***/
|
---|
[93462c6] | 2466 | void renderMainMenuGui() {
|
---|
| 2467 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
| 2468 |
|
---|
[f0cc877] | 2469 | {
|
---|
| 2470 | int padding = 4;
|
---|
| 2471 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
[e6bc0f4] | 2472 | ImGui::SetNextWindowSize(ImVec2(windowWidth + 2 * padding, windowHeight + 2 * padding), ImGuiCond_Always);
|
---|
[f0cc877] | 2473 | ImGui::Begin("WndMain", NULL,
|
---|
| 2474 | ImGuiWindowFlags_NoTitleBar |
|
---|
| 2475 | ImGuiWindowFlags_NoResize |
|
---|
| 2476 | ImGuiWindowFlags_NoMove);
|
---|
[93462c6] | 2477 |
|
---|
| 2478 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
| 2479 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
| 2480 | ImGui::SameLine();
|
---|
| 2481 | if (ImGui::Button("New Game")) {
|
---|
| 2482 | events.push(EVENT_GO_TO_GAME);
|
---|
| 2483 | }
|
---|
| 2484 |
|
---|
| 2485 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
| 2486 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
| 2487 | ImGui::SameLine();
|
---|
| 2488 | if (ImGui::Button("Quit")) {
|
---|
| 2489 | events.push(EVENT_QUIT);
|
---|
| 2490 | }
|
---|
| 2491 |
|
---|
[f0cc877] | 2492 | ImGui::End();
|
---|
| 2493 | }
|
---|
| 2494 |
|
---|
[c1ca5b5] | 2495 | ImGui::Render();
|
---|
| 2496 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
| 2497 | }
|
---|
[f133da0] | 2498 | /*** END OF REFACTORED CODE ***/
|
---|
[cf2d1e5] | 2499 |
|
---|
[a9d191a] | 2500 | void initGuiValueLists(map<string, vector<UIValue>> valueLists) {
|
---|
| 2501 | valueLists["stats value list"] = vector<UIValue>();
|
---|
| 2502 | valueLists["debug value list"] = vector<UIValue>();
|
---|
| 2503 | }
|
---|
| 2504 |
|
---|
[c4c205e] | 2505 | void renderGuiValueList(vector<UIValue>& values) {
|
---|
| 2506 | float maxWidth = 0.0f;
|
---|
| 2507 | float cursorStartPos = ImGui::GetCursorPosX();
|
---|
| 2508 |
|
---|
| 2509 | for (vector<UIValue>::iterator it = values.begin(); it != values.end(); it++) {
|
---|
| 2510 | float textWidth = ImGui::CalcTextSize(it->label.c_str()).x;
|
---|
| 2511 |
|
---|
| 2512 | if (maxWidth < textWidth)
|
---|
| 2513 | maxWidth = textWidth;
|
---|
| 2514 | }
|
---|
| 2515 |
|
---|
| 2516 | stringstream ss;
|
---|
| 2517 |
|
---|
| 2518 | for (vector<UIValue>::iterator it = values.begin(); it != values.end(); it++) {
|
---|
| 2519 | ss.str("");
|
---|
| 2520 | ss.clear();
|
---|
| 2521 |
|
---|
| 2522 | switch (it->type) {
|
---|
| 2523 | case UIVALUE_INT:
|
---|
| 2524 | ss << it->label << ": " << *(unsigned int*)it->value;
|
---|
| 2525 | break;
|
---|
| 2526 | case UIVALUE_DOUBLE:
|
---|
| 2527 | ss << it->label << ": " << *(double*)it->value;
|
---|
| 2528 | break;
|
---|
| 2529 | }
|
---|
| 2530 |
|
---|
| 2531 | float textWidth = ImGui::CalcTextSize(it->label.c_str()).x;
|
---|
| 2532 |
|
---|
| 2533 | ImGui::SetCursorPosX(cursorStartPos + maxWidth - textWidth);
|
---|
| 2534 | ImGui::Text("%s", ss.str().c_str());
|
---|
| 2535 | }
|
---|
| 2536 | }
|
---|
| 2537 |
|
---|
[dd9771c] | 2538 | Asteroid* createAsteroid(vec3 pos) {
|
---|
[1f3d32b] | 2539 | Asteroid* obj = new Asteroid();
|
---|
| 2540 | obj->type = TYPE_ASTEROID;
|
---|
[646f3f2] | 2541 | obj->hp = 10.0f;
|
---|
[cf2d1e5] | 2542 |
|
---|
[1f3d32b] | 2543 | obj->points = {
|
---|
[cf2d1e5] | 2544 | // front
|
---|
| 2545 | 1.0f, 1.0f, 1.0f,
|
---|
| 2546 | -1.0f, 1.0f, 1.0f,
|
---|
| 2547 | -1.0f, -1.0f, 1.0f,
|
---|
| 2548 | 1.0f, 1.0f, 1.0f,
|
---|
| 2549 | -1.0f, -1.0f, 1.0f,
|
---|
| 2550 | 1.0f, -1.0f, 1.0f,
|
---|
| 2551 |
|
---|
| 2552 | // top
|
---|
| 2553 | 1.0f, 1.0f, -1.0f,
|
---|
| 2554 | -1.0f, 1.0f, -1.0f,
|
---|
| 2555 | -1.0f, 1.0f, 1.0f,
|
---|
| 2556 | 1.0f, 1.0f, -1.0f,
|
---|
| 2557 | -1.0f, 1.0f, 1.0f,
|
---|
| 2558 | 1.0f, 1.0f, 1.0f,
|
---|
| 2559 |
|
---|
| 2560 | // bottom
|
---|
| 2561 | 1.0f, -1.0f, 1.0f,
|
---|
| 2562 | -1.0f, -1.0f, 1.0f,
|
---|
| 2563 | -1.0f, -1.0f, -1.0f,
|
---|
| 2564 | 1.0f, -1.0f, 1.0f,
|
---|
| 2565 | -1.0f, -1.0f, -1.0f,
|
---|
| 2566 | 1.0f, -1.0f, -1.0f,
|
---|
| 2567 |
|
---|
| 2568 | // back
|
---|
| 2569 | 1.0f, 1.0f, -1.0f,
|
---|
| 2570 | -1.0f, -1.0f, -1.0f,
|
---|
| 2571 | -1.0f, 1.0f, -1.0f,
|
---|
| 2572 | 1.0f, 1.0f, -1.0f,
|
---|
| 2573 | 1.0f, -1.0f, -1.0f,
|
---|
| 2574 | -1.0f, -1.0f, -1.0f,
|
---|
| 2575 |
|
---|
| 2576 | // right
|
---|
| 2577 | 1.0f, 1.0f, -1.0f,
|
---|
| 2578 | 1.0f, 1.0f, 1.0f,
|
---|
| 2579 | 1.0f, -1.0f, 1.0f,
|
---|
| 2580 | 1.0f, 1.0f, -1.0f,
|
---|
| 2581 | 1.0f, -1.0f, 1.0f,
|
---|
| 2582 | 1.0f, -1.0f, -1.0f,
|
---|
| 2583 |
|
---|
| 2584 | // left
|
---|
| 2585 | -1.0f, 1.0f, 1.0f,
|
---|
| 2586 | -1.0f, 1.0f, -1.0f,
|
---|
| 2587 | -1.0f, -1.0f, -1.0f,
|
---|
| 2588 | -1.0f, 1.0f, 1.0f,
|
---|
| 2589 | -1.0f, -1.0f, -1.0f,
|
---|
| 2590 | -1.0f, -1.0f, 1.0f,
|
---|
| 2591 | };
|
---|
[1f3d32b] | 2592 | obj->colors = {
|
---|
[cf2d1e5] | 2593 | // front
|
---|
[25b47d7] | 2594 | 0.4f, 0.4f, 0.4f,
|
---|
| 2595 | 0.4f, 0.4f, 0.4f,
|
---|
| 2596 | 0.4f, 0.4f, 0.4f,
|
---|
| 2597 | 0.4f, 0.4f, 0.4f,
|
---|
| 2598 | 0.4f, 0.4f, 0.4f,
|
---|
| 2599 | 0.4f, 0.4f, 0.4f,
|
---|
[cf2d1e5] | 2600 |
|
---|
| 2601 | // top
|
---|
[25b47d7] | 2602 | 0.4f, 0.4f, 0.4f,
|
---|
| 2603 | 0.4f, 0.4f, 0.4f,
|
---|
| 2604 | 0.4f, 0.4f, 0.4f,
|
---|
| 2605 | 0.4f, 0.4f, 0.4f,
|
---|
| 2606 | 0.4f, 0.4f, 0.4f,
|
---|
| 2607 | 0.4f, 0.4f, 0.4f,
|
---|
[cf2d1e5] | 2608 |
|
---|
| 2609 | // bottom
|
---|
[25b47d7] | 2610 | 0.4f, 0.4f, 0.4f,
|
---|
| 2611 | 0.4f, 0.4f, 0.4f,
|
---|
| 2612 | 0.4f, 0.4f, 0.4f,
|
---|
| 2613 | 0.4f, 0.4f, 0.4f,
|
---|
| 2614 | 0.4f, 0.4f, 0.4f,
|
---|
| 2615 | 0.4f, 0.4f, 0.4f,
|
---|
[cf2d1e5] | 2616 |
|
---|
| 2617 | // back
|
---|
[25b47d7] | 2618 | 0.4f, 0.4f, 0.4f,
|
---|
| 2619 | 0.4f, 0.4f, 0.4f,
|
---|
| 2620 | 0.4f, 0.4f, 0.4f,
|
---|
| 2621 | 0.4f, 0.4f, 0.4f,
|
---|
| 2622 | 0.4f, 0.4f, 0.4f,
|
---|
| 2623 | 0.4f, 0.4f, 0.4f,
|
---|
[cf2d1e5] | 2624 |
|
---|
| 2625 | // right
|
---|
[25b47d7] | 2626 | 0.4f, 0.4f, 0.4f,
|
---|
| 2627 | 0.4f, 0.4f, 0.4f,
|
---|
| 2628 | 0.4f, 0.4f, 0.4f,
|
---|
| 2629 | 0.4f, 0.4f, 0.4f,
|
---|
| 2630 | 0.4f, 0.4f, 0.4f,
|
---|
| 2631 | 0.4f, 0.4f, 0.4f,
|
---|
[cf2d1e5] | 2632 |
|
---|
| 2633 | // left
|
---|
[25b47d7] | 2634 | 0.4f, 0.4f, 0.4f,
|
---|
| 2635 | 0.4f, 0.4f, 0.4f,
|
---|
| 2636 | 0.4f, 0.4f, 0.4f,
|
---|
| 2637 | 0.4f, 0.4f, 0.4f,
|
---|
| 2638 | 0.4f, 0.4f, 0.4f,
|
---|
| 2639 | 0.4f, 0.4f, 0.4f,
|
---|
[cf2d1e5] | 2640 | };
|
---|
[1f3d32b] | 2641 | obj->texcoords = { 0.0f };
|
---|
[cf2d1e5] | 2642 |
|
---|
[dba67b2] | 2643 | mat4 T = translate(mat4(1.0f), pos);
|
---|
| 2644 | mat4 R = rotate(mat4(1.0f), 60.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 1.0f, -1.0f));
|
---|
[1f3d32b] | 2645 | obj->model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
|
---|
[cf2d1e5] | 2646 |
|
---|
[1f3d32b] | 2647 | obj->translate_mat = T;
|
---|
[95595de] | 2648 |
|
---|
[8316333] | 2649 | initObject(obj);
|
---|
[e9347b4] | 2650 | // This accounts for the scaling in model_base.
|
---|
| 2651 | // Dividing by 8 instead of 10 since the bounding radius algorithm
|
---|
| 2652 | // under-calculates the true value.
|
---|
| 2653 | // TODO: Once the intersection check with the sides of the asteroid is done,
|
---|
| 2654 | // this can be removed.
|
---|
[1f3d32b] | 2655 | obj->bounding_radius /= 8.0f;
|
---|
| 2656 |
|
---|
| 2657 | return obj;
|
---|
[cf2d1e5] | 2658 | }
|
---|
[5527206] | 2659 |
|
---|
[7e10667] | 2660 | // TODO: Maybe pass in startTime instead of calling glfwGetTime() here
|
---|
| 2661 | ParticleEffect* createExplosion(mat4 model_mat) {
|
---|
[dc19a39] | 2662 | ParticleEffect* obj = new ParticleEffect();
|
---|
[646f3f2] | 2663 |
|
---|
[7e10667] | 2664 | obj->type = TYPE_EXPLOSION;
|
---|
[646f3f2] | 2665 |
|
---|
| 2666 | initObject(obj);
|
---|
[7e10667] | 2667 |
|
---|
[646f3f2] | 2668 | obj->num_points = EXPLOSION_PARTICLE_COUNT;
|
---|
[7e10667] | 2669 | obj->model_base = model_mat;
|
---|
| 2670 | obj->startTime = glfwGetTime();
|
---|
| 2671 | obj->duration = 0.5f; // This is also hard-coded in the shader. TODO; Pass this to the shader in an indexable ubo.
|
---|
| 2672 |
|
---|
| 2673 | obj->particleVelocities.clear();
|
---|
| 2674 | obj->particleTimes.clear();
|
---|
| 2675 | float t_accum = 0.0f; // start time
|
---|
| 2676 |
|
---|
| 2677 | for (int i = 0; i < EXPLOSION_PARTICLE_COUNT; i++) {
|
---|
| 2678 | obj->particleTimes.push_back(t_accum);
|
---|
| 2679 | t_accum += 0.01f;
|
---|
| 2680 |
|
---|
| 2681 | float randx = ((float)rand() / (float)RAND_MAX) - 0.5f;
|
---|
| 2682 | float randy = ((float)rand() / (float)RAND_MAX) - 0.5f;
|
---|
| 2683 | obj->particleVelocities.push_back(randx);
|
---|
| 2684 | obj->particleVelocities.push_back(randy);
|
---|
| 2685 | obj->particleVelocities.push_back(0.0f);
|
---|
| 2686 | }
|
---|
[646f3f2] | 2687 |
|
---|
| 2688 | return obj;
|
---|
| 2689 | }
|
---|