diff --git a/SMCode/SM.Base/PostProcess/DefaultFiles/vertexFile.vert b/SMCode/SM.Base/PostProcess/DefaultFiles/vertexFile.vert index 0631261..de072b9 100644 --- a/SMCode/SM.Base/PostProcess/DefaultFiles/vertexFile.vert +++ b/SMCode/SM.Base/PostProcess/DefaultFiles/vertexFile.vert @@ -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); } \ No newline at end of file diff --git a/SMCode/SM.Base/PostProcess/PostProcessEffect.cs b/SMCode/SM.Base/PostProcess/PostProcessEffect.cs index 88851ee..f71cf94 100644 --- a/SMCode/SM.Base/PostProcess/PostProcessEffect.cs +++ b/SMCode/SM.Base/PostProcess/PostProcessEffect.cs @@ -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 RequiredFramebuffers { get; } @@ -26,5 +28,10 @@ namespace SM.Base.PostProcess { } + + public virtual void SceneChanged(GenericScene scene) + { + + } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index 6377ffc..3e807a9 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -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 _extensions = new Dictionary(); /// /// This contains the background. @@ -49,6 +51,28 @@ namespace SM.Base.Scene { } + /// + /// Adds a extension to the scene. + /// + /// + public virtual void SetExtension(object extension) + { + _extensions[extension.GetType()] = extension; + } + + /// + /// Gets a extension with the type. + /// + /// + /// + /// + public virtual T GetExtension() + { + object ext = _extensions[typeof(T)]; + if (ext == null) throw new Exception("[Error] Tried to get a extension, that doesn't exist."); + return (T)ext; + } + /// /// Called, when the user activates the scene. /// @@ -91,6 +115,9 @@ namespace SM.Base.Scene private TCollection _hud = new TCollection(); private TCollection _objectCollection = new TCollection(); + /// + /// If true, shows a axis helper at (0,0,0) + /// public bool ShowAxisHelper { get; set; } = false; /// @@ -159,6 +186,10 @@ namespace SM.Base.Scene DrawDebug(context); } + /// + /// Draws only the background. + /// + /// public void DrawBackground(DrawContext context) { var backgroundDrawContext = context; @@ -166,18 +197,30 @@ namespace SM.Base.Scene _Background?.Draw(backgroundDrawContext); } + /// + /// Draws only the main objects + /// + /// public void DrawMainObjects(DrawContext context) { if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix(); _objectCollection.Draw(context); } + /// + /// Draws only the HUD + /// + /// public void DrawHUD(DrawContext context) { context.View = HUDCamera.CalculateViewMatrix(); _hud.Draw(context); } + /// + /// Draw the debug informations. + /// + /// public virtual void DrawDebug(DrawContext context) { diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index 03a3fc8..0acb5b3 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -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 /// public abstract class GenericWindow : GameWindow { - private bool _loading; + internal bool _loading = true; + internal List _actionsAfterLoading = new List(); /// /// This tells you the current world scale. @@ -95,7 +97,6 @@ namespace SM.Base ExtensionManager.InitExtensions(); base.OnLoad(e); - _loading = true; } /// @@ -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 /// public virtual void SetScene(TScene scene) { + if (_loading) + { + _actionsAfterLoading.Add(() => SetScene(scene)); + return; + } + CurrentScene = scene; scene.Activate(); + RenderPipeline.SceneChanged(scene); } /// @@ -305,6 +318,12 @@ namespace SM.Base /// public void SetRenderPipeline(RenderPipeline pipeline) { + if (_loading) + { + _actionsAfterLoading.Add(() => SetRenderPipeline(pipeline)); + return; + } + RenderPipeline = pipeline; pipeline.Activate(this); } diff --git a/SMCode/SM.Base/Window/RenderPipeline.cs b/SMCode/SM.Base/Window/RenderPipeline.cs index a7436a4..fd87991 100644 --- a/SMCode/SM.Base/Window/RenderPipeline.cs +++ b/SMCode/SM.Base/Window/RenderPipeline.cs @@ -16,6 +16,10 @@ namespace SM.Base /// public abstract class RenderPipeline { + public bool IsInitialized { get; private set; } = false; + + protected GenericWindow _window { get; private set; } + /// /// The framebuffers, that are used in this Pipeline. /// @@ -51,19 +55,46 @@ namespace SM.Base } } + internal void Activate(GenericWindow window) + { + _window = window; + + if (!IsInitialized) + { + Initialization(window); + IsInitialized = true; + } + + Activation(window); + } + /// /// Occurs, when the pipeline was connected to a window. /// - protected internal virtual void Activate(GenericWindow window) + protected internal virtual void Activation(GenericWindow window) { } + + protected internal virtual void Initialization(GenericWindow window) + { + + } + /// /// Occurs, when the window is unloading. /// protected internal virtual void Unload() { } + + protected Framebuffer CreateWindowFramebuffer() + { + Framebuffer framebuffer = new Framebuffer(window: _window); + framebuffer.Append("color", 0); + framebuffer.Compile(); + return framebuffer; + } } /// @@ -80,5 +111,10 @@ namespace SM.Base { context.ActivePipeline = this; } + + protected internal virtual void SceneChanged(TScene scene) + { + + } } } \ No newline at end of file diff --git a/SMCode/SM2D/Light/LightPostEffect.cs b/SMCode/SM2D/Light/LightPostEffect.cs new file mode 100644 index 0000000..dc0d251 --- /dev/null +++ b/SMCode/SM2D/Light/LightPostEffect.cs @@ -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"]); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Light/LightSceneExtension.cs b/SMCode/SM2D/Light/LightSceneExtension.cs new file mode 100644 index 0000000..fa82165 --- /dev/null +++ b/SMCode/SM2D/Light/LightSceneExtension.cs @@ -0,0 +1,9 @@ +using OpenTK.Graphics; + +namespace SM2D.Light +{ + public class LightSceneExtension + { + public Color4 Ambient; + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Light/light.frag b/SMCode/SM2D/Light/light.frag new file mode 100644 index 0000000..af67798 --- /dev/null +++ b/SMCode/SM2D/Light/light.frag @@ -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; +} \ No newline at end of file diff --git a/SMCode/SM2D/Pipelines/Basic2DPipeline.cs b/SMCode/SM2D/Pipelines/Basic2DPipeline.cs index a74b4f1..049f3e7 100644 --- a/SMCode/SM2D/Pipelines/Basic2DPipeline.cs +++ b/SMCode/SM2D/Pipelines/Basic2DPipeline.cs @@ -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 { + 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); + } } } } \ No newline at end of file diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index 7ebd7c1..6eb70dc 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -45,6 +45,8 @@ + + @@ -75,5 +77,8 @@ 3.2.1 + + + \ No newline at end of file