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

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

Rename initWindow to initUI and move code for initializing the UI overlay into it

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