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