How to set matrices for different objects, in OpenGL?
Not sure how to do it, since I'm only seeing one instance of matrix-use drawn (only one of the objects has an energy bar
):
glDrawElementsInstanced(GL_TRIANGLES, this->energy.indices, GL_UNSIGNED_INT, this->energy.indiceArray, this->energy.instances);
Works fine for same type of objects, but on the image they are just place-holders.
I'm creating the matrix instance buffer object, along with VBO:
float* matrices = new float[this->energy.instances * 16];
for (int j = 0; j < this->energy.instances; j++) {
drx::util::MAT4F mat;
mat.LoadIdentity();
for (int y = 0, c = 0; y < 4; y++) {
for (int x = 0; x < 4; x++, c++) {
matrices[j * 16 + c] = mat.matrix[x][y];
}
}
}
glGenBuffers(1, &this->energy.IBO);
glBindBuffer(GL_ARRAY_BUFFER, this->energy.IBO);
glBufferData(GL_ARRAY_BUFFER, this->energy.instances * 16 * sizeof(float), matrices, GL_DYNAMIC_DRAW);
delete[] matrices;
for (int i = 0; i < 4; i++) {
glEnableVertexAttribArray(2 + i);
glVertexAttribPointer(2 + i, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(float), (void*)(i * 4 * sizeof(float)));
glVertexAttribDivisor(2 + i, 1);
}
Updating:
glBindBuffer(GL_ARRAY_BUFFER, this->energy.IBO);
for (int i = 0; i < this->energy.instances; i++) {
drx::util::MAT4F mat;
mat.LoadIdentity();
mat.Translate(this->lab->dob[i].pos.x, this->lab->dob[i].pos.y - 7.5f, 0.0f);
for (int y = 0, c = 0; y < 4; y++) {
for (int x = 0; x < 4; x++, c++) {
this->matrix[c] = mat.matrix[x][y];
}
}
glBufferSubData(GL_ARRAY_BUFFER, 16 * i * sizeof(float), 16 * sizeof(float), this->matrix);
}
And drawing:
glUseProgram(this->energy.Program);
this->energy.vertex.SetMVP(ortho);
glBindVertexArray(this->energy.VAO);
glDrawElements(GL_TRIANGLES, this->energy.indices, GL_UNSIGNED_INT, this->energy.indiceArray);