Improved PostProcessing System
This commit is contained in:
parent
0ab7f29b06
commit
e4e7db8dc0
10 changed files with 181 additions and 6 deletions
|
|
@ -4,11 +4,15 @@ layout(location = 0) in vec3 aPos;
|
||||||
layout(location = 1) in vec2 aTex;
|
layout(location = 1) in vec2 aTex;
|
||||||
|
|
||||||
uniform mat4 MVP;
|
uniform mat4 MVP;
|
||||||
|
uniform mat4 ModelMatrix;
|
||||||
|
|
||||||
out vec2 vTexture;
|
out vec2 vTexture;
|
||||||
|
out vec2 FragPos;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vTexture = aTex;
|
vTexture = aTex;
|
||||||
|
|
||||||
|
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
|
||||||
|
|
||||||
gl_Position = MVP * vec4(aPos, 1);
|
gl_Position = MVP * vec4(aPos, 1);
|
||||||
}
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ using System.Diagnostics;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
using OpenTK.Graphics.OpenGL4;
|
using OpenTK.Graphics.OpenGL4;
|
||||||
using SM.Base.Objects.Static;
|
using SM.Base.Objects.Static;
|
||||||
|
using SM.Base.Scene;
|
||||||
using SM.OGL.Framebuffer;
|
using SM.OGL.Framebuffer;
|
||||||
using SM.OGL.Shaders;
|
using SM.OGL.Shaders;
|
||||||
|
|
||||||
|
|
@ -12,6 +13,7 @@ namespace SM.Base.PostProcess
|
||||||
public abstract class PostProcessEffect
|
public abstract class PostProcessEffect
|
||||||
{
|
{
|
||||||
internal static Matrix4 Mvp;
|
internal static Matrix4 Mvp;
|
||||||
|
internal static Matrix4 Model;
|
||||||
|
|
||||||
public virtual ICollection<Framebuffer> RequiredFramebuffers { get; }
|
public virtual ICollection<Framebuffer> RequiredFramebuffers { get; }
|
||||||
|
|
||||||
|
|
@ -26,5 +28,10 @@ namespace SM.Base.PostProcess
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual void SceneChanged(GenericScene scene)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#region usings
|
#region usings
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
using SM.Base.Contexts;
|
using SM.Base.Contexts;
|
||||||
|
|
@ -15,6 +16,7 @@ namespace SM.Base.Scene
|
||||||
public abstract class GenericScene
|
public abstract class GenericScene
|
||||||
{
|
{
|
||||||
private IBackgroundItem _background;
|
private IBackgroundItem _background;
|
||||||
|
private Dictionary<Type, object> _extensions = new Dictionary<Type, object>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This contains the background.
|
/// 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>
|
/// <summary>
|
||||||
/// Called, when the user activates the scene.
|
/// Called, when the user activates the scene.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -91,6 +115,9 @@ namespace SM.Base.Scene
|
||||||
private TCollection _hud = new TCollection();
|
private TCollection _hud = new TCollection();
|
||||||
private TCollection _objectCollection = 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;
|
public bool ShowAxisHelper { get; set; } = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -159,6 +186,10 @@ namespace SM.Base.Scene
|
||||||
DrawDebug(context);
|
DrawDebug(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draws only the background.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
public void DrawBackground(DrawContext context)
|
public void DrawBackground(DrawContext context)
|
||||||
{
|
{
|
||||||
var backgroundDrawContext = context;
|
var backgroundDrawContext = context;
|
||||||
|
|
@ -166,18 +197,30 @@ namespace SM.Base.Scene
|
||||||
_Background?.Draw(backgroundDrawContext);
|
_Background?.Draw(backgroundDrawContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draws only the main objects
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
public void DrawMainObjects(DrawContext context)
|
public void DrawMainObjects(DrawContext context)
|
||||||
{
|
{
|
||||||
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
|
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
|
||||||
_objectCollection.Draw(context);
|
_objectCollection.Draw(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draws only the HUD
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
public void DrawHUD(DrawContext context)
|
public void DrawHUD(DrawContext context)
|
||||||
{
|
{
|
||||||
context.View = HUDCamera.CalculateViewMatrix();
|
context.View = HUDCamera.CalculateViewMatrix();
|
||||||
_hud.Draw(context);
|
_hud.Draw(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draw the debug informations.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
public virtual void DrawDebug(DrawContext context)
|
public virtual void DrawDebug(DrawContext context)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#region usings
|
#region usings
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
|
|
@ -27,7 +28,8 @@ namespace SM.Base
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class GenericWindow : GameWindow
|
public abstract class GenericWindow : GameWindow
|
||||||
{
|
{
|
||||||
private bool _loading;
|
internal bool _loading = true;
|
||||||
|
internal List<Action> _actionsAfterLoading = new List<Action>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This tells you the current world scale.
|
/// This tells you the current world scale.
|
||||||
|
|
@ -95,7 +97,6 @@ namespace SM.Base
|
||||||
ExtensionManager.InitExtensions();
|
ExtensionManager.InitExtensions();
|
||||||
|
|
||||||
base.OnLoad(e);
|
base.OnLoad(e);
|
||||||
_loading = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
@ -111,6 +112,10 @@ namespace SM.Base
|
||||||
if (_loading)
|
if (_loading)
|
||||||
{
|
{
|
||||||
_loading = false;
|
_loading = false;
|
||||||
|
|
||||||
|
_actionsAfterLoading.ForEach(a => a());
|
||||||
|
_actionsAfterLoading = null;
|
||||||
|
|
||||||
OnLoaded();
|
OnLoaded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -284,7 +289,8 @@ namespace SM.Base
|
||||||
ViewportCamera.RecalculateWorld(_worldScale, Aspect);
|
ViewportCamera.RecalculateWorld(_worldScale, Aspect);
|
||||||
RenderPipeline.Resize();
|
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) *
|
Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0) *
|
||||||
GenericCamera.OrthographicWorld;
|
GenericCamera.OrthographicWorld;
|
||||||
}
|
}
|
||||||
|
|
@ -295,8 +301,15 @@ namespace SM.Base
|
||||||
/// <param name="scene"></param>
|
/// <param name="scene"></param>
|
||||||
public virtual void SetScene(TScene scene)
|
public virtual void SetScene(TScene scene)
|
||||||
{
|
{
|
||||||
|
if (_loading)
|
||||||
|
{
|
||||||
|
_actionsAfterLoading.Add(() => SetScene(scene));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
CurrentScene = scene;
|
CurrentScene = scene;
|
||||||
scene.Activate();
|
scene.Activate();
|
||||||
|
RenderPipeline.SceneChanged(scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -305,6 +318,12 @@ namespace SM.Base
|
||||||
/// <param name="pipeline"></param>
|
/// <param name="pipeline"></param>
|
||||||
public void SetRenderPipeline(RenderPipeline<TScene> pipeline)
|
public void SetRenderPipeline(RenderPipeline<TScene> pipeline)
|
||||||
{
|
{
|
||||||
|
if (_loading)
|
||||||
|
{
|
||||||
|
_actionsAfterLoading.Add(() => SetRenderPipeline(pipeline));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
RenderPipeline = pipeline;
|
RenderPipeline = pipeline;
|
||||||
pipeline.Activate(this);
|
pipeline.Activate(this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,10 @@ namespace SM.Base
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class RenderPipeline
|
public abstract class RenderPipeline
|
||||||
{
|
{
|
||||||
|
public bool IsInitialized { get; private set; } = false;
|
||||||
|
|
||||||
|
protected GenericWindow _window { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The framebuffers, that are used in this Pipeline.
|
/// The framebuffers, that are used in this Pipeline.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -51,19 +55,46 @@ namespace SM.Base
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void Activate(GenericWindow window)
|
||||||
|
{
|
||||||
|
_window = window;
|
||||||
|
|
||||||
|
if (!IsInitialized)
|
||||||
|
{
|
||||||
|
Initialization(window);
|
||||||
|
IsInitialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Activation(window);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs, when the pipeline was connected to a window.
|
/// Occurs, when the pipeline was connected to a window.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected internal virtual void Activate(GenericWindow window)
|
protected internal virtual void Activation(GenericWindow window)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected internal virtual void Initialization(GenericWindow window)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Occurs, when the window is unloading.
|
/// Occurs, when the window is unloading.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected internal virtual void Unload()
|
protected internal virtual void Unload()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Framebuffer CreateWindowFramebuffer()
|
||||||
|
{
|
||||||
|
Framebuffer framebuffer = new Framebuffer(window: _window);
|
||||||
|
framebuffer.Append("color", 0);
|
||||||
|
framebuffer.Compile();
|
||||||
|
return framebuffer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -80,5 +111,10 @@ namespace SM.Base
|
||||||
{
|
{
|
||||||
context.ActivePipeline = this;
|
context.ActivePipeline = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected internal virtual void SceneChanged(TScene scene)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
20
SMCode/SM2D/Light/LightPostEffect.cs
Normal file
20
SMCode/SM2D/Light/LightPostEffect.cs
Normal 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"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
SMCode/SM2D/Light/LightSceneExtension.cs
Normal file
9
SMCode/SM2D/Light/LightSceneExtension.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
using OpenTK.Graphics;
|
||||||
|
|
||||||
|
namespace SM2D.Light
|
||||||
|
{
|
||||||
|
public class LightSceneExtension
|
||||||
|
{
|
||||||
|
public Color4 Ambient;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
SMCode/SM2D/Light/light.frag
Normal file
13
SMCode/SM2D/Light/light.frag
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ using SM.Base;
|
||||||
using SM.Base.Contexts;
|
using SM.Base.Contexts;
|
||||||
using SM.Base.Scene;
|
using SM.Base.Scene;
|
||||||
using SM.OGL.Framebuffer;
|
using SM.OGL.Framebuffer;
|
||||||
|
using SM2D.Light;
|
||||||
using SM2D.Shader;
|
using SM2D.Shader;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
@ -13,14 +14,32 @@ namespace SM2D.Pipelines
|
||||||
{
|
{
|
||||||
public class Basic2DPipeline : RenderPipeline<Scene.Scene>
|
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)
|
protected override void Render(ref DrawContext context, Scene.Scene scene)
|
||||||
{
|
{
|
||||||
base.Render(ref context, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -45,6 +45,8 @@
|
||||||
<Compile Include="Drawing\DrawParticles.cs" />
|
<Compile Include="Drawing\DrawParticles.cs" />
|
||||||
<Compile Include="Drawing\DrawText.cs" />
|
<Compile Include="Drawing\DrawText.cs" />
|
||||||
<Compile Include="GLWindow2D.cs" />
|
<Compile Include="GLWindow2D.cs" />
|
||||||
|
<Compile Include="Light\LightPostEffect.cs" />
|
||||||
|
<Compile Include="Light\LightSceneExtension.cs" />
|
||||||
<Compile Include="Object\Polygon.cs" />
|
<Compile Include="Object\Polygon.cs" />
|
||||||
<Compile Include="Object\PolygonVertex.cs" />
|
<Compile Include="Object\PolygonVertex.cs" />
|
||||||
<Compile Include="Pipelines\Adv2DPipeline.cs" />
|
<Compile Include="Pipelines\Adv2DPipeline.cs" />
|
||||||
|
|
@ -75,5 +77,8 @@
|
||||||
<Version>3.2.1</Version>
|
<Version>3.2.1</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Light\light.frag" />
|
||||||
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue