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

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

Create a system to draw and switch between different screens, a Screen class, a MainScreen class that extends it, and some classes for UI elements that can be added to screens.

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