2020-02-17 07:28:15 +00:00
using System ;
using OpenTK.Graphics.OpenGL ;
2020-02-20 21:06:37 +00:00
using OpenTK ;
2020-02-20 23:07:17 +00:00
using RecrownedGTK.Graphics.Render.Shaders ;
2020-02-17 07:28:15 +00:00
namespace RecrownedGTK.Graphics {
2020-02-20 21:06:37 +00:00
public class VertexAttributesArrayHandle : IDisposable {
2020-02-17 07:28:15 +00:00
private bool disposed ;
private int handle ;
2020-02-20 21:06:37 +00:00
public VertexAttributesArrayHandle ( ) {
2020-02-17 07:28:15 +00:00
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>
2020-02-20 23:07:17 +00:00
/// <param name="shader">The shader program used. Default is the one created from <see cref="RecrownedGTK.Graphics.Render.Shaders.Shader.CreateBasicShader"/>.</param>
2020-02-17 07:28:15 +00:00
/// <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>
2020-02-20 21:06:37 +00:00
/// <returns>The built <see cref="RecrownedGTK.Graphics.VertexAttributesArrayHandle"/>.</returns>
2020-03-01 23:09:07 +00:00
public static VertexAttributesArrayHandle CreateBasicVA ( ref Matrix4 transformMat , Shader shader , string positionAttribName = "aPosition" , string textureAttribName = "aTexture" , string colorAttribName = "aColor" , string transformUnifName = "transform" ) {
2020-02-20 21:06:37 +00:00
VertexAttributesArrayHandle vertexAttribs = new VertexAttributesArrayHandle ( ) ;
2020-02-20 05:30:10 +00:00
vertexAttribs . Bind ( ) ;
2020-02-20 21:06:37 +00:00
GL . UniformMatrix4 ( shader . GetAttribLocation ( transformUnifName ) , true , ref transformMat ) ;
2020-03-01 23:09:07 +00:00
2020-02-17 07:28:15 +00:00
GL . VertexAttribPointer ( shader . GetAttribLocation ( positionAttribName ) , 3 , VertexAttribPointerType . Float , false , 9 * sizeof ( float ) , 0 ) ;
2020-03-01 23:09:07 +00:00
GL . EnableVertexAttribArray ( shader . GetAttribLocation ( positionAttribName ) ) ;
2020-02-20 05:30:10 +00:00
GL . VertexAttribPointer ( shader . GetAttribLocation ( textureAttribName ) , 2 , VertexAttribPointerType . Float , false , 9 * sizeof ( float ) , 3 * sizeof ( float ) ) ;
2020-03-01 23:09:07 +00:00
GL . EnableVertexAttribArray ( shader . GetAttribLocation ( textureAttribName ) ) ;
2020-02-17 07:28:15 +00:00
GL . VertexAttribPointer ( shader . GetAttribLocation ( colorAttribName ) , 4 , VertexAttribPointerType . Float , false , 9 * sizeof ( float ) , 5 * sizeof ( float ) ) ;
2020-03-01 23:09:07 +00:00
GL . EnableVertexAttribArray ( shader . GetAttribLocation ( colorAttribName ) ) ;
2020-02-17 07:28:15 +00:00
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 ;
}
2020-02-20 21:06:37 +00:00
~ VertexAttributesArrayHandle ( ) {
2020-02-17 07:28:15 +00:00
Dispose ( false ) ;
}
}
}