Added Camera, Renderer, ... and moved example to sandbox

This commit is contained in:
Simon Lübeß
2021-01-09 12:05:05 +01:00
parent 2159f8087a
commit 4054a9b0c2
12 changed files with 670 additions and 167 deletions
@@ -105,6 +105,39 @@ namespace GlitchyEngine.Renderer
}
}
/**
* Replaces the buffer with the given name.
* @param name The name of the buffer to replace.
* @param buffer The new buffer.
* @param If set to true, the Collection will take ownership of the buffer; if false, the ownership will remain with the caller.
*/
public bool TryReplaceBuffer(StringView name, Buffer buffer, bool passOwnership = false)
{
if(_strToBuf.TryGetValue(name, let oldBuffer))
{
int index = GetIndexOfBuffer(oldBuffer);
ref BufferEntry bufferDesc = ref _buffers[index];
Log.EngineLogger.Assert(name == bufferDesc.Name);
if(bufferDesc.OwnsBuffer)
delete bufferDesc.Buffer;
bufferDesc.Buffer = buffer;
bufferDesc.OwnsBuffer = passOwnership;
_strToBuf[bufferDesc.Name] = buffer;
_idxToBuf[bufferDesc.Index] = buffer;
return true;
}
else
{
return false;
}
}
public void Add(int index, StringView name, Buffer buffer, bool passOwnership = false)
{
String nameStr = new String(name);
+112
View File
@@ -0,0 +1,112 @@
using System;
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public abstract class Camera
{
protected Matrix _view;
protected Matrix _transform;
protected Matrix _projection;
protected Matrix _viewProjection;
protected float _nearPlane;
protected float _farPlane;
protected Vector3 _position;
protected Vector3 _rotation;
/// If true the transform matrix is outdated and needs to be recalculated.
protected bool _transformOutdated = true;
/// If true the projection matrix is outdated and needs to be recalculated.
protected bool _projectionOutdated = true;
public Matrix Transform => _transform;
public Matrix View => _view;
public Matrix Projection => _projection;
public Matrix ViewProjection => _viewProjection;
public float NearPlane
{
get => _nearPlane;
set
{
if(_nearPlane == value)
return;
_nearPlane = value;
_projectionOutdated = true;
}
}
public float FarPlane
{
get => _farPlane;
set
{
if(_farPlane == value)
return;
_farPlane = value;
_projectionOutdated = true;
}
}
public Vector3 Position
{
get => _position;
set
{
if(_position == value)
return;
_position = value;
_transformOutdated = true;
}
}
public Vector3 Rotation
{
get => _rotation;
set
{
if(_rotation == value)
return;
_rotation = value;
_transformOutdated = true;
}
}
public void Update()
{
bool changed = false;
if(_transformOutdated)
{
UpdateTransform();
_transformOutdated = false;
changed = true;
}
if(_projectionOutdated)
{
UpdateProjection();
_projectionOutdated = false;
changed = true;
}
if(changed)
_viewProjection = _projection * _view;
}
protected abstract void UpdateTransform();
protected abstract void UpdateProjection();
}
}
+31
View File
@@ -0,0 +1,31 @@
using System;
namespace GlitchyEngine.Renderer
{
public class Effect
{
protected GraphicsContext _context;
internal VertexShader _vs;
internal PixelShader _ps;
public GraphicsContext Context => _context;
public VertexShader VertexShader
{
get => _vs;
set => _vs = value;
}
public PixelShader PixelShader
{
get => _ps;
set => _ps = value;
}
public void Bind(GraphicsContext context)
{
context.SetVertexShader(_vs);
context.SetPixelShader(_ps);
}
}
}
@@ -0,0 +1,92 @@
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public class OrthographicCamera : Camera
{
protected float _bottom, _left, _right, _top;
public float Bottom
{
get => _bottom;
set
{
if(_bottom == value)
return;
_bottom = value;
_projectionOutdated = true;
}
}
public float Left
{
get => _left;
set
{
if(_left == value)
return;
_left = value;
_projectionOutdated = true;
}
}
public float Right
{
get => _right;
set
{
if(_right == value)
return;
_right = value;
_projectionOutdated = true;
}
}
public float Top
{
get => _top;
set
{
if(_top == value)
return;
_top = value;
_projectionOutdated = true;
}
}
public float Width
{
get => _right - _left;
set
{
Left = -value / 2.0f;
Right = value / 2.0f;
}
}
public float Height
{
get => _top - _bottom;
set
{
Bottom = -value / 2.0f;
Top = value / 2.0f;
}
}
protected override void UpdateProjection()
{
_projection = Matrix.OrthographicProjectionOffCenter(_left, _right, _top, _bottom, _nearPlane, _farPlane);
}
protected override void UpdateTransform()
{
_transform = Matrix.Translation(_position) * Matrix.RotationZ(_rotation.Z) * Matrix.RotationY(_rotation.Y) * Matrix.RotationX(_rotation.X);
_view = _transform.Invert();
}
}
}
@@ -0,0 +1,104 @@
using System;
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
/**
* If FarPlane is float.PositiveInfinity the projection matrix will be an infinite projection (that means no far plane)
*/
public class PerspectiveCamera : Camera
{
public enum ProjectionType
{
/**
* The projection has a near and a far plane. The near plane has a depth of 0, the far plane a depth of 1.
*/
Limited,
/**
* The projection has a near and a far plane. The near plane has a depth of 1, the far plane a depth of 0.
*/
LimitedReversed,
/**
* The projection only has a near. The near plane has a depth of 0; infinite distance has a value of 1.
*/
Infinite,
/**
* The projection only has a near. The near plane has a depth of 1; infinite distance has a value of 0.
*/
InfiniteReversed
}
protected ProjectionType _projectionType;
protected float _fovY;
protected float _aspect;
/**
* Gets or Sets the projection type.
*/
public ProjectionType ProjectionType
{
get => _projectionType;
set
{
if(_projectionType == value)
return;
_projectionType = value;
_projectionOutdated = true;
}
}
public float FovY
{
get => _fovY;
set
{
if(_fovY == value)
return;
_fovY = value;
_projectionOutdated = true;
}
}
public float AspectRatio
{
get => _aspect;
set
{
if(_aspect == value)
return;
_aspect = value;
_projectionOutdated = true;
}
}
protected override void UpdateProjection()
{
Log.EngineLogger.Assert(_nearPlane < _farPlane, "The near plane must be smaller than the far plane.");
switch(_projectionType)
{
case .Limited:
_projection = Matrix.PerspectiveProjection(_fovY, _aspect, _nearPlane, _farPlane);
case .LimitedReversed:
_projection = Matrix.ReversedPerspectiveProjection(_fovY, _aspect, _nearPlane, _farPlane);
case .Infinite:
_projection = Matrix.InfinitePerspectiveProjection(_fovY, _aspect, _nearPlane); // todo: epsilon
case .InfiniteReversed:
_projection = Matrix.ReversedInfinitePerspectiveProjection(_fovY, _aspect, _nearPlane); // todo: epsilon
default:
Log.EngineLogger.Assert(false, "Unknown projection type.");
}
}
protected override void UpdateTransform()
{
_transform = Matrix.Translation(_position) * Matrix.RotationZ(_rotation.Z) * Matrix.RotationY(_rotation.Y) * Matrix.RotationX(_rotation.X);
_view = _transform.Invert();
}
}
}
@@ -0,0 +1,28 @@
using System;
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public static class RenderCommand
{
private static RendererAPI _rendererAPI;
public static RendererAPI RendererAPI
{
get => _rendererAPI;
set => _rendererAPI = value;
}
[Inline]
public static void Clear(RenderTarget renderTarget, ColorRGBA color)
{
_rendererAPI.Clear(renderTarget, color);
}
[Inline]
public static void DrawIndexed(GeometryBinding geometry)
{
_rendererAPI.DrawIndexed(geometry);
}
}
}
+42
View File
@@ -0,0 +1,42 @@
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public class Renderer
{
struct SceneConstants
{
public Matrix ViewProjection;
}
static GraphicsContext _context;
static Buffer<SceneConstants> _sceneConstants ~ delete _;
public static void Init(GraphicsContext context)
{
_context = context;
_sceneConstants = new Buffer<SceneConstants>(_context, .(0, .Constant, .Dynamic, .Write));
_sceneConstants.Update();
}
public static void BeginScene(Camera camera)
{
_sceneConstants.Data.ViewProjection = camera.ViewProjection;
_sceneConstants.Update();
}
public static void EndScene(){}
public static void Submit(GeometryBinding geometry, Effect effect)
{
effect.Bind(_context);
effect.PixelShader?.Buffers.TryReplaceBuffer("SceneConstants", _sceneConstants);
effect.VertexShader?.Buffers.TryReplaceBuffer("SceneConstants", _sceneConstants);
geometry.Bind();
RenderCommand.DrawIndexed(geometry);
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public class RendererAPI
{
public enum API
{
None,
D3D11
}
/**
* Gets which graphics API is active.
*/
public static extern API Api { get; }
public extern void Clear(RenderTarget renderTarget, ColorRGBA clearColor);
public extern void DrawIndexed(GeometryBinding geometry);
}
}