带有 GLUT 的 OpenGL:实体对象是透明的

OpenGL with GLUT: Solid objects are transperent

我正在尝试使用 glutSolidCube() 绘制具有 cube-like 头部的机器人,并使用 glutSolidSphere() 绘制它的眼睛。

出于某种原因,当我从另一边看机器人时,我仍然可以看到它的眼睛在立方体边缘的内侧。 IE。看起来立方体的边缘是透明的,我可以透过它们看到。

如何防止这种透明度?


正面:

Front



从头部的后缘和右缘看: Back

我在这里附上我的代码:

#include <GL/glut.h>
#include <stdio.h>

GLsizei winWidth = 800;
GLsizei winHeight = 800;


// Global Variables
static float eyeX = 0.0;
static float eyeY = 0.5;
static float eyeZ = 4.0;
static float robotBottomX = 0.0;
static float robotBottomZ = 0.0;

void init() {
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

void displayFloor() {
    int i, j;

    for (i = -20 ; i < 20 ; i++) {
        int startWithBlack = (i % 2 == 0);
        for (j = -20 ; j < 20 ; j++) {
            int black = ((startWithBlack && abs(j) % 2 == 0) || ((!startWithBlack) && abs(j) % 2 == 1));
            glColor3f(0.0, 1.0, 0.0);
            if (black) {
                glColor3f(1.0, 0.0, 0.0);
            }
            glPushMatrix();
            //glTranslatef((double)i + 0.5, 0.0, (double)j + 0.5);
            glTranslatef((double)i / 2.0, 0.0, (double)j / 2.0);
            glScalef(1.0, 0.0, 1.0);
            glutSolidCube(1.0);
            glPopMatrix();
        }
    }
}

void displayRobot() {
    puts("D: display function was called");

    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    displayFloor();
    glColor3f(0.2, 0.2, 0.2);
    glScalef(1.0, 1.0, 1.0);
    glTranslatef(robotBottomX, 0.0, robotBottomZ);
    glutSolidCube(1.0);
    glPushMatrix();
    glTranslatef(0.0, 0.6, 0.0);
    glScalef(0.2, 0.2, 0.2);
    glColor3f(0.5, 0.5, 0.2);
    glutSolidCube(1.0);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0.0, 0.95, 0.0);
    glScalef(0.5, 0.5, 0.5);
    glColor3f(0.2, 0.5, 0.2);
    glutSolidCube(1.0);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0.15, 1.1, 0.25);
    glScalef(0.05, 0.05, 0.0);
    glColor4f(0.7, 0.7, 0.7, 0.5);
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-0.15, 1.1, 0.28);
    glScalef(0.05, 0.05, 0.0);
    glColor4f(0.7, 0.7, 0.7, 0.5);
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();
    glPopMatrix();

    glFlush();
}


void winReshape(GLint newWidth, GLint newHeight) {
    glViewport(0, 0, newWidth, newHeight);
    glMatrixMode(GL_PROJECTION);
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT);

}

void main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Robot");

    init();
    glutDisplayFunc(displayRobot);
    glutReshapeFunc(winReshape);

    glutMainLoop();
}

我找到了解决方案,所以我会 post 在这里 public:

  1. init()函数中,需要添加:

    glEnable(GL_DEPTH_TEST);

  2. 在绘画函数中,在glClear()调用中,添加" | GL_DEPTH_BUFFER_BIT":

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  3. main()函数中,在glutInitDisplayMode()调用中,添加" | GLUT_DEPTH":

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);