Files
OpenVulkano/openVulkanoCpp/Scene/Camera.hpp
2020-10-16 00:44:22 +02:00

188 lines
4.2 KiB
C++

/*
* 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 <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Node.hpp"
#define GLM_FORECE_DEPTH_ZERO_TO_ONE
namespace openVulkanoCpp
{
namespace Scene
{
class Camera : public Node
{
protected:
float width, height, nearPlane, farPlane;
glm::mat4x4 projection, view, viewProjection;
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)
{
this->width = width;
this->height = height;
this->nearPlane = nearPlane;
this->farPlane = farPlane;
Node::Init();
UpdateProjectionMatrix();
}
virtual void SetSize(const float width, const float height)
{
this->width = width;
this->height = height;
UpdateProjectionMatrix();
}
void SetNearPlane(float nearPlane)
{
this->nearPlane = nearPlane;
}
void SetFarPlane(float farPlane)
{
this->farPlane = farPlane;
}
[[nodiscard]] float NearPlane() const
{
return nearPlane;
}
[[nodiscard]] float FarPlane() const
{
return farPlane;
}
virtual void UpdateProjectionMatrix() = 0;
void UpdateViewProjectionMatrix()
{ // In vulkan the screen space is defined as y=0=top and y=1=bottom and thus the coordinate have to be flipped
viewProjection = projection * glm::mat4x4(1,0,0,0,0,-1,0,0,0,0,1,0,0,0,0,1) * view;
}
void UpdateWorldMatrix(const glm::mat4x4& parentWorldMat) override
{
Node::UpdateWorldMatrix(parentWorldMat);
view = glm::inverse(GetWorldMatrix());
UpdateViewProjectionMatrix();
}
[[nodiscard]] const glm::mat4x4& GetViewProjectionMatrix() const
{
return viewProjection;
}
};
class PerspectiveCamera : public Camera
{
protected:
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);
aspect = width / height;
Camera::Init(width, height, nearPlane, farPlane);
}
void SetSize(const float width, const float height) override
{
aspect = width / height;
Camera::SetSize(width, height);
}
void SetAspect(const float aspect)
{
this->aspect = aspect;
Camera::SetSize(aspect, 1);
}
void SetFovX(const float fov)
{
SetFov(2 * atan(tan(fov * 0.5f) * aspect));
}
void SetFovXRad(const float fov)
{
SetFovRad(2 * atan(tan(fov * 0.5f) * aspect));
}
void SetFov(const float fov)
{
SetFovRad(glm::radians(fov));
}
void SetFovRad(const float fov)
{
this->fov = fov;
}
[[nodiscard]] float GetFov() const
{
return glm::degrees(fov);
}
[[nodiscard]] float GetFovX() const
{
return 2.0f * atanf(tanf(GetFov() * 0.5f) * aspect);
}
[[nodiscard]] float GetFovRad() const
{
return fov;
}
[[nodiscard]] float GetFovXRad() const
{
return 2.0f * atanf(tanf(fov * 0.5f) * aspect);
}
void UpdateProjectionMatrix() override
{
projection = glm::perspectiveLH_ZO(fov, aspect, nearPlane, farPlane);
UpdateViewProjectionMatrix();
}
};
class OrthographicCamera : public Camera
{
public:
void UpdateProjectionMatrix() override
{
const float widthHalf = width * 0.5f, heightHalf = height * 0.5f;
projection = glm::orthoLH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, nearPlane, farPlane);
UpdateViewProjectionMatrix();
}
};
}
}