<p><a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawElements.xhtml" rel="nofollow noreferrer">glDrawElements</a> 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.</p>

CC BY-SA 3.0
2018-03-08T14:38:27.463Z
2018-03-08T14:38:27.463Z
Reynolds
<p>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. </p>

<pre><code>// 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);
</code></pre>

CC BY-SA 3.0
2018-03-08T11:09:24.993Z
Are you sure scene.objects[k].vertices.size() is the same size as the number of indices?
Reynolds
CC BY-SA 3.0
2018-03-08T13:51:14.133Z
@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
CC BY-SA 3.0
2018-03-08T14:28:44.317Z
The second arguments is the number of indices you want to read from your array indices. So it should be indices.size().
Reynolds
CC BY-SA 3.0
2018-03-08T14:33:57.473Z
@Reynolds Oh my, thank you very much. I find openGL documentation very confusing.
Manh Nguyen Huu
<opengl-es>
What is wrong with my glDrawElement code
Manh Nguyen Huu
# What is wrong with my glDrawElement code

(Asked by: username_0 on 2018-03-08)

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);

```

**Tags:** opengl-es

> **Comments:**
> **username_1**: Are you sure scene.objects[k].vertices.size() is the same size as the number of indices?  
> **username_0**: @username_1 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  
> **username_1**: The second arguments is the number of indices you want to read from your array indices. So it should be indices.size().  
> **username_0**: @username_1 Oh my, thank you very much. I find openGL documentation very confusing.  

---

## 1 answers

### Answer by username_1 on 2018-03-08. Score: 1 | Accepted answer

[glDrawElements](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawElements.xhtml) 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.


---

