From 0cee4c3ffe9f26efc7c3cfa0283589e4a63d95ed Mon Sep 17 00:00:00 2001 From: Harrison Date: Mon, 17 Feb 2020 02:28:15 -0500 Subject: [PATCH] Renamed and added an utility function to get basic attributes. --- RecrownedGTK/Graphics/VertexArrayHandle.cs | 39 ------------- .../Graphics/VertexAttributesHandle.cs | 58 +++++++++++++++++++ 2 files changed, 58 insertions(+), 39 deletions(-) delete mode 100644 RecrownedGTK/Graphics/VertexArrayHandle.cs create mode 100644 RecrownedGTK/Graphics/VertexAttributesHandle.cs diff --git a/RecrownedGTK/Graphics/VertexArrayHandle.cs b/RecrownedGTK/Graphics/VertexArrayHandle.cs deleted file mode 100644 index 47c931b..0000000 --- a/RecrownedGTK/Graphics/VertexArrayHandle.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using OpenTK.Graphics.OpenGL; -namespace RecrownedGTK.Graphics { - public class VertexArrayHandle : IDisposable { - private bool disposed; - private int handle; - private bool begun; - public VertexArrayHandle() { - handle = GL.GenVertexArray(); - } - - public void Begin() { - if (begun) throw new InvalidOperationException("The VertexArrayHandle has already been started."); - GL.BindVertexArray(handle); - begun = true; - } - public void End() { - if (!begun) throw new InvalidOperationException("The VertexArrayHandle has never been started."); - GL.BindVertexArray(0); - begun = false; - } - public void Dispose() { - Dispose(true); - GC.SuppressFinalize(this); - } - protected virtual void Dispose(bool disposing) { - if (disposed) return; - if (disposing) { - - } - GL.BindVertexArray(0); - GL.DeleteVertexArray(handle); - disposed = true; - } - ~VertexArrayHandle() { - Dispose(false); - } - } -} \ No newline at end of file diff --git a/RecrownedGTK/Graphics/VertexAttributesHandle.cs b/RecrownedGTK/Graphics/VertexAttributesHandle.cs new file mode 100644 index 0000000..e169ba3 --- /dev/null +++ b/RecrownedGTK/Graphics/VertexAttributesHandle.cs @@ -0,0 +1,58 @@ +using System; +using OpenTK.Graphics.OpenGL; +using RecrownedGTK.Graphics.Render.Shader; +namespace RecrownedGTK.Graphics { + public class VertexAttributesHandle : IDisposable { + private bool disposed; + private int handle; + private bool begun; + public VertexAttributesHandle() { + handle = GL.GenVertexArray(); + } + + public void Begin() { + if (begun) throw new InvalidOperationException("The VertexArrayHandle has already been started."); + GL.BindVertexArray(handle); + begun = true; + } + public void End() { + if (!begun) throw new InvalidOperationException("The VertexArrayHandle has never been started."); + GL.BindVertexArray(0); + begun = false; + } + + /// + /// Creates a basic vertex attributes object handle that takes in vertices, texture coordinates, and a color. + /// + /// The shader program used. Default is the one created from . + /// The name of the attribute for the vertex's position in the shader. Default is "aPosition". + /// The name of the attribute for the texture's coordinate. Default is "aTexture". + /// The name of the attribute for color mixture. Default is "aColor". + /// The built . + public static VertexAttributesHandle CreateBasicVA(Shader shader = null, string positionAttribName = "aPosition", string textureAttribName = "aTexture", string colorAttribName = "aColor") { + if (shader == null) shader = Shader.CreateBasicShader(); + VertexAttributesHandle vertexAttribs = new VertexAttributesHandle(); + GL.VertexAttribPointer(shader.GetAttribLocation(positionAttribName), 3, VertexAttribPointerType.Float, false, 9 * sizeof(float), 0); + GL.VertexAttribPointer(shader.GetAttribLocation(textureAttribName), 2, VertexAttribPointerType.Float, false, 9 * sizeof(float), 3 * sizeof(float)); + GL.VertexAttribPointer(shader.GetAttribLocation(colorAttribName), 4, VertexAttribPointerType.Float, false, 9 * sizeof(float), 5 * sizeof(float)); + vertexAttribs.End(); + return vertexAttribs; + } + public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + protected virtual void Dispose(bool disposing) { + if (disposed) return; + if (disposing) { + + } + GL.BindVertexArray(0); + GL.DeleteVertexArray(handle); + disposed = true; + } + ~VertexAttributesHandle() { + Dispose(false); + } + } +} \ No newline at end of file