source: opengl-game/vulkan-game.hpp@ 0fe8433

feature/imgui-sdl points-test
Last change on this file since 0fe8433 was 0fe8433, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Create an addObject() method in VulkanGame (which wraps the old addObject() method in GraphicsPipeline_Vulkan), and move the list of objects and other code to manage objects from the pipeline to the VulkanGame class

  • Property mode set to 100644
File size: 9.2 KB
RevLine 
[99d44b2]1#ifndef _VULKAN_GAME_H
2#define _VULKAN_GAME_H
[e8ebc76]3
[60578ce]4#define GLM_FORCE_RADIANS
5#define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
[a79be34]6#define GLM_FORCE_RIGHT_HANDED
[60578ce]7
[771b33a]8#include <glm/glm.hpp>
[15104a8]9#include <glm/gtc/matrix_transform.hpp>
[771b33a]10
[0df3c9a]11#include "game-gui-sdl.hpp"
[7d2b0b9]12#include "graphics-pipeline_vulkan.hpp"
[0df3c9a]13
[b794178]14#include "vulkan-utils.hpp"
15
[15104a8]16using namespace glm;
17
[2e77b3f]18#ifdef NDEBUG
19 const bool ENABLE_VALIDATION_LAYERS = false;
20#else
21 const bool ENABLE_VALIDATION_LAYERS = true;
22#endif
23
[b8777b7]24struct ModelVertex {
[15104a8]25 vec3 pos;
26 vec3 color;
27 vec2 texCoord;
[771b33a]28};
29
30struct OverlayVertex {
[15104a8]31 vec3 pos;
32 vec2 texCoord;
33};
34
[3782d66]35struct ShipVertex {
36 vec3 pos;
37 vec3 color;
[06d959f]38 vec3 normal;
[cf727ca]39 unsigned int objIndex;
[3782d66]40};
41
[0fe8433]42// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
43// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
44template<class VertexType>
45struct SceneObject {
46 vector<VertexType> vertices;
47 vector<uint16_t> indices;
48
49 mat4 model_base;
50 mat4 model_transform;
51};
52
[055750a]53struct UBO_VP_mats {
[15104a8]54 alignas(16) mat4 view;
55 alignas(16) mat4 proj;
[771b33a]56};
57
[055750a]58struct SBO_SceneObject {
59 alignas(16) mat4 model;
60};
61
[99d44b2]62class VulkanGame {
[e8ebc76]63 public:
[34bdf3a]64 VulkanGame(int maxFramesInFlight);
[99d44b2]65 ~VulkanGame();
[0df3c9a]66
[b6e60b4]67 void run(int width, int height, unsigned char guiFlags);
[0df3c9a]68
69 private:
[34bdf3a]70 const int MAX_FRAMES_IN_FLIGHT;
71
[5ab1b20]72 const float NEAR_CLIP = 0.1f;
73 const float FAR_CLIP = 100.0f;
[60578ce]74 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
[5ab1b20]75
[15104a8]76 vec3 cam_pos;
77
[0df3c9a]78 GameGui* gui;
[c559904]79
80 SDL_version sdlVersion;
[b794178]81 SDL_Window* window = nullptr;
82 SDL_Renderer* renderer = nullptr;
83
84 SDL_Texture* uiOverlay = nullptr;
[c1d9b2a]85
86 VkInstance instance;
87 VkDebugUtilsMessengerEXT debugMessenger;
[fe5c3ba]88 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
[90a424f]89 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
[c1c2021]90 VkDevice device;
91
92 VkQueue graphicsQueue;
93 VkQueue presentQueue;
[0df3c9a]94
[502bd0b]95 VkSwapchainKHR swapChain;
96 vector<VkImage> swapChainImages;
97 VkFormat swapChainImageFormat;
[603b5bc]98 VkExtent2D swapChainExtent;
[f94eea9]99 vector<VkImageView> swapChainImageViews;
[603b5bc]100 vector<VkFramebuffer> swapChainFramebuffers;
[fa9fa1c]101
[6fc24c7]102 VkRenderPass renderPass;
[fa9fa1c]103 VkCommandPool commandPool;
[603b5bc]104 vector<VkCommandBuffer> commandBuffers;
[502bd0b]105
[603b5bc]106 VulkanImage depthImage;
[b794178]107
108 VkSampler textureSampler;
109
[0fe8433]110 VulkanImage floorTextureImage;
111 VkDescriptorImageInfo floorTextureImageDescriptor;
112
113 VulkanImage sdlOverlayImage;
114 VkDescriptorImageInfo sdlOverlayImageDescriptor;
115
116 TTF_Font* font;
117 SDL_Texture* fontSDLTexture;
118
119 SDL_Texture* imageSDLTexture;
120
121 vector<VkSemaphore> imageAvailableSemaphores;
122 vector<VkSemaphore> renderFinishedSemaphores;
123 vector<VkFence> inFlightFences;
124
125 size_t currentFrame;
126
127 bool framebufferResized;
128
[055750a]129 // TODO: I should probably rename the uniformBuffer* and storageBuffer*
130 // variables to better reflect the data they hold
131
[0fe8433]132 GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
133
134 vector<SceneObject<OverlayVertex>> overlayObjects;
135
136 // TODO: Rename all the variables related to modelPipeline to use the same pipelie name
137
138 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
139
140 vector<SceneObject<ModelVertex>> modelObjects;
141
[055750a]142 vector<VkBuffer> uniformBuffers_scenePipeline;
143 vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
144
145 vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
[b794178]146
[055750a]147 vector<VkBuffer> storageBuffers_scenePipeline;
148 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
149
150 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
[f97c5e7]151
[0fe8433]152 UBO_VP_mats object_VP_mats;
153 SBO_SceneObject so_Object;
154
155 GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
156
157 vector<SceneObject<ShipVertex>> shipObjects;
158
[3782d66]159 vector<VkBuffer> uniformBuffers_shipPipeline;
160 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
161
162 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
163
[055750a]164 vector<VkBuffer> storageBuffers_shipPipeline;
165 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
166
167 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
168
[0fe8433]169 UBO_VP_mats ship_VP_mats;
170 SBO_SceneObject so_Ship;
[0e09340]171
[b6e60b4]172 bool initWindow(int width, int height, unsigned char guiFlags);
[0df3c9a]173 void initVulkan();
[f97c5e7]174 void initGraphicsPipelines();
[15104a8]175 void initMatrices();
[0df3c9a]176 void mainLoop();
[8e02b6b]177 void updateScene(uint32_t currentImage);
[a0c5f28]178 void renderUI();
179 void renderScene();
[0df3c9a]180 void cleanup();
[c1d9b2a]181
182 void createVulkanInstance(const vector<const char*> &validationLayers);
183 void setupDebugMessenger();
184 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
[90a424f]185 void createVulkanSurface();
[fe5c3ba]186 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
[fa9fa1c]187 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
[c1c2021]188 void createLogicalDevice(
189 const vector<const char*> validationLayers,
190 const vector<const char*>& deviceExtensions);
[502bd0b]191 void createSwapChain();
[f94eea9]192 void createImageViews();
[6fc24c7]193 void createRenderPass();
194 VkFormat findDepthFormat();
[fa9fa1c]195 void createCommandPool();
[603b5bc]196 void createImageResources();
197
[b794178]198 void createTextureSampler();
[603b5bc]199 void createFramebuffers();
200 void createCommandBuffers();
[34bdf3a]201 void createSyncObjects();
[f94eea9]202
[0fe8433]203 template<class VertexType>
204 void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
205 const vector<VertexType>& vertices, vector<uint16_t> indices);
206
[06d959f]207 template<class VertexType>
208 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
209
[cf727ca]210 template<class VertexType>
211 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
212
[a79be34]213 template<class VertexType>
214 vector<VertexType> centerObject(vector<VertexType> vertices);
215
[cd1cb0f]216 template<class VertexType>
217 void transformObject(SceneObject<VertexType>& obj, mat4 mat);
218
[055750a]219 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
220 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
[f97c5e7]221
[d2d9286]222 void recreateSwapChain();
223
[c1c2021]224 void cleanupSwapChain();
[c1d9b2a]225
226 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
227 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
228 VkDebugUtilsMessageTypeFlagsEXT messageType,
229 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
230 void* pUserData);
[e8ebc76]231};
232
[0fe8433]233template<class VertexType>
234void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
235 const vector<VertexType>& vertices, vector<uint16_t> indices) {
236 size_t numVertices = pipeline.getNumVertices();
237
238 for (uint16_t& idx : indices) {
239 idx += numVertices;
240 }
241
242 objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
243
244 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
245}
246
[06d959f]247template<class VertexType>
248vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
249 for (unsigned int i = 0; i < vertices.size(); i += 3) {
250 vec3 p1 = vertices[i].pos;
251 vec3 p2 = vertices[i+1].pos;
252 vec3 p3 = vertices[i+2].pos;
253
[a79be34]254 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
[06d959f]255
256 // Add the same normal for all 3 vertices
257 vertices[i].normal = normal;
258 vertices[i+1].normal = normal;
259 vertices[i+2].normal = normal;
260 }
261
262 return vertices;
263}
264
[cf727ca]265template<class VertexType>
266vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
267 for (VertexType& vertex : vertices) {
268 vertex.objIndex = objIndex;
269 }
270
271 return vertices;
272}
273
[a79be34]274template<class VertexType>
275vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
276 float min_x = vertices[0].pos.x;
277 float max_x = vertices[0].pos.x;
278 float min_y = vertices[0].pos.y;
279 float max_y = vertices[0].pos.y;
280 float min_z = vertices[0].pos.z;
281 float max_z = vertices[0].pos.z;
282
283 // start from the second point
284 for (unsigned int i = 1; i < vertices.size(); i++) {
285 if (min_x > vertices[i].pos.x) {
286 min_x = vertices[i].pos.x;
287 } else if (max_x < vertices[i].pos.x) {
288 max_x = vertices[i].pos.x;
289 }
290
291 if (min_y > vertices[i].pos.y) {
292 min_y = vertices[i].pos.y;
293 } else if (max_y < vertices[i].pos.y) {
294 max_y = vertices[i].pos.y;
295 }
296
297 if (min_z > vertices[i].pos.z) {
298 min_z = vertices[i].pos.z;
299 } else if (max_z < vertices[i].pos.z) {
300 max_z = vertices[i].pos.z;
301 }
302 }
303
304 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
305
306 for (unsigned int i = 0; i < vertices.size(); i++) {
307 vertices[i].pos -= center;
308 }
309
310 return vertices;
311}
312
[cd1cb0f]313template<class VertexType>
314void VulkanGame::transformObject(SceneObject<VertexType>& obj, mat4 mat) {
315 obj.model_transform = mat * obj.model_transform;
316}
317
[99d44b2]318#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.