Improved PostProcessing System

This commit is contained in:
Michel Fedde 2020-12-13 16:13:00 +01:00
parent 0ab7f29b06
commit e4e7db8dc0
10 changed files with 181 additions and 6 deletions

View file

@ -4,11 +4,15 @@ layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
uniform mat4 MVP;
uniform mat4 ModelMatrix;
out vec2 vTexture;
out vec2 FragPos;
void main() {
vTexture = aTex;
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
gl_Position = MVP * vec4(aPos, 1);
}

View file

@ -4,6 +4,7 @@ using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base.Objects.Static;
using SM.Base.Scene;
using SM.OGL.Framebuffer;
using SM.OGL.Shaders;
@ -12,6 +13,7 @@ namespace SM.Base.PostProcess
public abstract class PostProcessEffect
{
internal static Matrix4 Mvp;
internal static Matrix4 Model;
public virtual ICollection<Framebuffer> RequiredFramebuffers { get; }
@ -26,5 +28,10 @@ namespace SM.Base.PostProcess
{
}
public virtual void SceneChanged(GenericScene scene)
{
}
}
}

View file

@ -1,5 +1,6 @@
#region usings
using System;
using System.Collections.Generic;
using System.Dynamic;
using SM.Base.Contexts;
@ -15,6 +16,7 @@ namespace SM.Base.Scene
public abstract class GenericScene
{
private IBackgroundItem _background;
private Dictionary<Type, object> _extensions = new Dictionary<Type, object>();
/// <summary>
/// This contains the background.
@ -49,6 +51,28 @@ namespace SM.Base.Scene
{
}
/// <summary>
/// Adds a extension to the scene.
/// </summary>
/// <param name="extension"></param>
public virtual void SetExtension(object extension)
{
_extensions[extension.GetType()] = extension;
}
/// <summary>
/// Gets a extension with the type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public virtual T GetExtension<T>()
{
object ext = _extensions[typeof(T)];
if (ext == null) throw new Exception("[Error] Tried to get a extension, that doesn't exist.");
return (T)ext;
}
/// <summary>
/// Called, when the user activates the scene.
/// </summary>
@ -91,6 +115,9 @@ namespace SM.Base.Scene
private TCollection _hud = new TCollection();
private TCollection _objectCollection = new TCollection();
/// <summary>
/// If true, shows a axis helper at (0,0,0)
/// </summary>
public bool ShowAxisHelper { get; set; } = false;
/// <summary>
@ -159,6 +186,10 @@ namespace SM.Base.Scene
DrawDebug(context);
}
/// <summary>
/// Draws only the background.
/// </summary>
/// <param name="context"></param>
public void DrawBackground(DrawContext context)
{
var backgroundDrawContext = context;
@ -166,18 +197,30 @@ namespace SM.Base.Scene
_Background?.Draw(backgroundDrawContext);
}
/// <summary>
/// Draws only the main objects
/// </summary>
/// <param name="context"></param>
public void DrawMainObjects(DrawContext context)
{
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
_objectCollection.Draw(context);
}
/// <summary>
/// Draws only the HUD
/// </summary>
/// <param name="context"></param>
public void DrawHUD(DrawContext context)
{
context.View = HUDCamera.CalculateViewMatrix();
_hud.Draw(context);
}
/// <summary>
/// Draw the debug informations.
/// </summary>
/// <param name="context"></param>
public virtual void DrawDebug(DrawContext context)
{

View file

@ -1,6 +1,7 @@
#region usings
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenTK;
@ -27,7 +28,8 @@ namespace SM.Base
/// </summary>
public abstract class GenericWindow : GameWindow
{
private bool _loading;
internal bool _loading = true;
internal List<Action> _actionsAfterLoading = new List<Action>();
/// <summary>
/// This tells you the current world scale.
@ -95,7 +97,6 @@ namespace SM.Base
ExtensionManager.InitExtensions();
base.OnLoad(e);
_loading = true;
}
/// <inheritdoc />
@ -111,6 +112,10 @@ namespace SM.Base
if (_loading)
{
_loading = false;
_actionsAfterLoading.ForEach(a => a());
_actionsAfterLoading = null;
OnLoaded();
}
}
@ -284,7 +289,8 @@ namespace SM.Base
ViewportCamera.RecalculateWorld(_worldScale, Aspect);
RenderPipeline.Resize();
PostProcessEffect.Mvp = Matrix4.CreateScale(_worldScale.X, -_worldScale.Y, 1) *
PostProcessEffect.Model = Matrix4.CreateScale(_worldScale.X, -_worldScale.Y, 1);
PostProcessEffect.Mvp = PostProcessEffect.Model *
Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0) *
GenericCamera.OrthographicWorld;
}
@ -295,8 +301,15 @@ namespace SM.Base
/// <param name="scene"></param>
public virtual void SetScene(TScene scene)
{
if (_loading)
{
_actionsAfterLoading.Add(() => SetScene(scene));
return;
}
CurrentScene = scene;
scene.Activate();
RenderPipeline.SceneChanged(scene);
}
/// <summary>
@ -305,6 +318,12 @@ namespace SM.Base
/// <param name="pipeline"></param>
public void SetRenderPipeline(RenderPipeline<TScene> pipeline)
{
if (_loading)
{
_actionsAfterLoading.Add(() => SetRenderPipeline(pipeline));
return;
}
RenderPipeline = pipeline;
pipeline.Activate(this);
}

