source: opengl-game/vulkan-game.hpp@ 20e4c2b

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

In VulkanGame, use ImGui for the UI instead of using SDL to draw elements onto an overlay, and remove everything associated with the overlay pipeline

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