Loads and loads of small improvements I added while developing on my game

This commit is contained in:
Michel Fedde 2021-03-02 19:54:19 +01:00
parent 41421b1df9
commit a7c71e7ea1
107 changed files with 2278 additions and 1023 deletions

View file

@ -14,20 +14,8 @@ namespace SM.OGL.Framebuffer
/// </summary>
public class ColorAttachment : TextureBase
{
/// <summary>
/// Creates a attachment with a specific id.
/// </summary>
/// <param name="attachmentId"></param>
public ColorAttachment(int attachmentId) : this(attachmentId, PixelInformation.RGBA_LDR)
{ }
public ColorAttachment(int attachmentID, PixelInformation pixelInformation)
{
AttachmentID = attachmentID;
PixelInformation = pixelInformation;
}
private int _multisamples;
/// <summary>
/// The ID the attachment was given.
/// </summary>
@ -50,6 +38,22 @@ namespace SM.OGL.Framebuffer
/// </summary>
public DrawBuffersEnum DrawBuffersEnum => DrawBuffersEnum.ColorAttachment0 + AttachmentID;
public bool IsMultisampled => _multisamples > 0;
/// <summary>
/// Creates a attachment with a specific id.
/// </summary>
/// <param name="attachmentId"></param>
public ColorAttachment(int attachmentId) : this(attachmentId, PixelInformation.RGBA_LDR)
{ }
public ColorAttachment(int attachmentID, PixelInformation pixelInformation, int multisamples = 0)
{
AttachmentID = attachmentID;
PixelInformation = pixelInformation;
_multisamples = multisamples;
Target = IsMultisampled ? TextureTarget.Texture2DMultisample : TextureTarget.Texture2D;
}
/// <summary>
/// Generates the attachment.
/// </summary>
@ -57,20 +61,36 @@ namespace SM.OGL.Framebuffer
public void Generate(Framebuffer f)
{
_id = GL.GenTexture();
if (IsMultisampled) GenerateMultisampledTexture(f);
else GenerateTexture(f);
}
private void GenerateTexture(Framebuffer f)
{
GL.BindTexture(TextureTarget.Texture2D, _id);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInformation.InternalFormat,
(int) f.Size.X, (int) f.Size.Y,
(int)f.Size.X, (int)f.Size.Y,
0, PixelInformation.Format, PixelInformation.DataType, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
(int) TextureMinFilter.Linear);
(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
(int) TextureMinFilter.Linear);
(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
(int) TextureParameterName.ClampToEdge);
(int)TextureParameterName.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
(int) TextureParameterName.ClampToEdge);
(int)TextureParameterName.ClampToEdge);
GL.BindTexture(TextureTarget.Texture2D, 0);
}
private void GenerateMultisampledTexture(Framebuffer f)
{
GL.BindTexture(TextureTarget.Texture2DMultisample, _id);
GL.TexImage2DMultisample(TextureTargetMultisample.Texture2DMultisample, _multisamples, PixelInformation.InternalFormat, (int)f.Size.X, (int)f.Size.Y, true);
GL.BindTexture(TextureTarget.Texture2DMultisample, 0);
}
}
}

View file

@ -14,6 +14,8 @@ namespace SM.OGL.Framebuffer
/// </summary>
public class Framebuffer : GLObject
{
protected override bool AutoCompile { get; } = true;
/// <summary>
/// Represents the screen buffer.
/// </summary>
@ -92,7 +94,7 @@ namespace SM.OGL.Framebuffer
GL.DrawBuffers(enums.Length, enums);
foreach (var pair in ColorAttachments)
GL.FramebufferTexture(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.ID,
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.Target, pair.Value.ID,
0);
var err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
@ -123,7 +125,6 @@ namespace SM.OGL.Framebuffer
ColorAttachments.Add(key, value);
}
/// <summary>
/// Activates the framebuffer without clearing the buffer.
/// </summary>

View file

@ -2,6 +2,7 @@
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
#endregion
@ -47,9 +48,35 @@ namespace SM.OGL.Mesh
/// <param name="y">If true, it takes the Y-value of maximum, otherwise the minimum.</param>
/// <param name="z">If true, it takes the Z-value of maximum, otherwise the minimum.</param>
/// <returns></returns>
public Vector3 this[bool x, bool y, bool z] =>
new Vector3(x ? Max.X : Min.X, y ? Max.Y : Min.Y, z ? Max.Z : Min.Z);
public Vector3 this[bool x, bool y, bool z] => Get(x,y,z);
public Vector3 Get(bool x, bool y, bool z)
{
return new Vector3(x ? Max.X : Min.X, y ? Max.Y : Min.Y, z ? Max.Z : Min.Z);
}
public Vector3 Get(bool xyz) => Get(xyz, xyz, xyz);
public Vector3 Get(Matrix4 transformation, bool x, bool y, bool z)
{
Vector3 get = Get(x, y, z);
return (new Vector4(get, 1) * transformation).Xyz;
}
public Vector3 Get(Matrix4 transformation, bool xyz) => Get(transformation, xyz, xyz, xyz);
public void Update(GenericMesh mesh)
{
int pos = 0;
foreach (float f in mesh.Vertex)
{
Min[pos] = Math.Min(Min[pos], f);
Max[pos] = Math.Max(Max[pos], f);
pos++;
pos %= mesh.Vertex.PointerSize;
}
}
/// <summary>
/// Updates the bounding box.
/// </summary>

