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