[99d44b2] | 1 | #ifndef _VULKAN_GAME_H
|
---|
| 2 | #define _VULKAN_GAME_H
|
---|
[e8ebc76] | 3 |
|
---|
[0807aeb] | 4 | #include <chrono>
|
---|
[e1f88a9] | 5 | #include <map>
|
---|
[0807aeb] | 6 |
|
---|
[60578ce] | 7 | #define GLM_FORCE_RADIANS
|
---|
| 8 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
[a79be34] | 9 | #define GLM_FORCE_RIGHT_HANDED
|
---|
[60578ce] | 10 |
|
---|
[771b33a] | 11 | #include <glm/glm.hpp>
|
---|
[15104a8] | 12 | #include <glm/gtc/matrix_transform.hpp>
|
---|
[771b33a] | 13 |
|
---|
[6bfd91c] | 14 | #include <vulkan/vulkan.h>
|
---|
| 15 |
|
---|
| 16 | #include <SDL2/SDL_ttf.h>
|
---|
| 17 |
|
---|
| 18 | #include "consts.hpp"
|
---|
[4e705d6] | 19 | #include "vulkan-utils.hpp"
|
---|
[7d2b0b9] | 20 | #include "graphics-pipeline_vulkan.hpp"
|
---|
[0df3c9a] | 21 |
|
---|
[4e705d6] | 22 | #include "game-gui-sdl.hpp"
|
---|
[b794178] | 23 |
|
---|
[e1f88a9] | 24 | #include "gui/screen.hpp"
|
---|
| 25 |
|
---|
[15104a8] | 26 | using namespace glm;
|
---|
[0807aeb] | 27 | using namespace std::chrono;
|
---|
[15104a8] | 28 |
|
---|
[2e77b3f] | 29 | #ifdef NDEBUG
|
---|
| 30 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
| 31 | #else
|
---|
| 32 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[5a1ace0] | 35 | struct OverlayVertex {
|
---|
[15104a8] | 36 | vec3 pos;
|
---|
| 37 | vec2 texCoord;
|
---|
[771b33a] | 38 | };
|
---|
| 39 |
|
---|
[5a1ace0] | 40 | struct ModelVertex {
|
---|
[15104a8] | 41 | vec3 pos;
|
---|
[5a1ace0] | 42 | vec3 color;
|
---|
[15104a8] | 43 | vec2 texCoord;
|
---|
[5a1ace0] | 44 | unsigned int objIndex;
|
---|
[15104a8] | 45 | };
|
---|
| 46 |
|
---|
[3782d66] | 47 | struct ShipVertex {
|
---|
| 48 | vec3 pos;
|
---|
| 49 | vec3 color;
|
---|
[06d959f] | 50 | vec3 normal;
|
---|
[cf727ca] | 51 | unsigned int objIndex;
|
---|
[3782d66] | 52 | };
|
---|
| 53 |
|
---|
[3e8cc8b] | 54 | struct AsteroidVertex {
|
---|
| 55 | vec3 pos;
|
---|
| 56 | vec3 color;
|
---|
| 57 | vec3 normal;
|
---|
| 58 | unsigned int objIndex;
|
---|
| 59 | };
|
---|
| 60 |
|
---|
[237cbec] | 61 | struct LaserVertex {
|
---|
| 62 | vec3 pos;
|
---|
| 63 | vec2 texCoord;
|
---|
| 64 | unsigned int objIndex;
|
---|
| 65 | };
|
---|
| 66 |
|
---|
[4a9416a] | 67 | struct ExplosionVertex {
|
---|
| 68 | vec3 particleStartVelocity;
|
---|
| 69 | float particleStartTime;
|
---|
| 70 | unsigned int objIndex;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
[2d87297] | 73 | struct SSBO_ModelObject {
|
---|
[055750a] | 74 | alignas(16) mat4 model;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[2d87297] | 77 | struct SSBO_Asteroid {
|
---|
[3e8cc8b] | 78 | alignas(16) mat4 model;
|
---|
| 79 | alignas(4) float hp;
|
---|
[4ece3bf] | 80 | alignas(4) unsigned int deleted;
|
---|
[3e8cc8b] | 81 | };
|
---|
| 82 |
|
---|
[237cbec] | 83 | struct SSBO_Laser {
|
---|
| 84 | alignas(16) mat4 model;
|
---|
| 85 | alignas(4) vec3 color;
|
---|
| 86 | alignas(4) unsigned int deleted;
|
---|
| 87 | };
|
---|
| 88 |
|
---|
[4a9416a] | 89 | struct SSBO_Explosion {
|
---|
| 90 | alignas(16) mat4 model;
|
---|
| 91 | alignas(4) float explosionStartTime;
|
---|
| 92 | alignas(4) float explosionDuration;
|
---|
| 93 | alignas(4) unsigned int deleted;
|
---|
| 94 | };
|
---|
| 95 |
|
---|
[52a02e6] | 96 | struct UBO_VP_mats {
|
---|
| 97 | alignas(16) mat4 view;
|
---|
| 98 | alignas(16) mat4 proj;
|
---|
| 99 | };
|
---|
| 100 |
|
---|
[4a9416a] | 101 | struct UBO_Explosion {
|
---|
| 102 | alignas(16) mat4 view;
|
---|
| 103 | alignas(16) mat4 proj;
|
---|
| 104 | alignas(4) float cur_time;
|
---|
| 105 | };
|
---|
| 106 |
|
---|
[4994692] | 107 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
| 108 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
| 109 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
| 110 | template<class VertexType, class SSBOType>
|
---|
| 111 | struct SceneObject {
|
---|
| 112 | vector<VertexType> vertices;
|
---|
| 113 | vector<uint16_t> indices;
|
---|
| 114 | SSBOType ssbo;
|
---|
| 115 |
|
---|
| 116 | mat4 model_base;
|
---|
| 117 | mat4 model_transform;
|
---|
| 118 |
|
---|
[5ba732a] | 119 | bool modified;
|
---|
| 120 |
|
---|
[4994692] | 121 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
| 122 | // parent class
|
---|
| 123 | vec3 center; // currently only matters for asteroids
|
---|
| 124 | float radius; // currently only matters for asteroids
|
---|
[3950236] | 125 | SceneObject<AsteroidVertex, SSBO_Asteroid>* targetAsteroid; // currently only used for lasers
|
---|
[4994692] | 126 | };
|
---|
| 127 |
|
---|
| 128 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
[2da64ef] | 129 | // Could probably use the same approach to make indices optional
|
---|
[4994692] | 130 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
| 131 | // them mamdatory
|
---|
[2da64ef] | 132 |
|
---|
[6104594] | 133 | // TODO: Make a singleton timer class instead
|
---|
| 134 | static float curTime;
|
---|
| 135 |
|
---|
[7297892] | 136 |
|
---|
| 137 | // TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
|
---|
| 138 |
|
---|
| 139 | struct BaseEffectOverTime {
|
---|
| 140 | bool deleted;
|
---|
| 141 |
|
---|
| 142 | virtual void applyEffect() = 0;
|
---|
| 143 |
|
---|
| 144 | BaseEffectOverTime() :
|
---|
| 145 | deleted(false) {
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | virtual ~BaseEffectOverTime() {
|
---|
| 149 | }
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | template<class VertexType, class SSBOType>
|
---|
| 153 | struct EffectOverTime : public BaseEffectOverTime {
|
---|
| 154 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline;
|
---|
| 155 | vector<SceneObject<VertexType, SSBOType>>& objects;
|
---|
| 156 | unsigned int objectIndex;
|
---|
| 157 | size_t effectedFieldOffset;
|
---|
| 158 | float startValue;
|
---|
| 159 | float startTime;
|
---|
| 160 | float changePerSecond;
|
---|
| 161 |
|
---|
| 162 | EffectOverTime(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 163 | vector<SceneObject<VertexType, SSBOType>>& objects, unsigned int objectIndex,
|
---|
| 164 | size_t effectedFieldOffset, float changePerSecond) :
|
---|
| 165 | pipeline(pipeline),
|
---|
| 166 | objects(objects),
|
---|
| 167 | objectIndex(objectIndex),
|
---|
| 168 | effectedFieldOffset(effectedFieldOffset),
|
---|
| 169 | startTime(curTime),
|
---|
| 170 | changePerSecond(changePerSecond) {
|
---|
| 171 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
| 172 |
|
---|
| 173 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
| 174 | ssboOffset + effectedFieldOffset;
|
---|
| 175 |
|
---|
| 176 | startValue = *reinterpret_cast<float*>(effectedFieldPtr);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | void applyEffect() {
|
---|
| 180 | if (objects[objectIndex].ssbo.deleted) {
|
---|
| 181 | this->deleted = true;
|
---|
| 182 | return;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
|
---|
| 186 |
|
---|
| 187 | unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
|
---|
| 188 | ssboOffset + effectedFieldOffset;
|
---|
| 189 |
|
---|
| 190 | *reinterpret_cast<float*>(effectedFieldPtr) = startValue + (curTime - startTime) * changePerSecond;
|
---|
| 191 |
|
---|
| 192 | objects[objectIndex].modified = true;
|
---|
| 193 | }
|
---|
| 194 | };
|
---|
| 195 |
|
---|
[99d44b2] | 196 | class VulkanGame {
|
---|
[e8ebc76] | 197 | public:
|
---|
[34bdf3a] | 198 | VulkanGame(int maxFramesInFlight);
|
---|
[99d44b2] | 199 | ~VulkanGame();
|
---|
[0df3c9a] | 200 |
|
---|
[b6e60b4] | 201 | void run(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 202 |
|
---|
[e1f88a9] | 203 | void goToScreen(Screen* screen);
|
---|
| 204 | void quitGame();
|
---|
| 205 |
|
---|
| 206 | map<ScreenType, Screen*> screens;
|
---|
| 207 | Screen* currentScreen;
|
---|
| 208 |
|
---|
[6bfd91c] | 209 | TTF_Font* lazyFont;
|
---|
[e1f88a9] | 210 | TTF_Font* proggyFont;
|
---|
| 211 |
|
---|
[f809ae6] | 212 | int score;
|
---|
| 213 | float fps;
|
---|
| 214 |
|
---|
[4e705d6] | 215 | GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
|
---|
| 216 |
|
---|
| 217 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
---|
| 218 |
|
---|
| 219 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
---|
| 220 |
|
---|
| 221 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
---|
| 222 |
|
---|
| 223 | GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser> laserPipeline;
|
---|
| 224 |
|
---|
| 225 | GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion> explosionPipeline;
|
---|
| 226 |
|
---|
[0df3c9a] | 227 | private:
|
---|
[52a02e6] | 228 | // TODO: Make these consts static
|
---|
[4e705d6] | 229 | // Also, maybe move them into consts.hpp
|
---|
[52a02e6] | 230 |
|
---|
[34bdf3a] | 231 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
| 232 |
|
---|
[5ab1b20] | 233 | const float NEAR_CLIP = 0.1f;
|
---|
| 234 | const float FAR_CLIP = 100.0f;
|
---|
[60578ce] | 235 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
[5ab1b20] | 236 |
|
---|
[4a9416a] | 237 | const int EXPLOSION_PARTICLE_COUNT = 300;
|
---|
| 238 | const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f);
|
---|
| 239 |
|
---|
[e1f88a9] | 240 | bool quit;
|
---|
| 241 |
|
---|
[15104a8] | 242 | vec3 cam_pos;
|
---|
| 243 |
|
---|
[4e705d6] | 244 | // TODO: Good place to start using smart pointers
|
---|
[0df3c9a] | 245 | GameGui* gui;
|
---|
[c559904] | 246 |
|
---|
| 247 | SDL_version sdlVersion;
|
---|
[b794178] | 248 | SDL_Window* window = nullptr;
|
---|
| 249 | SDL_Renderer* renderer = nullptr;
|
---|
| 250 |
|
---|
| 251 | SDL_Texture* uiOverlay = nullptr;
|
---|
[c1d9b2a] | 252 |
|
---|
| 253 | VkInstance instance;
|
---|
| 254 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
[fe5c3ba] | 255 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
[90a424f] | 256 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
[c1c2021] | 257 | VkDevice device;
|
---|
| 258 |
|
---|
| 259 | VkQueue graphicsQueue;
|
---|
| 260 | VkQueue presentQueue;
|
---|
[0df3c9a] | 261 |
|
---|
[502bd0b] | 262 | VkSwapchainKHR swapChain;
|
---|
| 263 | vector<VkImage> swapChainImages;
|
---|
| 264 | VkFormat swapChainImageFormat;
|
---|
[603b5bc] | 265 | VkExtent2D swapChainExtent;
|
---|
[f94eea9] | 266 | vector<VkImageView> swapChainImageViews;
|
---|
[603b5bc] | 267 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
[fa9fa1c] | 268 |
|
---|
[6fc24c7] | 269 | VkRenderPass renderPass;
|
---|
[fa9fa1c] | 270 | VkCommandPool commandPool;
|
---|
[603b5bc] | 271 | vector<VkCommandBuffer> commandBuffers;
|
---|
[502bd0b] | 272 |
|
---|
[603b5bc] | 273 | VulkanImage depthImage;
|
---|
[b794178] | 274 |
|
---|
[4e705d6] | 275 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
| 276 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
| 277 | vector<VkFence> inFlightFences;
|
---|
| 278 |
|
---|
| 279 | size_t currentFrame;
|
---|
| 280 |
|
---|
| 281 | bool framebufferResized;
|
---|
| 282 |
|
---|
[b794178] | 283 | VkSampler textureSampler;
|
---|
| 284 |
|
---|
[0fe8433] | 285 | VulkanImage sdlOverlayImage;
|
---|
| 286 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
| 287 |
|
---|
[4994692] | 288 | VulkanImage floorTextureImage;
|
---|
| 289 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
| 290 |
|
---|
[237cbec] | 291 | VulkanImage laserTextureImage;
|
---|
| 292 | VkDescriptorImageInfo laserTextureImageDescriptor;
|
---|
| 293 |
|
---|
[22217d4] | 294 | mat4 viewMat, projMat;
|
---|
| 295 |
|
---|
[860a0da] | 296 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
| 297 | // per pipeline.
|
---|
| 298 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
| 299 | // the objects vector, the ubo, and the ssbo
|
---|
| 300 |
|
---|
[2ba5617] | 301 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
| 302 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
| 303 |
|
---|
[4e705d6] | 304 | vector<SceneObject<OverlayVertex, void*>> overlayObjects;
|
---|
| 305 |
|
---|
[2d87297] | 306 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
[0fe8433] | 307 |
|
---|
[d25381b] | 308 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
---|
| 309 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
---|
| 310 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
---|
[b794178] | 311 |
|
---|
[0fe8433] | 312 | UBO_VP_mats object_VP_mats;
|
---|
| 313 |
|
---|
[2d87297] | 314 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
---|
[0fe8433] | 315 |
|
---|
[3782d66] | 316 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
| 317 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
| 318 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
| 319 |
|
---|
[0fe8433] | 320 | UBO_VP_mats ship_VP_mats;
|
---|
[0e09340] | 321 |
|
---|
[2d87297] | 322 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
[3e8cc8b] | 323 |
|
---|
| 324 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
---|
| 325 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
---|
| 326 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
---|
| 327 |
|
---|
| 328 | UBO_VP_mats asteroid_VP_mats;
|
---|
| 329 |
|
---|
[237cbec] | 330 | vector<SceneObject<LaserVertex, SSBO_Laser>> laserObjects;
|
---|
| 331 |
|
---|
| 332 | vector<VkBuffer> uniformBuffers_laserPipeline;
|
---|
| 333 | vector<VkDeviceMemory> uniformBuffersMemory_laserPipeline;
|
---|
| 334 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_laserPipeline;
|
---|
| 335 |
|
---|
| 336 | UBO_VP_mats laser_VP_mats;
|
---|
| 337 |
|
---|
[4a9416a] | 338 | vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects;
|
---|
| 339 |
|
---|
| 340 | vector<VkBuffer> uniformBuffers_explosionPipeline;
|
---|
| 341 | vector<VkDeviceMemory> uniformBuffersMemory_explosionPipeline;
|
---|
| 342 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_explosionPipeline;
|
---|
| 343 |
|
---|
| 344 | UBO_Explosion explosion_UBO;
|
---|
| 345 |
|
---|
[7297892] | 346 | vector<BaseEffectOverTime*> effects;
|
---|
| 347 |
|
---|
[0807aeb] | 348 | time_point<steady_clock> startTime;
|
---|
[6104594] | 349 | float prevTime, elapsedTime;
|
---|
[0807aeb] | 350 |
|
---|
[f809ae6] | 351 | float fpsStartTime;
|
---|
| 352 | int frameCount;
|
---|
| 353 |
|
---|
[0807aeb] | 354 | float shipSpeed = 0.5f;
|
---|
| 355 | float asteroidSpeed = 2.0f;
|
---|
| 356 |
|
---|
| 357 | float spawnRate_asteroid = 0.5;
|
---|
| 358 | float lastSpawn_asteroid;
|
---|
[4ece3bf] | 359 |
|
---|
[1f81ecc] | 360 | unsigned int leftLaserIdx = -1;
|
---|
[7297892] | 361 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
|
---|
[1f81ecc] | 362 |
|
---|
| 363 | unsigned int rightLaserIdx = -1;
|
---|
[7297892] | 364 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
|
---|
[1f81ecc] | 365 |
|
---|
[4e705d6] | 366 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 367 | void initVulkan();
|
---|
[f97c5e7] | 368 | void initGraphicsPipelines();
|
---|
[15104a8] | 369 | void initMatrices();
|
---|
[0df3c9a] | 370 | void mainLoop();
|
---|
[8e02b6b] | 371 | void updateScene(uint32_t currentImage);
|
---|
[a0c5f28] | 372 | void renderScene();
|
---|
[0df3c9a] | 373 | void cleanup();
|
---|
[c1d9b2a] | 374 |
|
---|
| 375 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
| 376 | void setupDebugMessenger();
|
---|
| 377 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
[90a424f] | 378 | void createVulkanSurface();
|
---|
[fe5c3ba] | 379 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[fa9fa1c] | 380 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
[c1c2021] | 381 | void createLogicalDevice(
|
---|
| 382 | const vector<const char*> validationLayers,
|
---|
| 383 | const vector<const char*>& deviceExtensions);
|
---|
[502bd0b] | 384 | void createSwapChain();
|
---|
[f94eea9] | 385 | void createImageViews();
|
---|
[6fc24c7] | 386 | void createRenderPass();
|
---|
| 387 | VkFormat findDepthFormat();
|
---|
[fa9fa1c] | 388 | void createCommandPool();
|
---|
[603b5bc] | 389 | void createImageResources();
|
---|
| 390 |
|
---|
[b794178] | 391 | void createTextureSampler();
|
---|
[603b5bc] | 392 | void createFramebuffers();
|
---|
| 393 | void createCommandBuffers();
|
---|
[34bdf3a] | 394 | void createSyncObjects();
|
---|
[f94eea9] | 395 |
|
---|
[4994692] | 396 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
| 397 | // stop using objects.back() to access the object that was just created
|
---|
[2d87297] | 398 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 399 | SceneObject<VertexType, SSBOType>& addObject(
|
---|
| 400 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 401 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 402 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 403 | bool pipelinesCreated);
|
---|
[0fe8433] | 404 |
|
---|
[2da64ef] | 405 | template<class VertexType, class SSBOType>
|
---|
| 406 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[4994692] | 407 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
---|
[2da64ef] | 408 |
|
---|
[1f81ecc] | 409 | template<class VertexType, class SSBOType>
|
---|
| 410 | void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 411 | SceneObject<VertexType, SSBOType>& obj, size_t index);
|
---|
| 412 |
|
---|
[06d959f] | 413 | template<class VertexType>
|
---|
| 414 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
| 415 |
|
---|
[cf727ca] | 416 | template<class VertexType>
|
---|
| 417 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
| 418 |
|
---|
[3b84bb6] | 419 | template<class VertexType, class SSBOType>
|
---|
| 420 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
[a79be34] | 421 |
|
---|
[52a02e6] | 422 | void addLaser(vec3 start, vec3 end, vec3 color, float width);
|
---|
| 423 | void translateLaser(size_t index, const vec3& translation);
|
---|
| 424 | void updateLaserTarget(size_t index);
|
---|
| 425 | bool getLaserAndAsteroidIntersection(SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid,
|
---|
| 426 | vec3& start, vec3& end, vec3& intersection);
|
---|
| 427 |
|
---|
[4a9416a] | 428 | void addExplosion(mat4 model_mat, float duration, float cur_time);
|
---|
| 429 |
|
---|
[055750a] | 430 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
[4994692] | 431 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
| 432 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
[f97c5e7] | 433 |
|
---|
[d2d9286] | 434 | void recreateSwapChain();
|
---|
| 435 |
|
---|
[c1c2021] | 436 | void cleanupSwapChain();
|
---|
[c1d9b2a] | 437 |
|
---|
| 438 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 439 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 440 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 441 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 442 | void* pUserData);
|
---|
[e8ebc76] | 443 | };
|
---|
| 444 |
|
---|
[4a9416a] | 445 | // Start of specialized no-op functions
|
---|
| 446 |
|
---|
| 447 | template<>
|
---|
| 448 | inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) {
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | // End of specialized no-op functions
|
---|
| 452 |
|
---|
[3b84bb6] | 453 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
---|
| 454 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
---|
| 455 | // Figure out a better way to allow the model matrix to be set during objecting creation
|
---|
[2ba5617] | 456 |
|
---|
| 457 | // TODO: Maybe return a reference to the object from this method if I decide that updating it
|
---|
| 458 | // immediately after creation is a good idea (such as setting model_base)
|
---|
| 459 | // Currently, model_base is set like this in a few places and the radius is set for asteroids
|
---|
| 460 | // to account for scaling
|
---|
[2d87297] | 461 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 462 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
|
---|
| 463 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[2d87297] | 464 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
[3b84bb6] | 465 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 466 | bool pipelinesCreated) {
|
---|
[2ba5617] | 467 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
| 468 | // currently, the passed in model is useless since it gets overridden in updateObject() anyway
|
---|
[0fe8433] | 469 | size_t numVertices = pipeline.getNumVertices();
|
---|
| 470 |
|
---|
| 471 | for (uint16_t& idx : indices) {
|
---|
| 472 | idx += numVertices;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[5ba732a] | 475 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f), false });
|
---|
[3b84bb6] | 476 |
|
---|
[2ba5617] | 477 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
[1f81ecc] | 478 |
|
---|
[4a9416a] | 479 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
[1f81ecc] | 480 | centerObject(obj);
|
---|
| 481 | }
|
---|
[2ba5617] | 482 |
|
---|
[4994692] | 483 | bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
|
---|
| 484 | this->commandPool, this->graphicsQueue);
|
---|
[0fe8433] | 485 |
|
---|
[3b84bb6] | 486 | if (pipelinesCreated) {
|
---|
[44f23af] | 487 | vkDeviceWaitIdle(device);
|
---|
| 488 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
| 489 |
|
---|
| 490 | // TODO: The pipeline recreation only has to be done once per frame where at least
|
---|
| 491 | // one SSBO is resized.
|
---|
| 492 | // Refactor the logic to check for any resized SSBOs after all objects for the frame
|
---|
| 493 | // are created and then recreate each of the corresponding pipelines only once per frame
|
---|
[3b84bb6] | 494 | if (storageBufferResized) {
|
---|
[44f23af] | 495 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
---|
| 496 | pipeline.createDescriptorPool(swapChainImages);
|
---|
| 497 | pipeline.createDescriptorSets(swapChainImages);
|
---|
[3b84bb6] | 498 | }
|
---|
| 499 |
|
---|
| 500 | createCommandBuffers();
|
---|
| 501 | }
|
---|
[4994692] | 502 |
|
---|
| 503 | return obj;
|
---|
[0fe8433] | 504 | }
|
---|
| 505 |
|
---|
[0807aeb] | 506 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
[2da64ef] | 507 | template<class VertexType, class SSBOType>
|
---|
| 508 | void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 509 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
|
---|
| 510 | SceneObject<VertexType, SSBOType>& obj = objects[index];
|
---|
| 511 |
|
---|
| 512 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
---|
[2ba5617] | 513 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
[2da64ef] | 514 |
|
---|
| 515 | pipeline.updateObject(index, obj.ssbo);
|
---|
[5ba732a] | 516 |
|
---|
| 517 | obj.modified = false;
|
---|
[2da64ef] | 518 | }
|
---|
| 519 |
|
---|
[1f81ecc] | 520 | template<class VertexType, class SSBOType>
|
---|
| 521 | void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 522 | SceneObject<VertexType, SSBOType>& obj, size_t index) {
|
---|
| 523 | pipeline.updateObjectVertices(index, obj.vertices, this->commandPool, this->graphicsQueue);
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[06d959f] | 526 | template<class VertexType>
|
---|
| 527 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
| 528 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
| 529 | vec3 p1 = vertices[i].pos;
|
---|
| 530 | vec3 p2 = vertices[i+1].pos;
|
---|
| 531 | vec3 p3 = vertices[i+2].pos;
|
---|
| 532 |
|
---|
[a79be34] | 533 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
[06d959f] | 534 |
|
---|
| 535 | // Add the same normal for all 3 vertices
|
---|
| 536 | vertices[i].normal = normal;
|
---|
| 537 | vertices[i+1].normal = normal;
|
---|
| 538 | vertices[i+2].normal = normal;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | return vertices;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[cf727ca] | 544 | template<class VertexType>
|
---|
| 545 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
| 546 | for (VertexType& vertex : vertices) {
|
---|
| 547 | vertex.objIndex = objIndex;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | return vertices;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
[3b84bb6] | 553 | template<class VertexType, class SSBOType>
|
---|
| 554 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
| 555 | vector<VertexType>& vertices = object.vertices;
|
---|
| 556 |
|
---|
[a79be34] | 557 | float min_x = vertices[0].pos.x;
|
---|
| 558 | float max_x = vertices[0].pos.x;
|
---|
| 559 | float min_y = vertices[0].pos.y;
|
---|
| 560 | float max_y = vertices[0].pos.y;
|
---|
| 561 | float min_z = vertices[0].pos.z;
|
---|
| 562 | float max_z = vertices[0].pos.z;
|
---|
| 563 |
|
---|
| 564 | // start from the second point
|
---|
| 565 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
[3b84bb6] | 566 | vec3& pos = vertices[i].pos;
|
---|
| 567 |
|
---|
| 568 | if (min_x > pos.x) {
|
---|
| 569 | min_x = pos.x;
|
---|
| 570 | } else if (max_x < pos.x) {
|
---|
| 571 | max_x = pos.x;
|
---|
[a79be34] | 572 | }
|
---|
| 573 |
|
---|
[3b84bb6] | 574 | if (min_y > pos.y) {
|
---|
| 575 | min_y = pos.y;
|
---|
| 576 | } else if (max_y < pos.y) {
|
---|
| 577 | max_y = pos.y;
|
---|
[a79be34] | 578 | }
|
---|
| 579 |
|
---|
[3b84bb6] | 580 | if (min_z > pos.z) {
|
---|
| 581 | min_z = pos.z;
|
---|
| 582 | } else if (max_z < pos.z) {
|
---|
| 583 | max_z = pos.z;
|
---|
[a79be34] | 584 | }
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
| 588 |
|
---|
| 589 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
| 590 | vertices[i].pos -= center;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
[2ba5617] | 593 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
| 594 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
| 595 |
|
---|
[3b84bb6] | 596 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
[a79be34] | 597 | }
|
---|
| 598 |
|
---|
[3b84bb6] | 599 | #endif // _VULKAN_GAME_H
|
---|