View file

@ -13,6 +13,9 @@ namespace SM.OGL.Mesh
/// </summary>
public abstract class GenericMesh : GLObject
{
private bool _boundingBoxUpdated = false;
public static int LastID { get; internal set; } = -1;
/// <summary>
/// Generates the AttribDataIndex
@ -69,14 +72,28 @@ namespace SM.OGL.Mesh
/// </summary>
public virtual int[] Indices { get; set; }
public void UpdateBoundingBox()
{
BoundingBox.Update(this);
_boundingBoxUpdated = true;
}
public void Activate()
{
GL.BindVertexArray(ID);
}
/// <inheritdoc />
public override void Compile()
{
_id = GL.GenVertexArray();
GL.BindVertexArray(_id);
if (Attributes == null || Attributes.Count == 0) throw new Exception("[Critical] The model requires attributes.");
if (!_boundingBoxUpdated)
UpdateBoundingBox();
foreach (var kvp in Attributes)
kvp.ConnectedVBO?.BindBuffer(kvp.Index);

View file

@ -0,0 +1,7 @@
namespace SM.OGL.Mesh
{
public interface ILineMesh
{
float LineWidth { get; set; }
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL;
namespace SM.OGL.Mesh
{
@ -50,5 +51,11 @@ namespace SM.OGL.Mesh
//if (vbo == null) return;
Add(new MeshAttribute(id, name, vbo));
}
public bool Has(string name)
{
VBO attribute = this[name];
return attribute != null && attribute.Active;
}
}
}

View file

@ -40,6 +40,8 @@ namespace SM.OGL.Mesh
/// </summary>
public int PointerStride;
public bool Active = true;
/// <summary>
/// Specifies the data type of each component in the array.
/// </summary>
@ -133,6 +135,14 @@ namespace SM.OGL.Mesh
Add(vector.X, vector.Y, z, w);
}
public void Add(params Vector2[] vectors)
{
foreach (Vector2 vector in vectors)
{
Add(vector);
}
}
/// <summary>
/// Adds a Vector3.
/// </summary>
@ -149,6 +159,13 @@ namespace SM.OGL.Mesh
Add(vector.X, vector.Y, vector.Z, w);
}
public void Add(params Vector3[] vectors)
{
foreach (Vector3 vector in vectors)
{
Add(vector);
}
}
/// <summary>
/// Adds a vector4.
/// </summary>
@ -172,6 +189,8 @@ namespace SM.OGL.Mesh
/// <param name="attribID">The id for the attribute.</param>
internal void BindBuffer(int attribID)
{
if (!Active) return;
var data = ToArray();
var buffer = GL.GenBuffer();

View file

@ -51,6 +51,7 @@
<Compile Include="GLSystem.cs" />
<Compile Include="Mesh\BoundingBox.cs" />
<Compile Include="Mesh\GenericMesh.cs" />
<Compile Include="Mesh\ILineMesh.cs" />
<Compile Include="Mesh\MeshAttribute.cs" />
<Compile Include="Mesh\MeshAttributeList.cs" />
<Compile Include="Mesh\VBO.cs" />

View file

@ -106,6 +106,11 @@ namespace SM.OGL.Shaders
GLDebugging.CheckGLErrors($"A error occured at shader creation for '{GetType()}': %code%");
}
public void Activate()
{
GL.UseProgram(ID);
}
/// <inheritdoc />
public override void Compile()
{
@ -122,7 +127,7 @@ namespace SM.OGL.Shaders
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="amount">The amounts for instancing.</param>
protected void DrawObject(GenericMesh mesh, int amount = 1)
public static void DrawObject(GenericMesh mesh, int amount = 1)
{
if (mesh.Indices != null)
GL.DrawElementsInstanced(mesh.PrimitiveType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount);
@ -138,7 +143,6 @@ namespace SM.OGL.Shaders
Uniforms.NextTexture = 0;
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.BindVertexArray(0);
}
}
}

View file

