Hi, so my vertex shader takes 3 glm::mat4 objects to render in the correct position. I was drawing each model 1 by 1 passing uniform matrices each render, but the amount of draw calls was starting to slow my framerate. So i changed it to use 3 matrix4 vbo’s. Everything now compiles, but I don’t see anything drawing. I’d love some help with this if anyone can. My code is as follows,
constants
static enum BUFFERS {
VERTEX_BUFFER, TEXCOORD_BUFFER, NORMAL_BUFFER, INDEX_BUFFER, MVP_MAT_VB, MODELVIEW_MAT_VB, NORMAL_MAT_VB
};
#define POSITION_LOCATION 0
#define TEX_COORD_LOCATION 1
#define NORMAL_LOCATION 2
#define MVP_LOCATION 3
#define MODEL_VIEW_LOCATION 7
#define NORMAL_MATRIX_LOCATION 11
Creating instance vbos
MeshEntry::MeshEntry(aiMesh *mesh) {
vbo[VERTEX_BUFFER] = NULL;
vbo[TEXCOORD_BUFFER] = NULL;
vbo[NORMAL_BUFFER] = NULL;
vbo[INDEX_BUFFER] = NULL;
vbo[MODELVIEW_MAT_VB] = NULL;
vbo[MVP_MAT_VB] = NULL;
vbo[NORMAL_MAT_VB] = NULL;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
elementCount = mesh->mNumFaces * 3;
if (mesh->HasPositions()) {
float *vertices = new float[mesh->mNumVertices * 3];
for (int i = 0; i < mesh->mNumVertices; ++i) {
vertices[i * 3] = mesh->mVertices[i].x;
vertices[i * 3 + 1] = mesh->mVertices[i].y;
vertices[i * 3 + 2] = mesh->mVertices[i].z;
}
glGenBuffers(1, &vbo[VERTEX_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 3 * mesh->mNumVertices * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(POSITION_LOCATION, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(POSITION_LOCATION);
delete vertices;
}
if (mesh->HasTextureCoords(0)) {
float *texCoords = new float[mesh->mNumVertices * 2];
for (int i = 0; i < mesh->mNumVertices; ++i) {
texCoords[i * 2] = mesh->mTextureCoords[0][i].x;
texCoords[i * 2 + 1] = mesh->mTextureCoords[0][i].y;
}
glGenBuffers(1, &vbo[TEXCOORD_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 2 * mesh->mNumVertices * sizeof(GLfloat), texCoords, GL_STATIC_DRAW);
glVertexAttribPointer(TEX_COORD_LOCATION, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(TEX_COORD_LOCATION);
delete texCoords;
}
if (mesh->HasNormals()) {
float *normals = new float[mesh->mNumVertices * 3];
for (int i = 0; i < mesh->mNumVertices; ++i) {
normals[i * 3] = mesh->mNormals[i].x;
normals[i * 3 + 1] = mesh->mNormals[i].y;
normals[i * 3 + 2] = mesh->mNormals[i].z;
}
glGenBuffers(1, &vbo[NORMAL_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 3 * mesh->mNumVertices * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(NORMAL_LOCATION, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(NORMAL_LOCATION);
delete normals;
}
glGenBuffers(1, &vbo[MVP_LOCATION]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[MVP_LOCATION]);
for (unsigned int i = 0; i < 4; i++) {
glEnableVertexAttribArray(MVP_LOCATION + i);
glVertexAttribPointer(MVP_LOCATION + i, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (const GLvoid*)(sizeof(GLfloat) * i * 4));
glVertexAttribDivisor(MVP_LOCATION + i, 1);
}
glGenBuffers(1, &vbo[MODELVIEW_MAT_VB]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[MODEL_VIEW_LOCATION]);
for (unsigned int i = 0; i < 4; i++) {
glEnableVertexAttribArray(MODEL_VIEW_LOCATION + i);
glVertexAttribPointer(MODEL_VIEW_LOCATION + i, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (const GLvoid*)(sizeof(GLfloat) * i * 4));
glVertexAttribDivisor(MODEL_VIEW_LOCATION + i, 1);
}
glGenBuffers(1, &vbo[NORMAL_MAT_VB]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_MATRIX_LOCATION]);
for (unsigned int i = 0; i < 4; i++) {
glEnableVertexAttribArray(NORMAL_MATRIX_LOCATION + i);
glVertexAttribPointer(NORMAL_MATRIX_LOCATION + i, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (const GLvoid*)(sizeof(GLfloat) * i * 4));
glVertexAttribDivisor(NORMAL_MATRIX_LOCATION + i, 1);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Instanced Rendering
void MeshEntry::renderInstanced(std::vector<glm::mat4> mvps, std::vector<glm::mat4> modelViews, std::vector<glm::mat4> normalMats)
{
glBindBuffer(GL_ARRAY_BUFFER, vbo[MVP_MAT_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * mvps.size(), &mvps[0][0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[MODELVIEW_MAT_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * mvps.size(), &modelViews[0][0], GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_MAT_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * mvps.size(), &normalMats[0][0], GL_DYNAMIC_DRAW);
glBindVertexArray(vao);
glDrawElementsInstanced(GL_TRIANGLES, elementCount, GL_UNSIGNED_INT, NULL, mvps.size());
// Make sure the VAO is not changed from the outside
glBindVertexArray(0);
}
Vertex Shader
#version 430 core
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec2 VertexTex;
layout (location = 2) in vec3 VertexNormal;
layout (location = 3) in mat4 MVP;
layout (location = 7) in mat4 ModelViewMatrix;
layout (location = 11) in mat4 NormalMatrix;
out Data
{
vec3 Position;
vec3 Normal;
vec2 TexCoord;
} data;
void main()
{
data.Normal = normalize( NormalMatrix * vec4(VertexNormal, 0) ).xyz;
data.Position = vec3( ModelViewMatrix * vec4( VertexPosition, 1 ) );
data.TexCoord = VertexTex;
gl_Position = MVP * vec4( VertexPosition, 1 );
}
Again this code worked fine before I started sticking matrices in VBO’s so the inputs are guaranteed to be fine.
I’ve only started tackling opengl in the last week, so forgive me for posting all the code I think might be relevant, as honestly I’m not sure what could be going wrong.
Thanks in advance for any help,
George.