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
This commit is contained in:
Michel Fedde 2021-03-04 22:17:03 +01:00
parent a7c71e7ea1
commit c6bf5c75bc
16 changed files with 114 additions and 26 deletions

View file

@ -1,10 +1,12 @@
#region usings #region usings
using System.Collections.Generic; using System.Collections.Generic;
using OpenTK.Graphics.ES11;
using SM.Base; using SM.Base;
using SM.Base.Scene; using SM.Base.Scene;
using SM.Base.Windows; using SM.Base.Windows;
using SM.OGL.Mesh; using SM.OGL.Mesh;
using PrimitiveType = OpenTK.Graphics.OpenGL4.PrimitiveType;
#endregion #endregion
@ -38,6 +40,8 @@ namespace SM.Base.Drawing
/// <inheritdoc /> /// <inheritdoc />
public ICollection<string> Flags { get; set; } public ICollection<string> Flags { get; set; }
public PrimitiveType? ForcedMeshType { get; set; }
/// <summary> /// <summary>
/// This value determents if the object should draw something. /// This value determents if the object should draw something.
/// </summary> /// </summary>
@ -68,7 +72,9 @@ namespace SM.Base.Drawing
/// <param name="context"></param> /// <param name="context"></param>
protected virtual void DrawContext(ref DrawContext context) protected virtual void DrawContext(ref DrawContext context)
{ {
context.ForcedType = ForcedMeshType;
context.TextureMatrix *= TextureTransform.GetMatrix(); context.TextureMatrix *= TextureTransform.GetMatrix();
context.LastObject = this;
} }
} }

View file

@ -122,7 +122,7 @@ namespace SM.Base.Scene
/// Draws only the background. /// Draws only the background.
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
public void DrawBackground(DrawContext context) public virtual void DrawBackground(DrawContext context)
{ {
var backgroundDrawContext = context; var backgroundDrawContext = context;
backgroundDrawContext.SetCamera(BackgroundCamera); backgroundDrawContext.SetCamera(BackgroundCamera);
@ -133,7 +133,7 @@ namespace SM.Base.Scene
/// Draws only the main objects /// Draws only the main objects
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
public void DrawMainObjects(DrawContext context) public virtual void DrawMainObjects(DrawContext context)
{ {
if (!context.Window.ForceViewportCamera && Camera != null) context.SetCamera(Camera); if (!context.Window.ForceViewportCamera && Camera != null) context.SetCamera(Camera);
_objectCollection.Draw(context); _objectCollection.Draw(context);
@ -143,7 +143,7 @@ namespace SM.Base.Scene
/// Draws only the HUD /// Draws only the HUD
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
public void DrawHUD(DrawContext context) public virtual void DrawHUD(DrawContext context)
{ {
context.SetCamera(HUDCamera); context.SetCamera(HUDCamera);
_hud?.Draw(context); _hud?.Draw(context);

View file

@ -41,6 +41,8 @@ namespace SM.Base.Drawing
if (context.Mesh is ILineMesh lineMesh) if (context.Mesh is ILineMesh lineMesh)
GL.LineWidth(context.Material.ShaderArguments.Get("LineWidth", lineMesh.LineWidth)); 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) if (context.Material.Blending)
{ {

View file

@ -57,7 +57,7 @@ namespace SM.Base.Drawing
uniforms["HasVColor"] uniforms["HasVColor"]
.SetUniform1(context.Mesh.Attributes.Has("color")); .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) static void InstancedSetUniforms(UniformCollection uniforms, DrawContext context)
@ -86,7 +86,7 @@ namespace SM.Base.Drawing
shaderInstanceI++; shaderInstanceI++;
} }
DrawObject(context.Mesh, shaderInstanceI); DrawObject(context.ForcedType.GetValueOrDefault(context.Mesh.PrimitiveType), context.Mesh, shaderInstanceI);
} }
private string _vertexPreset; private string _vertexPreset;

View file

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenTK; using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base.Drawing; using SM.Base.Drawing;
using SM.Base.Scene; using SM.Base.Scene;
using SM.OGL.Mesh; using SM.OGL.Mesh;
@ -11,12 +12,14 @@ namespace SM.Base.Windows
public IGenericWindow Window { get; internal set; } public IGenericWindow Window { get; internal set; }
public GenericScene Scene { get; internal set; } public GenericScene Scene { get; internal set; }
public RenderPipeline RenderPipeline { get; internal set; } public RenderPipeline RenderPipeline { get; internal set; }
public object LastObject { get; internal set; }
public GenericCamera UseCamera { get; internal set; } public GenericCamera UseCamera { get; internal set; }
public Matrix4 World => UseCamera.World; public Matrix4 World => UseCamera.World;
public Matrix4 View => UseCamera.View; public Matrix4 View => UseCamera.View;
public GenericMesh Mesh { get; set; } public GenericMesh Mesh { get; set; }
public PrimitiveType? ForcedType { get; set; }
public Material Material { get; set; } public Material Material { get; set; }
public MaterialShader Shader => Material.CustomShader ?? RenderPipeline.DefaultShader; public MaterialShader Shader => Material.CustomShader ?? RenderPipeline.DefaultShader;

View file

@ -47,10 +47,13 @@ namespace SM.Base.Windows
DefaultShader ??= SMRenderer.DefaultMaterialShader; DefaultShader ??= SMRenderer.DefaultMaterialShader;
} }
public Framebuffer CreateWindowFramebuffer(int multisamples) public Framebuffer CreateWindowFramebuffer(int multisamples = 0)
{ {
Framebuffer framebuffer = new Framebuffer(window: ConnectedWindow); Framebuffer framebuffer = new Framebuffer(window: ConnectedWindow);
framebuffer.Append("color", new ColorAttachment(0, PixelInformation.RGBA_LDR, multisamples)); framebuffer.Append("color", new ColorAttachment(0, PixelInformation.RGBA_LDR, multisamples));
RenderbufferAttachment depthAttach = RenderbufferAttachment.Depth;
depthAttach.Multisample = multisamples;
framebuffer.AppendRenderbuffer(depthAttach);
return framebuffer; return framebuffer;
} }
} }

