Depth test is inverted by default in OpenGL or am I mistaken?

I play with OpenGL all week or equivalent. After 2D, I'm trying 3D now. I want to reproduce a 3D scene that you can see in the third video at http://johnnylee.net/projects/wii/ .
It was hard for me to do everything right, with textures and depth.

Recently, I had 2 problems that have somewhat visually the same effect:

  • One with textures that don't work well in 3D using the methods I found for 2D.
  • One with objects appearing from the bottom to the top. Like the problem presented here: Depth buffer in OpenGL

I solved both problems, but I would like to know if I am right. especially for the second point.


For the first , I think I have it. I have an image of a round target, with alpha for something outside the disk. It booted normally in OpenGL. Some (due to my z-ordering issues) other targets behind this have suffered from hiding the transparent areas of the square square square that I used to draw it.

The reason for this was that every part of the texture is considered completely opaque, given the depth buffer. When used glEnable(GL_ALPHA_TEST)with a test for glAlphaFunc(GL_GREATER, 0.5f), the alpha layer of the texture acts as an indicator of opacity per pixel (Boolean) and thus makes blending completely useless (because my image has Boolean transparency).

: , - , -, ?


, . 0 glClearDepth(0.0f), "" glDepthFunc(GL_GREATER).

, , 1.0, "" GL_LESS . , ...

, , , , , , !


, (, ), , :

    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowSize(600, 600); // Size of the OpenGL window
        glutCreateWindow("OpenGL - 3D Test"); // Creates OpenGL Window
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);

        PngImage* pi = new PngImage(); // custom class that reads well PNG with transparency
        pi->read_from_file("target.png");
        GLuint texs[1];
        glGenTextures(1, texs);
        target_texture = texs[0];
        glBindTexture(GL_TEXTURE_2D, target_texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, pi->getGLInternalFormat(), pi->getWidth(), pi->getHeight(), 0, pi->getGLFormat(), GL_UNSIGNED_BYTE, pi->getTexels());

        glutMainLoop(); // never returns!
        return 0;
    }

    void reshape(int w, int h) {
        glViewport(0, 0, (GLsizei) w, (GLsizei) h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(-1, 1, -1, 1);
        gluPerspective(45.0, w/(GLdouble)h, 0.5, 10.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }

    void display(void) {
        // The stared *** lines in this function make the (ugly?) fix for my second problem
        glClearColor(0, 0, 0, 1.00);
        glClearDepth(0);          // ***
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_DEPTH_FUNC);  // ***
        glDepthFunc(GL_GREATER);  // ***

        draw_scene();

        glutSwapBuffers();
        glutPostRedisplay();
    }

    void draw_scene() {
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(1.5, 0, -3, 0, 0, 1, 0, 1, 0);

        glColor4f(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_TEXTURE_2D);
        // The following 2 lines fix the first problem
        glEnable(GL_ALPHA_TEST);       // makes highly transparent parts
        glAlphaFunc(GL_GREATER, 0.2f); // as not existent/not drawn
        glBindTexture(GL_TEXTURE_2D, target_texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        // Drawing a textured target
        float x = 0, y = 0, z = 0, size = 0.2;
        glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(x-size, y-size, z);
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(x+size, y-size, z);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(x+size, y+size, z);
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(x-size, y+size, z);
        glEnd();
        // Drawing an textured target behind the other (but drawn after)
        float x = 0, y = 0, z = 2, size = 0.2;
        glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(x-size, y-size, z);
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(x+size, y-size, z);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(x+size, y+size, z);
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(x-size, y+size, z);
        glEnd();
    }
+3
4

1 ( ), , , , , . 1, , , , , . , , , .

, gluLookAt , z = 2 quad , z = 0. , ?

-, , , , . / .

, - , . gluOrtho gluPerspective. . , , , , .

+4

: , -, -, ?

, , - .

0

: , - modelViewProjection.

( ), , , - . , .

0

, glOrtho zNear to -1 zFar 1, [0,1] ( glDepthRange , , ), . , zNear , , .

0

Source: https://habr.com/ru/post/1775070/


All Articles