source: opengl-game/vulkan-game.hpp@ 4a9416a

feature/imgui-sdl
Last change on this file since 4a9416a was 4a9416a, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Create a pipeline and shaders to render explosions

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