View file

@ -114,6 +114,7 @@ namespace SM.Base.Windows
}; };
drawContext.SetCamera(window.ViewportCamera); drawContext.SetCamera(window.ViewportCamera);
GL.DepthFunc(DepthFunction.Lequal);
window.CurrentRenderPipeline?.Render(ref drawContext); window.CurrentRenderPipeline?.Render(ref drawContext);
} }

View file

@ -30,6 +30,22 @@ namespace SM.OGL.Framebuffer
private IFramebufferWindow _window; private IFramebufferWindow _window;
private float _windowScale; private float _windowScale;
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer;
/// <summary>
/// Contains the size of the framebuffer.
/// </summary>
public Vector2 Size { get; private set; }
/// <summary>
/// Contains all color attachments.
/// </summary>
public Dictionary<string, ColorAttachment> ColorAttachments { get; private set; } =
new Dictionary<string, ColorAttachment>();
public List<RenderbufferAttachment> RenderbufferAttachments { get; } = new List<RenderbufferAttachment>();
/// <summary> /// <summary>
/// Creates a buffer without any options. /// Creates a buffer without any options.
/// </summary> /// </summary>
@ -58,20 +74,6 @@ namespace SM.OGL.Framebuffer
Size = size; Size = size;
} }
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer;
/// <summary>
/// Contains the size of the framebuffer.
/// </summary>
public Vector2 Size { get; private set; }
/// <summary>
/// Contains all color attachments.
/// </summary>
public Dictionary<string, ColorAttachment> ColorAttachments { get; private set; } =
new Dictionary<string, ColorAttachment>();
/// <inheritdoc /> /// <inheritdoc />
public override void Compile() 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, GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.Target, pair.Value.ID,
0); 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); var err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
if (err != FramebufferErrorCode.FramebufferComplete) if (err != FramebufferErrorCode.FramebufferComplete)
throw new Exception("Failed loading framebuffer.\nProblem: " + err); throw new Exception("Failed loading framebuffer.\nProblem: " + err);
@ -125,6 +133,11 @@ namespace SM.OGL.Framebuffer
ColorAttachments.Add(key, value); ColorAttachments.Add(key, value);
} }
public void AppendRenderbuffer(RenderbufferAttachment attachment)
{
RenderbufferAttachments.Add(attachment);
}
/// <summary> /// <summary>
/// Activates the framebuffer without clearing the buffer. /// Activates the framebuffer without clearing the buffer.
/// </summary> /// </summary>

