# glDrawElements() draws only part of my mesh... :-x

**URL:** <https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076>\
**Category:** OpenGL: Basic Coding\
**Created:** [November 6, 2009, 1:22am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076 "2009-11-06T01:22:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakesh\_thp](https://community.khronos.org/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@rakesh\_thp](https://community.khronos.org/u/rakesh_thp)\
**Post date:** [November 6, 2009, 1:22am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/1 "2009-11-06T01:22:53Z")

</div>

Hello…

I have recently shifted from Display lists to VBOs… It’s a very nice thing in OpenGL i found to render huge amount of data smoothly…

Let me explain you the scenario… I have maintained a list of triangle objects as

```auto
std::map<unsigned int, EObjects*> MeshElements

```

Thus, MeshElements contains all the triangles generated by the mesh generator. Now inorder to display, i call a routine first as shown below:

```auto

mVertexCount = MeshElements.size();

mVertices = new GLfloat[mVertexCount * 6];
mIndices = new GLuint[mVertexCount * 3];

int v_ind = 0, i_ind = 0;

for( std::map<unsigned int, EObjects*>::iterator it = MeshElements.begin(); it != MeshElements.end(); it++ )
{
     EObjects *ob = (*it).second;
     mVertices[v_ind] = ob->GetNodeOne().GetX(); v_ind++;
     mVertices[v_ind] = ob->GetNodeOne().GetY(); v_ind++;
     mIndices[i_ind] = ob->GetNodeOneIndex(); i_ind++;
     mVertices[v_ind] = ob->GetNodeTwo().GetX(); v_ind++;
     mVertices[v_ind] = ob->GetNodeTwo().GetY(); v_ind++;
     mIndices[i_ind] = ob->GetNodeTwoIndex(); i_ind++;
     mVertices[v_ind] = ob->GetNodeThree().GetX(); v_ind++;
     mVertices[v_ind] = ob->GetNodeThree().GetY(); v_ind++;
     mIndices[i_ind] = ob->GetNodeThreeIndex(); i_ind++;
}

glGenGuffersARB(BufferSize, BufferName);

```

where BufferSize and BufferName are declared globally as follows:

```auto

enum{
  INDEX_OBJECT = 0,
  POSITION_OBJECT = 1
};

static const int BufferSize = 2;
static GLuint BufferName[BufferSize];

```

Below is my rendering code:

```auto

glBindBufferARB( GL_ARRAY_BUFFER_ARB, BufferName[POSITION_OBJECT]);
glBufferDataARB( GL_ARRAY_BUFFER_ARB, mVertexCount * 6 * sizeof(float), mVertices, GL_STREAM_DRAW_ARB );
glVertexPointer(2, GL_FLOAT, 0, 0);

glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, BufferName[INDEX_OBJECT] );
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, mVertexCount * 3 * sizeof(GLuint), mIndices, GL_STREAM_DRAW_ARB );

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);

glDrawElements( GL_TRIANGLES, mVertexCount*3, GL_UNSIGNED_INT, NULL );

glDisableClientState(GL_INDEX_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

```

This code is working fine… But, only the outer part of the mesh is displayed… the Inner part is not displayed… I mean to say, if the mesh is of shape “D”, it should have filled all the triangles in “D”… Instead of doing so, it is rather, drawing triangles only along the border of the shape “D” Inner part, triangles are not drawn…

Before using glDrawElements(), i was using glDrawArrays()… It also produced the same output… So I used, GL\_POINTS with glDrawArrays(), just to check if it is reading all the points… In response to this test, it rendered all the points… But when i said, GL\_TRIANGLES, it is not rendering the inner part…

On the other hand, GL\_POINTS with glDrawElements() is also rendering only at the boundaries… Not inner part like glDrawArrays()…

Is something wrong with my code above…?? Please someone guide me properly…

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![rakesh\_thp](https://community.khronos.org/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@rakesh\_thp](https://community.khronos.org/u/rakesh_thp)\
**Post date:** [November 12, 2009, 3:18am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/2 "2009-11-12T03:18:26Z")

</div>

Hello,

I’m sorry but is there no one who can give solution or atleast tell me whats the problem and where it lies…??

Thanks

---

<div class="post-metadata">

**Author:** ![Dark\_Photon](https://community.khronos.org/user_avatar/community.khronos.org/dark_photon/32/4349_2.png) [@Dark\_Photon](https://community.khronos.org/u/Dark_Photon)\
**Post date:** [November 12, 2009, 5:30am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/3 "2009-11-12T05:30:53Z")

</div>

> [@rakesh\_thp](#):
>
> Below is my rendering code:
> 
> ```auto
> 
> ...
> glEnableClientState(GL_INDEX_ARRAY);
> ...
> 
> ```

Where did you dig up this code? Check this out:

[Common Mistakes – glEnableClientState(GL\_INDEX\_ARRAY)](http://www.opengl.org/wiki/Common_Mistakes#glEnableClientState.28GL_INDEX_ARRAY.29)

Check out one of these tutorials. It should give you what you need:

- [oZone3D.Net Tutorials: OpenGL Vertex Buffer Objects](http://www.ozone3d.net/tutorials/opengl_vbo.php)
- [SongHo: OpenGL Vertex Buffer Object (VBO)](http://www.songho.ca/opengl/gl_vbo.html)

---

<div class="post-metadata">

**Author:** ![DmitryM](https://community.khronos.org/user_avatar/community.khronos.org/dmitrym/32/138_2.png) [@DmitryM](https://community.khronos.org/u/DmitryM)\
**Post date:** [November 12, 2009, 6:39am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/4 "2009-11-12T06:39:51Z")

</div>

Where do you get the geometry?  
Some triangles may not be draw due to face culling.  
Try to glDisable(GL\_CULL\_FACE) before drawing.

And add some checks at the end of the loading process to be sure you read the correct amount of data:

```auto

assert(i_ind == mVertexCount * 3);
assert(v_ind == mVertexCount * 6);

```

---

<div class="post-metadata">

**Author:** ![rakesh\_thp](https://community.khronos.org/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@rakesh\_thp](https://community.khronos.org/u/rakesh_thp)\
**Post date:** [November 12, 2009, 11:15pm UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/5 "2009-11-12T23:15:28Z")

</div>

Well that glIndexArrays() was just to try whether it will draw all elements… It was my last alternative… 🙂

Anyhow it displays all points…

Displaying triangles with glDrawArrays() also not working… Producing same output… I also tried with glDisable(GL\_CULL\_FACE) as mentioned by one of the sir… But my fortunate is not that good enough to produce the required out put… I think no need to check the number of elements after loading… Coz, the same code with

```auto
glDrawArrays( GL_POINTS, 0, mVertexCount * 3);

```

works fine… I mean all points are displayed… If i replace it by

```auto
glDrawArrays( GL_TRIANGLES, 0, mVertexCount);

```

produces undesired output… Only outside boundary is being displayed… IT’s very strange but true… 🙂

Any more suggestions…??

Thanks anyways

---

<div class="post-metadata">

**Author:** ![scratt](https://community.khronos.org/user_avatar/community.khronos.org/scratt/32/120_2.png) [@scratt](https://community.khronos.org/u/scratt)\
**Post date:** [November 13, 2009, 4:32am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/6 "2009-11-13T04:32:55Z")

</div>

Your mVertexCount count should be the same in both commands as it is a vertex count, literally.

So you should be using mVertexCount \* 3 in both draw commands.

For example : glDrawArrays(GL\_TRIANGLE\_STRIP, 0, 4) draws two triangles using 4 vertices and glDrawArrays(GL\_TRIANGLES, 0, 3) draws one triangle using 3 vertices.

---

<div class="post-metadata">

**Author:** ![rakesh\_thp](https://community.khronos.org/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@rakesh\_thp](https://community.khronos.org/u/rakesh_thp)\
**Post date:** [November 13, 2009, 10:33pm UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/7 "2009-11-13T22:33:21Z")

</div>

oh is it…?? But part of mesh is displyed… why is that so…?? if thats the case, then is there any method to build the array of vertices…?? like, in my case, I have stored the triangle objects in a list… and each triangle object is have three members…

NodeOne  
NodeTwo  
NodeThree

Now if i want to build the vertices for VBO, how can i do it…?? As far as my mind worked i used the following logic… 🙂

```auto
mVertexCount = MeshElements.size();

mVertices = new GLfloat[mVertexCount * 6];
mIndices = new GLuint[mVertexCount * 3];

int v_ind = 0, i_ind = 0;

for( std::map<unsigned int, EObjects*>::iterator it = MeshElements.begin(); it != MeshElements.end(); it++ )
{
     EObjects *ob = (*it).second;
     mVertices[v_ind] = ob->GetNodeOne().GetX(); v_ind++;
     mVertices[v_ind] = ob->GetNodeOne().GetY(); v_ind++;
     mIndices[i_ind] = ob->GetNodeOneIndex(); i_ind++;
     mVertices[v_ind] = ob->GetNodeTwo().GetX(); v_ind++;
     mVertices[v_ind] = ob->GetNodeTwo().GetY(); v_ind++;
     mIndices[i_ind] = ob->GetNodeTwoIndex(); i_ind++;
     mVertices[v_ind] = ob->GetNodeThree().GetX(); v_ind++;
     mVertices[v_ind] = ob->GetNodeThree().GetY(); v_ind++;
     mIndices[i_ind] = ob->GetNodeThreeIndex(); i_ind++;
}

```

Is there any change i should do there…???

Thanks

---

<div class="post-metadata">

**Author:** ![scratt](https://community.khronos.org/user_avatar/community.khronos.org/scratt/32/120_2.png) [@scratt](https://community.khronos.org/u/scratt)\
**Post date:** [November 14, 2009, 1:07am UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/8 "2009-11-14T01:07:26Z")

</div>

> [@rakesh\_thp](#):
>
> oh is it…?? But part of mesh is displyed… why is that so…??

Part of the mesh will be displayed as you have asked OpenGL to draw one third of the required vertices.

---

<div class="post-metadata">

**Author:** ![rakesh\_thp](https://community.khronos.org/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@rakesh\_thp](https://community.khronos.org/u/rakesh_thp)\
**Post date:** [November 17, 2009, 9:11pm UTC](https://community.khronos.org/t/gldrawelements-draws-only-part-of-my-mesh-x/59076/9 "2009-11-17T21:11:20Z")

</div>

so u mean to say, i got to use,

```auto

glDrawArrays( GL_TRIANGLES, 0, mVertexCount*3);

```

to get the proper output…???
