From c6bf5c75bc932047664158c6dee37dac01ce1cf2 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Thu, 4 Mar 2021 22:17:03 +0100 Subject: [PATCH] 2021-04-03 + Added Field to DrawingBasis to enfore PrimitiveTypes + LastObject to context for Debugging + AppendRenderbuffer to Framebuffer ~ Added a depthbuffer to RenderPipeline.CreateWindowFramebuffer ~ Proper ZIndices over Z-Translation and DepthBuffer ~ Made "ShaderArguments["LineWidth"]" set the LineWidth even if the object isn't maked as LineMesh ~ Made that the SM2D.PolygonVertex can translate OpenTK.Vector2 by itself. ~ Made the SM2D.Transformation.ZIndex a CVector1 instead a float --- SMCode/SM.Base/Drawing/DrawingBasis.cs | 6 +++ SMCode/SM.Base/Scene/GenericScene.cs | 6 +-- SMCode/SM.Base/Shaders/MaterialShader.cs | 2 + SMCode/SM.Base/Shaders/SimpleShader.cs | 4 +- SMCode/SM.Base/Window/Contexts/DrawContext.cs | 3 ++ SMCode/SM.Base/Window/RenderPipeline.cs | 5 ++- SMCode/SM.Base/Window/WindowCode.cs | 1 + SMCode/SM.OGL/Framebuffer/Framebuffer.cs | 41 ++++++++++++------- .../Framebuffer/RenderbufferAttachment.cs | 35 ++++++++++++++++ SMCode/SM.OGL/SM.OGL.csproj | 1 + SMCode/SM.OGL/Shaders/GenericShader.cs | 14 ++++++- SMCode/SM2D/Drawing/DrawBackground.cs | 2 +- SMCode/SM2D/Object/PolygonVertex.cs | 2 + SMCode/SM2D/Scene/Camera.cs | 4 +- SMCode/SM2D/Scene/Scene.cs | 8 ++++ SMCode/SM2D/Types/Transformation.cs | 6 ++- 16 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 SMCode/SM.OGL/Framebuffer/RenderbufferAttachment.cs diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index b235784..38daa5d 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -1,10 +1,12 @@ #region usings using System.Collections.Generic; +using OpenTK.Graphics.ES11; using SM.Base; using SM.Base.Scene; using SM.Base.Windows; using SM.OGL.Mesh; +using PrimitiveType = OpenTK.Graphics.OpenGL4.PrimitiveType; #endregion @@ -38,6 +40,8 @@ namespace SM.Base.Drawing /// public ICollection Flags { get; set; } + public PrimitiveType? ForcedMeshType { get; set; } + /// /// This value determents if the object should draw something. /// @@ -68,7 +72,9 @@ namespace SM.Base.Drawing /// protected virtual void DrawContext(ref DrawContext context) { + context.ForcedType = ForcedMeshType; context.TextureMatrix *= TextureTransform.GetMatrix(); + context.LastObject = this; } } diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index edff7fa..8c2796b 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -122,7 +122,7 @@ namespace SM.Base.Scene /// Draws only the background. /// /// - public void DrawBackground(DrawContext context) + public virtual void DrawBackground(DrawContext context) { var backgroundDrawContext = context; backgroundDrawContext.SetCamera(BackgroundCamera); @@ -133,7 +133,7 @@ namespace SM.Base.Scene /// Draws only the main objects /// /// - public void DrawMainObjects(DrawContext context) + public virtual void DrawMainObjects(DrawContext context) { if (!context.Window.ForceViewportCamera && Camera != null) context.SetCamera(Camera); _objectCollection.Draw(context); @@ -143,7 +143,7 @@ namespace SM.Base.Scene /// Draws only the HUD /// /// - public void DrawHUD(DrawContext context) + public virtual void DrawHUD(DrawContext context) { context.SetCamera(HUDCamera); _hud?.Draw(context); diff --git a/SMCode/SM.Base/Shaders/MaterialShader.cs b/SMCode/SM.Base/Shaders/MaterialShader.cs index ba4b142..504aea6 100644 --- a/SMCode/SM.Base/Shaders/MaterialShader.cs +++ b/SMCode/SM.Base/Shaders/MaterialShader.cs @@ -41,6 +41,8 @@ namespace SM.Base.Drawing if (context.Mesh is ILineMesh lineMesh) GL.LineWidth(context.Material.ShaderArguments.Get("LineWidth", lineMesh.LineWidth)); + else if (context.Material.ShaderArguments.ContainsKey("LineWidth")) + GL.LineWidth((float)context.Material.ShaderArguments["LineWidth"]); if (context.Material.Blending) { diff --git a/SMCode/SM.Base/Shaders/SimpleShader.cs b/SMCode/SM.Base/Shaders/SimpleShader.cs index 27dbf9d..1a6d2e6 100644 --- a/SMCode/SM.Base/Shaders/SimpleShader.cs +++ b/SMCode/SM.Base/Shaders/SimpleShader.cs @@ -57,7 +57,7 @@ namespace SM.Base.Drawing uniforms["HasVColor"] .SetUniform1(context.Mesh.Attributes.Has("color")); - DrawObject(context.Mesh); + DrawObject(context.ForcedType.GetValueOrDefault(context.Mesh.PrimitiveType), context.Mesh); } static void InstancedSetUniforms(UniformCollection uniforms, DrawContext context) @@ -86,7 +86,7 @@ namespace SM.Base.Drawing shaderInstanceI++; } - DrawObject(context.Mesh, shaderInstanceI); + DrawObject(context.ForcedType.GetValueOrDefault(context.Mesh.PrimitiveType), context.Mesh, shaderInstanceI); } private string _vertexPreset; diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index eaf6930..7f32c6a 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using OpenTK; +using OpenTK.Graphics.OpenGL4; using SM.Base.Drawing; using SM.Base.Scene; using SM.OGL.Mesh; @@ -11,12 +12,14 @@ namespace SM.Base.Windows public IGenericWindow Window { get; internal set; } public GenericScene Scene { get; internal set; } public RenderPipeline RenderPipeline { get; internal set; } + public object LastObject { get; internal set; } public GenericCamera UseCamera { get; internal set; } public Matrix4 World => UseCamera.World; public Matrix4 View => UseCamera.View; public GenericMesh Mesh { get; set; } + public PrimitiveType? ForcedType { get; set; } public Material Material { get; set; } public MaterialShader Shader => Material.CustomShader ?? RenderPipeline.DefaultShader; diff --git a/SMCode/SM.Base/Window/RenderPipeline.cs b/SMCode/SM.Base/Window/RenderPipeline.cs index 2777f10..4513cfd 100644 --- a/SMCode/SM.Base/Window/RenderPipeline.cs +++ b/SMCode/SM.Base/Window/RenderPipeline.cs @@ -47,10 +47,13 @@ namespace SM.Base.Windows DefaultShader ??= SMRenderer.DefaultMaterialShader; } - public Framebuffer CreateWindowFramebuffer(int multisamples) + public Framebuffer CreateWindowFramebuffer(int multisamples = 0) { Framebuffer framebuffer = new Framebuffer(window: ConnectedWindow); framebuffer.Append("color", new ColorAttachment(0, PixelInformation.RGBA_LDR, multisamples)); + RenderbufferAttachment depthAttach = RenderbufferAttachment.Depth; + depthAttach.Multisample = multisamples; + framebuffer.AppendRenderbuffer(depthAttach); return framebuffer; } } diff --git a/SMCode/SM.Base/Window/WindowCode.cs b/SMCode/SM.Base/Window/WindowCode.cs index ea2c565..a666aaa 100644 --- a/SMCode/SM.Base/Window/WindowCode.cs +++ b/SMCode/SM.Base/Window/WindowCode.cs @@ -114,6 +114,7 @@ namespace SM.Base.Windows }; drawContext.SetCamera(window.ViewportCamera); + GL.DepthFunc(DepthFunction.Lequal); window.CurrentRenderPipeline?.Render(ref drawContext); } diff --git a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs index 1fdd3bf..1bb426e 100644 --- a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs +++ b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs @@ -30,6 +30,22 @@ namespace SM.OGL.Framebuffer private IFramebufferWindow _window; private float _windowScale; + /// + public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer; + + /// + /// Contains the size of the framebuffer. + /// + public Vector2 Size { get; private set; } + + /// + /// Contains all color attachments. + /// + public Dictionary ColorAttachments { get; private set; } = + new Dictionary(); + + public List RenderbufferAttachments { get; } = new List(); + /// /// Creates a buffer without any options. /// @@ -58,20 +74,6 @@ namespace SM.OGL.Framebuffer Size = size; } - /// - public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer; - - /// - /// Contains the size of the framebuffer. - /// - public Vector2 Size { get; private set; } - - /// - /// Contains all color attachments. - /// - public Dictionary ColorAttachments { get; private set; } = - new Dictionary(); - /// public override void Compile() { @@ -97,6 +99,12 @@ namespace SM.OGL.Framebuffer GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.Target, pair.Value.ID, 0); + foreach (RenderbufferAttachment attachment in RenderbufferAttachments) + { + int att = attachment.Generate(this); + GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachment.FramebufferAttachment, RenderbufferTarget.Renderbuffer, att); + } + var err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (err != FramebufferErrorCode.FramebufferComplete) throw new Exception("Failed loading framebuffer.\nProblem: " + err); @@ -125,6 +133,11 @@ namespace SM.OGL.Framebuffer ColorAttachments.Add(key, value); } + public void AppendRenderbuffer(RenderbufferAttachment attachment) + { + RenderbufferAttachments.Add(attachment); + } + /// /// Activates the framebuffer without clearing the buffer. /// diff --git a/SMCode/SM.OGL/Framebuffer/RenderbufferAttachment.cs b/SMCode/SM.OGL/Framebuffer/RenderbufferAttachment.cs new file mode 100644 index 0000000..e7cf6bf --- /dev/null +++ b/SMCode/SM.OGL/Framebuffer/RenderbufferAttachment.cs @@ -0,0 +1,35 @@ +using OpenTK.Graphics.OpenGL4; + +namespace SM.OGL.Framebuffer +{ + public struct RenderbufferAttachment + { + public static readonly RenderbufferAttachment Depth = new RenderbufferAttachment(RenderbufferStorage.Depth24Stencil8, FramebufferAttachment.DepthStencilAttachment); + + public RenderbufferStorage Storage; + public FramebufferAttachment FramebufferAttachment; + + public int Multisample; + + public RenderbufferAttachment(RenderbufferStorage storage, FramebufferAttachment framebufferAttachment, int multisample = 0) + { + Storage = storage; + FramebufferAttachment = framebufferAttachment; + Multisample = multisample; + } + + public int Generate(Framebuffer f) + { + int rbo = GL.GenRenderbuffer(); + + GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, rbo); + if (Multisample != 0) + GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, Multisample, Storage, (int)f.Size.X, (int)f.Size.Y); + else + GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, Storage, (int)f.Size.X, (int)f.Size.Y); + GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); + + return rbo; + } + } +} \ No newline at end of file diff --git a/SMCode/SM.OGL/SM.OGL.csproj b/SMCode/SM.OGL/SM.OGL.csproj index a802a51..74321d3 100644 --- a/SMCode/SM.OGL/SM.OGL.csproj +++ b/SMCode/SM.OGL/SM.OGL.csproj @@ -44,6 +44,7 @@ + diff --git a/SMCode/SM.OGL/Shaders/GenericShader.cs b/SMCode/SM.OGL/Shaders/GenericShader.cs index e911463..d0b6cf2 100644 --- a/SMCode/SM.OGL/Shaders/GenericShader.cs +++ b/SMCode/SM.OGL/Shaders/GenericShader.cs @@ -132,7 +132,19 @@ namespace SM.OGL.Shaders if (mesh.Indices != null) GL.DrawElementsInstanced(mesh.PrimitiveType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount); else - GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount); + GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count / mesh.Vertex.PointerSize, amount); + } + /// + /// Draws the mesh while forcing a primitive type instead of using the mesh type. + /// + /// The mesh. + /// The amounts for instancing. + public static void DrawObject(PrimitiveType forcedType, GenericMesh mesh, int amount = 1) + { + if (mesh.Indices != null) + GL.DrawElementsInstanced(forcedType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount); + else + GL.DrawArraysInstanced(forcedType, 0, mesh.Vertex.Count / mesh.Vertex.PointerSize, amount); } /// diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs index 2b62d60..f939fb0 100644 --- a/SMCode/SM2D/Drawing/DrawBackground.cs +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -57,7 +57,7 @@ namespace SM2D.Drawing protected override void DrawContext(ref DrawContext context) { base.DrawContext(ref context); - context.ModelMatrix = Matrix4.CreateScale((context.UseCamera as Camera).WorldScale.X, (context.UseCamera as Camera).WorldScale.Y, 1); + context.ModelMatrix = Matrix4.CreateScale((context.UseCamera as Camera).WorldScale.X, (context.UseCamera as Camera).WorldScale.Y, 0) * Matrix4.CreateTranslation(0,0, -1.1f); context.Shader.Draw(context); } } diff --git a/SMCode/SM2D/Object/PolygonVertex.cs b/SMCode/SM2D/Object/PolygonVertex.cs index d43ca52..a288e05 100644 --- a/SMCode/SM2D/Object/PolygonVertex.cs +++ b/SMCode/SM2D/Object/PolygonVertex.cs @@ -17,5 +17,7 @@ namespace SM2D.Object Vertex = vertex; Color = color; } + + public static implicit operator PolygonVertex(Vector2 vec) => new PolygonVertex(vec, Color4.White); } } \ No newline at end of file diff --git a/SMCode/SM2D/Scene/Camera.cs b/SMCode/SM2D/Scene/Camera.cs index a643dee..0abd1c2 100644 --- a/SMCode/SM2D/Scene/Camera.cs +++ b/SMCode/SM2D/Scene/Camera.cs @@ -38,7 +38,7 @@ namespace SM2D.Scene protected override Matrix4 ViewCalculation(IGenericWindow window) { - return Matrix4.LookAt(Position.X, Position.Y, 1, Position.X, Position.Y, 0, 0, 1, 0); + return Matrix4.LookAt(Position.X, Position.Y, 2f, Position.X, Position.Y, 0f, 0, 1, 0); } protected override bool WorldCalculation(IGenericWindow window, out Matrix4 world) @@ -50,7 +50,7 @@ namespace SM2D.Scene _resizeCounter = ResizeCounter; CalculateWorldScale(window); - world = Matrix4.CreateOrthographic(WorldScale.X, WorldScale.Y, .0001f, 1.5f); + world = Matrix4.CreateOrthographic(WorldScale.X, WorldScale.Y, .001f, 3.2f); return true; } diff --git a/SMCode/SM2D/Scene/Scene.cs b/SMCode/SM2D/Scene/Scene.cs index 0625838..74d3fe3 100644 --- a/SMCode/SM2D/Scene/Scene.cs +++ b/SMCode/SM2D/Scene/Scene.cs @@ -1,5 +1,6 @@ #region usings +using System.Drawing.Drawing2D; using OpenTK; using OpenTK.Graphics; using SM.Base; @@ -39,6 +40,13 @@ namespace SM2D.Scene set => _Background = value; } + public override void DrawHUD(DrawContext context) + { + context.ModelMatrix *= Matrix4.CreateTranslation(0,0,1); + + base.DrawHUD(context); + } + public override void DrawDebug(DrawContext context) { if (ShowAxisHelper) diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs index 2f32a73..1930ed9 100644 --- a/SMCode/SM2D/Types/Transformation.cs +++ b/SMCode/SM2D/Types/Transformation.cs @@ -27,15 +27,17 @@ namespace SM2D.Types public bool VerticalFlip { get; set; } = false; - public int ZIndex { get; set; } + public CVector1 ZIndex { get; set; } = new CVector1(0); protected override Matrix4 RequestMatrix() { + float z = 1 / (float) ZIndexPercision * ZIndex; + return Matrix4.CreateScale(Size.X, Size.Y, 1) * Matrix4.CreateRotationX(MathHelper.DegreesToRadians(HorizontalFlip ? 180 : 0)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(VerticalFlip ? 180 : 0)) * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) * - Matrix4.CreateTranslation(Position.X, Position.Y, -(1 / (float)ZIndexPercision * ZIndex)); + Matrix4.CreateTranslation(Position.X, Position.Y, z); } public void TurnTo(Vector2 v)