Jump to content
  • Advertisement
Sign in to follow this  

Problems with glDrawElements()

This topic is 3611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have problem trying to render an object using glDrawElements() . I am loading external objects in my class, Mesh that contain m_pVertex and m_nNormal, both sub classes of Mesh. i initialize indices as follows: GLushort *faces_indices; for(int i=0; i < mesh.m_nFace; i++){ indices[i*3] = (GLushort)mesh.m_pFace.v0()+1; indices[(i*3)+1] = (GLushort)mesh.m_pFace.v1()+1; indices[(i*3)+2] =(GLushort) mesh.m_pFace.v2()+1; } ---------------------------------- My problem is that the connectivity of the object is wrong.The points are correct but when i go to GL_LINES mode some faces are missing. I have tried everything, my guess is that the indices are different from faces . Can you help me ? glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3,GL_FLOAT,sizeof(CVertex3D),&Mesh.m_pVertex[0]); glNormalPointer(GL_FLOAT,sizeof(CNormal3D),&Mesh.m_pNormal[0]); glDrawElements(gl_mode,3*Mesh.m_nFace,GL_UNSIGNED_SHORT,indices); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);

Share this post

Link to post
Share on other sites
The indices are different for GL_LINES.

Your setup is sufficient for GL_POINTS and GL_TRIANGLES. For GL_POINTS each vertex is rendered individually, with GL_TRIANGLES 3 indices form a triangle.

So now imagine what happens with GL_LINES. The first edge of the triangle will be rendered fine (i.e. T1.vertex1 and T1.vertex2). The second line however will be formed from T1.vertex3 and T2.vertex1.

Short (a bit exaggerated) example:

Consider the following index setup. Using GL_TRIANGLES, everything is fine.
3 6

/\ /\

/ \ / \

1----2 4----5


3_ 6

\_ \

\ \

1----2 4 5

You see? 3 lines (one incorrect) instead of 2 triangles.

Share this post

Link to post
Share on other sites
Hi thanks for the reply , i thought i could switch to every GL_MODE with indices. My problem though is different. When i render the object the surface and the points seems to be ok ,but the normals dont appear right . I calculate the normals and they are correct if i render the object with glBegin() glEnd().
Thats why i thought the indices are not setup up correctly.
Are there any other options for glNormalPointer or is the below statement correct ? glNormalPointer(GL_FLOAT,sizeof(CNormal3D),&Mesh.m_pNormal[0]);
when the normals are in :

Share this post

Link to post
Share on other sites
You need to have a normal for each vertex, not for each triangle (since you have only 1 index at all to address vertex positions as well as normals). Have you considered this crucial point?

Share this post

Link to post
Share on other sites
No i thought i need one normal for each face, i also just realize that my geometry has problems so i have to fix that first. I guess that the problem starts from the indices. The vertices are fine , i am using a 4 vertices cube , but when i am copying the faces to an indices array (which is the size of faces*3), the cube has problems . I get confused with the indices , are the same with faces or not ?

Share this post

Link to post
Share on other sites
It seems that in your set-up you have 1 array per vertex property. The properties you are dealing with are vertex positions and vertex normals (and perhaps others, but they are all handled the same way). A cube has 8 corners, and so it requires at least 8 vertices. However, if you pick out one of them and approach it from different adjacent faces, you'll see that one of the properties is constant (namely the position), the other property is different (namely the normal). But a single vertex can have only one composition of all it properties. Hence, you need only 8 vertices if both the positions and normals would be the same, but in your case you need 6*4=24 (noof faces by noof corners per face) vertices due to the varying normals. The faces are quads but rendered as triangles, so you have to feed the GPU with 6*2=12 triangles, requiring 12*3=36 indices.

In summary, you need the arrays
mesh.m_pVertex[24] // vertex positions
mesh.m_pNormal[24] // vertex normals
mesh.m_pFace[36] // triangle indices

Then fill in the array e.g. like follows: Iterate over the 6 faces, generate the 4 vertices in CW order for the current face, and for each vertex write the position to m_pVertex and copy the face normal to m_pNormal. So, for each face 4 vertex positions and 4 vertex normals are written to the arrays. Then generate the indices for the array. E.g. if the current "base index" (i.e. the index of the 1st of the 4 vertices for the current face) is i, the possible index list for a face (remember: a quad split into 2 triangles) can be i, i+1, i+2, i, i+2, i+3.

Share this post

Link to post
Share on other sites
Hi thansk again for your reply , apparently the geometry is correct but the normals are the problem now. As you can see in the screenshot here : http://www.flickr.com/photos/mikeathan/2713266941/

the left cube is the one drawn using glDrawElements() and the second is drawn using glBegin()/glEnd(). Both they use the same normals but when they are drawn they don't reflect the light the same .
The second example shows the problem better using a different object


Again the first is rendered using glDrawElements() and the second using glBegin/End();

Any ideas ?/

Share this post

Link to post
Share on other sites
I doubt of the identity of the normals over a face.

Look e.g. at the top face of the cube. The front corner of the top face has the same gray color as the top-left corner of the front face as well as the right-top corner of the left face. This shows clearly that the normals of the top-front vertices of those 3 faces are identical! That's an error.

Moreover, there is a gradient from the front to the rear. Hence the normals at the front of the top face differ from those at its rear.

As already stated, all normals of a given face must be the same to yield in the correct rendering.

E.g. the vertex position of the top face may be
m_pVertex[0] = { +1, +1, +1 };
m_pVertex[1] = { -1, +1, +1 };
m_pVertex[2] = { -1, +1, -1 };
m_pVertex[3] = { +1, +1, -1 };
and the corresponding normals (all the same values!)
m_pNormal[0] = { 0, +1, 0 };
m_pNormal[1] = { 0, +1, 0 };
m_pNormal[2] = { 0, +1, 0 };
m_pNormal[3] = { 0, +1, 0 };
and its indices are
m_pIndex[0] = 0;
m_pIndex[1] = 1;
m_pIndex[2] = 2;
m_pIndex[3] = 0;
m_pIndex[4] = 2;
m_pIndex[5] = 3;

Notice that no other face will use the vertices with indices 0 up to 3. They all have their own quadruple of vertices, and each quadruple is to be build analogously to those shown above. This is necessary since in your case the vertices all differ and hence cannot be shared by two of the quads.

EDIT: Please bear in mind that the immediate mode is in fact a piece of software that build a vertex array from the calls. So it is able to handle things its own way. E.g. it duplicates a once set normal over and over again for each glVertex call until another glNormal is called. Such things are not done (by the driver) if you use VAs/VBOs directly. So you cannot rely on getting the same result. You have to verify the usage of VAs/VBOs by its own.

Share this post

Link to post
Share on other sites

Hi again. A problem i saw is that when i change the coordinates like changing the vertices or face_indices index, and keeping the same order for the normal array , the object is not constructed properly but the faces appear like the normals have been applied . The object reflects correctly the light even when the geometry is wrong.

is that an exception? is there something wrong with the verts/faces/normals, even when they appear correct ? or should i check the normals ?

The same verts/faces/normals works fine with glbegin/end .
Also the texture for glDrawElements and glbegin/end works fine too.

Share this post

Link to post
Share on other sites
This is the calculate normals function :
for(int i=0;i<faceList.size();i++){

u.x = c.x - a.x;
u.y = c.y - a.y;
u.z = c.z - a.z;
v.x = b.x - a.x;
v.y = b.y - a.y;
v.z = b.z - a.z;
Cross(u, v, w);



I thought that the number of normals have to be the same number of faces , does it have to be the same number of vertices instead?if yes why does it works fine with glbegin/end

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!