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