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