简体   繁体   中英

OpengGL glDrawElements draws lines but not triangles

I have the following code:

for(auto m:shapes) {
    glVertexPointer(3, GL_DOUBLE, 0, m->verts);

    glNormalPointer(GL_DOUBLE, 0, m->normals);

    glDrawElements(GL_TRIANGLES, m->num_triangles*3, GL_UNSIGNED_INT, m->indices);
}

(In the pics the circle and the point does not matter, they are drawn by some other code. Sorry, I can't post images, I am a new user...)

http://dl.dropbox.com/u/5269/Screen%20shot%202011-12-10%20at%2012.38.28%20PM.png

It seem it does not draw anything.

If I change GL_TRIANGLES to GL_LINES:

for(auto m:shapes) {
    glVertexPointer(3, GL_DOUBLE, 0, m->verts);

    glNormalPointer(GL_DOUBLE, 0, m->normals);

    glDrawElements(GL_TRIANGLES, m->num_triangles*3, GL_UNSIGNED_INT, m->indices);
}

http://dl.dropbox.com/u/5269/Screen%20shot%202011-12-10%20at%2012.44.56%20PM.png

So it seems that the coordinates are valid in the vertext buffer, and the index buffer is OK too. glGetError returns 0. And the weirdest thing is that the same code works for other vertex buffers generated by the same source.

Any ideas?

Did you use the very same m->indices for both examples (which, by the way, are identical as also Brett Hale noted, because you probably didn't change the enumeration constant in the second example) ?

GL_LINES expects a stream of pairs, where GL_TRIANGLES expect a stream of triplets. I don't know how you filled m->indices , but if you did something like this:

0 1 1 2 2 3 3 4 4 5 5 6 ...

You would probably experience the same problem. When you interpret the stream as made of pairs you get all the segments that you are probably seeing:

01   12   23   34   45   56 ...

But when you interpret them as triplets you get triangles such that two vertices coincide, so they are degenerate and hence are not drawn.

011   223   344   556 ...

It is also unlikely that you can successfully use the same expression m->num_triangles*3 for both calls. Maybe that's another issue in the post, and you did change it in your code. Have a look at glBegin Man Page .

Try setting the polygon mode to GL_LINE (glPolygonMode) before your for loop. If you see the lines it might be something with your projection matrix for 2d drawing. Also, are the lines you show in the second image correct ? Seemed random to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM