[3b7d497] | 1 | #ifndef _SDL_GAME_H
|
---|
| 2 | #define _SDL_GAME_H
|
---|
| 3 |
|
---|
[40eb092] | 4 | #include <chrono>
|
---|
| 5 | #include <map>
|
---|
[3b7d497] | 6 | #include <vector>
|
---|
[40eb092] | 7 |
|
---|
[3b7d497] | 8 | #include <vulkan/vulkan.h>
|
---|
| 9 |
|
---|
| 10 | #include <SDL2/SDL.h>
|
---|
[c6f0793] | 11 |
|
---|
[4a777d2] | 12 | #define GLM_FORCE_RADIANS
|
---|
| 13 | #define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
|
---|
| 14 | #define GLM_FORCE_RIGHT_HANDED
|
---|
| 15 |
|
---|
| 16 | #include <glm/glm.hpp>
|
---|
| 17 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 18 |
|
---|
[6493e43] | 19 | #include "IMGUI/imgui_impl_vulkan.h"
|
---|
| 20 |
|
---|
[3b7d497] | 21 | #include "consts.hpp"
|
---|
[8aa4888] | 22 | #include "game-gui-sdl.hpp"
|
---|
[ce9dc9f] | 23 | #include "vulkan-utils.hpp"
|
---|
[4a777d2] | 24 | #include "graphics-pipeline_vulkan.hpp"
|
---|
[8aa4888] | 25 | #include "vulkan-buffer.hpp"
|
---|
[3b7d497] | 26 |
|
---|
[4a777d2] | 27 | using namespace glm;
|
---|
[3b7d497] | 28 | using namespace std;
|
---|
[40eb092] | 29 | using namespace std::chrono;
|
---|
[3b7d497] | 30 |
|
---|
| 31 | #define VulkanGame NewVulkanGame
|
---|
| 32 |
|
---|
| 33 | #ifdef NDEBUG
|
---|
| 34 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
| 35 | #else
|
---|
| 36 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
[4a777d2] | 39 | // TODO: Consider if there is a better way of dealing with all the vertex types and ssbo types, maybe
|
---|
| 40 | // by consolidating some and trying to keep new ones to a minimum
|
---|
| 41 |
|
---|
| 42 | struct ModelVertex {
|
---|
| 43 | vec3 pos;
|
---|
| 44 | vec3 color;
|
---|
| 45 | vec2 texCoord;
|
---|
| 46 | vec3 normal;
|
---|
| 47 | unsigned int objIndex;
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | struct LaserVertex {
|
---|
| 51 | vec3 pos;
|
---|
| 52 | vec2 texCoord;
|
---|
| 53 | unsigned int objIndex;
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | struct ExplosionVertex {
|
---|
| 57 | vec3 particleStartVelocity;
|
---|
| 58 | float particleStartTime;
|
---|
| 59 | unsigned int objIndex;
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[8aa4888] | 62 | // Currently using these as the dynamic UBO types as well
|
---|
| 63 | // TODO: Rename them to something more general
|
---|
| 64 |
|
---|
[4a777d2] | 65 | struct SSBO_ModelObject {
|
---|
| 66 | alignas(16) mat4 model;
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | struct SSBO_Asteroid {
|
---|
| 70 | alignas(16) mat4 model;
|
---|
| 71 | alignas(4) float hp;
|
---|
| 72 | alignas(4) unsigned int deleted;
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | struct UBO_VP_mats {
|
---|
| 76 | alignas(16) mat4 view;
|
---|
| 77 | alignas(16) mat4 proj;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[996dd3e] | 80 | // TODO: Use this struct for uniform buffers as well and probably combine it with the VulkanBuffer class
|
---|
| 81 | // Also, probably better to make this a vector of structs where each struct
|
---|
| 82 | // has a VkBuffer, VkDeviceMemory, and VkDescriptorBufferInfo
|
---|
[a3cefaa] | 83 | // TODO: Maybe change the structure here since VkDescriptorBufferInfo already stores a reference to the VkBuffer
|
---|
[1abebc1] | 84 | struct BufferSet {
|
---|
[996dd3e] | 85 | vector<VkBuffer> buffers;
|
---|
| 86 | vector<VkDeviceMemory> memory;
|
---|
| 87 | vector<VkDescriptorBufferInfo> infoSet;
|
---|
[8aa4888] | 88 | VkBufferUsageFlags usages;
|
---|
| 89 | VkMemoryPropertyFlags properties;
|
---|
[996dd3e] | 90 | };
|
---|
| 91 |
|
---|
[4a777d2] | 92 | // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
|
---|
| 93 | // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
|
---|
| 94 | // TODO: Maybe create a typedef for each of the templated SceneObject types
|
---|
| 95 | template<class VertexType, class SSBOType>
|
---|
| 96 | struct SceneObject {
|
---|
| 97 | vector<VertexType> vertices;
|
---|
| 98 | vector<uint16_t> indices;
|
---|
| 99 | SSBOType ssbo;
|
---|
| 100 |
|
---|
| 101 | mat4 model_base;
|
---|
| 102 | mat4 model_transform;
|
---|
| 103 |
|
---|
| 104 | // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
|
---|
| 105 | // parent class
|
---|
| 106 | vec3 center; // currently only matters for asteroids
|
---|
| 107 | float radius; // currently only matters for asteroids
|
---|
[bb76950] | 108 |
|
---|
| 109 | // Move the targetAsteroid stuff out of this class since it is very specific to lasers
|
---|
| 110 | // and makes moving SceneObject into its own header file more problematic
|
---|
[4a777d2] | 111 | SceneObject<ModelVertex, SSBO_Asteroid>* targetAsteroid; // currently only used for lasers
|
---|
| 112 | };
|
---|
| 113 |
|
---|
| 114 | // TODO: Have to figure out how to include an optional ssbo parameter for each object
|
---|
| 115 | // Could probably use the same approach to make indices optional
|
---|
| 116 | // Figure out if there are sufficient use cases to make either of these optional or is it fine to make
|
---|
[c1ec4f6] | 117 | // them mandatory
|
---|
[4a777d2] | 118 |
|
---|
| 119 |
|
---|
| 120 | // TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
|
---|
| 121 |
|
---|
[40eb092] | 122 | // TODO: Maybe move this to a different header
|
---|
| 123 |
|
---|
| 124 | enum UIValueType {
|
---|
| 125 | UIVALUE_INT,
|
---|
| 126 | UIVALUE_DOUBLE,
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | struct UIValue {
|
---|
| 130 | UIValueType type;
|
---|
| 131 | string label;
|
---|
| 132 | void* value;
|
---|
| 133 |
|
---|
| 134 | UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
|
---|
| 135 | };
|
---|
| 136 |
|
---|
[3b7d497] | 137 | class VulkanGame {
|
---|
[914bb99] | 138 |
|
---|
[3b7d497] | 139 | public:
|
---|
[914bb99] | 140 |
|
---|
[ce9dc9f] | 141 | VulkanGame();
|
---|
[3b7d497] | 142 | ~VulkanGame();
|
---|
| 143 |
|
---|
[ce9dc9f] | 144 | void run(int width, int height, unsigned char guiFlags);
|
---|
[3b7d497] | 145 |
|
---|
| 146 | private:
|
---|
[914bb99] | 147 |
|
---|
[3b7d497] | 148 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
| 149 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
| 150 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
| 151 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
| 152 | void* pUserData);
|
---|
| 153 |
|
---|
[4a777d2] | 154 | // TODO: Maybe pass these in as parameters to some Camera class
|
---|
| 155 | const float NEAR_CLIP = 0.1f;
|
---|
| 156 | const float FAR_CLIP = 100.0f;
|
---|
| 157 | const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 deg
|
---|
| 158 |
|
---|
[c6f0793] | 159 | bool done;
|
---|
| 160 |
|
---|
[4a777d2] | 161 | vec3 cam_pos;
|
---|
| 162 |
|
---|
[3b7d497] | 163 | // TODO: Good place to start using smart pointers
|
---|
| 164 | GameGui* gui;
|
---|
| 165 |
|
---|
| 166 | SDL_version sdlVersion;
|
---|
| 167 | SDL_Window* window;
|
---|
| 168 |
|
---|
[ce9dc9f] | 169 | VkInstance instance;
|
---|
[7865c5b] | 170 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
| 171 | VkSurfaceKHR vulkanSurface;
|
---|
| 172 | VkPhysicalDevice physicalDevice;
|
---|
[ce9dc9f] | 173 | VkDevice device;
|
---|
[6493e43] | 174 |
|
---|
| 175 | VkQueue graphicsQueue;
|
---|
| 176 | VkQueue presentQueue;
|
---|
[ce9dc9f] | 177 |
|
---|
| 178 | // TODO: Maybe make a swapchain struct for convenience
|
---|
| 179 | VkSurfaceFormatKHR swapChainSurfaceFormat;
|
---|
| 180 | VkPresentModeKHR swapChainPresentMode;
|
---|
| 181 | VkExtent2D swapChainExtent;
|
---|
| 182 | uint32_t swapChainMinImageCount;
|
---|
| 183 | uint32_t swapChainImageCount;
|
---|
| 184 |
|
---|
| 185 | VkSwapchainKHR swapChain;
|
---|
| 186 | vector<VkImage> swapChainImages;
|
---|
| 187 | vector<VkImageView> swapChainImageViews;
|
---|
| 188 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
| 189 |
|
---|
| 190 | VkRenderPass renderPass;
|
---|
| 191 |
|
---|
| 192 | VkCommandPool resourceCommandPool;
|
---|
| 193 |
|
---|
| 194 | vector<VkCommandPool> commandPools;
|
---|
| 195 | vector<VkCommandBuffer> commandBuffers;
|
---|
| 196 |
|
---|
| 197 | VulkanImage depthImage;
|
---|
| 198 |
|
---|
| 199 | // These are per frame
|
---|
| 200 | vector<VkSemaphore> imageAcquiredSemaphores;
|
---|
| 201 | vector<VkSemaphore> renderCompleteSemaphores;
|
---|
| 202 |
|
---|
| 203 | // These are per swap chain image
|
---|
| 204 | vector<VkFence> inFlightFences;
|
---|
| 205 |
|
---|
| 206 | uint32_t imageIndex;
|
---|
| 207 | uint32_t currentFrame;
|
---|
| 208 |
|
---|
[28ea92f] | 209 | bool shouldRecreateSwapChain;
|
---|
| 210 |
|
---|
[4a777d2] | 211 | VkSampler textureSampler;
|
---|
| 212 |
|
---|
| 213 | VulkanImage floorTextureImage;
|
---|
| 214 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
| 215 |
|
---|
| 216 | mat4 viewMat, projMat;
|
---|
| 217 |
|
---|
[e469aed] | 218 | // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
|
---|
| 219 | VkDescriptorPool imguiDescriptorPool;
|
---|
| 220 |
|
---|
[4a777d2] | 221 | // TODO: Probably restructure the GraphicsPipeline_Vulkan class based on what I learned about descriptors and textures
|
---|
| 222 | // while working on graphics-library. Double-check exactly what this was and note it down here.
|
---|
| 223 | // Basically, I think the point was that if I have several models that all use the same shaders and, therefore,
|
---|
| 224 | // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan
|
---|
| 225 | // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures
|
---|
[9d21aac] | 226 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
[4a777d2] | 227 |
|
---|
[1abebc1] | 228 | BufferSet storageBuffers_modelPipeline;
|
---|
[a3cefaa] | 229 | VulkanBuffer<SSBO_ModelObject> objects_modelPipeline;
|
---|
[c163d81] | 230 | BufferSet uniformBuffers_modelPipeline;
|
---|
[996dd3e] | 231 |
|
---|
[4a777d2] | 232 | // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
|
---|
| 233 | // per pipeline.
|
---|
| 234 | // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
|
---|
| 235 | // the objects vector, the ubo, and the ssbo
|
---|
| 236 |
|
---|
| 237 | // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
|
---|
| 238 | // if there is a need to add other uniform variables to one or more of the shaders
|
---|
| 239 |
|
---|
| 240 | vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
|
---|
| 241 |
|
---|
| 242 | UBO_VP_mats object_VP_mats;
|
---|
| 243 |
|
---|
[40eb092] | 244 | /*** High-level vars ***/
|
---|
| 245 |
|
---|
[85b5fec] | 246 | // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
|
---|
| 247 | void (VulkanGame::* currentRenderScreenFn)(int width, int height);
|
---|
[40eb092] | 248 |
|
---|
| 249 | map<string, vector<UIValue>> valueLists;
|
---|
| 250 |
|
---|
| 251 | int score;
|
---|
| 252 | float fps;
|
---|
| 253 |
|
---|
| 254 | // TODO: Make a separate singleton Timer class
|
---|
| 255 | time_point<steady_clock> startTime;
|
---|
[5081b9a] | 256 | float fpsStartTime, curTime, prevTime, elapsedTime;
|
---|
[40eb092] | 257 |
|
---|
| 258 | int frameCount;
|
---|
| 259 |
|
---|
| 260 | /*** Functions ***/
|
---|
| 261 |
|
---|
[3b7d497] | 262 | bool initUI(int width, int height, unsigned char guiFlags);
|
---|
[40eb092] | 263 | void initVulkan();
|
---|
[4a777d2] | 264 | void initGraphicsPipelines();
|
---|
| 265 | void initMatrices();
|
---|
[40eb092] | 266 | void renderLoop();
|
---|
[4a777d2] | 267 | void updateScene();
|
---|
[40eb092] | 268 | void cleanup();
|
---|
[3b7d497] | 269 |
|
---|
| 270 | void createVulkanInstance(const vector<const char*>& validationLayers);
|
---|
| 271 | void setupDebugMessenger();
|
---|
| 272 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
| 273 | void createVulkanSurface();
|
---|
[ce9dc9f] | 274 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
[3b7d497] | 275 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
| 276 | void createLogicalDevice(const vector<const char*>& validationLayers,
|
---|
[ce9dc9f] | 277 | const vector<const char*>& deviceExtensions);
|
---|
| 278 | void chooseSwapChainProperties();
|
---|
| 279 | void createSwapChain();
|
---|
| 280 | void createImageViews();
|
---|
| 281 | void createResourceCommandPool();
|
---|
[e469aed] | 282 | void createImageResources();
|
---|
| 283 | VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
|
---|
| 284 | void createRenderPass();
|
---|
[ce9dc9f] | 285 | void createCommandPools();
|
---|
| 286 | void createFramebuffers();
|
---|
| 287 | void createCommandBuffers();
|
---|
| 288 | void createSyncObjects();
|
---|
| 289 |
|
---|
[4a777d2] | 290 | void createTextureSampler();
|
---|
| 291 |
|
---|
[e469aed] | 292 | void initImGuiOverlay();
|
---|
| 293 | void cleanupImGuiOverlay();
|
---|
| 294 |
|
---|
[9d21aac] | 295 | // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class
|
---|
| 296 |
|
---|
[8aa4888] | 297 | void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags usages, VkMemoryPropertyFlags properties,
|
---|
[c163d81] | 298 | BufferSet& set);
|
---|
[4a777d2] | 299 |
|
---|
[bb76950] | 300 | void resizeBufferSet(BufferSet& set, VkDeviceSize newSize, VkCommandPool commandPool, VkQueue graphicsQueue,
|
---|
| 301 | bool copyData);
|
---|
[9d21aac] | 302 |
|
---|
| 303 | template<class SSBOType>
|
---|
[1abebc1] | 304 | void updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo);
|
---|
[9d21aac] | 305 |
|
---|
[4a777d2] | 306 | // TODO: Since addObject() returns a reference to the new object now,
|
---|
| 307 | // stop using objects.back() to access the object that was just created
|
---|
| 308 | template<class VertexType, class SSBOType>
|
---|
[b8072d3] | 309 | SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
[9d21aac] | 310 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
[b8072d3] | 311 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
[8dcbf62] | 312 | VulkanBuffer<SSBOType>& objectBuffer, SSBOType ssbo);
|
---|
[4a777d2] | 313 |
|
---|
| 314 | template<class VertexType>
|
---|
| 315 | vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
|
---|
| 316 |
|
---|
| 317 | template<class VertexType>
|
---|
| 318 | vector<VertexType> addVertexNormals(vector<VertexType> vertices);
|
---|
| 319 |
|
---|
| 320 | template<class VertexType, class SSBOType>
|
---|
| 321 | void centerObject(SceneObject<VertexType, SSBOType>& object);
|
---|
| 322 |
|
---|
[4e2c709] | 323 | void renderFrame(ImDrawData* draw_data);
|
---|
| 324 | void presentFrame();
|
---|
| 325 |
|
---|
[ce9dc9f] | 326 | void recreateSwapChain();
|
---|
| 327 |
|
---|
| 328 | void cleanupSwapChain();
|
---|
[6493e43] | 329 |
|
---|
[40eb092] | 330 | /*** High-level functions ***/
|
---|
| 331 |
|
---|
[85b5fec] | 332 | void renderMainScreen(int width, int height);
|
---|
| 333 | void renderGameScreen(int width, int height);
|
---|
[40eb092] | 334 |
|
---|
| 335 | void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
|
---|
| 336 | void renderGuiValueList(vector<UIValue>& values);
|
---|
| 337 |
|
---|
[85b5fec] | 338 | void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
|
---|
[40eb092] | 339 | void quitGame();
|
---|
[ce9dc9f] | 340 | };
|
---|
[6493e43] | 341 |
|
---|
[9d21aac] | 342 | // TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them
|
---|
| 343 | template<class SSBOType>
|
---|
[1abebc1] | 344 | void VulkanGame::updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo) {
|
---|
| 345 | for (size_t i = 0; i < set.memory.size(); i++) {
|
---|
[c074f81] | 346 | VulkanUtils::copyDataToMemory(device, &ssbo, set.memory[i], objIndex * sizeof(SSBOType), sizeof(ssbo), false);
|
---|
[9d21aac] | 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[b7fc3c2] | 350 | // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model and to change
|
---|
| 351 | // the model matrix later by setting model_transform and then calculating the new ssbo.model.
|
---|
[9d21aac] | 352 | // Figure out a better way to allow the model matrix to be set during object creation
|
---|
[4a777d2] | 353 | template<class VertexType, class SSBOType>
|
---|
[9d21aac] | 354 | SceneObject<VertexType, SSBOType>& VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
|
---|
| 355 | GraphicsPipeline_Vulkan<VertexType>& pipeline,
|
---|
| 356 | const vector<VertexType>& vertices, vector<uint16_t> indices,
|
---|
[8dcbf62] | 357 | VulkanBuffer<SSBOType>& objectBuffer, SSBOType ssbo) {
|
---|
[4a777d2] | 358 | // TODO: Use the model field of ssbo to set the object's model_base
|
---|
[b7fc3c2] | 359 | // currently, the passed-in model is useless since it gets overridden when ssbo.model is recalculated
|
---|
[4a777d2] | 360 | size_t numVertices = pipeline.getNumVertices();
|
---|
| 361 |
|
---|
| 362 | for (uint16_t& idx : indices) {
|
---|
| 363 | idx += numVertices;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[c1ec4f6] | 366 | objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
|
---|
[8dcbf62] | 367 | objectBuffer.add(ssbo);
|
---|
[4a777d2] | 368 |
|
---|
| 369 | SceneObject<VertexType, SSBOType>& obj = objects.back();
|
---|
| 370 |
|
---|
| 371 | // TODO: Specify whether to center the object outside of this function or, worst case, maybe
|
---|
| 372 | // with a boolean being passed in here, so that I don't have to rely on checking the specific object
|
---|
| 373 | // type
|
---|
[8dcbf62] | 374 | // TODO: Actually, I've already defined a no-op centerObject method for explosions
|
---|
| 375 | // Maybe I should do the same for lasers and remove this conditional altogether
|
---|
[4a777d2] | 376 | if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
|
---|
| 377 | centerObject(obj);
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[9d21aac] | 380 | pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
|
---|
| 381 |
|
---|
[4a777d2] | 382 | return obj;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | template<class VertexType>
|
---|
| 386 | vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
|
---|
| 387 | for (VertexType& vertex : vertices) {
|
---|
| 388 | vertex.objIndex = objIndex;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | return vertices;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | template<class VertexType>
|
---|
| 395 | vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
|
---|
| 396 | for (unsigned int i = 0; i < vertices.size(); i += 3) {
|
---|
| 397 | vec3 p1 = vertices[i].pos;
|
---|
| 398 | vec3 p2 = vertices[i + 1].pos;
|
---|
| 399 | vec3 p3 = vertices[i + 2].pos;
|
---|
| 400 |
|
---|
| 401 | vec3 normal = normalize(cross(p2 - p1, p3 - p1));
|
---|
| 402 |
|
---|
| 403 | // Add the same normal for all 3 vertices
|
---|
| 404 | vertices[i].normal = normal;
|
---|
| 405 | vertices[i + 1].normal = normal;
|
---|
| 406 | vertices[i + 2].normal = normal;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | return vertices;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | template<class VertexType, class SSBOType>
|
---|
| 413 | void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
|
---|
| 414 | vector<VertexType>& vertices = object.vertices;
|
---|
| 415 |
|
---|
| 416 | float min_x = vertices[0].pos.x;
|
---|
| 417 | float max_x = vertices[0].pos.x;
|
---|
| 418 | float min_y = vertices[0].pos.y;
|
---|
| 419 | float max_y = vertices[0].pos.y;
|
---|
| 420 | float min_z = vertices[0].pos.z;
|
---|
| 421 | float max_z = vertices[0].pos.z;
|
---|
| 422 |
|
---|
| 423 | // start from the second point
|
---|
| 424 | for (unsigned int i = 1; i < vertices.size(); i++) {
|
---|
| 425 | vec3& pos = vertices[i].pos;
|
---|
| 426 |
|
---|
| 427 | if (min_x > pos.x) {
|
---|
| 428 | min_x = pos.x;
|
---|
| 429 | }
|
---|
| 430 | else if (max_x < pos.x) {
|
---|
| 431 | max_x = pos.x;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | if (min_y > pos.y) {
|
---|
| 435 | min_y = pos.y;
|
---|
| 436 | }
|
---|
| 437 | else if (max_y < pos.y) {
|
---|
| 438 | max_y = pos.y;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | if (min_z > pos.z) {
|
---|
| 442 | min_z = pos.z;
|
---|
| 443 | }
|
---|
| 444 | else if (max_z < pos.z) {
|
---|
| 445 | max_z = pos.z;
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
|
---|
| 450 |
|
---|
| 451 | for (unsigned int i = 0; i < vertices.size(); i++) {
|
---|
| 452 | vertices[i].pos -= center;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | object.radius = std::max(max_x - center.x, max_y - center.y);
|
---|
| 456 | object.radius = std::max(object.radius, max_z - center.z);
|
---|
| 457 |
|
---|
| 458 | object.center = vec3(0.0f, 0.0f, 0.0f);
|
---|
| 459 | }
|
---|
| 460 |
|
---|
[3b7d497] | 461 | #endif // _SDL_GAME_H
|
---|