So I have been reading about VAO’s and indexed rendering lately, I tried it myself and am running into some problems. The code below should store an object into 4 VBOs and save the state with a VAO, and then draw it.
LiteEngine::Mesh::Mesh(std::vector<int>vecIndices, std::vector<int> texIndices, std::vector<int>norIndices, std::vector<vec3>vertices, std::vector<vec2>texCoords, std::vector<vec3>normals){
std::vector<vec3> Vec;
std::vector<vec2> Tex;
std::vector<vec3> Nor;
std::vector<unsigned int> indices;
for(int i = 0;i < vecIndices.size();i++){
int inVec = vecIndices[i];
int inTex = texIndices[i];
int inNor = norIndices[i];
vec3 vec = vertices[inVec-1];
vec2 tex;
if(inTex!= -1)
tex = texCoords[inTex-1];
else
tex = (vec2)vec;
vec3 nor;
if(inNor!=-1)
nor = normals[inNor-1];
else
nor = vec;
long index = findMatch(vec, tex, nor, Vec, Tex, Nor);
if(index = -1){
Vec.push_back(vec);
Tex.push_back(tex);
Nor.push_back(nor);
indices.push_back((unsigned int)Vec.size()-1);
}else{
indices.push_back((unsigned int)index);
}
faces++;
}
std::vector<int>().swap(vecIndices);
std::vector<int>().swap(texIndices);
std::vector<int>().swap(norIndices);
std::vector<vec3>().swap(vertices);
std::vector<vec2>().swap(texCoords);
std::vector<vec3>().swap(normals);
glGenVertexArrays(1, &meshID);
std::cout << "woohoo
";
glBindVertexArray(meshID);
std::cout << "made it here
";
unsigned int ids[4];
glGenBuffers(1, ids);
vecID = ids[0];
texID = ids[1];
norID = ids[2];
indicesID = ids[3];
glBindBuffer(GL_ARRAY_BUFFER, vecID);
glBufferData(GL_ARRAY_BUFFER, Vec.size() * sizeof(vec3), &Vec, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vec3), 0);
glBindBuffer(GL_ARRAY_BUFFER, texID);
glBufferData(GL_ARRAY_BUFFER, Tex.size() * sizeof(vec2), &Tex, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vec2), 0);
glBindBuffer(GL_ARRAY_BUFFER, norID);
glBufferData(GL_ARRAY_BUFFER, Nor.size() * sizeof(vec3), &Nor, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vec3), 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Drawing code
void LiteEngine::Engine::drawFrame(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
//204
glBindVertexArray(mesh->meshID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indicesID);
glDrawArrays(GL_TRIANGLES, 0, mesh->faces);
glDrawElements(GL_TRIANGLES, mesh->faces, GL_UNSIGNED_INT, (void*)0);
glBindVertexArray(0);
glutSwapBuffers();
}
I’m just looking to see if I am using VAOs correctly, if everything there is correct, I will know it’s a problem with my object loading.