Skip to content

Commit

Permalink
updated vao header and source
Browse files Browse the repository at this point in the history
  • Loading branch information
humanshell committed Feb 17, 2016
1 parent 39b6cea commit 472ac5d
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 42 deletions.
100 changes: 100 additions & 0 deletions include/glisy/vao.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#define GLISY_MAX_VAO_ATTRIBS 64
#endif

/**
* Glisy VAO Attribute struct type.
*/

typedef struct glisy_vao_attribute glisy_vao_attribute;
struct glisy_vao_attribute {
GLuint location;
Expand All @@ -26,6 +30,10 @@ struct glisy_vao_attribute {
} buffer;
};

/**
* Glisy VAO struct type.
*/

typedef struct glisy_vao glisy_vao;
struct glisy_vao {
GLuint handle;
Expand All @@ -34,40 +42,132 @@ struct glisy_vao {
GLboolean useElements;
};

/**
* This function initializes a glisy_vao struct with default values. Memory is
* allocated and initialized, and glGenVertexArrays() is called to generate a
* value for the handle member. The length member defaults to 0 and useElements
* defaults to false. If the vao param is NULL or undefined the function returns.
*
* @param vao * - pointer to a glisy_vao struct.
*/

void
glisy_vao_init(glisy_vao *vao);

/**
* This function calls glBindVertexArray() to bind the verticies stored in the
* memory location of the vao handle member. If the vao param is NULL or
* undefined the function returns.
*
* @param vao * - pointer to a glisy_vao struct.
*/

void
glisy_vao_bind(glisy_vao *vao);

/**
* This function calls glBindVertexArray(0) to clear any bound verticies. If the
* vao param is NULL or undefined the function returns.
*
* @param vao * - pointer to a glisy_vao struct.
*/

void
glisy_vao_unbind(glisy_vao *vao);

/**
* This function calls glDeleteVertexArrays() to destroy any verticies stored in
* the memory location of the vao handle member. If the vao param is NULL or
* undefined the function returns.
*
* @param vao * - pointer to a glisy_vao struct.
*/

void
glisy_vao_dispose(glisy_vao *vao);

/**
* This function resets and reinitializes the vao and all of its attributes. The
* elements param is optional. If a properly initialized glisy_buffer is passed
* in, it will be bound and the vao useElements member will be set to true. If
* elements are not being used, pass NULL to disable. If the vao param is NULL
* or undefined the function will return.
*
* @param vao * - pointer to a glisy_vao struct.
* @param elements * - pointer to a glisy_buffer struct.
*/

void
glisy_vao_update(glisy_vao *vao, glisy_buffer *elements);

/**
* This function pushes attr param onto the vao's internal attributes member
* list of attributes. If the vao or attr params are NULL or undefined the
* function returns false, otherwise it returns the new length of the vao.
*
* @param vao * - pointer to a glisy_vao struct.
* @param attr * - pointer to a glisy_vao_attribute struct.
*/

GLuint
glisy_vao_push(glisy_vao *vao, glisy_vao_attribute *attr);

/**
* @TODO(jwerle): implement glisy_vao_pop
*
* @param vao * - pointer to a glisy_vao struct.
*/

GLuint
glisy_vao_pop(glisy_vao *vao);

/**
* @TODO(jwerle): implement glisy_vao_splice
*
* @param vao * - pointer to a glisy_vao struct.
* @param start - index at which to start slicing.
* @param count - length of the slice.
*/

GLuint
glisy_vao_splice(glisy_vao *vao,
GLint start,
GLuint count);

/**
* This function places a vao attribute into vao at the index specified by
* location. If the location exceeds GLISY_MAX_VAO_ATTRIBS or if the vao's
* length is already at that maximum, nothing will be done. If the vao or attr
* params are NULL or undefined the function returns false, otherwise it returns
* the new vao length.
*
* @param vao * - pointer to a glisy_vao struct.
* @param location - index at which to set the new vao attribute.
* @param attr * - pointer to a glisy_vao_attribute struct.
*/

GLuint
glisy_vao_set(glisy_vao *vao,
GLuint location,
glisy_vao_attribute *attr);

/**
* @TODO(jwerle): implement glisy_vao_remove
*
* @param vao * - pointer to a glisy_vao struct.
* @param location - index of the attr to be removed.
*/

GLuint
glisy_vao_remove(glisy_vao *vao, GLuint location);

/**
* This function initializes and binds a glisy_vao_attribute struct. If the
* attribute param is NULL or undefined the function returns.
*
* @param attribute * - pointer to a glisy_vao_attribute struct.
*/

void
glisy_vao_attribute_bind(glisy_vao_attribute *attribute);

Expand Down
120 changes: 78 additions & 42 deletions src/vao.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,38 @@

void
glisy_vao_init(glisy_vao *vao) {
if (!vao) return;
vao->length = 0;
vao->useElements = GL_FALSE;
memset(vao->attributes, 0, sizeof(glisy_vao_attribute) * GLISY_MAX_VAO_ATTRIBS);
glGenVertexArrays(1, &vao->handle);
}

void
glisy_vao_attribute_bind(glisy_vao_attribute *attribute) {
GLfloat data[4];
GLuint dimension = attribute->buffer.dimension;
GLuint location = attribute->location;
memcpy(&data, &attribute->buffer.data, sizeof(data));
switch (dimension) {
case 4:
glVertexAttrib4f(location, data[0], data[1], data[2], data[3]);
break;
case 3:
glVertexAttrib3f(location, data[0], data[1], data[2]);
break;
case 2:
glVertexAttrib2f(location, data[0], data[1]);
break;
case 1:
glVertexAttrib1f(location, data[0]);
break;
}
}

void
glisy_vao_bind(glisy_vao *vao) {
if (!vao) return;
glBindVertexArray(vao->handle);
}

void
glisy_vao_unbind(glisy_vao *vao) {
if (!vao) return;
glBindVertexArray(0);
}

void
glisy_vao_dispose(glisy_vao *vao) {
if (!vao) return;
glDeleteVertexArrays(1, &vao->handle);
vao->handle = 0;
vao->length = 0;
vao->useElements = GL_FALSE;
}

GLuint
glisy_vao_push(glisy_vao *vao, glisy_vao_attribute *attr) {
if (vao->length < GLISY_MAX_VAO_ATTRIBS) {
attr->location = vao->length;
vao->attributes[vao->length++] = *attr;
}
return vao->length;
}

GLuint
glisy_vao_set(glisy_vao *vao, GLuint location, glisy_vao_attribute *attr) {
GLuint max = GLISY_MAX_VAO_ATTRIBS;
if (vao->length < max && location < max) {
attr->location = location;
vao->attributes[location] = *attr;
vao->length = location < vao->length ? vao->length : location;
}
return vao->length;
}

void
glisy_vao_update(glisy_vao *vao, glisy_buffer *elements) {
if (!vao) return;

// number of bound attributes for this VAO
GLuint length = vao->length;

Expand Down Expand Up @@ -123,3 +87,75 @@ glisy_vao_update(glisy_vao *vao, glisy_buffer *elements) {
glEnableVertexAttribArray(location);
}
}

GLuint
glisy_vao_push(glisy_vao *vao, glisy_vao_attribute *attr) {
if (!vao) return GL_FALSE;
if (!attr) return GL_FALSE;

if (vao->length < GLISY_MAX_VAO_ATTRIBS) {
attr->location = vao->length;
vao->attributes[vao->length++] = *attr;
}

return vao->length;
}

GLuint
glisy_vao_pop(glisy_vao *vao) {
if (!vao) return GL_FALSE;
//@TODO(jwerle): implement glisy_vao_pop
return 0;
}

GLuint
glisy_vao_splice(glisy_vao *vao, GLint start, GLuint count) {
if (!vao) return GL_FALSE;
//@TODO(jwerle): implement glisy_vao_splice
return 0;
}

GLuint
glisy_vao_set(glisy_vao *vao, GLuint location, glisy_vao_attribute *attr) {
if (!vao) return GL_FALSE;
if (!attr) return GL_FALSE;
GLuint max = GLISY_MAX_VAO_ATTRIBS;

if (vao->length < max && location < max) {
attr->location = location;
vao->attributes[location] = *attr;
vao->length = location < vao->length ? vao->length : location;
}

return vao->length;
}

GLuint
glisy_vao_remove(glisy_vao *vao, GLuint location) {
if (!vao) return GL_FALSE;
//@TODO(jwerle): implement glisy_vao_remove
return 0;
}

void
glisy_vao_attribute_bind(glisy_vao_attribute *attribute) {
if (!attribute) return;
GLfloat data[4];
GLuint dimension = attribute->buffer.dimension;
GLuint location = attribute->location;
memcpy(&data, &attribute->buffer.data, sizeof(data));
switch (dimension) {
case 4:
glVertexAttrib4f(location, data[0], data[1], data[2], data[3]);
break;
case 3:
glVertexAttrib3f(location, data[0], data[1], data[2]);
break;
case 2:
glVertexAttrib2f(location, data[0], data[1]);
break;
case 1:
glVertexAttrib1f(location, data[0]);
break;
}
}

0 comments on commit 472ac5d

Please sign in to comment.