1
$\begingroup$

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);
$\endgroup$
4
  • $\begingroup$ Are you sure scene.objects[k].vertices.size() is the same size as the number of indices? $\endgroup$
    – Reynolds
    Mar 8 '18 at 11:09
  • $\begingroup$ @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 $\endgroup$ Mar 8 '18 at 13:51
  • $\begingroup$ The second arguments is the number of indices you want to read from your array indices. So it should be indices.size(). $\endgroup$
    – Reynolds
    Mar 8 '18 at 14:28
  • $\begingroup$ @Reynolds Oh my, thank you very much. I find openGL documentation very confusing. $\endgroup$ Mar 8 '18 at 14:33
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.