c++ - OpenGL C++ 深度缓冲区不工作

标签 c++ opengl depth-buffer

尽管在 glutInitDisplayMode 中使用 GLUT_DEPTH 作为参数并执行glClear(GL_DEPTH_BUFFER_BIT) 在显示函数的开头。我不知道我还缺少什么。

下面是一个最小工作示例和图1、图2,如果想看图1、图2下的参数,请注释掉。

图 1(蓝色上方):

Picture 1

图2(红色下方):

Picture 2

例子:

#include <vector>
#include <gl\glut.h>

typedef std::vector<float> floatvec;

// Figure 1 (above blue)
float posX = 8.00f;
float posY = 7.54f;
float posZ = -0.89f;
float angleX = 300.50f;
float angleY = 45.33f;

// Figure 2 (below red)
float posX = 4.12f;
float posY = -4.87f;
float posZ = -3.84f;
float angleX = 343.25f;
float angleY = -45.00f;

int screenW = 720;
int screenH = 540;
float fMin = 0.5;
float fMax = 100.0;
float alpha = 60.0;

// Draws a rectangle in 3D
void DrawQuad(floatvec c) {
    glBegin(GL_QUADS);
    glVertex3f(-c[0], -c[1], -c[2]);
    glVertex3f(-c[3], -c[4], -c[5]);
    glVertex3f(-c[6], -c[7], -c[8]);
    glVertex3f(-c[9], -c[10], -c[11]);
    glEnd();
}

void Display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glPushMatrix();
    glRotatef(angleY, 1.0f, 0.0f, 0.0f);
    glRotatef(angleX, 0.0f, 1.0f, 0.0f);
    glRotatef(180.0, 0.0f, 0.0f, 1.0f);
    glTranslatef(posX, posY, posZ);

    // Draws the tiles
    glColor3f(1.0, 0.0, 0.0); // Red
    DrawQuad({ 0, 0, 0,
        5, 0, 0,
        5, 0, 5,
        0, 0, 5 });
    glColor3f(0.0, 0.0, 1.0); // Blue
    DrawQuad({ 0, 3, 0,
        5, 3, 0,
        5, 3, 5,
        0, 3, 5 });

    glPopMatrix();
    glutSwapBuffers();
}

void Reshape(int width, int height) {
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(alpha, (GLfloat)width / (GLfloat)height, fMin, fMax);
    glMatrixMode(GL_MODELVIEW);
}

int main(int iArgc, char** cppArgv) {
    glutInit(&iArgc, cppArgv);
    glEnable(GL_DEPTH_TEST);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(screenW, screenH);
    glutCreateWindow("Example");
    glutDisplayFunc(Display);
    glutIdleFunc(Display);
    glutReshapeFunc(Reshape);
    glutMainLoop();
    return 0;
}

最佳答案

你只需要改变调用glEnable(GL_DEPTH_TEST);的地方

将它放在 glutCreateWindow 之后,它会正常工作。

像这样:

int main(int iArgc, char** cppArgv) {
    glutInit(&iArgc, cppArgv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(screenW, screenH);
    glutCreateWindow("Example");
    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(Display);
    glutIdleFunc(Display);
    glutReshapeFunc(Reshape);
    glutMainLoop();
    return 0;
}

刚刚在这里测试过,它有效。

关于c++ - OpenGL C++ 深度缓冲区不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32357826/

相关文章:

opengl - GLSL 着色器 : occlusion order and culling

c++ - OpenGL:指定将什么值写入深度缓冲区?

c++ - 对数深度缓冲区 OpenGL

c++ - 我如何验证给定类的每个实例都被应用程序终止破坏了?

c++ - 如何检查 GLFW 窗口是否正在运行?

c++ - 现代 OpenGL(4.6) - 将着色器编译到库中

c++ - OpenGL 游戏在 Win7 中运行良好,在 Windows 8 中下降到 5fps?

c++ - 无法从好友类访问成员

c++ - 有什么 "standard"的方法可以计算数值梯度吗?

c++ - Boost::Regex 在 C++ 中的用法