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