Viewport controls

This commit is contained in:
Dynamitos
2022-11-17 16:47:42 +01:00
parent f635ee2100
commit 4ba0bf3b45
73 changed files with 627 additions and 449 deletions
+8
View File
@@ -0,0 +1,8 @@
target_sources(Engine
PRIVATE
Camera.h
Camera.cpp
Component.h
StaticMesh.h
Transform.h
Transform.cpp)
+88
View File
@@ -0,0 +1,88 @@
#include "Camera.h"
#include "Actor/Actor.h"
#include <algorithm>
#include <iostream>
using namespace Seele;
using namespace Seele::Component;
using namespace Seele::Math;
Camera::Camera()
: aspectRatio(0)
, fieldOfView(glm::radians(70.f))
, bNeedsViewBuild(false)
, bNeedsProjectionBuild(false)
, viewMatrix(Matrix4())
, projectionMatrix(Matrix4())
{
yaw = 0;
pitch = 0;
}
Camera::~Camera()
{
}
void Camera::mouseMove(float deltaYaw, float deltaPitch)
{
yaw -= deltaYaw / 500.f;
pitch += deltaPitch / 500.f;
//std::cout << "Yaw: " << yaw << " Pitch: " << pitch << std::endl;
Vector cameraDirection = glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch)));
Vector xyz = glm::cross(cameraDirection, Vector(0, 0, 1));
Quaternion result = Quaternion(glm::dot(cameraDirection, Vector(0, 0, 1)) + 1, xyz.x, xyz.y, xyz.z);
//std::cout << "Result " << Vector(0, 0, 1) * glm::normalize(result) << " cameraDirection: " << cameraDirection << std::endl;
getTransform().setRelativeRotation(result);
bNeedsViewBuild = true;
}
void Camera::mouseScroll(float x)
{
getTransform().addRelativeLocation(getTransform().getForward()*x);
bNeedsViewBuild = true;
}
void Camera::moveX(float amount)
{
getTransform().addRelativeLocation(getTransform().getForward()*amount);
bNeedsViewBuild = true;
}
void Camera::moveY(float amount)
{
getTransform().addRelativeLocation(getTransform().getRight()*amount);
bNeedsViewBuild = true;
}
void Camera::setViewport(Gfx::PViewport newViewport)
{
viewport = newViewport;
aspectRatio = viewport->getSizeX() / (float)viewport->getSizeY();
bNeedsProjectionBuild = true;
}
void Camera::buildViewMatrix()
{
Vector eyePos = getTransform().getPosition();//getAbsoluteTransform().getPosition();
Vector lookAt = eyePos + glm::normalize(Vector(cos(yaw) * cos(pitch), sin(pitch), sin(yaw) * cos(pitch)));
//std::cout << "Eye: " << eyePos << " lookAt: " << lookAt << std::endl;
viewMatrix = glm::lookAt(eyePos, lookAt, Vector(0, 1, 0));
bNeedsViewBuild = false;
}
void Camera::buildProjectionMatrix()
{
projectionMatrix = glm::perspective(fieldOfView, aspectRatio, 1.0f, 1000.f);
static Matrix4 correctionMatrix =
Matrix4(
Vector4(1, 0, 0, 0),
Vector4(0, 1, 0, 0),
Vector4(0, 0, 0.5f, 0),
Vector4(0, 0, 0.5f, 1));
projectionMatrix = correctionMatrix * projectionMatrix;
bNeedsProjectionBuild = false;
}
+54
View File
@@ -0,0 +1,54 @@
#pragma once
#include "Component.h"
#include "Math/Matrix.h"
#include "Graphics/GraphicsResources.h"
#include "Transform.h"
namespace Seele
{
namespace Component
{
struct Camera
{
REQUIRE_COMPONENT(Transform)
Camera();
~Camera();
Math::Matrix4 getViewMatrix() const
{
assert (!bNeedsViewBuild);
return viewMatrix;
}
Math::Matrix4 getProjectionMatrix() const
{
assert (!bNeedsProjectionBuild);
return projectionMatrix;
}
Math::Vector getCameraPosition() const
{
return Math::Vector(viewMatrix[3]);
}
void setViewport(Gfx::PViewport viewport);
void mouseMove(float deltaX, float deltaY);
void mouseScroll(float x);
void moveX(float amount);
void moveY(float amount);
float aspectRatio;
float fieldOfView;
void buildViewMatrix();
void buildProjectionMatrix();
Math::Matrix4 viewMatrix;
Math::Matrix4 projectionMatrix;
private:
bool bNeedsViewBuild;
bool bNeedsProjectionBuild;
Gfx::PViewport viewport;
//Transforms relative to actor
float yaw;
float pitch;
};
} // namespace Component
} // namespace Seele
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <concepts>
#include <tuple>
namespace Seele
{
template<typename... Types>
struct Dependencies;
template<>
struct Dependencies<>
{};
template<typename This, typename... Rest>
struct Dependencies<This, Rest...> : public Dependencies<Rest...>
{
template<typename... Right>
Dependencies<This, Rest..., Right...> operator|(const Dependencies<Right...>& other)
{
return Dependencies<This, Rest..., Right...>();
}
};
namespace Component
{
#pragma warning(disable: 4505)
template<typename Comp>
static int accessComponent(Comp& comp);
}
template<typename T, typename... Deps>
concept is_component = std::same_as<decltype(T::dependencies), Dependencies<Deps...>>;
#define REQUIRE_COMPONENT(x) \
private: \
x& get##x() { return *tl_##x; } \
const x& get##x() const { return *tl_##x; }; \
public: \
static Dependencies<x> dependencies;
#define DECLARE_COMPONENT(x) \
thread_local extern x* tl_##x; \
template<> \
int accessComponent<x>(x& val) { tl_##x = &val; return 0; }
#define DEFINE_COMPONENT(x) \
thread_local x* Seele::Component::tl_##x;
} // namespace Seele
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "Graphics/GraphicsResources.h"
namespace Seele
{
namespace Component
{
struct StaticMesh
{
PVertexShaderInput vertexBuffer;
Gfx::PIndexBuffer indexBuffer;
PMaterialAsset material;
};
} // namespace Component
} // namespace Seele
+34
View File
@@ -0,0 +1,34 @@
#include "Transform.h"
using namespace Seele::Component;
DEFINE_COMPONENT(Transform)
void Transform::setRelativeLocation(Math::Vector location)
{
transform = Math::Transform(location, transform.getRotation(), transform.getScale());
}
void Transform::setRelativeRotation(Math::Vector rotation)
{
transform = Math::Transform(transform.getPosition(), Math::Quaternion(rotation), transform.getScale());
}
void Transform::setRelativeRotation(Math::Quaternion rotation)
{
transform = Math::Transform(transform.getPosition(), rotation, transform.getScale());
}
void Transform::setRelativeScale(Math::Vector scale)
{
transform = Math::Transform(transform.getPosition(), transform.getRotation(), scale);
}
void Transform::addRelativeLocation(Math::Vector translation)
{
transform = Math::Transform(transform.getPosition() + translation, transform.getRotation(), transform.getScale());
}
void Transform::addRelativeRotation(Math::Vector rotation)
{
transform = Math::Transform(transform.getPosition(), transform.getRotation() * Math::Quaternion(rotation), transform.getScale());
}
void Transform::addRelativeRotation(Math::Quaternion rotation)
{
transform = Math::Transform(transform.getPosition(), transform.getRotation() * rotation, transform.getScale());
}
+52
View File
@@ -0,0 +1,52 @@
#pragma once
#include "Math/Transform.h"
#include "Component.h"
namespace Seele
{
namespace Component
{
struct Transform
{
Transform() {}
Transform(const Transform& other) : transform(other.transform) {}
Transform(Transform&& other) : transform(other.transform) {}
Transform& operator=(const Transform& other) {
if (&other != this)
{
transform = other.transform;
}
return *this;
}
Transform& operator=(Transform&& other) {
if(&other != this)
{
transform = std::move(other.transform);
}
return *this;
}
Math::Vector getPosition() const { return transform.getPosition(); }
Math::Quaternion getRotation() const { return transform.getRotation(); }
Math::Vector getScale() const { return transform.getScale(); }
Math::Vector getForward() const { return transform.getForward(); }
Math::Vector getUp() const { return transform.getUp(); }
Math::Vector getRight() const { return transform.getRight(); }
Math::Matrix4 toMatrix() const { return transform.toMatrix(); }
void setRelativeLocation(Math::Vector location);
void setRelativeRotation(Math::Quaternion rotation);
void setRelativeRotation(Math::Vector rotation);
void setRelativeScale(Math::Vector scale);
void addRelativeLocation(Math::Vector translation);
void addRelativeRotation(Math::Quaternion rotation);
void addRelativeRotation(Math::Vector rotation);
private:
Math::Transform transform;
};
DECLARE_COMPONENT(Transform)
} // namespace Component
} // namespace Seele