recrownedgtk/RecrownedGTK/Graphics/VertexAttributesArrayHandle.cs

70 lines
3.3 KiB
C#
Raw Normal View History

using System;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using RecrownedGTK.Graphics.Render.Shaders;
namespace RecrownedGTK.Graphics {
/// <summary>
/// The attributes of the vertex.
/// Handle for openGL.
/// </summary>
public class VertexAttributesArrayHandle : IDisposable {
private bool disposed;
private int handle;
/// <summary>
/// Generates an openGL vertex array.
/// </summary>
public VertexAttributesArrayHandle() {
handle = GL.GenVertexArray();
}
/// <summary>
/// Tells openGL to bind this vertex attributes array.
/// </summary>
public void Bind() {
GL.BindVertexArray(handle);
}
/// <summary>
/// Creates a basic vertex attributes object handle that takes in vertices, texture coordinates, and a color.
/// First 3 values are normalized vertex position (x, y, z), second two are texture coordinates, and last 4 are color values.
/// </summary>
/// <param name="shader">The shader program used. Default is the one created from <see cref="RecrownedGTK.Graphics.Render.Shaders.Shader.CreateBasicShader"/>.</param>
/// <param name="positionAttribName">The name of the attribute for the vertex's position in the shader. Default is "aPosition".</param>
/// <param name="textureAttribName">The name of the attribute for the texture's coordinate. Default is "aTexture".</param>
/// <param name="colorAttribName">The name of the attribute for color mixture. Default is "aColor".</param>
/// <returns>The built <see cref="RecrownedGTK.Graphics.VertexAttributesArrayHandle"/>.</returns>
public static VertexAttributesArrayHandle CreateBasicVA(ref Matrix4 transformMat, Shader shader, string positionAttribName = "aPosition", string textureAttribName = "aTexture", string colorAttribName = "aColor", string transformUnifName = "transform") {
VertexAttributesArrayHandle vertexAttribs = new VertexAttributesArrayHandle();
vertexAttribs.Bind();
GL.UniformMatrix4(shader.GetAttribLocation(transformUnifName), true, ref transformMat);
GL.VertexAttribPointer(shader.GetAttribLocation(positionAttribName), 3, VertexAttribPointerType.Float, false, 9 * sizeof(float), 0);
GL.EnableVertexAttribArray(shader.GetAttribLocation(positionAttribName));
GL.VertexAttribPointer(shader.GetAttribLocation(textureAttribName), 2, VertexAttribPointerType.Float, false, 9 * sizeof(float), 3 * sizeof(float));
GL.EnableVertexAttribArray(shader.GetAttribLocation(textureAttribName));
GL.VertexAttribPointer(shader.GetAttribLocation(colorAttribName), 4, VertexAttribPointerType.Float, false, 9 * sizeof(float), 5 * sizeof(float));
GL.EnableVertexAttribArray(shader.GetAttribLocation(colorAttribName));
return vertexAttribs;
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (disposed) return;
if (disposing) {
}
GL.DeleteVertexArray(handle);
disposed = true;
}
~VertexAttributesArrayHandle() {
Dispose(false);
}
}
}