【问题标题】:OpenGL depth test is not workingOpenGL深度测试不起作用
【发布时间】:2017-01-30 12:17:45
【问题描述】:

我正在尝试使用 OpenGL 创建模型,并尝试启用深度测试。

我在我的main 中使用这些命令:

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);

这在我的展示中:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

我什至尝试添加:

glDepthFunc(GL_LEQUAL);

而且它不起作用。我仍然可以看到我认为是深度问题。 这是一个显示问题的视频:https://www.youtube.com/watch?v=OafrRH4Mzjc

  • 注意:在该视频中,电路板是从右到左、从上到下构建的,所以第一个角度可以,但其他任何角度都不好。

我错过了什么?


编辑:复制的最小示例文件:

#define _CRT_SECURE_NO_WARNINGS

#define SIZE_MOVES 17

#include <stdio.h>

/* Include the GLUT library. This file (glut.h) contains gl.h and glu.h */
#include <GL\glew.h>
#include <GL\freeglut.h>

static int  left_click = GLUT_UP;
static int  right_click = GLUT_UP;
static int  xold;
static int  yold;
static float rotate_x = 146;
static float rotate_y = -26;

int width, height;

GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0, 5, -10, 0 };

GLfloat mat_specular[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat mat_shininess[] = { 1 };


// colors
const GLfloat colors[2][4] = {
    { 1.0, 1.0, 1.0, 1.0 }, //white
    { 0.0, 0.0, 0.0, 1.0 } //black
};

// rgb
const GLfloat rgb[3][4] = {
    { 1.0, 0.0, 0.0, 1.0 },
    { 0.0, 1.0, 0.0, 1.0 },
    { 0.0, 0.0, 1.0, 1.0 }
};

void resetMaterial() {
    GLfloat c[] = { 1, 1, 1, 1 };
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}

void drawSquare(int color) {
    glPushMatrix(); {
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[color]);

        glScalef(1, 0.5, 1);
        glutSolidCube(1);
    } glPopMatrix();
}

void drawBoard() {
    for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++) {
            glPushMatrix(); {
                glTranslatef(i + 0.5, 0, j + 0.5);
                drawSquare((i + j) % 2);
            } glPopMatrix();
        }
}

void drawAxes() {
    glBegin(GL_LINES); {
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[0]);
        glVertex3f(-2, 0, 0); glVertex3f(5, 0, 0);
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[1]);
        glVertex3f(0, -2, 0); glVertex3f(0, 5, 0);
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[2]);
        glVertex3f(0, 0, -2); glVertex3f(0, 0, 5);
    } glEnd();
}

void letThereBeLight() {
    /*Add ambient light*/
    GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

    /*Add positioned light*/
    GLfloat lightColor1[] = { 0.2f, 0.2f, 0.1f, 1.0f };
    GLfloat lightPosition1[] = { -8, 8, 5, 0.0f };

    glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor1);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition1);

    /*Add directed light*/
    GLfloat lightColor2[] = { 0.3, 0.3, 0.3, 1.0f };
    GLfloat lightPosition2[] = { 8, 8, -5, 1.0f };
    glLightfv(GL_LIGHT1, GL_AMBIENT, lightColor2);
    glLightfv(GL_LIGHT1, GL_POSITION, lightPosition2);
}

void display(void) {
    // Clear frame buffer and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set up viewing transformation, looking down -Z axis
    glLoadIdentity();
    gluLookAt(0, 5, -15, 0, 0, 3, 0, 1, 0);

    letThereBeLight();

    resetMaterial();

    // Rotate view:
    glPushMatrix(); {
        glRotatef(rotate_y, 1, 0, 0);
        glRotatef(rotate_x, 0, 1, 0);

        glLightfv(GL_LIGHT0, GL_POSITION, light_position);

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[0]);

        glPushMatrix(); {
            glTranslatef(-4, 0, -4); // Move to center
            drawBoard();
        } glPopMatrix();

        drawAxes(); // For debuging
    } glPopMatrix();

    /* End */
    glFlush();
    glutSwapBuffers();
}

void mouseFunc(int button, int state, int x, int y) {
    if (GLUT_LEFT_BUTTON == button)
        left_click = state;

    xold = x;
    yold = y;
}

