I am trying to use the VBO and Instancing mechanism in the most efficient way. I have a voxel-based world, and I would like to attract them using as few callbacks as possible. The code below prepares a VBO with a quadrant:
void VoxelView::initVBOs() {
float data[6][3] = {
{ -0.5f, 0.5f, 0.0f },
{ -0.5f, -0.5f, 0.0f },
{ 0.5f, -0.5f, 0.0f },
{ 0.5f, -0.5f, 0.0f },
{ 0.5f, 0.5f, 0.0f },
{ -0.5f, 0.5f, 0.0f }
};
glGenBuffers(1, &triangleVBO);
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
glBufferData(GL_ARRAY_BUFFER, 6 * 3 * sizeof(float), data, GL_STATIC_DRAW);
glVertexAttribPointer(shaderAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(shaderAttribute);
glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);
vertexSource = filetobuf("Shaders/exampleVertexShader1.vert");
fragmentSource = filetobuf("Shaders/exampleFragmentShader1.frag");
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, (const GLchar**)&vertexSource, 0);
glShaderSource(fragmentShader, 1, (const GLchar**)&fragmentSource, 0);
free(vertexSource);
free(fragmentSource);
glCompileShader(vertexShader);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindAttribLocation(shaderProgram, shaderAttribute, "in_Position");
glLinkProgram(shaderProgram);
I represent the square as follows:
void VoxelView::renderVBO()
{
glUseProgram(shaderProgram);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
I would like to draw this square (which uses VBO) several times using instancing mechanizm. I would like it to be pretty simple as I want to implement it for more complex code. I know that to use instancing I have to use the glDrawElementsInstanced method , but I don't know how to do it. Does anyone know how to do this?