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