void motionFunc(int x, int y) {
    if (GLUT_DOWN == left_click) {
        rotate_y = rotate_y + (y - yold) / 5.f;
        rotate_x = rotate_x + (x - xold) / 5.f;
        glutPostRedisplay();
    }

    xold = x;
    yold = y;
}


void reshapeFunc(int new_width, int new_height) {
    width = new_width;
    height = new_height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50, width / height, 1, 20);

    glMatrixMode(GL_MODELVIEW);

    glutPostRedisplay();
}

int main(int argc, char **argv) {
    /* Creation of the window */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(900, 600);
    glEnable(GL_DEPTH_TEST);

    glutCreateWindow("Chess");

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_POLYGON_SMOOTH);
    glEnable(GL_LINE_SMOOTH);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glShadeModel(GL_SMOOTH);
    glDisable(GL_COLOR_MATERIAL);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    /* Declaration of the callbacks */
    glutDisplayFunc(&display);
    glutReshapeFunc(&reshapeFunc);
    glutMouseFunc(&mouseFunc);
    glutMotionFunc(&motionFunc);

    /* Loop */
    glutMainLoop();

    /* Never reached */
    return 0;
}

【问题讨论】:

  • 您是否尝试删除 glDepthFunc 和 glEnable(GL_CULL_FACE) 调用以删除与这些相关的可能罪魁祸首?
  • @Bartvbl 我做到了。移除剔除看起来更糟,因为它现在也绘制了背部。删除深度函数没有任何作用
  • 那么这超出了我对 GLUT 的了解。我可以推荐使用 GLFW 和 GLEW 之类的东西吗?它们是现代绑定,正在使用新版本的 OpenGL 进行更新,我相信使用 GLFW 您会默认获得深度缓冲区。
  • @Bartvbl 如果这是我自己的主动,当然可以。然而,这是一项学术任务,学术界总是10年前的事了。还是谢谢
  • 没有看到你是怎么画的......还有你的投影矩阵是什么?

标签: c++ opengl glut


【解决方案1】:
gluPerspective(50, width / height, 0, 20);
                                   ^ wat

zNear 必须是 greater than zero(强调我的):

深度缓冲区精度受为zNear 指定的值影响 和zFarzFarzNear 的比值越大,越少 深度缓冲区将有效区分表面 彼此靠近。

如果r = zFar / zNear 大约是log2(r) 位深度缓冲区 精度丢失。因为r 在 zNear 接近 0 时接近无穷大,所以 zNear 绝不能设置为 0。


编辑:鉴于新发布的 MCVE:

int main(int argc, char **argv) {
    /* Creation of the window */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(900, 600);
    glEnable(GL_DEPTH_TEST);  // too soon

    glutCreateWindow("Chess");
    ...
}

就像datenwolf said:在拥有当前 GL 上下文之前,您是 glEnable()ing(glutCreateWindow() 创建上下文并使其成为当前上下文)。

之后 glutCreateWindow()之前不要调用任何gl*()函数。

【讨论】:

  • 感谢您指出这一点,我不知道。但是,我将其更改为 1,仍然得到相同的结果。
  • 我添加了一个编辑。从一个空文件开始,我添加了主要(设置)、我的变量、我的照明(否则一切看起来都是白色的)、一个显示功能和一个使用鼠标左键拖动的可旋转视图,以旋转模型并查看问题,就像在视频中一样。
  • @genpfault 看起来你明白了......(或者 datenwolf 不知道他为什么被否决)直到现在才看到他的帖子......
  • 非常感谢!我之前确实尝试过移动它,但是,我得到了一个空的结果屏幕。现在,我得到了想要的结果。
  • 非常感谢!我正在为为什么物体没有显示而扯掉头发,我不知道 zNear 不能是 0.0。
【解决方案2】:
   public void onSurfaceChanged(int w, int h)
    {
        Matrix.frustumM(projectionMatrix,0,-(9f/18.5f),(9f/18.5f),-1f,1f,1.0f,1000f);
    }

我不得不将我的接近剪切形式从 0.01f 更改为 1.0f。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-18
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2018-07-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多