Camera and Movementhooks are implemented but Camerarotation is faulty

This commit is contained in:
t
2025-04-07 23:01:58 +02:00
parent ce8bc3a96b
commit 76ea39b784
4 changed files with 215 additions and 26 deletions
+75 -22
View File
@@ -27,16 +27,35 @@
#include <chrono>
#include <unordered_map>
namespace vapp{
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();
};
// 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;
@@ -46,7 +65,8 @@ 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;
@@ -67,7 +87,8 @@ 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;
}
};
@@ -93,22 +114,26 @@ 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;
@@ -118,10 +143,11 @@ 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,22 +239,28 @@ namespace vapp{
void createRenderPass();
void createFramebuffers();
void createCommandPool();
VkFormat findSupportedFormat(const std::vector<VkFormat> &candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
VkFormat findSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling,
VkFormatFeatureFlags features);
VkFormat findDepthFormat();
void createColorResources();
void createDepthResources();
VkCommandBuffer beginSingleTimeCommands();
void endSingleTimeCommands(VkCommandBuffer commandBuffer);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout,
uint32_t mipLevels);
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
void createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory &imageMemory);
void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels);
void createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples,
VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory);
void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight,
uint32_t mipLevels);
void createTextureImage();
VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels);
void createTextureImageView();
void createTextureSampler();
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties,
VkBuffer& buffer, VkDeviceMemory& bufferMemory);
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
void copyBufferOld(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
void loadModel();
@@ -244,11 +276,12 @@ namespace vapp{
void drawFrame();
void cleanupSwapChain();
void recreateSwapChain();
void initWindow(const char *windowName);
void initWindow(const char* windowName);
void initVulkan();
void mainLoop();
void cleanup();
#pragma endregion
public:
#pragma region PublicFields
#ifdef DEBUG
@@ -262,19 +295,39 @@ namespace vapp{
std::string TEXTURE_PATH;
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
float yaw = -90.0f;
float pitch = 0.0f;
float lastX = 400.0f;
float lastY = 300.0f;
bool firstMouse = true;
glm::vec3 cameraPos = glm::vec3(2.0f, 2.0f, 2.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, 1.0f);
#pragma endregion
#pragma region PublicFunctions
void run(const char *windowName, const uint32_t width = 800, const uint32_t height = 600);
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);
#pragma endregion
};
} // vapp
namespace std{
template<> struct hash<vapp::Vertex>{
size_t operator()(vapp::Vertex const &vertex) const{
namespace std
{
template <>
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);
(hash<glm::vec2>()(vertex.texCoord) << 1);
}
};
}