[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] | 18 | using namespace glm;
|
---|
[0807aeb] | 19 | using 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] | 27 | struct OverlayVertex {
|
---|
[15104a8] | 28 | vec3 pos;
|
---|
| 29 | vec2 texCoord;
|
---|
[771b33a] | 30 | };
|
---|
| 31 |
|
---|
[5a1ace0] | 32 | struct ModelVertex {
|
---|
[15104a8] | 33 | vec3 pos;
|
---|
[5a1ace0] | 34 | vec3 color;
|
---|
[15104a8] | 35 | vec2 texCoord;
|
---|
[5a1ace0] | 36 | unsigned int objIndex;
|
---|
[15104a8] | 37 | };
|
---|
| 38 |
|
---|
[3782d66] | 39 | struct ShipVertex {
|
---|
| 40 | vec3 pos;
|
---|
| 41 | vec3 color;
|
---|
[06d959f] | 42 | vec3 normal;
|
---|
[cf727ca] | 43 | unsigned int objIndex;
|
---|
[3782d66] | 44 | };
|
---|
| 45 |
|
---|
[3e8cc8b] | 46 | struct AsteroidVertex {
|
---|
| 47 | vec3 pos;
|
---|
| 48 | vec3 color;
|
---|
| 49 | vec3 normal;
|
---|
| 50 | unsigned int objIndex;
|
---|
| 51 | };
|
---|
| 52 |
|
---|
[055750a] | 53 | struct UBO_VP_mats {
|
---|
[15104a8] | 54 | alignas(16) mat4 view;
|
---|
| 55 | alignas(16) mat4 proj;
|
---|
[771b33a] | 56 | };
|
---|
| 57 |
|
---|
[2d87297] | 58 | struct SSBO_ModelObject {
|
---|
[055750a] | 59 | alignas(16) mat4 model;
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[2d87297] | 62 | struct SSBO_Asteroid {
|
---|
[3e8cc8b] | 63 | alignas(16) mat4 model;
|
---|
| 64 | alignas(4) float hp;
|
---|
[4ece3bf] | 65 | alignas(4) unsigned int deleted;
|
---|
[3e8cc8b] | 66 | };
|
---|
| 67 |
|
---|
[4994692] | 68 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
| 69 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
| 70 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
| 71 | template<class VertexType, class SSBOType>
|
---|
| 72 | struct SceneObject {
|
---|
| 73 | vector<VertexType> vertices;
|
---|
| 74 | vector<uint16_t> indices;
|
---|
| 75 | SSBOType ssbo;
|
---|
| 76 |
|
---|
| 77 | mat4 model_base;
|
---|
| 78 | mat4 model_transform;
|
---|
| 79 |
|
---|
| 80 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
| 81 | // parent class
|
---|
| 82 | vec3 center; // currently only matters for asteroids
|
---|
| 83 | float radius; // currently only matters for asteroids
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
[2da64ef] | 87 | // Could probably use the same approach to make indices optional
|
---|
[4994692] | 88 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
| 89 | // them mamdatory
|
---|
[2da64ef] | 90 |
|
---|
[99d44b2] | 91 | class VulkanGame {
|
---|
[e8ebc76] | 92 | public:
|
---|
[34bdf3a] | 93 | VulkanGame(int maxFramesInFlight);
|
---|
[99d44b2] | 94 | ~VulkanGame();
|
---|
[0df3c9a] | 95 |
|
---|
[b6e60b4] | 96 | void run(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 97 |
|
---|
| 98 | private:
|
---|
[34bdf3a] | 99 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
| 100 |
|
---|
[5ab1b20] | 101 | const float NEAR_CLIP = 0.1f;
|
---|
| 102 | const float FAR_CLIP = 100.0f;
|
---|
[60578ce] | 103 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
|
---|
[5ab1b20] | 104 |
|
---|
[15104a8] | 105 | vec3 cam_pos;
|
---|
| 106 |
|
---|
[0df3c9a] | 107 | GameGui* gui;
|
---|
[c559904] | 108 |
|
---|
| 109 | SDL_version sdlVersion;
|
---|
[b794178] | 110 | SDL_Window* window = nullptr;
|
---|
| 111 | SDL_Renderer* renderer = nullptr;
|
---|
| 112 |
|
---|
| 113 | SDL_Texture* uiOverlay = nullptr;
|
---|
[c1d9b2a] | 114 |
|
---|
| 115 | VkInstance instance;
|
---|
| 116 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
[fe5c3ba] | 117 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
[90a424f] | 118 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
[c1c2021] | 119 | VkDevice device;
|
---|
| 120 |
|
---|
| 121 | VkQueue graphicsQueue;
|
---|
| 122 | VkQueue presentQueue;
|
---|
[0df3c9a] | 123 |
|
---|
[502bd0b] | 124 | VkSwapchainKHR swapChain;
|
---|
| 125 | vector<VkImage> swapChainImages;
|
---|
| 126 | VkFormat swapChainImageFormat;
|
---|
[603b5bc] | 127 | VkExtent2D swapChainExtent;
|
---|
[f94eea9] | 128 | vector<VkImageView> swapChainImageViews;
|
---|
[603b5bc] | 129 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
[fa9fa1c] | 130 |
|
---|
[6fc24c7] | 131 | VkRenderPass renderPass;
|
---|
[fa9fa1c] | 132 | VkCommandPool commandPool;
|
---|
[603b5bc] | 133 | vector<VkCommandBuffer> commandBuffers;
|
---|
[502bd0b] | 134 |
|
---|
[603b5bc] | 135 | VulkanImage depthImage;
|
---|
[b794178] | 136 |
|
---|
| 137 | VkSampler textureSampler;
|
---|
| 138 |
|
---|
[0fe8433] | 139 | VulkanImage sdlOverlayImage;
|
---|
| 140 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
| 141 |
|
---|
[4994692] | 142 | VulkanImage floorTextureImage;
|
---|
| 143 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
| 144 |
|
---|
[0fe8433] | 145 | TTF_Font* font;
|
---|
| 146 | SDL_Texture* fontSDLTexture;
|
---|
| 147 |
|
---|
| 148 | SDL_Texture* imageSDLTexture;
|
---|
| 149 |
|
---|
| 150 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
| 151 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
| 152 | vector<VkFence> inFlightFences;
|
---|
| 153 |
|
---|
| 154 | size_t currentFrame;
|
---|
| 155 |
|
---|
| 156 | bool framebufferResized;
|
---|
| 157 |
|
---|
[22217d4] | 158 | mat4 viewMat, projMat;
|
---|
| 159 |
|
---|
[2d87297] | 160 | GraphicsPipeline_Vulkan<OverlayVertex, void*> overlayPipeline;
|
---|
| 161 | vector<SceneObject<OverlayVertex, void*>> overlayObjects;
|
---|
[0fe8433] | 162 |
|
---|
[860a0da] | 163 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
| 164 | // per pipeline.
|
---|
| 165 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
| 166 | // the objects vector, the ubo, and the ssbo
|
---|
| 167 |
|
---|
[2ba5617] | 168 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
| 169 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
| 170 |
|
---|
[2d87297] | 171 | GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
|
---|
| 172 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
[0fe8433] | 173 |
|
---|
[d25381b] | 174 | vector<VkBuffer> uniformBuffers_modelPipeline;
|
---|
| 175 | vector<VkDeviceMemory> uniformBuffersMemory_modelPipeline;
|
---|
| 176 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_modelPipeline;
|
---|
[b794178] | 177 |
|
---|
[0fe8433] | 178 | UBO_VP_mats object_VP_mats;
|
---|
| 179 |
|
---|
[2d87297] | 180 | GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline;
|
---|
| 181 | vector<SceneObject<ShipVertex, SSBO_ModelObject>> shipObjects;
|
---|
[0fe8433] | 182 |
|
---|
[3782d66] | 183 | vector<VkBuffer> uniformBuffers_shipPipeline;
|
---|
| 184 | vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
|
---|
| 185 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
|
---|
| 186 |
|
---|
[0fe8433] | 187 | UBO_VP_mats ship_VP_mats;
|
---|
[0e09340] | 188 |
|
---|
[2d87297] | 189 | GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline;
|
---|
| 190 | vector<SceneObject<AsteroidVertex, SSBO_Asteroid>> asteroidObjects;
|
---|
[3e8cc8b] | 191 |
|
---|
| 192 | vector<VkBuffer> uniformBuffers_asteroidPipeline;
|
---|
| 193 | vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
|
---|
| 194 | vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
|
---|
| 195 |
|
---|
| 196 | UBO_VP_mats asteroid_VP_mats;
|
---|
| 197 |
|
---|
[0807aeb] | 198 | time_point<steady_clock> startTime;
|
---|
| 199 | float curTime, prevTime, elapsedTime;
|
---|
| 200 |
|
---|
| 201 | float shipSpeed = 0.5f;
|
---|
| 202 | float asteroidSpeed = 2.0f;
|
---|
| 203 |
|
---|
| 204 | float spawnRate_asteroid = 0.5;
|
---|
| 205 | float lastSpawn_asteroid;
|
---|
[4ece3bf] | 206 |
|
---|
[b6e60b4] | 207 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
[0df3c9a] | 208 | void initVulkan();
|
---|
[f97c5e7] | 209 | void initGraphicsPipelines();
|
---|
[15104a8] | 210 | void initMatrices();
|
---|
[0df3c9a] | 211 | void mainLoop();
|
---|
[8e02b6b] | 212 | void updateScene(uint32_t currentImage);
|
---|
[a0c5f28] | 213 | void renderUI();
|
---|
| 214 | void renderScene();
|
---|
[0df3c9a] | 215 | void cleanup();
|
---|
[c1d9b2a] | 216 |
|
---|
| 217 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
| 218 | void setupDebugMessenger();
|
---|
| 219 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
[90a424f] | 220 | void createVulkanSurface();
|
---|
[fe5c3ba] | 221 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[fa9fa1c] | 222 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
[c1c2021] | 223 | void createLogicalDevice(
|
---|
| 224 | const vector<const char*> validationLayers,
|
---|
| 225 | const vector<const char*>& deviceExtensions);
|
---|
[502bd0b] | 226 | void createSwapChain();
|
---|
[f94eea9] | 227 | void createImageViews();
|
---|
[6fc24c7] | 228 | void createRenderPass();
|
---|
| 229 | VkFormat findDepthFormat();
|
---|
[fa9fa1c] | 230 | void createCommandPool();
|
---|
[603b5bc] | 231 | void createImageResources();
|
---|
| 232 |
|
---|
[b794178] | 233 | void createTextureSampler();
|
---|
[603b5bc] | 234 | void createFramebuffers();
|
---|
| 235 | void createCommandBuffers();
|
---|
[34bdf3a] | 236 | void createSyncObjects();
|
---|
[f94eea9] | 237 |
|
---|
[4994692] | 238 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
| 239 | // stop using objects.back() to access the object that was just created
|
---|
[2d87297] | 240 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 241 | SceneObject<VertexType, SSBOType>& addObject(
|
---|
| 242 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 243 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
| 244 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 245 | bool pipelinesCreated);
|
---|
[0fe8433] | 246 |
|
---|
[2da64ef] | 247 | template<class VertexType, class SSBOType>
|
---|
| 248 | void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[4994692] | 249 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
|
---|
[2da64ef] | 250 |
|
---|
[06d959f] | 251 | template<class VertexType>
|
---|
| 252 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
| 253 |
|
---|
[cf727ca] | 254 | template<class VertexType>
|
---|
| 255 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
| 256 |
|
---|
[3b84bb6] | 257 | template<class VertexType, class SSBOType>
|
---|
| 258 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
[a79be34] | 259 |
|
---|
[055750a] | 260 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
|
---|
[4994692] | 261 | vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
| 262 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
[f97c5e7] | 263 |
|
---|
[d2d9286] | 264 | void recreateSwapChain();
|
---|
| 265 |
|
---|
[c1c2021] | 266 | void cleanupSwapChain();
|
---|
[c1d9b2a] | 267 |
|
---|
| 268 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 269 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 270 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 271 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 272 | void* pUserData);
|
---|
[e8ebc76] | 273 | };
|
---|
| 274 |
|
---|
[3b84bb6] | 275 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
|
---|
| 276 | // and to change the model matrix later by setting model_transform and then calling updateObject()
|
---|
| 277 | // Figure out a better way to allow the model matrix to be set during objecting creation
|
---|
[2ba5617] | 278 |
|
---|
| 279 | // TODO: Maybe return a reference to the object from this method if I decide that updating it
|
---|
| 280 | // immediately after creation is a good idea (such as setting model_base)
|
---|
| 281 | // Currently, model_base is set like this in a few places and the radius is set for asteroids
|
---|
| 282 | // to account for scaling
|
---|
[2d87297] | 283 | template<class VertexType, class SSBOType>
|
---|
[4994692] | 284 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
|
---|
| 285 | vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[2d87297] | 286 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
|
---|
[3b84bb6] | 287 | const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
|
---|
| 288 | bool pipelinesCreated) {
|
---|
[2ba5617] | 289 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
| 290 | // currently, the passed in model is useless since it gets overridden in updateObject() anyway
|
---|
[0fe8433] | 291 | size_t numVertices = pipeline.getNumVertices();
|
---|
| 292 |
|
---|
| 293 | for (uint16_t& idx : indices) {
|
---|
| 294 | idx += numVertices;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[2d87297] | 297 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
|
---|
[3b84bb6] | 298 |
|
---|
[2ba5617] | 299 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
| 300 | centerObject(obj);
|
---|
| 301 |
|
---|
[4994692] | 302 | bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
|
---|
| 303 | this->commandPool, this->graphicsQueue);
|
---|
[0fe8433] | 304 |
|
---|
[3b84bb6] | 305 | if (pipelinesCreated) {
|
---|
[44f23af] | 306 | vkDeviceWaitIdle(device);
|
---|
| 307 | vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
|
---|
| 308 |
|
---|
| 309 | // TODO: The pipeline recreation only has to be done once per frame where at least
|
---|
| 310 | // one SSBO is resized.
|
---|
| 311 | // Refactor the logic to check for any resized SSBOs after all objects for the frame
|
---|
| 312 | // are created and then recreate each of the corresponding pipelines only once per frame
|
---|
[3b84bb6] | 313 | if (storageBufferResized) {
|
---|
[44f23af] | 314 | pipeline.createPipeline(pipeline.vertShaderFile, pipeline.fragShaderFile);
|
---|
| 315 | pipeline.createDescriptorPool(swapChainImages);
|
---|
| 316 | pipeline.createDescriptorSets(swapChainImages);
|
---|
[3b84bb6] | 317 | }
|
---|
| 318 |
|
---|
| 319 | createCommandBuffers();
|
---|
| 320 | }
|
---|
[4994692] | 321 |
|
---|
| 322 | return obj;
|
---|
[0fe8433] | 323 | }
|
---|
| 324 |
|
---|
[0807aeb] | 325 | // TODO: Just pass in the single object instead of a list of all of them
|
---|
[2da64ef] | 326 | template<class VertexType, class SSBOType>
|
---|
| 327 | void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 328 | GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) {
|
---|
| 329 | SceneObject<VertexType, SSBOType>& obj = objects[index];
|
---|
| 330 |
|
---|
| 331 | obj.ssbo.model = obj.model_transform * obj.model_base;
|
---|
[2ba5617] | 332 | obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
|
---|
[2da64ef] | 333 |
|
---|
| 334 | pipeline.updateObject(index, obj.ssbo);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[06d959f] | 337 | template<class VertexType>
|
---|
| 338 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
| 339 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
| 340 | vec3 p1 = vertices[i].pos;
|
---|
| 341 | vec3 p2 = vertices[i+1].pos;
|
---|
| 342 | vec3 p3 = vertices[i+2].pos;
|
---|
| 343 |
|
---|
[a79be34] | 344 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
[06d959f] | 345 |
|
---|
| 346 | // Add the same normal for all 3 vertices
|
---|
| 347 | vertices[i].normal = normal;
|
---|
| 348 | vertices[i+1].normal = normal;
|
---|
| 349 | vertices[i+2].normal = normal;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | return vertices;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[cf727ca] | 355 | template<class VertexType>
|
---|
| 356 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
| 357 | for (VertexType& vertex : vertices) {
|
---|
| 358 | vertex.objIndex = objIndex;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | return vertices;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[3b84bb6] | 364 | template<class VertexType, class SSBOType>
|
---|
| 365 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
| 366 | vector<VertexType>& vertices = object.vertices;
|
---|
| 367 |
|
---|
[a79be34] | 368 | float min_x = vertices[0].pos.x;
|
---|
| 369 | float max_x = vertices[0].pos.x;
|
---|
| 370 | float min_y = vertices[0].pos.y;
|
---|
| 371 | float max_y = vertices[0].pos.y;
|
---|
| 372 | float min_z = vertices[0].pos.z;
|
---|
| 373 | float max_z = vertices[0].pos.z;
|
---|
| 374 |
|
---|
| 375 | // start from the second point
|
---|
| 376 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
[3b84bb6] | 377 | vec3& pos = vertices[i].pos;
|
---|
| 378 |
|
---|
| 379 | if (min_x > pos.x) {
|
---|
| 380 | min_x = pos.x;
|
---|
| 381 | } else if (max_x < pos.x) {
|
---|
| 382 | max_x = pos.x;
|
---|
[a79be34] | 383 | }
|
---|
| 384 |
|
---|
[3b84bb6] | 385 | if (min_y > pos.y) {
|
---|
| 386 | min_y = pos.y;
|
---|
| 387 | } else if (max_y < pos.y) {
|
---|
| 388 | max_y = pos.y;
|
---|
[a79be34] | 389 | }
|
---|
| 390 |
|
---|
[3b84bb6] | 391 | if (min_z > pos.z) {
|
---|
| 392 | min_z = pos.z;
|
---|
| 393 | } else if (max_z < pos.z) {
|
---|
| 394 | max_z = pos.z;
|
---|
[a79be34] | 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
| 399 |
|
---|
| 400 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
| 401 | vertices[i].pos -= center;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[2ba5617] | 404 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
| 405 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
| 406 |
|
---|
[3b84bb6] | 407 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
[a79be34] | 408 | }
|
---|
| 409 |
|
---|
[3b84bb6] | 410 | #endif // _VULKAN_GAME_H
|
---|