diff --git a/src/camera.h b/src/engine/camera.h similarity index 91% rename from src/camera.h rename to src/engine/camera.h index 1163e69..babd7ae 100644 --- a/src/camera.h +++ b/src/engine/camera.h @@ -5,14 +5,7 @@ #include #include -const float CAMERA_FOV = 60.0f; -const float SENSITIVITY = 0.1f; -const float SPEED = 2.5f; -const float PLAYER_HEIGHT = 0.5f; -const float MAP_FLOOR = 0.0f; -const float YAW = 0.0f; -const float PITCH = 0.0f; -const float ZOOM = 45.0f; +#include "../global.h" enum Camera_Movement { FORWARD, @@ -107,5 +100,6 @@ private: Up = glm::normalize(glm::cross(Right, Front)); // down which results in slower movement. } }; +Camera camera(glm::vec3(ORIGINX, MAP_FLOOR + PLAYER_HEIGHT, ORIGINZ)); #endif diff --git a/src/global.h b/src/global.h new file mode 100644 index 0000000..95897f5 --- /dev/null +++ b/src/global.h @@ -0,0 +1,31 @@ +#ifndef GLOBAL_H +#define GLOBAL_H + +#include +#include + +const uint32_t SCR_WIDTH = 800; +const uint32_t SCR_HEIGHT = 600; +const std::string MODEL_PATH = "res/models/viking_room.obj"; +const std::string TEXTURE_PATH = "res/textures/viking_room.png"; +const int MAX_FRAMES_IN_FLIGHT = 1; + +const float CAMERA_FOV = 60.0f; +const float SENSITIVITY = 0.1f; +const float SPEED = 1.5f; +const float PLAYER_HEIGHT = 0.5f; +const float MAP_FLOOR = 0.0f; +const float YAW = 0.0f; +const float PITCH = 0.0f; +const float ZOOM = 45.0f; +const float ORIGINX = 0.5f; +const float ORIGINZ = -0.5f; + +float deltaTime = 0.0f; // time between current frame and last frame +float lastFrame = 0.0f; + +float lastX = SCR_WIDTH / 2.0f; +float lastY = SCR_HEIGHT / 2.0f; +bool firstMouse = true; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 4f0d422..0821073 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,8 @@ -#include "application.h" -#include "camera.h" +#include + +#include "global.h" +#include "vulkan/application.h" +#include "engine/camera.h" int main() { Application app; diff --git a/src/application.h b/src/vulkan/application.h similarity index 94% rename from src/application.h rename to src/vulkan/application.h index 07a69fb..a25a02d 100644 --- a/src/application.h +++ b/src/vulkan/application.h @@ -27,117 +27,10 @@ #include #include -#include "camera.h" - -const uint32_t SCR_WIDTH = 800; -const uint32_t SCR_HEIGHT = 600; -const std::string MODEL_PATH = "res/models/viking_room.obj"; -const std::string TEXTURE_PATH = "res/textures/viking_room.png"; -const int MAX_FRAMES_IN_FLIGHT = 1; - -void mouse_callback(GLFWwindow* window, double xpos, double ypos); -void processInput(GLFWwindow *window); -float deltaTime = 0.0f; // time between current frame and last frame -float lastFrame = 0.0f; - -Camera camera(glm::vec3(0.0f, 0.0f, -3.0f)); -float lastX = SCR_WIDTH / 2.0f; -float lastY = SCR_HEIGHT / 2.0f; -bool firstMouse = true; - -const std::vector validationLayers = {"VK_LAYER_KHRONOS_validation"}; -const std::vector deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; - -#ifdef NDEBUG -const bool enableValidationLayers = false; -#else -const bool enableValidationLayers = true; -#endif - -VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger) { - auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); - if (func != nullptr) { - return func(instance, pCreateInfo, pAllocator, pDebugMessenger); - } else { - return VK_ERROR_EXTENSION_NOT_PRESENT; - } -} - -void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator) { - auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); - if (func != nullptr) { - func(instance, debugMessenger, pAllocator); - } -} - -struct QueueFamilyIndices { - std::optional graphicsFamily; - std::optional presentFamily; - - bool isComplete() { - return graphicsFamily.has_value() && presentFamily.has_value(); - } -}; - -struct SwapChainSupportDetails { - VkSurfaceCapabilitiesKHR capabilities; - std::vector formats; - std::vector presentModes; -}; - -struct Vertex { - glm::vec3 pos; - glm::vec3 color; - glm::vec2 texCoord; - - static VkVertexInputBindingDescription getBindingDescription() { - VkVertexInputBindingDescription bindingDescription{}; - bindingDescription.binding = 0; - bindingDescription.stride = sizeof(Vertex); - bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; - - return bindingDescription; - } - - static std::array getAttributeDescriptions() { - std::array attributeDescriptions{}; - - attributeDescriptions[0].binding = 0; - attributeDescriptions[0].location = 0; - attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[0].offset = offsetof(Vertex, pos); - - attributeDescriptions[1].binding = 0; - attributeDescriptions[1].location = 1; - attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[1].offset = offsetof(Vertex, color); - - attributeDescriptions[2].binding = 0; - attributeDescriptions[2].location = 2; - attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT; - attributeDescriptions[2].offset = offsetof(Vertex, texCoord); - - return attributeDescriptions; - } - - bool operator==(const Vertex& other) const { - return pos == other.pos && color == other.color && texCoord == other.texCoord; - } -}; - -namespace std { - template<> struct hash { - size_t operator()(Vertex const& vertex) const { - return ((hash()(vertex.pos) ^ (hash()(vertex.color) << 1)) >> 1) ^ (hash()(vertex.texCoord) << 1); - } - }; -} - -struct UniformBufferObject { - alignas(16) glm::mat4 model; - alignas(16) glm::mat4 view; - alignas(16) glm::mat4 proj; -}; +#include "../global.h" +#include "../engine/camera.h" +#include "vkvalidationlayers.h" +#include "vkvertexbuffers.h" class Application { public: diff --git a/src/vulkan/vkvalidationlayers.h b/src/vulkan/vkvalidationlayers.h new file mode 100644 index 0000000..9cbfd5a --- /dev/null +++ b/src/vulkan/vkvalidationlayers.h @@ -0,0 +1,34 @@ +#ifndef VKVALIDATIONLAYERS_H +#define VKVALIDATIONLAYERS_H + +#define GLFW_INCLUDE_VULKAN +#include +#include +#include + +const std::vector validationLayers = {"VK_LAYER_KHRONOS_validation"}; +const std::vector deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; + +#ifdef NDEBUG +const bool enableValidationLayers = false; +#else +const bool enableValidationLayers = true; +#endif + +VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger) { + auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); + if (func != nullptr) { + return func(instance, pCreateInfo, pAllocator, pDebugMessenger); + } else { + return VK_ERROR_EXTENSION_NOT_PRESENT; + } +} + +void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator) { + auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); + if (func != nullptr) { + func(instance, debugMessenger, pAllocator); + } +} + +#endif diff --git a/src/vulkan/vkvertexbuffers.h b/src/vulkan/vkvertexbuffers.h new file mode 100644 index 0000000..35b542f --- /dev/null +++ b/src/vulkan/vkvertexbuffers.h @@ -0,0 +1,84 @@ +#ifndef VKVERTEXBUFFERS_H +#define VKVERTEXBUFFERS_H + +#define GLFW_INCLUDE_VULKAN +#include +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#define GLM_ENABLE_EXPERIMENTAL +#include +#include +#include +#include +#include + +struct QueueFamilyIndices { + std::optional graphicsFamily; + std::optional presentFamily; + + bool isComplete() { + return graphicsFamily.has_value() && presentFamily.has_value(); + } +}; + +struct SwapChainSupportDetails { + VkSurfaceCapabilitiesKHR capabilities; + std::vector formats; + std::vector presentModes; +}; + +struct Vertex { + glm::vec3 pos; + glm::vec3 color; + glm::vec2 texCoord; + + static VkVertexInputBindingDescription getBindingDescription() { + VkVertexInputBindingDescription bindingDescription{}; + bindingDescription.binding = 0; + bindingDescription.stride = sizeof(Vertex); + bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + + return bindingDescription; + } + + static std::array getAttributeDescriptions() { + std::array attributeDescriptions{}; + + attributeDescriptions[0].binding = 0; + attributeDescriptions[0].location = 0; + attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[0].offset = offsetof(Vertex, pos); + + attributeDescriptions[1].binding = 0; + attributeDescriptions[1].location = 1; + attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[1].offset = offsetof(Vertex, color); + + attributeDescriptions[2].binding = 0; + attributeDescriptions[2].location = 2; + attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT; + attributeDescriptions[2].offset = offsetof(Vertex, texCoord); + + return attributeDescriptions; + } + + bool operator==(const Vertex& other) const { + return pos == other.pos && color == other.color && texCoord == other.texCoord; + } +}; + +namespace std { + template<> struct hash { + size_t operator()(Vertex const& vertex) const { + return ((hash()(vertex.pos) ^ (hash()(vertex.color) << 1)) >> 1) ^ (hash()(vertex.texCoord) << 1); + } + }; +} + +struct UniformBufferObject { + alignas(16) glm::mat4 model; + alignas(16) glm::mat4 view; + alignas(16) glm::mat4 proj; +}; + +#endif