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

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

In VulkanGame, make the ship move when the player holds down the right or left arrow keys

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