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

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

Remove the modified field from the SceneObject object

  • Property mode set to 100644
File size: 21.0 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>
[5192672]7#include <vector>
[0807aeb]8
[20e4c2b]9#include <vulkan/vulkan.h>
10
11#include <SDL2/SDL.h>
12#include <SDL2/SDL_ttf.h>
13
[60578ce]14#define GLM_FORCE_RADIANS
15#define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
[a79be34]16#define GLM_FORCE_RIGHT_HANDED
[60578ce]17
[771b33a]18#include <glm/glm.hpp>
[15104a8]19#include <glm/gtc/matrix_transform.hpp>
[771b33a]20
[ea2b4dc]21#include "IMGUI/imgui_impl_vulkan.h"
22
[6bfd91c]23#include "consts.hpp"
[b8efa56]24#include "utils.hpp"
[8aa4888]25#include "game-gui-sdl.hpp"
[b8efa56]26#include "vulkan-utils.hpp"
[a3cefaa]27#include "graphics-pipeline_vulkan.hpp"
[8aa4888]28#include "vulkan-buffer.hpp"
[b794178]29
[15104a8]30using namespace glm;
[0807aeb]31using namespace std::chrono;
[15104a8]32
[2e77b3f]33#ifdef NDEBUG
34 const bool ENABLE_VALIDATION_LAYERS = false;
35#else
36 const bool ENABLE_VALIDATION_LAYERS = true;
37#endif
38
[cefdf23]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
[5a1ace0]42struct ModelVertex {
[15104a8]43 vec3 pos;
[5a1ace0]44 vec3 color;
[15104a8]45 vec2 texCoord;
[a00eb06]46 vec3 normal;
[5a1ace0]47 unsigned int objIndex;
[15104a8]48};
49
[237cbec]50struct LaserVertex {
51 vec3 pos;
52 vec2 texCoord;
53 unsigned int objIndex;
54};
55
[4a9416a]56struct ExplosionVertex {
57 vec3 particleStartVelocity;
58 float particleStartTime;
59 unsigned int objIndex;
60};
61
[2d87297]62struct SSBO_ModelObject {
[055750a]63 alignas(16) mat4 model;
64};
65
[2d87297]66struct SSBO_Asteroid {
[3e8cc8b]67 alignas(16) mat4 model;
68 alignas(4) float hp;
[4ece3bf]69 alignas(4) unsigned int deleted;
[3e8cc8b]70};
71
[237cbec]72struct SSBO_Laser {
73 alignas(16) mat4 model;
74 alignas(4) vec3 color;
75 alignas(4) unsigned int deleted;
76};
77
[4a9416a]78struct SSBO_Explosion {
79 alignas(16) mat4 model;
80 alignas(4) float explosionStartTime;
81 alignas(4) float explosionDuration;
82 alignas(4) unsigned int deleted;
83};
84
[52a02e6]85struct UBO_VP_mats {
86 alignas(16) mat4 view;
87 alignas(16) mat4 proj;
88};
89
[4a9416a]90struct UBO_Explosion {
91 alignas(16) mat4 view;
92 alignas(16) mat4 proj;
93 alignas(4) float cur_time;
94};
95
[996dd3e]96// TODO: Use this struct for uniform buffers as well and probably combine it with the VulkanBuffer class
97// Also, probably better to make this a vector of structs where each struct
98// has a VkBuffer, VkDeviceMemory, and VkDescriptorBufferInfo
[a3cefaa]99// TODO: Maybe change the structure here since VkDescriptorBufferInfo already stores a reference to the VkBuffer
[1abebc1]100struct BufferSet {
[996dd3e]101 vector<VkBuffer> buffers;
102 vector<VkDeviceMemory> memory;
103 vector<VkDescriptorBufferInfo> infoSet;
[8aa4888]104 VkBufferUsageFlags usages;
105 VkMemoryPropertyFlags properties;
[996dd3e]106};
107
[4994692]108// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
109// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
110// TODO: Maybe create a typedef for each of the templated SceneObject types
111template<class VertexType, class SSBOType>
112struct SceneObject {
113 vector<VertexType> vertices;
114 vector<uint16_t> indices;
115 SSBOType ssbo;
116
117 mat4 model_base;
118 mat4 model_transform;
119
120 // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
121 // parent class
122 vec3 center; // currently only matters for asteroids
123 float radius; // currently only matters for asteroids
[bb76950]124
125 // Move the targetAsteroid stuff out of this class since it is very specific to lasers
126 // and makes moving SceneObject into its own header file more problematic
[b8efa56]127 SceneObject<ModelVertex, 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
[c1ec4f6]133// them mandatory
[2da64ef]134
[7297892]135
136// TODO: Look into using dynamic_cast to check types of SceneObject and EffectOverTime
137
138struct BaseEffectOverTime {
139 bool deleted;
140
[5192672]141 virtual void applyEffect(float curTime) = 0;
[7297892]142
143 BaseEffectOverTime() :
144 deleted(false) {
145 }
146
147 virtual ~BaseEffectOverTime() {
148 }
149};
150
151template<class VertexType, class SSBOType>
152struct EffectOverTime : public BaseEffectOverTime {
[9d21aac]153 GraphicsPipeline_Vulkan<VertexType>& pipeline;
[7297892]154 vector<SceneObject<VertexType, SSBOType>>& objects;
155 unsigned int objectIndex;
156 size_t effectedFieldOffset;
157 float startValue;
158 float startTime;
159 float changePerSecond;
160
[9d21aac]161 EffectOverTime(GraphicsPipeline_Vulkan<VertexType>& pipeline, vector<SceneObject<VertexType, SSBOType>>& objects,
162 unsigned int objectIndex, size_t effectedFieldOffset, float startTime, float changePerSecond)
163 : pipeline(pipeline)
164 , objects(objects)
165 , objectIndex(objectIndex)
166 , effectedFieldOffset(effectedFieldOffset)
167 , startTime(startTime)
168 , changePerSecond(changePerSecond) {
[7297892]169 size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
170
171 unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
172 ssboOffset + effectedFieldOffset;
173
174 startValue = *reinterpret_cast<float*>(effectedFieldPtr);
175 }
176
[5192672]177 void applyEffect(float curTime) {
[7297892]178 if (objects[objectIndex].ssbo.deleted) {
179 this->deleted = true;
180 return;
181 }
182
183 size_t ssboOffset = offset_of(&SceneObject<VertexType, SSBOType>::ssbo);
184
185 unsigned char* effectedFieldPtr = reinterpret_cast<unsigned char*>(&objects[objectIndex]) +
186 ssboOffset + effectedFieldOffset;
187
188 *reinterpret_cast<float*>(effectedFieldPtr) = startValue + (curTime - startTime) * changePerSecond;
189 }
190};
191
[cefdf23]192// TODO: Maybe move this to a different header
193
[20e4c2b]194enum UIValueType {
195 UIVALUE_INT,
196 UIVALUE_DOUBLE,
197};
198
199struct UIValue {
200 UIValueType type;
201 string label;
202 void* value;
203
204 UIValue(UIValueType _type, string _label, void* _value) : type(_type), label(_label), value(_value) {}
205};
206
[e8445f0]207/* TODO: The following syntax (note the const keyword) means the function will not modify
208 * its params. I should use this where appropriate
209 *
210 * [return-type] [func-name](params...) const { ... }
211 */
212
[99d44b2]213class VulkanGame {
[914bb99]214
[e8ebc76]215 public:
[cefdf23]216
[3f32dfd]217 VulkanGame();
[99d44b2]218 ~VulkanGame();
[0df3c9a]219
[b6e60b4]220 void run(int width, int height, unsigned char guiFlags);
[0df3c9a]221
222 private:
[cefdf23]223
[c324d6a]224 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
225 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
226 VkDebugUtilsMessageTypeFlagsEXT messageType,
227 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
228 void* pUserData);
229
[cefdf23]230 // TODO: Maybe pass these in as parameters to some Camera class
[5ab1b20]231 const float NEAR_CLIP = 0.1f;
232 const float FAR_CLIP = 100.0f;
[cefdf23]233 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 deg
[5ab1b20]234
[4a9416a]235 const int EXPLOSION_PARTICLE_COUNT = 300;
236 const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f);
237
[c6f0793]238 bool done;
[e1f88a9]239
[15104a8]240 vec3 cam_pos;
241
[4e705d6]242 // TODO: Good place to start using smart pointers
[0df3c9a]243 GameGui* gui;
[c559904]244
245 SDL_version sdlVersion;
[b794178]246 SDL_Window* window = nullptr;
[c1d9b2a]247
[301c90a]248 int drawableWidth, drawableHeight;
249
[c1d9b2a]250 VkInstance instance;
251 VkDebugUtilsMessengerEXT debugMessenger;
[7865c5b]252 VkSurfaceKHR vulkanSurface;
[90a424f]253 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
[c1c2021]254 VkDevice device;
255
256 VkQueue graphicsQueue;
257 VkQueue presentQueue;
[0df3c9a]258
[3f32dfd]259 // TODO: Maybe make a swapchain struct for convenience
260 VkSurfaceFormatKHR swapChainSurfaceFormat;
261 VkPresentModeKHR swapChainPresentMode;
262 VkExtent2D swapChainExtent;
263 uint32_t swapChainMinImageCount;
[c324d6a]264 uint32_t swapChainImageCount;
[502bd0b]265 VkSwapchainKHR swapChain;
266 vector<VkImage> swapChainImages;
[f94eea9]267 vector<VkImageView> swapChainImageViews;
[603b5bc]268 vector<VkFramebuffer> swapChainFramebuffers;
[fa9fa1c]269
[6fc24c7]270 VkRenderPass renderPass;
[3f32dfd]271
272 VkCommandPool resourceCommandPool;
273
[9c0a614]274 vector<VkCommandPool> commandPools;
[603b5bc]275 vector<VkCommandBuffer> commandBuffers;
[502bd0b]276
[603b5bc]277 VulkanImage depthImage;
[b794178]278
[3f32dfd]279 // These are per frame
280 vector<VkSemaphore> imageAcquiredSemaphores;
281 vector<VkSemaphore> renderCompleteSemaphores;
282
283 // These are per swap chain image
[4e705d6]284 vector<VkFence> inFlightFences;
285
[3f32dfd]286 uint32_t imageIndex;
287 uint32_t currentFrame;
[4e705d6]288
[28ea92f]289 bool shouldRecreateSwapChain;
[4e705d6]290
[b794178]291 VkSampler textureSampler;
292
[4994692]293 VulkanImage floorTextureImage;
294 VkDescriptorImageInfo floorTextureImageDescriptor;
295
[237cbec]296 VulkanImage laserTextureImage;
297 VkDescriptorImageInfo laserTextureImageDescriptor;
298
[22217d4]299 mat4 viewMat, projMat;
300
[cefdf23]301 // Maybe at some point create an imgui pipeline class, but I don't think it makes sense right now
302 VkDescriptorPool imguiDescriptorPool;
303
304 // TODO: Probably restructure the GraphicsPipeline_Vulkan class based on what I learned about descriptors and textures
305 // while working on graphics-library. Double-check exactly what this was and note it down here.
306 // Basically, I think the point was that if I have several modesl that all use the same shaders and, therefore,
307 // the same pipeline, but use different textures, the approach I took when initially creating GraphicsPipeline_Vulkan
308 // wouldn't work since the whole pipeline couldn't have a common set of descriptors for the textures
[9d21aac]309 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
310 GraphicsPipeline_Vulkan<ModelVertex> shipPipeline;
311 GraphicsPipeline_Vulkan<ModelVertex> asteroidPipeline;
312 GraphicsPipeline_Vulkan<LaserVertex> laserPipeline;
313 GraphicsPipeline_Vulkan<ExplosionVertex> explosionPipeline;
[cefdf23]314
[1abebc1]315 BufferSet storageBuffers_modelPipeline;
[a3cefaa]316 VulkanBuffer<SSBO_ModelObject> objects_modelPipeline;
[c163d81]317 BufferSet uniformBuffers_modelPipeline;
[a3cefaa]318
[1abebc1]319 BufferSet storageBuffers_shipPipeline;
[a3cefaa]320 VulkanBuffer<SSBO_ModelObject> objects_shipPipeline;
[c163d81]321 BufferSet uniformBuffers_shipPipeline;
[a3cefaa]322
[1abebc1]323 BufferSet storageBuffers_asteroidPipeline;
[a3cefaa]324 VulkanBuffer<SSBO_Asteroid> objects_asteroidPipeline;
[c163d81]325 BufferSet uniformBuffers_asteroidPipeline;
[a3cefaa]326
[1abebc1]327 BufferSet storageBuffers_laserPipeline;
[a3cefaa]328 VulkanBuffer<SSBO_Laser> objects_laserPipeline;
[c163d81]329 BufferSet uniformBuffers_laserPipeline;
[a3cefaa]330
[1abebc1]331 BufferSet storageBuffers_explosionPipeline;
[a3cefaa]332 VulkanBuffer<SSBO_Explosion> objects_explosionPipeline;
[c163d81]333 BufferSet uniformBuffers_explosionPipeline;
[996dd3e]334
[860a0da]335 // TODO: Maybe make the ubo objects part of the pipeline class since there's only one ubo
336 // per pipeline.
337 // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
338 // the objects vector, the ubo, and the ssbo
339
[2ba5617]340 // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
341 // if there is a need to add other uniform variables to one or more of the shaders
342
[2d87297]343 vector<SceneObject<ModelVertex, SSBO_ModelObject>> modelObjects;
[0fe8433]344
345 UBO_VP_mats object_VP_mats;
346
[8d92284]347 vector<SceneObject<ModelVertex, SSBO_ModelObject>> shipObjects;
[0fe8433]348
349 UBO_VP_mats ship_VP_mats;
[0e09340]350
[b8efa56]351 vector<SceneObject<ModelVertex, SSBO_Asteroid>> asteroidObjects;
[3e8cc8b]352
353 UBO_VP_mats asteroid_VP_mats;
354
[237cbec]355 vector<SceneObject<LaserVertex, SSBO_Laser>> laserObjects;
356
357 UBO_VP_mats laser_VP_mats;
358
[4a9416a]359 vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects;
360
361 UBO_Explosion explosion_UBO;
362
[7297892]363 vector<BaseEffectOverTime*> effects;
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;
[b8efa56]372 EffectOverTime<ModelVertex, SSBO_Asteroid>* leftLaserEffect = nullptr;
[1f81ecc]373
374 unsigned int rightLaserIdx = -1;
[b8efa56]375 EffectOverTime<ModelVertex, SSBO_Asteroid>* rightLaserEffect = nullptr;
[1f81ecc]376
[20e4c2b]377 /*** High-level vars ***/
378
[301c90a]379 // TODO: Just typedef the type of this function to RenderScreenFn or something since it's used in a few places
380 void (VulkanGame::* currentRenderScreenFn)(int width, int height);
[20e4c2b]381
382 map<string, vector<UIValue>> valueLists;
383
384 int score;
385 float fps;
386
387 // TODO: Make a separate TImer class
388 time_point<steady_clock> startTime;
389 float fpsStartTime, curTime, prevTime, elapsedTime;
390
391 int frameCount;
392
393 /*** Functions ***/
394
[4e705d6]395 bool initUI(int width, int height, unsigned char guiFlags);
[0df3c9a]396 void initVulkan();
[f97c5e7]397 void initGraphicsPipelines();
[15104a8]398 void initMatrices();
[20e4c2b]399 void renderLoop();
[3f32dfd]400 void updateScene();
[0df3c9a]401 void cleanup();
[c1d9b2a]402
[c324d6a]403 void createVulkanInstance(const vector<const char*>& validationLayers);
[c1d9b2a]404 void setupDebugMessenger();
405 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
[90a424f]406 void createVulkanSurface();
[fe5c3ba]407 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
[fa9fa1c]408 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
[c324d6a]409 void createLogicalDevice(const vector<const char*>& validationLayers,
[c1c2021]410 const vector<const char*>& deviceExtensions);
[3f32dfd]411 void chooseSwapChainProperties();
[502bd0b]412 void createSwapChain();
[f94eea9]413 void createImageViews();
[3f32dfd]414 void createResourceCommandPool();
[603b5bc]415 void createImageResources();
[cefdf23]416 VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section
417 void createRenderPass();
418 void createCommandPools();
[603b5bc]419 void createFramebuffers();
420 void createCommandBuffers();
[34bdf3a]421 void createSyncObjects();
[f94eea9]422
[3f32dfd]423 void createTextureSampler();
424
[cefdf23]425 void initImGuiOverlay();
426 void cleanupImGuiOverlay();
427
[9d21aac]428 // TODO: Maybe move these to a different class, possibly VulkanBuffer or some new related class
429
[8aa4888]430 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags usages, VkMemoryPropertyFlags properties,
[c163d81]431 BufferSet& set);
[9d21aac]432
[bb76950]433 void resizeBufferSet(BufferSet& set, VkDeviceSize newSize, VkCommandPool commandPool, VkQueue graphicsQueue,
434 bool copyData);
[9d21aac]435
436 template<class SSBOType>
[1abebc1]437 void updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo);
[3b7d497]438
[4994692]439 // TODO: Since addObject() returns a reference to the new object now,
440 // stop using objects.back() to access the object that was just created
[2d87297]441 template<class VertexType, class SSBOType>
[9d21aac]442 SceneObject<VertexType, SSBOType>& addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
443 GraphicsPipeline_Vulkan<VertexType>& pipeline,
444 const vector<VertexType>& vertices, vector<uint16_t> indices,
[8dcbf62]445 VulkanBuffer<SSBOType>& objectBuffer, SSBOType ssbo);
[0fe8433]446
[cefdf23]447 template<class VertexType>
448 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
449
450 template<class VertexType>
451 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
452
453 template<class VertexType, class SSBOType>
454 void centerObject(SceneObject<VertexType, SSBOType>& object);
455
[1f81ecc]456 template<class VertexType, class SSBOType>
[9d21aac]457 void updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
[1f81ecc]458 SceneObject<VertexType, SSBOType>& obj, size_t index);
459
[52a02e6]460 void addLaser(vec3 start, vec3 end, vec3 color, float width);
461 void translateLaser(size_t index, const vec3& translation);
462 void updateLaserTarget(size_t index);
[b8efa56]463 bool getLaserAndAsteroidIntersection(SceneObject<ModelVertex, SSBO_Asteroid>& asteroid,
[52a02e6]464 vec3& start, vec3& end, vec3& intersection);
465
[4a9416a]466 void addExplosion(mat4 model_mat, float duration, float cur_time);
467
[ea2b4dc]468 void renderFrame(ImDrawData* draw_data);
[4e2c709]469 void presentFrame();
470
[d2d9286]471 void recreateSwapChain();
472
[c1c2021]473 void cleanupSwapChain();
[20e4c2b]474
475 /*** High-level functions ***/
476
[301c90a]477 void renderMainScreen(int width, int height);
478 void renderGameScreen(int width, int height);
[20e4c2b]479
480 void initGuiValueLists(map<string, vector<UIValue>>& valueLists);
481 void renderGuiValueList(vector<UIValue>& values);
482
[301c90a]483 void goToScreen(void (VulkanGame::* renderScreenFn)(int width, int height));
[20e4c2b]484 void quitGame();
[e8ebc76]485};
486
[4a9416a]487// Start of specialized no-op functions
488
489template<>
490inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) {
491}
492
493// End of specialized no-op functions
494
[9d21aac]495// TODO: See if it makes sense to pass in the current swapchain index instead of updating all of them
496template<class SSBOType>
[1abebc1]497void VulkanGame::updateBufferSet(BufferSet& set, size_t objIndex, SSBOType& ssbo) {
498 for (size_t i = 0; i < set.memory.size(); i++) {
[c074f81]499 VulkanUtils::copyDataToMemory(device, &ssbo, set.memory[i], objIndex * sizeof(SSBOType), sizeof(ssbo), false);
[9d21aac]500 }
501}
502
[b7fc3c2]503// TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model and to change
504// the model matrix later by setting model_transform and then calculating the new ssbo.model.
[9d21aac]505// Figure out a better way to allow the model matrix to be set during object creation
[2d87297]506template<class VertexType, class SSBOType>
[9d21aac]507SceneObject<VertexType, SSBOType>& VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
508 GraphicsPipeline_Vulkan<VertexType>& pipeline,
509 const vector<VertexType>& vertices, vector<uint16_t> indices,
[8dcbf62]510 VulkanBuffer<SSBOType>& objectBuffer, SSBOType ssbo) {
[2ba5617]511 // TODO: Use the model field of ssbo to set the object's model_base
[b7fc3c2]512 // currently, the passed-in model is useless since it gets overridden when ssbo.model is recalculated
[0fe8433]513 size_t numVertices = pipeline.getNumVertices();
514
515 for (uint16_t& idx : indices) {
516 idx += numVertices;
517 }
518
[c1ec4f6]519 objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
[8dcbf62]520 objectBuffer.add(ssbo);
[3b84bb6]521
[2ba5617]522 SceneObject<VertexType, SSBOType>& obj = objects.back();
[1f81ecc]523
[cefdf23]524 // TODO: Specify whether to center the object outside of this function or, worst case, maybe
525 // with a boolean being passed in here, so that I don't have to rely on checking the specific object
526 // type
[8dcbf62]527 // TODO: Actually, I've already defined a no-op centerObject method for explosions
528 // Maybe I should do the same for lasers and remove this conditional altogether
[4a9416a]529 if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
[1f81ecc]530 centerObject(obj);
531 }
[2ba5617]532
[9d21aac]533 pipeline.addObject(obj.vertices, obj.indices, resourceCommandPool, graphicsQueue);
534
[4994692]535 return obj;
[0fe8433]536}
537
[cefdf23]538template<class VertexType>
539vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
540 for (VertexType& vertex : vertices) {
541 vertex.objIndex = objIndex;
542 }
[2da64ef]543
[cefdf23]544 return vertices;
[1f81ecc]545}
546
[914bb99]547// This function sets all the normals for a face to be parallel
548// This is good for models that should have distinct faces, but bad for models that should appear smooth
549// Maybe add an option to set all copies of a point to have the same normal and have the direction of
550// that normal be the weighted average of all the faces it is a part of, where the weight from each face
551// is its surface area.
552
553// TODO: Since the current approach to normal calculation basicaly makes indexed drawing useless, see if it's
554// feasible to automatically enable/disable indexed drawing based on which approach is used
[06d959f]555template<class VertexType>
556vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
557 for (unsigned int i = 0; i < vertices.size(); i += 3) {
558 vec3 p1 = vertices[i].pos;
[cefdf23]559 vec3 p2 = vertices[i + 1].pos;
560 vec3 p3 = vertices[i + 2].pos;
[06d959f]561
[a79be34]562 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
[06d959f]563
564 // Add the same normal for all 3 vertices
565 vertices[i].normal = normal;
[cefdf23]566 vertices[i + 1].normal = normal;
567 vertices[i + 2].normal = normal;
[cf727ca]568 }
569
570 return vertices;
571}
572
[3b84bb6]573template<class VertexType, class SSBOType>
574void VulkanGame::centerObject(SceneObject<VertexType, SSBOType>& object) {
575 vector<VertexType>& vertices = object.vertices;
576
[a79be34]577 float min_x = vertices[0].pos.x;
578 float max_x = vertices[0].pos.x;
579 float min_y = vertices[0].pos.y;
580 float max_y = vertices[0].pos.y;
581 float min_z = vertices[0].pos.z;
582 float max_z = vertices[0].pos.z;
583
584 // start from the second point
585 for (unsigned int i = 1; i < vertices.size(); i++) {
[3b84bb6]586 vec3& pos = vertices[i].pos;
587
588 if (min_x > pos.x) {
589 min_x = pos.x;
590 } else if (max_x < pos.x) {
591 max_x = pos.x;
[a79be34]592 }
593
[3b84bb6]594 if (min_y > pos.y) {
595 min_y = pos.y;
596 } else if (max_y < pos.y) {
597 max_y = pos.y;
[a79be34]598 }
599
[3b84bb6]600 if (min_z > pos.z) {
601 min_z = pos.z;
602 } else if (max_z < pos.z) {
603 max_z = pos.z;
[a79be34]604 }
605 }
606
607 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
608
609 for (unsigned int i = 0; i < vertices.size(); i++) {
610 vertices[i].pos -= center;
611 }
612
[2ba5617]613 object.radius = std::max(max_x - center.x, max_y - center.y);
614 object.radius = std::max(object.radius, max_z - center.z);
615
[3b84bb6]616 object.center = vec3(0.0f, 0.0f, 0.0f);
[a79be34]617}
618
[cefdf23]619template<class VertexType, class SSBOType>
[9d21aac]620void VulkanGame::updateObjectVertices(GraphicsPipeline_Vulkan<VertexType>& pipeline,
[cefdf23]621 SceneObject<VertexType, SSBOType>& obj, size_t index) {
622 pipeline.updateObjectVertices(index, obj.vertices, resourceCommandPool, graphicsQueue);
623}
624
[3b84bb6]625#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.