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