I'm loading a .obj model of a cube currently using those flags :
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals | aiProcess_JoinIdenticalVertices);
The model only contains 8 vertices (each sommet of the cube) and thus no duplicate vertices to specify assimp to draw all of the triangles needed. As a result, I only get 3 triangles of my box drawn.
I believe this is because I use :
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
Where vertices.size() is equal to 22 (it's the return of mesh->mNumVertices).
I don't get this 22, to me it should either be 8 or 6*2*3 = 36 (6 faces with 2 triangles each). Can anyone explain where it comes from ?
I though of using GL_TRIANGLE_STRIP but the vertices are not in the right order (I don't even know if they can be). I get more triangles but not the right ones.
So my main question is : is there a flag I could add to ReadFile -or something else- to write a copy of the vertices for each triangle to be drawn in the .obj file.
PS : I exported the Wavefront model from Blender, maybe it's on Blender that I can set something to export redondant vertices.
My .obj file : o 1
# Vertex list
v -0.5 -0.5 0.5
v -0.5 -0.5 -0.5
v -0.5 0.5 -0.5
v -0.5 0.5 0.5
v 0.5 -0.5 0.5
v 0.5 -0.5 -0.5
v 0.5 0.5 -0.5
v 0.5 0.5 0.5
usemtl Default
f 4 3 2 1
f 2 6 5 1
f 3 7 6 2
f 8 7 3 4
f 5 8 4 1
f 6 7 8 5
EDIT : How I set up the mesh with Qt OpenGL types (and now with EBO):
void setupMesh(QOpenGLShaderProgram* program)
{
// Create Vertex Array Object
if (!VAO->isCreated()) {
VAO->create();
}
VAO->bind();
VBO.create();
VBO.bind();
VBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
construct_sg_vertex();
VBO.allocate(this->m_vec.constData(), this->m_vec.size() * sizeof(m_vec.at(0)));
EBO.create();
EBO.bind(); //glBindBuffer(GL_ARRAY_BUFFER, ebo);
EBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
EBO.allocate(indices.data(),indices.size()*sizeof(GLuint));
// 0 : positions
program->enableAttributeArray(0);
program->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
//1: colors
program->enableAttributeArray(1);
program->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());
VBO.release();
EBO.release();
}
private:
QOpenGLBuffer VBO ;
QOpenGLVertexArrayObject* VAO = new QOpenGLVertexArrayObject();
QOpenGLBuffer EBO ; //New Element Buffer
};
With this added indices buffer I changed the way I draw to : VAO->bind() ; glDrawElements(GL_TRIANGLES, indices.size(),GL_UNSIGNED_INT,0);
As a result, nothing is rendered at all...