source: opengl-game/vulkan-game.hpp@ 699e83a

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

Add a GameScreen class to render the main gameplay

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