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