source: opengl-game/vulkan-game.hpp@ 3e8cc8b

feature/imgui-sdl points-test
Last change on this file since 3e8cc8b was 3e8cc8b, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

In VulkanGame, add the asteroid shader pipeline and start implementing asteroids flying at the player from the top of the screen

  • Property mode set to 100644
File size: 10.0 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
42struct AsteroidVertex {
43 vec3 pos;
44 vec3 color;
45 vec3 normal;
46 unsigned int objIndex;
47};
48
49// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
50// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
51template<class VertexType>
52struct SceneObject {
53 vector<VertexType> vertices;
54 vector<uint16_t> indices;
55
56 mat4 model_base;
57 mat4 model_transform;
58};
59
60struct UBO_VP_mats {
61 alignas(16) mat4 view;
62 alignas(16) mat4 proj;
63};
64
65struct SBO_SceneObject {
66 alignas(16) mat4 model;
67};
68
69struct SBO_Asteroid {
70 alignas(16) mat4 model;
71 alignas(4) float hp;
72};
73
74class VulkanGame {
75 public:
76 VulkanGame(int maxFramesInFlight);
77 ~VulkanGame();
78
79 void run(int width, int height, unsigned char guiFlags);
80
81 private:
82 const int MAX_FRAMES_IN_FLIGHT;
83
84 const float NEAR_CLIP = 0.1f;
85 const float FAR_CLIP = 100.0f;
86 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
87
88 vec3 cam_pos;
89
90 GameGui* gui;
91
92 SDL_version sdlVersion;
93 SDL_Window* window = nullptr;
94 SDL_Renderer* renderer = nullptr;
95
96 SDL_Texture* uiOverlay = nullptr;
97
98 VkInstance instance;
99 VkDebugUtilsMessengerEXT debugMessenger;
100 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
101 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
102 VkDevice device;
103
104 VkQueue graphicsQueue;
105 VkQueue presentQueue;
106
107 VkSwapchainKHR swapChain;
108 vector<VkImage> swapChainImages;
109 VkFormat swapChainImageFormat;
110 VkExtent2D swapChainExtent;
111 vector<VkImageView> swapChainImageViews;
112 vector<VkFramebuffer> swapChainFramebuffers;
113
114 VkRenderPass renderPass;
115 VkCommandPool commandPool;
116 vector<VkCommandBuffer> commandBuffers;
117
118 VulkanImage depthImage;
119
120 VkSampler textureSampler;
121
122 VulkanImage floorTextureImage;
123 VkDescriptorImageInfo floorTextureImageDescriptor;
124
125 VulkanImage sdlOverlayImage;
126 VkDescriptorImageInfo sdlOverlayImageDescriptor;
127
128 TTF_Font* font;
129 SDL_Texture* fontSDLTexture;
130
131 SDL_Texture* imageSDLTexture;
132
133 vector<VkSemaphore> imageAvailableSemaphores;
134 vector<VkSemaphore> renderFinishedSemaphores;
135 vector<VkFence> inFlightFences;
136
137 size_t currentFrame;
138
139 bool framebufferResized;
140
141 // TODO: I should probably rename the uniformBuffer* and storageBuffer*
142 // variables to better reflect the data they hold
143
144 // TODO: Create a struct that holds the buffers, memory, and info objects (Probably in VulkanUtils)
145
146 GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
147
148 vector<SceneObject<OverlayVertex>> overlayObjects;
149
150 // TODO: Rename all the variables related to modelPipeline to use the same pipelie name
151
152 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
153
154 vector<SceneObject<ModelVertex>> modelObjects;
155
156 vector<VkBuffer> uniformBuffers_scenePipeline;
157 vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
158 vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
159
160 vector<VkBuffer> storageBuffers_scenePipeline;
161 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
162 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
163
164 UBO_VP_mats object_VP_mats;
165 SBO_SceneObject so_Object;
166
167 GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
168
169 vector<SceneObject<ShipVertex>> shipObjects;
170
171 vector<VkBuffer> uniformBuffers_shipPipeline;
172 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
173 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
174
175 vector<VkBuffer> storageBuffers_shipPipeline;
176 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
177 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
178
179 UBO_VP_mats ship_VP_mats;
180 SBO_SceneObject so_Ship;
181
182 GraphicsPipeline_Vulkan<AsteroidVertex> asteroidPipeline;
183
184 vector<SceneObject<AsteroidVertex>> asteroidObjects;
185
186 vector<VkBuffer> uniformBuffers_asteroidPipeline;
187 vector<VkDeviceMemory> uniformBuffersMemory_asteroidPipeline;
188 vector<VkDescriptorBufferInfo> uniformBufferInfoList_asteroidPipeline;
189
190 vector<VkBuffer> storageBuffers_asteroidPipeline;
191 vector<VkDeviceMemory> storageBuffersMemory_asteroidPipeline;
192 vector<VkDescriptorBufferInfo> storageBufferInfoList_asteroidPipeline;
193
194 UBO_VP_mats asteroid_VP_mats;
195 SBO_Asteroid so_Asteroid;
196
197 bool initWindow(int width, int height, unsigned char guiFlags);
198 void initVulkan();
199 void initGraphicsPipelines();
200 void initMatrices();
201 void mainLoop();
202 void updateScene(uint32_t currentImage);
203 void renderUI();
204 void renderScene();
205 void cleanup();
206
207 void createVulkanInstance(const vector<const char*> &validationLayers);
208 void setupDebugMessenger();
209 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
210 void createVulkanSurface();
211 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
212 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
213 void createLogicalDevice(
214 const vector<const char*> validationLayers,
215 const vector<const char*>& deviceExtensions);
216 void createSwapChain();
217 void createImageViews();
218 void createRenderPass();
219 VkFormat findDepthFormat();
220 void createCommandPool();
221 void createImageResources();
222
223 void createTextureSampler();
224 void createFramebuffers();
225 void createCommandBuffers();
226 void createSyncObjects();
227
228 template<class VertexType>
229 void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
230 const vector<VertexType>& vertices, vector<uint16_t> indices);
231
232 template<class VertexType>
233 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
234
235 template<class VertexType>
236 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
237
238 template<class VertexType>
239 vector<VertexType> centerObject(vector<VertexType> vertices);
240
241 template<class VertexType>
242 void transformObject(SceneObject<VertexType>& obj, mat4 mat);
243
244 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
245 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
246
247 void recreateSwapChain();
248
249 void cleanupSwapChain();
250
251 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
252 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
253 VkDebugUtilsMessageTypeFlagsEXT messageType,
254 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
255 void* pUserData);
256};
257
258template<class VertexType>
259void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
260 const vector<VertexType>& vertices, vector<uint16_t> indices) {
261 size_t numVertices = pipeline.getNumVertices();
262
263 for (uint16_t& idx : indices) {
264 idx += numVertices;
265 }
266
267 objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
268
269 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
270}
271
272template<class VertexType>
273vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
274 for (unsigned int i = 0; i < vertices.size(); i += 3) {
275 vec3 p1 = vertices[i].pos;
276 vec3 p2 = vertices[i+1].pos;
277 vec3 p3 = vertices[i+2].pos;
278
279 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
280
281 // Add the same normal for all 3 vertices
282 vertices[i].normal = normal;
283 vertices[i+1].normal = normal;
284 vertices[i+2].normal = normal;
285 }
286
287 return vertices;
288}
289
290template<class VertexType>
291vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
292 for (VertexType& vertex : vertices) {
293 vertex.objIndex = objIndex;
294 }
295
296 return vertices;
297}
298
299template<class VertexType>
300vector<VertexType> VulkanGame::centerObject(vector<VertexType> vertices) {
301 float min_x = vertices[0].pos.x;
302 float max_x = vertices[0].pos.x;
303 float min_y = vertices[0].pos.y;
304 float max_y = vertices[0].pos.y;
305 float min_z = vertices[0].pos.z;
306 float max_z = vertices[0].pos.z;
307
308 // start from the second point
309 for (unsigned int i = 1; i < vertices.size(); i++) {
310 if (min_x > vertices[i].pos.x) {
311 min_x = vertices[i].pos.x;
312 } else if (max_x < vertices[i].pos.x) {
313 max_x = vertices[i].pos.x;
314 }
315
316 if (min_y > vertices[i].pos.y) {
317 min_y = vertices[i].pos.y;
318 } else if (max_y < vertices[i].pos.y) {
319 max_y = vertices[i].pos.y;
320 }
321
322 if (min_z > vertices[i].pos.z) {
323 min_z = vertices[i].pos.z;
324 } else if (max_z < vertices[i].pos.z) {
325 max_z = vertices[i].pos.z;
326 }
327 }
328
329 vec3 center = vec3(min_x + max_x, min_y + max_y, min_z + max_z) / 2.0f;
330
331 for (unsigned int i = 0; i < vertices.size(); i++) {
332 vertices[i].pos -= center;
333 }
334
335 return vertices;
336}
337
338template<class VertexType>
339void VulkanGame::transformObject(SceneObject<VertexType>& obj, mat4 mat) {
340 obj.model_transform = mat * obj.model_transform;
341}
342
343#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.