2020-02-17 07:28:15 +00:00
using System ;
using OpenTK.Graphics.OpenGL ;
using RecrownedGTK.Graphics.Render.Shader ;
namespace RecrownedGTK.Graphics {
public class VertexAttributesHandle : IDisposable {
private bool disposed ;
private int handle ;
public VertexAttributesHandle ( ) {
handle = GL . GenVertexArray ( ) ;
}
2020-02-20 05:30:10 +00:00
public void Bind ( ) {
2020-02-17 07:28:15 +00:00
GL . BindVertexArray ( handle ) ;
}
/// <summary>
/// Creates a basic vertex attributes object handle that takes in vertices, texture coordinates, and a color.
2020-02-17 07:30:50 +00:00
/// First 3 values are normalized vertex position (x, y, z), second two are texture coordinates, and last 4 are color values.
2020-02-17 07:28:15 +00:00
/// </summary>
/// <param name="shader">The shader program used. Default is the one created from <see cref="RecrownedGTK.Graphics.Render.Shader.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.VertexAttributesHandle"/>.</returns>
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 ( ) ;
2020-02-20 05:30:10 +00:00
vertexAttribs . Bind ( ) ;
2020-02-17 07:28:15 +00:00
GL . VertexAttribPointer ( shader . GetAttribLocation ( positionAttribName ) , 3 , VertexAttribPointerType . Float , false , 9 * sizeof ( float ) , 0 ) ;
2020-02-20 05:30:10 +00:00
GL . VertexAttribPointer ( shader . GetAttribLocation ( textureAttribName ) , 2 , VertexAttribPointerType . Float , false , 9 * sizeof ( float ) , 3 * sizeof ( float ) ) ;
2020-02-17 07:28:15 +00:00
GL . VertexAttribPointer ( shader . GetAttribLocation ( colorAttribName ) , 4 , VertexAttribPointerType . Float , false , 9 * sizeof ( float ) , 5 * sizeof ( float ) ) ;
vertexAttribs . End ( ) ;
return vertexAttribs ;
}
2020-02-20 05:30:10 +00:00
2020-02-17 07:28:15 +00:00
public void Dispose ( ) {
Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
2020-02-20 05:30:10 +00:00
2020-02-17 07:28:15 +00:00
protected virtual void Dispose ( bool disposing ) {
if ( disposed ) return ;
if ( disposing ) {
}
GL . DeleteVertexArray ( handle ) ;
disposed = true ;
}
~ VertexAttributesHandle ( ) {
Dispose ( false ) ;
}
}
}