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);