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