1

I am reading a model from an obj file and draw it using glDrawElements. For some reasons, only a small part of it is drawn, even though I dont even have back-face culling turned on.

// vertices 
glEnableVertexAttribArray(0);
float * objectGeometry = scene.objects[k].verticesAsFloatArray();
glVertexAttribPointer(0, 4, GL_FLOAT, 0, 0, objectGeometry);

// normals
glEnableVertexAttribArray(1);
float * objectNormals = scene.objects[k].normalsAsFloatArray();
glVertexAttribPointer(1, 3, GL_FLOAT, 0, 0, objectNormals);

// indices
int * indices = scene.objects[k].facesAsIndiceArray();
glDrawElements(GL_TRIANGLES, scene.objects[k].vertices.size(), GL_UNSIGNED_INT, indices);
Nicol Bolas
  • 9,762
  • 18
  • 25
Manh Nguyen Huu
  • 207
  • 1
  • 2
  • 12
  • Are you sure scene.objects[k].vertices.size() is the same size as the number of indices? – Reynolds Mar 08 '18 at 11:09
  • @Reynolds No, it doesn't. But I thought the whole point of having indices is that you dont need to duplicate vertices? Because one vertices could belong to many triangles – Manh Nguyen Huu Mar 08 '18 at 13:51
  • The second arguments is the number of indices you want to read from your array indices. So it should be indices.size(). – Reynolds Mar 08 '18 at 14:28
  • @Reynolds Oh my, thank you very much. I find openGL documentation very confusing. – Manh Nguyen Huu Mar 08 '18 at 14:33

1 Answers1

1

glDrawElements specifies the second argument to be the number of elements to be taken from the buffer of indices, not the number of vertices. The correct number would be the number of triangles times three.

Reynolds
  • 1,238
  • 6
  • 13