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