From aea890971b18041e8f4c6366833eddf5f2295644 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Fri, 16 Oct 2020 00:44:22 +0200 Subject: [PATCH] Cleanup Camera code --- openVulkanoCpp/Scene/Camera.hpp | 71 ++++++++++++++++++------------ openVulkanoCpp/Vulkan/Renderer.cpp | 2 +- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/openVulkanoCpp/Scene/Camera.hpp b/openVulkanoCpp/Scene/Camera.hpp index d159454..63db48c 100644 --- a/openVulkanoCpp/Scene/Camera.hpp +++ b/openVulkanoCpp/Scene/Camera.hpp @@ -1,4 +1,11 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + #pragma once + #define _USE_MATH_DEFINES #include #include @@ -12,13 +19,19 @@ namespace openVulkanoCpp class Camera : public Node { protected: - float nearPlane, farPlane; - float width, height; - public: + float width, height, nearPlane, farPlane; + glm::mat4x4 projection, view, viewProjection; - Camera() = default; - virtual ~Camera() override = default; + Camera() : width(0), height(0), nearPlane(0), farPlane(0), projection(1), view(1), viewProjection(1) {} + + Camera(float width, float height, float nearPlane, float farPlane) + : width(width), height(height), nearPlane(nearPlane), farPlane(farPlane) + , projection(1), view(1), viewProjection(1) + { + } + + ~Camera() override = default; public: void Init(float width, float height, float nearPlane, float farPlane) @@ -31,7 +44,7 @@ namespace openVulkanoCpp UpdateProjectionMatrix(); } - virtual void SetSize(const float& width, const float& height) + virtual void SetSize(const float width, const float height) { this->width = width; this->height = height; @@ -49,12 +62,12 @@ namespace openVulkanoCpp } - float NearPlane() const + [[nodiscard]] float NearPlane() const { return nearPlane; } - float FarPlane() const + [[nodiscard]] float FarPlane() const { return farPlane; } @@ -73,23 +86,27 @@ namespace openVulkanoCpp UpdateViewProjectionMatrix(); } - const glm::mat4x4& GetViewProjectionMatrix() const + [[nodiscard]] const glm::mat4x4& GetViewProjectionMatrix() const { return viewProjection; } - - const glm::mat4x4* GetViewProjectionMatrixPointer() const - { - return &viewProjection; - } }; class PerspectiveCamera : public Camera { protected: - float fov, aspect; + float fov = 0, aspect = 0; public: + PerspectiveCamera() = default; + ~PerspectiveCamera() override = default; + + PerspectiveCamera(float fovDegrees, float width, float height, float nearPlane = 0.1f, float farPlane = 1000.0f) + : Camera() + { + PerspectiveCamera::UpdateProjectionMatrix(); + } + void Init(float fovDegrees, float width, float height, float nearPlane, float farPlane) { this->fov = glm::radians(fovDegrees); @@ -97,56 +114,56 @@ namespace openVulkanoCpp Camera::Init(width, height, nearPlane, farPlane); } - void SetSize(const float& width, const float& height) override + void SetSize(const float width, const float height) override { aspect = width / height; Camera::SetSize(width, height); } - void SetAspect(const float& aspect) + void SetAspect(const float aspect) { this->aspect = aspect; Camera::SetSize(aspect, 1); } - void SetFovX(const float& fov) + void SetFovX(const float fov) { SetFov(2 * atan(tan(fov * 0.5f) * aspect)); } - void SetFovXRad(const float& fov) + void SetFovXRad(const float fov) { SetFovRad(2 * atan(tan(fov * 0.5f) * aspect)); } - void SetFov(const float& fov) + void SetFov(const float fov) { SetFovRad(glm::radians(fov)); } - void SetFovRad(const float& fov) + void SetFovRad(const float fov) { this->fov = fov; } - float GetFov() const + [[nodiscard]] float GetFov() const { return glm::degrees(fov); } - float GetFovX() const + [[nodiscard]] float GetFovX() const { - return 2 * atan(tan(GetFov() * 0.5f) * aspect); + return 2.0f * atanf(tanf(GetFov() * 0.5f) * aspect); } - float GetFovRad() const + [[nodiscard]] float GetFovRad() const { return fov; } - float GetFovXRad() const + [[nodiscard]] float GetFovXRad() const { - return 2 * atan(tan(fov * 0.5f) * aspect); + return 2.0f * atanf(tanf(fov * 0.5f) * aspect); } void UpdateProjectionMatrix() override diff --git a/openVulkanoCpp/Vulkan/Renderer.cpp b/openVulkanoCpp/Vulkan/Renderer.cpp index f6cbaba..0d169be 100644 --- a/openVulkanoCpp/Vulkan/Renderer.cpp +++ b/openVulkanoCpp/Vulkan/Renderer.cpp @@ -140,7 +140,7 @@ namespace openVulkanoCpp::Vulkan vk::CommandBufferInheritanceInfo inheritance = { context.swapChainRenderPass.renderPass, 0, context.swapChainRenderPass.GetFrameBuffer()->GetCurrentFrameBuffer() }; cmdHelper->cmdBuffer.begin(vk::CommandBufferBeginInfo{ vk::CommandBufferUsageFlagBits::eOneTimeSubmit | vk::CommandBufferUsageFlagBits::eRenderPassContinue, &inheritance }); shader->Record(cmdHelper->cmdBuffer, currentImageId); - cmdHelper->cmdBuffer.pushConstants(context.pipeline.pipelineLayout, vk::ShaderStageFlagBits::eVertex, 0, 64, scene->GetCamera()->GetViewProjectionMatrixPointer()); + cmdHelper->cmdBuffer.pushConstants(context.pipeline.pipelineLayout, vk::ShaderStageFlagBits::eVertex, 0, 64, &scene->GetCamera()->GetViewProjectionMatrix()); Scene::Drawable** drawablePointer; while((drawablePointer = jobQueue->Pop()) != nullptr) {