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
-44
View File
@@ -1,44 +0,0 @@
#include "Actor.h"
#include "Scene/Scene.h"
using namespace Seele;
Actor::Actor(PScene scene)
: Entity(scene)
{
scene->attachComponent<Component::Transform>(identifier);
}
Actor::~Actor()
{
}
void Actor::setParent(PActor newParent)
{
if(parent != nullptr)
{
parent->removeChild(this);
}
parent = newParent;
}
void Actor::addChild(PActor child)
{
children.add(child);
child->setParent(this);
}
void Actor::removeChild(PActor child)
{
children.remove(children.find(child), false);
child->setParent(nullptr);
}
const Component::Transform& Actor::getTransform() const
{
return scene->accessComponent<Component::Transform>(identifier);
}
//Component::Transform& Actor::getTransform()
//{
// return scene->accessComponent<Component::Transform>(identifier);
//}
-31
View File
@@ -1,31 +0,0 @@
#pragma once
#include "Entity.h"
#include "Scene/Component/Transform.h"
namespace Seele
{
DECLARE_REF(Actor)
// Actors are entities that are part of the scene hierarchy
// In order for that hierarchy to make sense it requires at least a transform component
class Actor : public Entity
{
public:
Actor(PScene scene);
virtual ~Actor();
PActor getParent();
void addChild(PActor child);
void removeChild(PActor child);
Array<PActor> getChildren();
// Returns a read-only copy of the actors transform
const Component::Transform& getTransform() const;
protected:
//Component::Transform& getTransform();
void setParent(PActor parent);
PActor parent;
Array<PActor> children;
};
DEFINE_REF(Actor)
} // namespace Seele
-8
View File
@@ -1,8 +0,0 @@
target_sources(Engine
PUBLIC
Actor.cpp
Actor.h
CameraActor.cpp
CameraActor.h
Entity.cpp
Entity.h)
-25
View File
@@ -1,25 +0,0 @@
#include "CameraActor.h"
#include "Scene/Scene.h"
using namespace Seele;
CameraActor::CameraActor(PScene scene)
: Actor(scene)
{
scene->attachComponent<Component::Camera>(identifier);
scene->accessComponent<Component::Transform>(identifier).setRelativeLocation(Math::Vector(10, 5, 14));
}
CameraActor::~CameraActor()
{
}
Component::Camera& CameraActor::getCameraComponent()
{
return scene->accessComponent<Component::Camera>(identifier);
}
const Component::Camera& CameraActor::getCameraComponent() const
{
return scene->accessComponent<Component::Camera>(identifier);
}
-17
View File
@@ -1,17 +0,0 @@
#pragma once
#include "Actor.h"
#include "Scene/Component/Camera.h"
namespace Seele
{
class CameraActor : public Actor
{
public:
CameraActor(PScene scene);
virtual ~CameraActor();
Component::Camera& getCameraComponent();
const Component::Camera& getCameraComponent() const;
private:
};
DEFINE_REF(CameraActor)
} // namespace Seele
-15
View File
@@ -1,15 +0,0 @@
#include "Entity.h"
using namespace Seele;
Entity::Entity(PScene scene)
: identifier(scene->createEntity())
, scene(scene)
{
}
Entity::~Entity()
{
}
-25
View File
@@ -1,25 +0,0 @@
#pragma once
#include <entt/entt.hpp>
#include "MinimalEngine.h"
#include "Scene/Scene.h"
namespace Seele
{
// An entity describes a part of a scene
// It is just a wrapper the ID of a registry
class Entity
{
public:
Entity(PScene scene);
virtual ~Entity();
template<typename Component, typename... Args>
void attachComponent(Args... args)
{
scene->attachComponent<Component>(identifier, args...);
}
protected:
PScene scene;
entt::entity identifier;
};
DEFINE_REF(Entity)
} // namespace Seele
+1 -5
View File
@@ -1,9 +1,5 @@
target_sources(Engine
PUBLIC
PRIVATE
Util.h
Scene.cpp
Scene.h)
add_subdirectory(Actor/)
add_subdirectory(Component/)
add_subdirectory(System/)
@@ -1,8 +0,0 @@
target_sources(Engine
PUBLIC
Component.h
Camera.h
Camera.cpp
StaticMesh.h
Transform.h
Transform.cpp)
-88
View File
@@ -1,88 +0,0 @@
#include "Camera.h"
#include "Scene/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(true)
, bNeedsProjectionBuild(true)
, 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 = Vector(0, 0, 0);//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
@@ -1,54 +0,0 @@
#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()
{
assert (!bNeedsViewBuild);
return viewMatrix;
}
Math::Matrix4 getProjectionMatrix()
{
assert (!bNeedsProjectionBuild);
return projectionMatrix;
}
Math::Vector getCameraPosition()
{
return getTransform().getPosition();
}
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();
private:
bool bNeedsViewBuild;
bool bNeedsProjectionBuild;
Gfx::PViewport viewport;
//Transforms relative to actor
Math::Matrix4 viewMatrix;
Math::Matrix4 projectionMatrix;
float yaw;
float pitch;
};
} // namespace Component
} // namespace Seele
-47
View File
@@ -1,47 +0,0 @@
#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
@@ -1,15 +0,0 @@
#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
@@ -1,34 +0,0 @@
#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
@@ -1,52 +0,0 @@
#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
-7
View File
@@ -1,7 +0,0 @@
target_sources(Engine
PUBLIC
Executor.h
Executor.cpp
CameraSystem.h
CameraSystem.cpp
SystemBase.h)
-11
View File
@@ -1,11 +0,0 @@
#include "CameraSystem.h"
#include "Scene/Component/Camera.h"
using namespace Seele;
using namespace Seele::System;
void CameraSystem::update(Component::Camera& camera)
{
camera.buildViewMatrix();
camera.buildProjectionMatrix();
}
-18
View File
@@ -1,18 +0,0 @@
#pragma once
#include "SystemBase.h"
#include "Scene/Component/Camera.h"
namespace Seele
{
namespace System
{
class CameraSystem : public SystemBase<Component::Camera>
{
public:
CameraSystem(entt::registry& registry) : SystemBase(registry) {}
virtual ~CameraSystem() {}
virtual void update(Component::Camera& component);
private:
};
} // namespace System
} // namespace Seele
-4
View File
@@ -1,4 +0,0 @@
#include "Executor.h"
using namespace Seele;
using namespace Seele::System;
-19
View File
@@ -1,19 +0,0 @@
#pragma once
#include "MinimalEngine.h"
#include "SystemBase.h"
#include <thread_pool/thread_pool.h>
namespace Seele
{
namespace System
{
class Executor
{
public:
Executor();
~Executor();
private:
dp::thread_pool<> pool;
};
} // namespace System
} // namespace Seele
-47
View File
@@ -1,47 +0,0 @@
#pragma once
#include <entt/entt.hpp>
#include <thread_pool/thread_pool.h>
#include "Scene/Component/Component.h"
namespace Seele
{
namespace System
{
template<typename... Components>
class SystemBase
{
public:
SystemBase(entt::registry& registry) : registry(registry) {}
virtual ~SystemBase() {}
template<typename Comp>
auto getDependencies()
{
return Comp::dependencies;
}
template<typename... Dep>
auto mergeDependencies(Dep... deps)
{
return (deps | ...);
}
template<typename... Deps>
void setupView(Dependencies<Deps...>, dp::thread_pool<>&)
{
registry.view<Components..., Deps...>().each([&](Components&... comp, Deps&... deps){
//pool.enqueue_detach([&](){
int ret = (accessComponent(deps) + ...);
assert(ret == 0);
update((comp,...));
//});
});
}
void run(dp::thread_pool<>& pool)
{
setupView(mergeDependencies((getDependencies<Components>(),...)), pool);
}
virtual void update(Components&... components) = 0;
private:
entt::registry& registry;
};
} // namespace System
} // namespace Seele