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