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
Line 
1#ifndef _VULKAN_GAME_H
2#define _VULKAN_GAME_H
3
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
6#define GLM_FORCE_RIGHT_HANDED
7
8#include <glm/glm.hpp>
9#include <glm/gtc/matrix_transform.hpp>
10
11#include "game-gui-sdl.hpp"
12#include "graphics-pipeline_vulkan.hpp"
13
14#include "vulkan-utils.hpp"
15
16using namespace glm;
17
18#ifdef NDEBUG
19 const bool ENABLE_VALIDATION_LAYERS = false;
20#else
21 const bool ENABLE_VALIDATION_LAYERS = true;
22#endif
23
24struct ModelVertex {
25 vec3 pos;
26 vec3 color;
27 vec2 texCoord;
28};
29
30struct OverlayVertex {
31 vec3 pos;
32 vec2 texCoord;
33};
34
35struct ShipVertex {
36 vec3 pos;
37 vec3 color;
38 vec3 normal;
39 unsigned int objIndex;
40};
41
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
53struct UBO_VP_mats {
54 alignas(16) mat4 view;
55 alignas(16) mat4 proj;
56};
57
58struct SBO_SceneObject {
59 alignas(16) mat4 model;
60};
61
62class VulkanGame {
63 public:
64 VulkanGame(int maxFramesInFlight);
65 ~VulkanGame();
66
67 void run(int width, int height, unsigned char guiFlags);
68
69 private:
70 const int MAX_FRAMES_IN_FLIGHT;
71
72 const float NEAR_CLIP = 0.1f;
73 const float FAR_CLIP = 100.0f;
74 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
75
76 vec3 cam_pos;
77
78 GameGui* gui;
79
80 SDL_version sdlVersion;
81 SDL_Window* window = nullptr;
82 SDL_Renderer* renderer = nullptr;
83
84 SDL_Texture* uiOverlay = nullptr;
85
86 VkInstance instance;
87 VkDebugUtilsMessengerEXT debugMessenger;
88 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
89 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
90 VkDevice device;
91
92 VkQueue graphicsQueue;
93 VkQueue presentQueue;
94
95 VkSwapchainKHR swapChain;
96 vector<VkImage> swapChainImages;
97 VkFormat swapChainImageFormat;
98 VkExtent2D swapChainExtent;
99 vector<VkImageView> swapChainImageViews;
100 vector<VkFramebuffer> swapChainFramebuffers;
101
102 VkRenderPass renderPass;
103 VkCommandPool commandPool;
104 vector<VkCommandBuffer> commandBuffers;
105
106 VulkanImage depthImage;
107
108 VkSampler textureSampler;
109
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
129 // TODO: I should probably rename the uniformBuffer* and storageBuffer*
130 // variables to better reflect the data they hold
131
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
142 vector<VkBuffer> uniformBuffers_scenePipeline;
143 vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
144
145 vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
146
147 vector<VkBuffer> storageBuffers_scenePipeline;
148 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
149
150 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
151
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
159 vector<VkBuffer> uniformBuffers_shipPipeline;
160 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
161
162 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
163
164 vector<VkBuffer> storageBuffers_shipPipeline;
165 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
166
167 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
168
169 UBO_VP_mats ship_VP_mats;
170 SBO_SceneObject so_Ship;
171
172 bool initWindow(int width, int height, unsigned char guiFlags);
173 void initVulkan();
174 void initGraphicsPipelines();
175 void initMatrices();
176 void mainLoop();
177 void updateScene(uint32_t currentImage);
178 void renderUI();
179 void renderScene();
180 void cleanup();
181
182 void createVulkanInstance(const vector<const char*> &validationLayers);
183 void setupDebugMessenger();
184 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
185 void createVulkanSurface();
186 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
187 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
188 void createLogicalDevice(
189 const vector<const char*> validationLayers,
190 const vector<const char*>& deviceExtensions);
191 void createSwapChain();
192 void createImageViews();
193 void createRenderPass();
194 VkFormat findDepthFormat();
195 void createCommandPool();
196 void createImageResources();
197
198 void createTextureSampler();
199 void createFramebuffers();
200 void createCommandBuffers();
201 void createSyncObjects();
202
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
207 template<class VertexType>
208 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
209
210 template<class VertexType>
211 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
212
213 template<class VertexType>
214 vector<VertexType> centerObject(vector<VertexType> vertices);
215
216 template<class VertexType>
217 void transformObject(SceneObject<VertexType>& obj, mat4 mat);
218
219 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
220 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
221
222 void recreateSwapChain();
223
224 void cleanupSwapChain();
225
226 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
227 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
228 VkDebugUtilsMessageTypeFlagsEXT messageType,
229 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
230 void* pUserData);
231};
232
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
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
254 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
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
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
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
313template<class VertexType>
314void VulkanGame::transformObject(SceneObject<VertexType>& obj, mat4 mat) {
315 obj.model_transform = mat * obj.model_transform;
316}
317
318#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.