View file

@ -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;
}
}
}

View file

@ -44,6 +44,7 @@
<Compile Include="Framebuffer\ColorAttachment.cs" /> <Compile Include="Framebuffer\ColorAttachment.cs" />
<Compile Include="Framebuffer\Framebuffer.cs" /> <Compile Include="Framebuffer\Framebuffer.cs" />
<Compile Include="Framebuffer\IFramebufferWindow.cs" /> <Compile Include="Framebuffer\IFramebufferWindow.cs" />
<Compile Include="Framebuffer\RenderbufferAttachment.cs" />
<Compile Include="GLCustomActions.cs" /> <Compile Include="GLCustomActions.cs" />
<Compile Include="GLDebugging.cs" /> <Compile Include="GLDebugging.cs" />
<Compile Include="GLObject.cs" /> <Compile Include="GLObject.cs" />

View file

@ -132,7 +132,19 @@ namespace SM.OGL.Shaders
if (mesh.Indices != null) if (mesh.Indices != null)
GL.DrawElementsInstanced(mesh.PrimitiveType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount); GL.DrawElementsInstanced(mesh.PrimitiveType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount);
else else
GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount); GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count / mesh.Vertex.PointerSize, amount);
}
/// <summary>
/// Draws the mesh while forcing a primitive type instead of using the mesh type.
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="amount">The amounts for instancing.</param>
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);
} }
/// <summary> /// <summary>

View file

@ -57,7 +57,7 @@ namespace SM2D.Drawing
protected override void DrawContext(ref DrawContext context) protected override void DrawContext(ref DrawContext context)
{ {
base.DrawContext(ref 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); context.Shader.Draw(context);
} }
} }

View file

@ -17,5 +17,7 @@ namespace SM2D.Object
Vertex = vertex; Vertex = vertex;
Color = color; Color = color;
} }
public static implicit operator PolygonVertex(Vector2 vec) => new PolygonVertex(vec, Color4.White);
} }
} }

View file

@ -38,7 +38,7 @@ namespace SM2D.Scene
protected override Matrix4 ViewCalculation(IGenericWindow window) 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) protected override bool WorldCalculation(IGenericWindow window, out Matrix4 world)
@ -50,7 +50,7 @@ namespace SM2D.Scene
_resizeCounter = ResizeCounter; _resizeCounter = ResizeCounter;
CalculateWorldScale(window); CalculateWorldScale(window);
world = Matrix4.CreateOrthographic(WorldScale.X, WorldScale.Y, .0001f, 1.5f); world = Matrix4.CreateOrthographic(WorldScale.X, WorldScale.Y, .001f, 3.2f);
return true; return true;
} }

View file

@ -1,5 +1,6 @@
#region usings #region usings
using System.Drawing.Drawing2D;
using OpenTK; using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using SM.Base; using SM.Base;
@ -39,6 +40,13 @@ namespace SM2D.Scene
set => _Background = value; 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) public override void DrawDebug(DrawContext context)
{ {
if (ShowAxisHelper) if (ShowAxisHelper)

View file

@ -27,15 +27,17 @@ namespace SM2D.Types
public bool VerticalFlip { get; set; } = false; public bool VerticalFlip { get; set; } = false;
public int ZIndex { get; set; } public CVector1 ZIndex { get; set; } = new CVector1(0);
protected override Matrix4 RequestMatrix() protected override Matrix4 RequestMatrix()
{ {
float z = 1 / (float) ZIndexPercision * ZIndex;
return Matrix4.CreateScale(Size.X, Size.Y, 1) * return Matrix4.CreateScale(Size.X, Size.Y, 1) *
Matrix4.CreateRotationX(MathHelper.DegreesToRadians(HorizontalFlip ? 180 : 0)) * Matrix4.CreateRotationX(MathHelper.DegreesToRadians(HorizontalFlip ? 180 : 0)) *
Matrix4.CreateRotationY(MathHelper.DegreesToRadians(VerticalFlip ? 180 : 0)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(VerticalFlip ? 180 : 0)) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) * 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) public void TurnTo(Vector2 v)