Hello!
I’ve got this problem where i’m sending all my vertices in one buffer object and
all it’s model matrices in another. The problem is that is seems that vertex shader is only getting the first model matrix (or none)
because all the models are drawn in the same place.
Here is the init code:
GLint matrix_loc = glGetAttribLocation(mShaderProgram,"model_matrix");
glGenBuffers(1, &mMatrixBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mMatrixBuffer);
// Loop over each column of the matrix...
for (unsigned int i = 0; i < 4; i++)
{
GLuint index = matrix_loc + i;
GLsizei stride = sizeof(glm::mat4);
GLsizei offset = (sizeof(GLfloat) * i * 4);
// Enable it
glEnableVertexAttribArray(index);
// Set up the vertex attribute
glVertexAttribPointer(index, // Location
4, GL_FLOAT, GL_FALSE, // vec4
stride, // Stride
(const GLvoid*)offset); // Start offset
// Make it instanced
glVertexAttribDivisor(index, 1);
}
glBindVertexArray(0);
Here is the data upload code:
Drawable* d = mDrawings[i];
glm::mat4 mat = d->getMatrix();
ModelData model = d->getData();
std::vector<glm::mat4>& mats = mMatrices;
for (int d = 0; d < model.size * 3; d += 3) {
//upload vertices
glm::vec4 v = glm::vec4(model.vertices[d], model.vertices[d + 1],
model.vertices[d + 2], 1);
verts.push_back(v.x);
verts.push_back(v.y);
verts.push_back(v.z);
//upload model matrices
mats.push_back(mat);
}
glBindBuffer(GL_ARRAY_BUFFER, mMatrixBuffer);
glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::mat4) * mMatrices.size(),
&mMatrices[0][0], drawType);
And here is the vertex shader:
#version 330
layout (location = 0) in vec3 position_vec;
layout (location = 1) in vec3 normal_vec;
layout (location = 2) in vec2 texture_vec;
layout (location = 3) in mat4 model_matrix;
out vec2 texture_vec0;
out vec3 normal_vec0;
uniform mat4 projection_view_mat;
void main()
{
gl_Position = projection_view_mat * model_matrix * vec4(position_vec,1);
texture_vec0 = texture_vec;
normal_vec0 = normal_vec;
}
Edit:
Draw method:
glUseProgramObjectARB(mShaderProgram);
GLint PVMLoc = glGetUniformLocationARB(mShaderProgram, "projection_view_mat");
glm::mat4 projView = mProjection * mView;
glUniformMatrix4fv(PVMLoc, 1,
GL_FALSE, &projView[0][0]);
GLint loc1 = glGetUniformLocationARB(mShaderProgram, "tex");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 1);
glUniform1iARB(loc1, 1);
glBindVertexArray(mArrayBuffer); // Bind our Vertex Array Object
glDrawArrays(GL_TRIANGLES, 0, mVertices.size()); // Draw our square
glBindVertexArray(0); // Unbind our Vertex Array Object
check_gl_error();
}
}
Thanks.