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