質問

I use OpenGL to draw some contents on the screen.

Here's the initialization :

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 10, -10);
glMatrixMode(GL_MODELVIEW);

then I draw the objects using this :

void draw(...)
{
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBegin(GL_QUADS);  //Begining the cube's drawing
    {
        glTexCoord3f(tu1, tv1, 1);  glVertex3f(offset,      _y, _z);
        glTexCoord3f(tu2, tv1, 1);  glVertex3f(offset + w,  _y, _z);
        glTexCoord3f(tu2, tv2, 1);  glVertex3f(offset + w,  _y + h,  _z);
        glTexCoord3f(tu1, tv2, 1);  glVertex3f(offset,      _y + h,  _z);
    }
    glEnd();    
    glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}

But the depth is not respected (tested with a lot of _z values). It varies with the order of call, the nearer will be the last drawn.

If I draw the pink bar first :

enter image description here

If I draw the pink bar after the cream strip :

enter image description here

役に立ちましたか?

解決

When you see painter's algorithm (last object drawn appears in front), it means a problem with your depth buffer. In your case, you need to enable depth testing and writing and in your initialization (it's off by default):

glEnable(GL_DEPTH_TEST);

You'll also want to clear the depth buffer at the beginning of each frame (clearing the color buffer is usually a good idea as well):

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Have a look at what the OpenGL wiki has to say about depth buffering.

他のヒント

Please keep in mind the following items which might help you: 1. The OpenGL follows the right-handed rule. 2. If you are using push and pops for handing the matrices then the last one you push is the first one that is applied while rendering.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top