1 | #ifndef _VULKAN_GAME_H
|
---|
2 | #define _VULKAN_GAME_H
|
---|
3 |
|
---|
4 | #include <chrono>
|
---|
5 |
|
---|
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
|
---|
8 | #define GLM_FORCE_RIGHT_HANDED
|
---|
9 |
|
---|
10 | #include <glm/glm.hpp>
|
---|
11 | #include <glm/gtc/matrix_transform.hpp>
|
---|
12 |
|
---|
13 | #include "game-gui-sdl.hpp"
|
---|
14 | #include "graphics-pipeline_vulkan.hpp"
|
---|
15 |
|
---|
16 | #include "vulkan-utils.hpp"
|
---|
17 |
|
---|
18 | using namespace glm;
|
---|
19 | using namespace std::chrono;
|
---|
20 |
|
---|
21 | #ifdef NDEBUG
|
---|
22 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
23 | #else
|
---|
24 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | struct OverlayVertex {
|
---|
28 | vec3 pos;
|
---|
29 | vec2 texCoord;
|
---|
30 | };
|
---|
31 |
|
---|
32 | struct ModelVertex {
|
---|
33 | vec3 pos;
|
---|
34 | vec3 color;
|
---|
35 | vec2 texCoord;
|
---|
36 | unsigned int objIndex;
|
---|
37 | };
|
---|
38 |
|
---|
39 | struct ShipVertex {
|
---|
40 | vec3 pos;
|
---|
41 | vec3 color;
|
---|
42 | vec3 normal;
|
---|
43 | unsigned int objIndex;
|
---|
44 | };
|
---|
45 |
|
---|
46 | struct AsteroidVertex {
|
---|
47 | vec3 pos;
|
---|
48 | vec3 color;
|
---|
49 | vec3 normal;
|
---|
50 | unsigned int objIndex;
|
---|
51 | };
|
---|
52 |
|
---|
53 | struct LaserVertex {
|
---|
54 | vec3 pos;
|
---|
55 | vec2 texCoord;
|
---|
56 | unsigned int objIndex;
|
---|
57 | };
|
---|
58 |
|
---|
59 | struct ExplosionVertex {
|
---|
60 | vec3 particleStartVelocity;
|
---|
61 | float particleStartTime;
|
---|
62 | unsigned int objIndex;
|
---|
63 | };
|
---|
64 |
|
---|
65 | struct SSBO_ModelObject {
|
---|
66 | alignas(16) mat4 model;
|
---|
67 | };
|
---|
68 |
|
---|
69 | struct SSBO_Asteroid {
|
---|
70 | alignas(16) mat4 model;
|
---|
71 | alignas(4) float hp;
|
---|
72 | alignas(4) unsigned int deleted;
|
---|
73 | };
|
---|
74 |
|
---|
75 | struct SSBO_Laser {
|
---|
76 | alignas(16) mat4 model;
|
---|
77 | alignas(4) vec3 color;
|
---|
78 | alignas(4) unsigned int deleted;
|
---|
79 | };
|
---|
80 |
|
---|
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 |
|
---|
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 |
|
---|
111 | bool modified;
|
---|
112 |
|
---|
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
|
---|
117 | SceneObject<AsteroidVertex, SSBO_Asteroid>* targetAsteroid; // currently only used for lasers
|
---|
118 | };
|
---|
119 |
|
---|
120 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
121 | // Could probably use the same approach to make indices optional
|
---|
122 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
123 | // them mamdatory
|
---|
124 |
|
---|
125 | // TODO: Make a singleton timer class instead
|
---|
126 | static float curTime;
|
---|
127 |
|
---|
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 |
|
---|
188 | class VulkanGame {
|
---|
189 | public:
|
---|
190 | VulkanGame(int maxFramesInFlight);
|
---|
191 | ~VulkanGame();
|
---|
192 |
|
---|
193 | void run(int width, int height, unsigned char guiFlags);
|
---|
194 |
|
---|
195 | private:
|
---|
196 | // TODO: Make these consts static
|
---|
197 |
|
---|
198 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
199 |
|
---|
200 | const float NEAR_CLIP = 0.1f;
|
---|
201 | const float FAR_CLIP = 100.0f;
|
---|
202 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
203 |
|
---|
204 | const int EXPLOSION_PARTICLE_COUNT = 300;
|
---|
205 |
|
---|
206 | vec3 cam_pos;
|
---|
207 |
|
---|
208 | GameGui* gui;
|
---|
209 |
|
---|
210 | SDL_version sdlVersion;
|
---|
211 | SDL_Window* window = nullptr;
|
---|
212 | SDL_Renderer* renderer = nullptr;
|
---|
213 |
|
---|
214 | SDL_Texture* uiOverlay = nullptr;
|
---|
215 |
|
---|
216 | VkInstance instance;
|
---|
217 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
218 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
219 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
220 | VkDevice device;
|
---|
221 |
|
---|
222 | VkQueue graphicsQueue;
|
---|
223 | VkQueue presentQueue;
|
---|
224 |
|
---|
225 | VkSwapchainKHR swapChain;
|
---|
226 | vector<VkImage> swapChainImages;
|
---|
227 | VkFormat swapChainImageFormat;
|
---|
228 | VkExtent2D swapChainExtent;
|
---|
229 | vector<VkImageView> swapChainImageViews;
|
---|
230 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
231 |
|
---|
232 | VkRenderPass renderPass;
|
---|
233 | VkCommandPool commandPool;
|
---|
234 | vector<VkCommandBuffer> commandBuffers;
|
---|
235 |
|
---|
236 | VulkanImage depthImage;
|
---|
237 |
|
---|
238 | VkSampler textureSampler;
|
---|
239 |
|
---|
240 | VulkanImage sdlOverlayImage;
|
---|
241 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
242 |
|
---|
243 | VulkanImage floorTextureImage;
|
---|
244 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
245 |
|
---|
246 | VulkanImage laserTextureImage;
|
---|
247 | VkDescriptorImageInfo laserTextureImageDescriptor;
|
---|
248 |
|
---|
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 |
|
---|
262 | mat4 viewMat, projMat;
|
---|
263 |
|
---|
264 | GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
|
---|
265 | vector<SceneObject<OverlayVertex, void*>> overlayObjects;
|
---|
266 |
|
---|
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 |
|
---|
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 |
|
---|
275 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
---|
276 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
277 |
|
---|
278 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
---|
279 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
---|
280 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
---|
281 |
|
---|
282 | UBO_VP_mats object_VP_mats;
|
---|
283 |
|
---|
284 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
---|
285 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
---|
286 |
|
---|
287 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
288 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
289 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
290 |
|
---|
291 | UBO_VP_mats ship_VP_mats;
|
---|
292 |
|
---|
293 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
---|
294 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
320 | vector<BaseEffectOverTime*> effects;
|
---|
321 |
|
---|
322 | time_point<steady_clock> startTime;
|
---|
323 | float prevTime, elapsedTime;
|
---|
324 |
|
---|
325 | float shipSpeed = 0.5f;
|
---|
326 | float asteroidSpeed = 2.0f;
|
---|
327 |
|
---|
328 | float spawnRate_asteroid = 0.5;
|
---|
329 | float lastSpawn_asteroid;
|
---|
330 |
|
---|
331 | unsigned int leftLaserIdx = -1;
|
---|
332 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
|
---|
333 |
|
---|
334 | unsigned int rightLaserIdx = -1;
|
---|
335 | EffectOverTime<AsteroidVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
|
---|
336 |
|
---|
337 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
338 | void initVulkan();
|
---|
339 | void initGraphicsPipelines();
|
---|
340 | void initMatrices();
|
---|
341 | void mainLoop();
|
---|
342 | void updateScene(uint32_t currentImage);
|
---|
343 | void renderUI();
|
---|
344 | void renderScene();
|
---|
345 | void cleanup();
|
---|
346 |
|
---|
347 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
348 | void setupDebugMessenger();
|
---|
349 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
350 | void createVulkanSurface();
|
---|
351 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
352 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
353 | void createLogicalDevice(
|
---|
354 | const vector<const char*> validationLayers,
|
---|
355 | const vector<const char*>& deviceExtensions);
|
---|
356 | void createSwapChain();
|
---|
357 | void createImageViews();
|
---|
358 | void createRenderPass();
|
---|
359 | VkFormat findDepthFormat();
|
---|
360 | void createCommandPool();
|
---|
361 | void createImageResources();
|
---|
362 |
|
---|
363 | void createTextureSampler();
|
---|
364 | void createFramebuffers();
|
---|
365 | void createCommandBuffers();
|
---|
366 | void createSyncObjects();
|
---|
367 |
|
---|
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
|
---|
370 | template<class VertexType, class SSBOType>
|
---|
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);
|
---|
376 |
|
---|
377 | template<class VertexType, class SSBOType>
|
---|
378 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
379 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
---|
380 |
|
---|
381 | template<class VertexType, class SSBOType>
|
---|
382 | void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
383 | SceneObject<VertexType, SSBOType>& obj, size_t index);
|
---|
384 |
|
---|
385 | template<class VertexType>
|
---|
386 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
387 |
|
---|
388 | template<class VertexType>
|
---|
389 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
390 |
|
---|
391 | template<class VertexType, class SSBOType>
|
---|
392 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
393 |
|
---|
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 |
|
---|
402 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
403 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
404 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
405 |
|
---|
406 | void recreateSwapChain();
|
---|
407 |
|
---|
408 | void cleanupSwapChain();
|
---|
409 |
|
---|
410 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
411 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
412 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
413 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
414 | void* pUserData);
|
---|
415 | };
|
---|
416 |
|
---|
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 |
|
---|
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
|
---|
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
|
---|
433 | template<class VertexType, class SSBOType>
|
---|
434 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
|
---|
435 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
436 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
437 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
438 | bool pipelinesCreated) {
|
---|
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
|
---|
441 | size_t numVertices = pipeline.getNumVertices();
|
---|
442 |
|
---|
443 | for (uint16_t& idx : indices) {
|
---|
444 | idx += numVertices;
|
---|
445 | }
|
---|
446 |
|
---|
447 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f), false });
|
---|
448 |
|
---|
449 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
450 |
|
---|
451 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
452 | centerObject(obj);
|
---|
453 | }
|
---|
454 |
|
---|
455 | bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
|
---|
456 | this->commandPool, this->graphicsQueue);
|
---|
457 |
|
---|
458 | if (pipelinesCreated) {
|
---|
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
|
---|
466 | if (storageBufferResized) {
|
---|
467 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
---|
468 | pipeline.createDescriptorPool(swapChainImages);
|
---|
469 | pipeline.createDescriptorSets(swapChainImages);
|
---|
470 | }
|
---|
471 |
|
---|
472 | createCommandBuffers();
|
---|
473 | }
|
---|
474 |
|
---|
475 | return obj;
|
---|
476 | }
|
---|
477 |
|
---|
478 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
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;
|
---|
485 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
486 |
|
---|
487 | pipeline.updateObject(index, obj.ssbo);
|
---|
488 |
|
---|
489 | obj.modified = false;
|
---|
490 | }
|
---|
491 |
|
---|
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 |
|
---|
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 |
|
---|
505 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
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 |
|
---|
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 |
|
---|
525 | template<class VertexType, class SSBOType>
|
---|
526 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
527 | vector<VertexType>& vertices = object.vertices;
|
---|
528 |
|
---|
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++) {
|
---|
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;
|
---|
544 | }
|
---|
545 |
|
---|
546 | if (min_y > pos.y) {
|
---|
547 | min_y = pos.y;
|
---|
548 | } else if (max_y < pos.y) {
|
---|
549 | max_y = pos.y;
|
---|
550 | }
|
---|
551 |
|
---|
552 | if (min_z > pos.z) {
|
---|
553 | min_z = pos.z;
|
---|
554 | } else if (max_z < pos.z) {
|
---|
555 | max_z = pos.z;
|
---|
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 |
|
---|
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 |
|
---|
568 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
569 | }
|
---|
570 |
|
---|
571 | #endif // _VULKAN_GAME_H
|
---|