Added Renderer2D and 2D Example

This commit is contained in:
Simon Lübeß
2021-05-30 19:38:24 +02:00
parent 64579f9603
commit 562f4855bd
6 changed files with 692 additions and 0 deletions
+1
View File
@@ -5,6 +5,7 @@ Dependencies = {GlitchyEngine = "*", corlib = "*", LodePng = "*"}
Name = "Sandbox"
TargetType = "BeefGUIApplication"
StartupObject = "GlitchyEngine.Program"
ProcessorMacros = ["SANDBOX_2D"]
[Configs.Debug.Win64]
CLibType = "DynamicDebug"
@@ -0,0 +1,44 @@
cbuffer Constants : register(b0)
{
float3x3 World;
float4 Color;
bool HasTexture;
};
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
struct VS_Input
{
float2 Position : POSITION;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.TexCoord = input.Position;
output.Position = float4(mul(float3(input.Position, 1.0f), World), 1.0f);
return output;
}
float4 PS(PS_Input input) : SV_Target0
{
float4 color = Color;
if(HasTexture)
{
color *= Texture.Sample(Sampler, input.TexCoord);
}
return color;
}
#effect[VS=VS, PS=PS]
@@ -0,0 +1,36 @@
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
struct VS_Input
{
float2 Position : POSITION;
float4x4 Tranform : TRANSFORM;
float4 Color : COLOR;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(float4(input.Position, 0.0f, 1.0f), input.Tranform);
output.TexCoord = input.Position;
output.Color = input.Color;
return output;
}
float4 PS(PS_Input input) : SV_Target0
{
float4 color = input.Color * Texture.Sample(Sampler, input.TexCoord);
return color;
}
#effect[VS=VS, PS=PS]
+2
View File
@@ -338,7 +338,9 @@ namespace Sandbox
PushLayer(new ExampleLayer());
}
#if !SANDBOX_2D
[Export, LinkName("CreateApplication")]
#endif
public static Application CreateApplication()
{
return new SandboxApp();
+309
View File
@@ -0,0 +1,309 @@
using System;
using GlitchyEngine;
using GlitchyEngine.Events;
using System.Diagnostics;
using GlitchLog;
using GlitchyEngine.ImGui;
using ImGui;
using GlitchyEngine.Renderer;
using GlitchyEngine.Math;
using GlitchyEngine.World;
namespace Sandbox
{
class ExampleLayer2D : Layer
{
struct Vertexy : IVertexData
{
public Vector2 Position;
public this(Vector2 position)
{
Position = position;
}
public this(float x, float y)
{
Position = .(x, y);
}
public static VertexElement[] VertexElements ~ delete _;
public static VertexElement[] IVertexData.VertexElements => VertexElements;
static this()
{
VertexElements = new VertexElement[]
(
VertexElement(.R32G32_Float, "POSITION")
);
}
}
RasterizerState _rasterizerState ~ _?.ReleaseRef();
GraphicsContext _context ~ _?.ReleaseRef();
Texture2D _texture ~ _?.ReleaseRef();
Texture2D _ge_logo ~ _?.ReleaseRef();
BlendState _alphaBlendState ~ _?.ReleaseRef();
BlendState _opaqueBlendState ~ _?.ReleaseRef();
EffectLibrary _effectLibrary ~ delete _;
EcsWorld _world = new EcsWorld() ~ delete _;
Renderer2D Renderer2D ~ delete _;
[AllowAppend]
public this() : base("Example")
{
Application.Get().Window.IsVSync = false;
_context = Application.Get().Window.Context..AddRef();
_effectLibrary = new EffectLibrary(_context);
_effectLibrary.LoadNoRefInc("content\\Shaders\\basicShader.hlsl");
// Create rasterizer state
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
_rasterizerState = new RasterizerState(_context, rsDesc);
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
_ge_logo = new Texture2D(_context, "content/Textures/GE_Logo.dds");
let sampler = SamplerStateManager.GetSampler(
SamplerStateDescription()
{
MagFilter = .Point
});
_texture.SamplerState = sampler;
_ge_logo.SamplerState = sampler;
sampler.ReleaseRef();
BlendStateDescription blendDesc = .();
blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All);
_alphaBlendState = new BlendState(_context, blendDesc);
_opaqueBlendState = new BlendState(_context, .Default);
InitEcs();
Renderer2D = new Renderer2D(_context, _effectLibrary);
//Init2D();
}
VertexLayout layout ~ delete _;
GeometryBinding quadBinding ~ _?.ReleaseRef();
/*
void Init2D()
{
var effect = _effectLibrary.Load("content\\Shaders\\render2dShader.hlsl", "Renderer2D");
layout = new VertexLayout(_context, Vertexy.VertexElements, effect.VertexShader);
effect.ReleaseRef();
VertexBuffer quadVertices = new VertexBuffer(_context, typeof(Vertexy), 4, .Immutable);
Vertexy[4] vertices = .(
Vertexy(0, 0),
Vertexy(0, 1),
Vertexy(1, 1),
Vertexy(1, 0)
);
quadVertices.SetData(vertices);
IndexBuffer quadIndices = new IndexBuffer(_context, 6, .Immutable);
uint16[6] indices = .(
0, 1, 2,
2, 3, 0
);
quadIndices.SetData(indices);
quadBinding = new GeometryBinding(_context);
quadBinding.SetPrimitiveTopology(.TriangleList);
quadBinding.SetVertexBufferSlot(quadVertices, 0);
quadBinding.SetVertexLayout(layout);
quadBinding.SetIndexBuffer(quadIndices);
quadVertices.ReleaseRef();
quadIndices.ReleaseRef();
}
*/
Entity _cameraEntity;
void InitEcs()
{
_world.Register<TransformComponent>();
_world.Register<MeshComponent>();
_world.Register<CameraComponent>();
// Create camera entity
_cameraEntity = _world.NewEntity();
var cameraTransform = _world.AssignComponent<TransformComponent>(_cameraEntity);
var camera = _world.AssignComponent<CameraComponent>(_cameraEntity);
*cameraTransform = TransformComponent();
camera.NearPlane = 0.1f;
camera.FarPlane = 10.0f;
camera.FovY = Math.PI_f / 4;
cameraTransform.Position = .(0, -1, -5);
/*
for(int x < 20)
for(int y < 20)
{
Entity entity = _world.NewEntity();
var transform = _world.AssignComponent<TransformComponent>(entity);
transform.Transform = Matrix.Translation(x * 0.2f, y * 0.2f, 0) * Matrix.Scaling(0.1f);
var mesh = _world.AssignComponent<MeshComponent>(entity);
mesh.Mesh = _quadGeometryBinding;
}
*/
}
public override void Update(GameTime gameTime)
{
var cameraTransform = _world.GetComponent<TransformComponent>(_cameraEntity);
if(Application.Get().Window.IsActive)
{
Vector2 movement = .();
if(Input.IsKeyPressed(Key.W))
{
movement.Y += 1;
}
if(Input.IsKeyPressed(Key.S))
{
movement.Y -= 1;
}
if(Input.IsKeyPressed(Key.A))
{
movement.X -= 1;
}
if(Input.IsKeyPressed(Key.D))
{
movement.X += 1;
}
if(movement != .Zero)
movement.Normalize();
movement *= (float)(gameTime.FrameTime.TotalSeconds);
cameraTransform.Position += .(movement, 0);
cameraTransform.Update();
}
var camera = _world.GetComponent<CameraComponent>(_cameraEntity);
camera.Aspect = Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height;
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
// Draw test geometry
_context.SetRenderTarget(null);
_context.BindRenderTargets();
_context.SetRasterizerState(_rasterizerState);
_context.SetViewport(_context.SwapChain.BackbufferViewport);
Renderer2D.Begin(.SortByTexture, .(80, 80));
//_opaqueBlendState.Bind();
_alphaBlendState.Bind();
//Renderer2D.DrawQuad(5, 5, 1, 1, .Red);
//Renderer2D.DrawQuad(5, 5, 1, 1, .Blue);
Random r = scope Random(1337);
//_texture.Bind(0);
//_ge_logo.Bind(1);
for(int x < 40)
for(int y < 40)
{
int i = (x + y) % 2 + 1;//((x + (y * 40)) ^ (x * y)) % 3;
if(i == 1)
{
Renderer2D.Draw(_texture, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)));
}
else if(i == 2)
{
Renderer2D.Draw(_ge_logo, 2 * x, 2 * y, 1, 1, .(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)));
}
}
Renderer2D.Draw(_texture, 0, 0, 20, 10, .White, 20);
/*
Renderer2D.Draw(_texture, 10, 50, 100, 100, .White);
Renderer2D.Draw(_texture, 120, 50, 100, 100, .White);
Renderer2D.Draw(_texture, 10, 160, 100, 100, .White);
Renderer2D.Draw(_texture, 120, 160, 100, 100, .White);
*/
//_alphaBlendState.Bind();
//Renderer2D.Draw(_ge_logo, 80, 50, 100, 100, .White);
Renderer2D.End();
}
ColorRGBA _squareColor0 = ColorRGBA.CornflowerBlue;
ColorRGBA _squareColor1;
public override void OnEvent(Event event)
{
EventDispatcher dispatcher = scope EventDispatcher(event);
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
}
private bool OnImGuiRender(ImGuiRenderEvent e)
{
ImGui.Begin("Test");
ImGui.ColorEdit3("Square Color", ref _squareColor0);
_squareColor1 = ColorRGBA.White - _squareColor0;
ImGui.End();
return false;
}
}
class SandboxApp2D : Application
{
public this()
{
PushLayer(new ExampleLayer2D());
}
#if SANDBOX_2D
[Export, LinkName("CreateApplication")]
#endif
public static Application CreateApplication()
{
return new SandboxApp2D();
}
}
}