@ -14,17 +14,17 @@ namespace SM.OGL.Shaders
/// <summary>
/// Contains the vertex file.
/// </summary>
public ShaderFile Vertex;
public ShaderFile[] Vertex;
/// <summary>
/// Contains the geometry file.
/// </summary>
public ShaderFile Geometry;
public ShaderFile[] Geometry;
/// <summary>
/// Contains the fragment file.
/// </summary>
public ShaderFile Fragment;
public ShaderFile[] Fragment;
/// <summary>
/// Creating the collection with vertex and fragment files.
@ -44,6 +44,14 @@ namespace SM.OGL.Shaders
/// <param name="fragment"></param>
/// <param name="geometry"></param>
public ShaderFileCollection(ShaderFile vertex, ShaderFile fragment, ShaderFile geometry = default)
{
Vertex = new []{vertex};
if (geometry != null) Geometry = new[] {geometry};
else Geometry = default;
Fragment = new []{fragment};
}
public ShaderFileCollection(ShaderFile[] vertex, ShaderFile[] fragment, ShaderFile[] geometry = default)
{
Vertex = vertex;
Geometry = geometry;
@ -56,9 +64,15 @@ namespace SM.OGL.Shaders
/// <param name="shader"></param>
internal void Append(GenericShader shader)
{
Vertex.Compile(shader, ShaderType.VertexShader);
Geometry?.Compile(shader, ShaderType.GeometryShader);
Fragment.Compile(shader, ShaderType.FragmentShader);
foreach (ShaderFile file in Vertex)
file.Compile(shader, ShaderType.VertexShader);
if (Geometry != null)
foreach (ShaderFile file in Geometry)
file.Compile(shader, ShaderType.GeometryShader);
foreach (ShaderFile file in Fragment)
file.Compile(shader, ShaderType.FragmentShader);
}
/// <summary>
@ -67,9 +81,15 @@ namespace SM.OGL.Shaders
/// <param name="shader"></param>
internal void Detach(GenericShader shader)
{
GL.DetachShader(shader, Vertex);
if (Geometry != null) GL.DetachShader(shader, Geometry);
GL.DetachShader(shader, Fragment);
foreach (ShaderFile file in Vertex)
GL.DetachShader(shader, file);
if (Geometry != null)
foreach (ShaderFile file in Geometry)
GL.DetachShader(shader, file);
foreach (ShaderFile file in Fragment)
GL.DetachShader(shader, file);
GLDebugging.CheckGLErrors($"Error at detaching '{shader.GetType()}'");
}

View file

@ -354,7 +354,7 @@ namespace SM.OGL.Shaders
#region Matrix2
public void SetMatrix2(ref Matrix2 matrix, bool transpose = false)
public void SetMatrix2(Matrix2 matrix, bool transpose = false)
{
GL.UniformMatrix2(Location, transpose, ref matrix);
}
@ -383,7 +383,7 @@ namespace SM.OGL.Shaders
#region Matrix3
public void SetMatrix3(ref Matrix3 matrix, bool transpose = false)
public void SetMatrix3(Matrix3 matrix, bool transpose = false)
{
GL.UniformMatrix3(Location, transpose, ref matrix);
}
@ -485,7 +485,7 @@ namespace SM.OGL.Shaders
{
Parent.NextTexture = texturePos + 1;
GL.ActiveTexture(TextureUnit.Texture0 + texturePos);
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.BindTexture(texture.Target, texture);
SetUniform1(texturePos);
}

View file

@ -12,6 +12,7 @@ namespace SM.OGL.Shaders
public string Name { get; internal set; }
public UniformCollection Parent { get; internal set; }
public GenericShader ParentShader { get; internal set; }
public int Length => storedUniforms.Count;
public Dictionary<string, Uniform> this[int index] => Get(index);

View file

@ -24,7 +24,7 @@ namespace SM.OGL.Shaders
}
catch (KeyNotFoundException)
{
GLCustomActions.AtWarning?.Invoke("Uniform '" + KeyString + key + "' was not found. Tried to recreate it.");
GLCustomActions.AtWarning?.Invoke("Uniform '" + KeyString + key + "' at '" + ParentShader.GetType().Name + "' was not found. Tried to recreate it.");
var u = new Uniform(GL.GetUniformLocation(ParentShader, KeyString + key), this);
Add(key, u);
return u;

View file

@ -13,12 +13,15 @@ namespace SM.OGL.Texture
{
/// <inheritdoc />
protected override bool AutoCompile { get; } = true;
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Texture;
public PixelInformation PixelInformation;
public TextureTarget Target { get; set; } = TextureTarget.Texture2D;
/// <summary>
/// The texture filter.
/// <para>Default: <see cref="TextureMinFilter.Linear" /></para>