void loadVertices(GLBufferHandles *bufferHandles, Vertex *triangleData, int triCount,
unsigned int *indicesData, int indexCount, GLuint programId,
ShapeType type, GLuint textureId, Matrix4 PVM, float zoom) {
glUseProgram(programId);
glCheckError();
GLuint vaoHandle;
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
GLuint vertices;
GLuint indices;
if(bufferHandles && bufferHandles->valid) {
//get cached vertex data and index data
vertices = bufferHandles->verticesIndex;
indices = bufferHandles->indicesIndex;
} else {
glGenBuffers(1, &vertices);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glBufferData(GL_ARRAY_BUFFER, triCount*sizeof(Vertex), triangleData, GL_STREAM_DRAW);
glGenBuffers(1, &indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount*sizeof(unsigned int), indicesData, GL_STREAM_DRAW);
if(bufferHandles) {
assert(!bufferHandles->valid);
bufferHandles->verticesIndex = vertices;
bufferHandles->indicesIndex = indices;
bufferHandles->valid = true;
}
}
GLint PVMUniform = glGetUniformLocation(programId, "PVM");
glCheckError();
glUniformMatrix4fv(PVMUniform, 1, GL_FALSE, PVM.val);
glCheckError();
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glCheckError();
if(type == SHAPE_TEXTURE) {
GLint texUniform = glGetUniformLocation(programId, "tex");
glCheckError();
glUniform1i(texUniform, 1);
glCheckError();
glActiveTexture(GL_TEXTURE1);
glCheckError();
glBindTexture(GL_TEXTURE_2D, textureId);
glCheckError();
}
GLint vertexAttrib = glGetAttribLocation(programId, "vertex");
glCheckError();
GLint texUVAttrib = glGetAttribLocation(programId, "texUV");
glCheckError();
GLint colorAttrib = glGetAttribLocation(programId, "color");
glCheckError();
glEnableVertexAttribArray(texUVAttrib);
glCheckError();
unsigned int texUV_offset = 6;
glVertexAttribPointer(texUVAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char *)0) + (texUV_offset*sizeof(float)));
glCheckError();
glEnableVertexAttribArray(colorAttrib);
glCheckError();
unsigned int color_offset = 8;
glVertexAttribPointer(colorAttrib, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char *)0) + (color_offset*sizeof(float)));
glCheckError();
glEnableVertexAttribArray(vertexAttrib);
glCheckError();
glVertexAttribPointer(vertexAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glCheckError();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
glCheckError();
glDeleteVertexArrays(1, &vaoHandle);
if(!bufferHandles) {
glDeleteBuffers(1, &vertices);
glDeleteBuffers(1, &indices);
}
glUseProgram(0);
}