Added camera

This commit is contained in:
Ano-sys
2025-07-04 00:55:50 +02:00
parent 318263051a
commit 6bc2ea1650
7 changed files with 285 additions and 250 deletions
+28 -51
View File
@@ -27,35 +27,18 @@
#include <chrono>
#include <unordered_map>
namespace vapp
{
class Camera
{
private:
glm::mat4 projectionMatrix{1.f};
glm::mat4 viewMatrix{1.f};
public:
void setOthographicProjection(float left, float right, float bottom, float top);
void setPerspectiveProjection(float fovy, float aspect, float zNear, float zFar);
void setViewDirection(glm::vec3 position, glm::vec3 direction, glm::vec3 up);
void setViewTarget(glm::vec3 position, glm::vec3 target, glm::vec3 up);
void setViewYXZ(glm::vec3 position, glm::vec3 rotation);
const glm::mat4& getProjectionMatrix();
const glm::mat4& getViewMatrix();
};
#include "../camera/camera.hpp"
namespace vapp{
// Change here for other presentMode
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR;
struct Vertex
{
struct Vertex{
glm::vec3 pos;
glm::vec3 color;
glm::vec2 texCoord;
static VkVertexInputBindingDescription getBindingDescription()
{
static VkVertexInputBindingDescription getBindingDescription(){
VkVertexInputBindingDescription bindingDescription{};
bindingDescription.binding = 0;
@@ -65,8 +48,7 @@ namespace vapp
return bindingDescription;
}
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions()
{
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions(){
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions{};
attributeDescriptions[0].binding = 0;
@@ -87,8 +69,7 @@ namespace vapp
return attributeDescriptions;
}
bool operator==(const Vertex& other) const
{
bool operator==(const Vertex& other) const{
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
@@ -114,26 +95,22 @@ namespace vapp
*/
// END Remove
struct UniformBufferObject
{
struct UniformBufferObject{
alignas(16) glm::mat4 model;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 proj;
};
struct QueueFamilyIndices
{
struct QueueFamilyIndices{
std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
bool isComplete()
{
bool isComplete(){
return graphicsFamily.has_value();
}
};
struct SwapChainSupportDetails
{
struct SwapChainSupportDetails{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
@@ -143,11 +120,10 @@ namespace vapp
static std::array<VkVertexInputAttributeDescription, 2> getAttributeDescription();
class Vulkan
{
class Vulkan{
private:
#pragma region PrivateFields
GLFWwindow* window;
GLFWwindow *window;
uint32_t _width, _height;
VkInstance instance;
@@ -213,6 +189,14 @@ namespace vapp
VkImage colorImage;
VkDeviceMemory colorImageMemory;
VkImageView colorImageView;
#pragma region GLFWFunctions
static void mouseCallback(GLFWwindow *win, double xpos, double ypos);
static void scrollCallback(GLFWwindow *win, double xoffset, double yoffset);
void processInput();
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
#pragma endregion
#pragma endregion
#pragma region PrivateFunctions
bool checkValidationLayerSupport();
@@ -276,7 +260,7 @@ namespace vapp
void drawFrame();
void cleanupSwapChain();
void recreateSwapChain();
void initWindow(const char* windowName);
void initWindow(const char *windowName);
void initVulkan();
void mainLoop();
void cleanup();
@@ -296,11 +280,12 @@ namespace vapp
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
Camera camera;
float yaw = -90.0f;
float pitch = 0.0f;
float lastX = 400.0f;
float lastY = 300.0f;
float lastX = 0.0f, lastY = 0.0f;
bool firstMouse = true;
float deltaTime = 0.0f, lastFrame = 0.0f;
glm::vec3 cameraPos = glm::vec3(2.0f, 2.0f, 2.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, 0.0f);
@@ -308,23 +293,15 @@ namespace vapp
#pragma endregion
#pragma region PublicFunctions
void run(const char* windowName, const uint32_t width = 800, const uint32_t height = 600);
#pragma endregion
#pragma region GLFWFunctions
void handleKeyboardInput();
void handleMouseInput(GLFWwindow* window, double xpos, double ypos);
void run(const char *windowName, const uint32_t width = 800, const uint32_t height = 600);
#pragma endregion
};
} // vapp
namespace std
{
namespace std{
template <>
struct hash<vapp::Vertex>
{
size_t operator()(vapp::Vertex const& vertex) const
{
struct hash<vapp::Vertex>{
size_t operator()(vapp::Vertex const& vertex) const{
return ((hash<glm::vec3>()(vertex.pos) ^
(hash<glm::vec3>()(vertex.color) << 1)) >> 1) ^
(hash<glm::vec2>()(vertex.texCoord) << 1);