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