Componentified Camera

This commit is contained in:
Simon Lübeß
2021-05-17 20:07:58 +02:00
parent 3e0c4b2eb7
commit 7fb1f30303
6 changed files with 234 additions and 34 deletions
@@ -0,0 +1,98 @@
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public struct CameraComponent
{
public enum Projection
{
/**
* A perspective projection with near and far plane.
*/
Perspective,
/**
* A perspective projection with near and far plane,
* except that the closer a pixel is to the far plane the smaller its depth-value gets.
*/
PerspectiveReversed,
/**
* A perspective projection with a near plane and a far plane at infinite distance.
*/
PerspectiveInfinite,
/**
* A perspective projection with a near plane and a far plane at infinite distance,
* except that the closer a pixel is to the far plane the smaller its depth-value gets.
*/
PerspectiveReversedInfinite,
/**
* An orthographic projection.
*/
Orthographic
}
float _nearPlane;
float _farPlane;
Projection _projectionType;
float _fovY;
float _aspect;
public float NearPlane
{
get => _nearPlane;
set mut => _nearPlane = value;
}
public float FarPlane
{
get => _farPlane;
set mut => _farPlane = value;
}
public Projection ProjectionType
{
get => _projectionType;
set mut => _projectionType = value;
}
public float FovY
{
get => _fovY;
set mut => _fovY = value;
}
public float FovX
{
get => _fovY * _aspect;
set mut => _fovY = value / _aspect;
}
public float Aspect
{
get => _aspect;
set mut => _aspect = value;
}
public Matrix Projection => CalculateProjection();
public Matrix CalculateProjection()
{
Log.EngineLogger.Assert(_nearPlane < _farPlane, "The near plane must be smaller than the far plane.");
switch(_projectionType)
{
case .Perspective:
return Matrix.PerspectiveProjection(_fovY, _aspect, _nearPlane, _farPlane);
case .PerspectiveReversed:
return Matrix.ReversedPerspectiveProjection(_fovY, _aspect, _nearPlane, _farPlane);
case .PerspectiveInfinite:
return Matrix.InfinitePerspectiveProjection(_fovY, _aspect, _nearPlane);
case .PerspectiveReversedInfinite:
return Matrix.ReversedInfinitePerspectiveProjection(_fovY, _aspect, _nearPlane);
default:
Log.EngineLogger.Assert(false, "Unknown projection type.");
}
return .Identity;
}
}
}
@@ -0,0 +1,7 @@
namespace GlitchyEngine.Renderer
{
public struct MeshComponent
{
public GeometryBinding Mesh;
}
}
+13
View File
@@ -1,4 +1,5 @@
using GlitchyEngine.Math;
using GlitchyEngine.World;
namespace GlitchyEngine.Renderer
{
@@ -36,6 +37,18 @@ namespace GlitchyEngine.Renderer
RenderCommand.Init();
}
public static void BeginScene(EcsWorld world, Entity cameraEntity)
{
var camera = world.GetComponent<CameraComponent>(cameraEntity);
var transform = world.GetComponent<TransformComponent>(cameraEntity);
var trans = transform.Transform;
var view = trans.Invert();
var proj = camera.Projection;
_sceneConstants.ViewProjection = proj * view;
}
public static void BeginScene(Camera camera)
{
_sceneConstants.ViewProjection = camera.ViewProjection;
+56 -5
View File
@@ -4,16 +4,67 @@ namespace GlitchyEngine.World
{
public struct TransformComponent
{
static int _id;
Vector3 _position = .Zero;
Vector3 _rotation = .Zero;
Vector3 _scale = .One;
public static int ID {get => _id; set => _id = value; }
internal Matrix _transform;
Matrix _transform;
bool _dirty;
public Matrix Transform
{
get => _transform;
get mut
{
return _transform;
}
set mut => _transform = value;
}
public Vector3 Position
{
get => _position;
set mut
{
if(_position == value)
return;
_position = value;
_dirty = true;
}
}
public Vector3 Rotation
{
get => _rotation;
set mut
{
if(_rotation == value)
return;
_rotation = value;
_dirty = true;
}
}
public Vector3 Scale
{
get => _scale;
set mut
{
if(_scale == value)
return;
_scale = value;
_dirty = true;
}
}
public void Update() mut
{
if(!_dirty)
return;
_transform = .Translation(_position) * .RotationX(_rotation.X) *
.RotationY(_rotation.Y) * .RotationZ(_rotation.Z) * .Scaling(_scale);
_dirty = false;
}
}
}