source: opengl-game/vulkan-game.hpp@ a00eb06

feature/imgui-sdl
Last change on this file since a00eb06 was a00eb06, checked in by Dmitry Portnoy <dportnoy@…>, 4 years ago

In VulkanGame, add a normal varying attribute to ModelVertex

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