source: opengl-game/vulkan-game.hpp@ 7f60b28

feature/imgui-sdl
Last change on this file since 7f60b28 was 3b7d497, checked in by Dmitry Portnoy <dportnoy@…>, 4 years ago

Start implementing an ImGUI ui on top of SDL and Vulkan using some example code

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