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
+8 -163
View File
@@ -2,9 +2,6 @@ using System;
using GlitchyEngine.Events;
using GlitchyEngine.ImGui;
using GlitchyEngine.Math;
using DirectX.D3D11;
using DirectX.Common;
using DirectX.D3DCompiler;
using System.Diagnostics;
using GlitchyEngine.Renderer;
@@ -15,6 +12,7 @@ namespace GlitchyEngine
static Application s_Instance = null;
private Window _window ~ delete _;
private RendererAPI _rendererApi ~ delete _;
private bool _running = true;
private LayerStack _layerStack = new LayerStack() ~ delete _;
@@ -37,146 +35,15 @@ namespace GlitchyEngine
_window = new Window(.Default);
_window.EventCallback = new => OnEvent;
_rendererApi = new RendererAPI();
_rendererApi.Context = _window.Context;
RenderCommand.RendererAPI = _rendererApi;
Renderer.Init(_window.Context);
_imGuiLayer = new ImGuiLayer();
PushOverlay(_imGuiLayer);
MakeTestTriangle();
}
struct VertexColor : IVertexData
{
public Vector3 Position;
public Color Color;
public this() => this = default;
public this(Vector3 pos, Color color)
{
Position = pos;
Color = color;
}
//public static readonly InputElementDescription[] InputLayout ~ delete _;
public static readonly VertexLayout VertexLayout ~ delete _;
public static VertexLayout IVertexData.VertexLayout => VertexLayout;
static this()
{
//VertexLayout = new VertexLayout();
}
}
VertexLayout _vertexLayout ~ delete _;
GeometryBinding _geometryBinding ~ delete _;
VertexBuffer _vertexBuffer ~ delete _;
IndexBuffer _indexBuffer ~ delete _;
GeometryBinding _quadGeometryBinding ~ delete _;
VertexBuffer _quadVertexBuffer ~ delete _;
IndexBuffer _quadIndexBuffer ~ delete _;
RasterizerState _rasterizerState ~ delete _;
VertexShader _vertexShader ~ delete _;
PixelShader _pixelShader ~ delete _;
Buffer<ColorRGBA> _cBuffer ~ delete _;
private Vector3 CircleCoord(float angle)
{
return .(Math.Cos(angle), Math.Sin(angle), 0);
}
private void MakeTestTriangle()
{
_vertexShader = Shader.FromFile!<VertexShader>(_window.Context, "content\\basicShader.hlsl", "VS");
// Create Input Layout
_vertexLayout = new VertexLayout(_window.Context, new .(
VertexElement(.R32G32B32_Float, "POSITION"),
VertexElement(.R8G8B8A8_UNorm, "COLOR"),
), _vertexShader);
//
// Load pixel shader
//
_pixelShader = Shader.FromFile!<PixelShader>(_window.Context, "content\\basicShader.hlsl", "PS");
//Todo: _pixelShader.[Friend]_buffers = new .[1];
_cBuffer = new Buffer<ColorRGBA>(_window.Context, .(0, .Constant, .Immutable, .None));
_cBuffer.Data = .White;
_cBuffer.Update();
_pixelShader.Buffers.ReplaceBuffer("Constants", _cBuffer);
// Create hexagon
{
_geometryBinding = new GeometryBinding(_window.Context);
_geometryBinding.SetPrimitiveTopology(.TriangleList);
_geometryBinding.SetVertexLayout(_vertexLayout);
float pO3 = Math.PI_f / 3.0f;
VertexColor[?] vertices = .(
VertexColor(.Zero, Color(255,255,255)),
VertexColor(CircleCoord(0), Color(255, 0, 0)),
VertexColor(CircleCoord(pO3), Color(255,255, 0)),
VertexColor(CircleCoord(pO3*2), Color( 0,255, 0)),
VertexColor(CircleCoord(Math.PI_f), Color( 0,255,255)),
VertexColor(CircleCoord(-pO3*2), Color( 0, 0,255)),
VertexColor(CircleCoord(-pO3), Color(255, 0,255)),
);
_vertexBuffer = new VertexBuffer(Window.Context, typeof(VertexColor), (.)vertices.Count, .Immutable);
_vertexBuffer.SetData(vertices);
_geometryBinding.SetVertexBufferSlot(_vertexBuffer, 0);
uint16[?] indices = .(
0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 5,
0, 5, 6,
0, 6, 1);
_indexBuffer = new IndexBuffer(Window.Context, (.)indices.Count, .Immutable);
_indexBuffer.SetData(indices);
_geometryBinding.SetIndexBuffer(_indexBuffer);
}
// Create Quad
{
_quadGeometryBinding = new GeometryBinding(_window.Context);
_quadGeometryBinding.SetPrimitiveTopology(.TriangleList);
_quadGeometryBinding.SetVertexLayout(_vertexLayout);
VertexColor[?] vertices = .(
VertexColor(Vector3(-0.75f, 0.75f, 0), Color.Blue),
VertexColor(Vector3(-0.75f, -0.75f, 0), Color.Blue),
VertexColor(Vector3(0.75f, -0.75f, 0), Color.Blue),
VertexColor(Vector3(0.75f, 0.75f, 0), Color.Blue),
);
_quadVertexBuffer = new VertexBuffer(Window.Context, typeof(VertexColor), (.)vertices.Count, .Immutable);
_quadVertexBuffer.SetData(vertices);
_quadGeometryBinding.SetVertexBufferSlot(_quadVertexBuffer, 0);
uint16[?] indices = .(
0, 1, 2,
2, 3, 0);
_quadIndexBuffer = new IndexBuffer(Window.Context, (.)indices.Count, .Immutable);
_quadIndexBuffer.SetData(indices);
_quadGeometryBinding.SetIndexBuffer(_quadIndexBuffer);
}
// Create rasterizer state
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
_rasterizerState = new RasterizerState(Window.Context, rsDesc);
}
public void OnEvent(Event e)
@@ -200,28 +67,6 @@ namespace GlitchyEngine
Input.NewFrame();
_window.Context.ClearRenderTarget(null, .(0.2f, 0.2f, 0.2f));
// Draw test geometry
{
_window.Context.SetRenderTarget(null);
_window.Context.BindRenderTargets();
_window.Context.SetVertexShader(_vertexShader);
_window.Context.SetPixelShader(_pixelShader);
_window.Context.SetRasterizerState(_rasterizerState);
_window.Context.SetViewport(Window.Context.SwapChain.BackbufferViewport);
_geometryBinding.Bind();
_window.Context.DrawIndexed(_geometryBinding.IndexCount);
_quadGeometryBinding.Bind();
_window.Context.DrawIndexed(_quadGeometryBinding.IndexCount);
}
for(Layer layer in _layerStack)
layer.Update(_gameTime);
@@ -0,0 +1,27 @@
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
extension RendererAPI
{
private GraphicsContext _context;
public GraphicsContext Context
{
get => _context;
set => _context = value;
}
public static override API Api => .D3D11;
public override void Clear(RenderTarget renderTarget, ColorRGBA clearColor)
{
_context.ClearRenderTarget(renderTarget, clearColor);
}
public override void DrawIndexed(GeometryBinding geometry)
{
_context.DrawIndexed(geometry.IndexCount, geometry.IndexByteOffset, 0);
}
}
}
@@ -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);
}
}