Moggle

moggle.core.vao



struct Vao;
A vertex attribute object.

This is a wrapper around a GLuint generated by glGenVertexArrays(1, &id). Initially, this id is 0. glGenVertexArrays is automatically called the first time bind() is called.

const @property GLuint id();
The id of this Vao, or 0 if it is not yet created in OpenGL.

const @property bool created();
const bool opCast(T : bool)();
Check if this Vao is already created in OpenGL.

void create();
Force the creation of a OpenGL Vao, or do nothing if already created. (Calls glGenVertexArrays.)

void destroy();
Destroy the OpenGL Vao and reset the id back to 0. (Calls glDeleteVertexArrays.)

void bind();
Create the OpenGL Vao, if needed, and bind it. (Calls glBindVertexArray.)

void setAttribute()(GLuint index, ref GenericVbo vbo, AttributeParameters parameters);
void setAttribute(T)(GLuint index, ref SpecificVbo!T vbo);
Add or change an attribute. (Calls glEnableVertexAttribArray and glVertexAttribPointer.)

The second version automatically deduces the parameters for glVertexAttribPointer using attributeParametersFor!T.

void disableAttribute(GLuint index);
Disable an attribute. (Calls glDisableVertexAttribArray.)

alias AttributeParameters = (int, uint, bool, int, const(void)*);
The tuple of parameters for glVertexAttribPointer that specify the type information.

The parameters are:



You should check the documentation of glVertexAttribPointer for their details.

template attributeParametersFor(T)
template attributeParametersFor(T, string member) if (is(T == struct))
The (automatically deduced) correct AttributeParameters for T.

Works for GLdouble, GLfloat, GLint, GLuint, GLshort, GLushort, GLbyte, GLubyte, Normalized versions of these, and Matrices, Vectors and HVectors of all these.

The second version takes the name the member of T, for when the buffer contains an array of T but only a single member of that T is what you want parameters for. This automatically sets the stride and the offset to to the correct values (T.sizeof and T.member.offsetof, respectively).

Examples:
// These two lines do the exact same.
glVertexAttribPointer(1, attributeParametersFor!int);
glVertexAttribPointer(1, 1, GL_INT, false, int.sizeof, null);
// These two lines do the exact same.
glVertexAttribPointer(1, attributeParametersFor!Matrix3f);
glVertexAttribPointer(1, 9, GL_FLOAT, false, Matrix3f.sizeof, null);
// These two lines do the exact same.
glVertexAttribPointer(1, attributeParametersFor!(Vector!(Normalized!ubyte, 4)));
glVertexAttribPointer(1, 4, GL_UBYTE, true, Vector!(Normalized!ubyte, 4).sizeof, null);
struct Vertex { HVector4f position; Vector3f normal; HVector4f color; }
// These two lines do the exact same.
glVertexAttribPointer(1, attributeParametersFor!(Vertex, "normal"));
glVertexAttribPointer(1, 3, GL_FLOAT, false, Vertex.size, cast(const(void)*)Vertex.normal.offsetof);