View file

@ -16,6 +16,10 @@ namespace SM.Base
/// </summary>
public abstract class RenderPipeline
{
public bool IsInitialized { get; private set; } = false;
protected GenericWindow _window { get; private set; }
/// <summary>
/// The framebuffers, that are used in this Pipeline.
/// </summary>
@ -51,19 +55,46 @@ namespace SM.Base
}
}
internal void Activate(GenericWindow window)
{
_window = window;
if (!IsInitialized)
{
Initialization(window);
IsInitialized = true;
}
Activation(window);
}
/// <summary>
/// Occurs, when the pipeline was connected to a window.
/// </summary>
protected internal virtual void Activate(GenericWindow window)
protected internal virtual void Activation(GenericWindow window)
{
}
protected internal virtual void Initialization(GenericWindow window)
{
}
/// <summary>
/// Occurs, when the window is unloading.
/// </summary>
protected internal virtual void Unload()
{
}
protected Framebuffer CreateWindowFramebuffer()
{
Framebuffer framebuffer = new Framebuffer(window: _window);
framebuffer.Append("color", 0);
framebuffer.Compile();
return framebuffer;
}
}
/// <summary>
@ -80,5 +111,10 @@ namespace SM.Base
{
context.ActivePipeline = this;
}
protected internal virtual void SceneChanged(TScene scene)
{
}
}
}

View file

@ -0,0 +1,20 @@
using System.Reflection;
using SM.Base.PostProcess;
using SM.Base.Scene;
using SM.OGL.Framebuffer;
using SM.Utility;
namespace SM2D.Light
{
public class LightPostEffect : PostProcessEffect
{
PostProcessShader _shader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile("SM2D.Light.light.frag"));
public override void Draw(Framebuffer main)
{
base.Draw(main);
_shader.Draw(main.ColorAttachments["color"]);
}
}
}

View file

@ -0,0 +1,9 @@
using OpenTK.Graphics;
namespace SM2D.Light
{
public class LightSceneExtension
{
public Color4 Ambient;
}
}

View file

@ -0,0 +1,13 @@
#version 330
in vec2 vTexture;
vec4 GetRenderColor();
uniform vec4 Ambient = vec4(1);
layout(location = 0) out vec4 color;
void main() {
color = GetRenderColor() * Ambient;
}

View file

@ -5,6 +5,7 @@ using SM.Base;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Framebuffer;
using SM2D.Light;
using SM2D.Shader;
#endregion
@ -13,14 +14,32 @@ namespace SM2D.Pipelines
{
public class Basic2DPipeline : RenderPipeline<Scene.Scene>
{
private Framebuffer _tempWindow;
private Light.LightPostEffect _lightEffect;
protected override void Initialization(GenericWindow window)
{
_tempWindow = CreateWindowFramebuffer();
_lightEffect = new LightPostEffect();
}
protected override void Render(ref DrawContext context, Scene.Scene scene)
{
base.Render(ref context, scene);
Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
_tempWindow.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
scene?.Draw(context);
if (scene != null)
{
scene.DrawBackground(context);
scene.DrawMainObjects(context);
Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
_lightEffect.Draw(_tempWindow);
scene.DrawHUD(context);
}
}
}
}

View file

@ -45,6 +45,8 @@
<Compile Include="Drawing\DrawParticles.cs" />
<Compile Include="Drawing\DrawText.cs" />
<Compile Include="GLWindow2D.cs" />
<Compile Include="Light\LightPostEffect.cs" />
<Compile Include="Light\LightSceneExtension.cs" />
<Compile Include="Object\Polygon.cs" />
<Compile Include="Object\PolygonVertex.cs" />
<Compile Include="Pipelines\Adv2DPipeline.cs" />
@ -75,5 +77,8 @@
<Version>3.2.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Light\light.frag" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>