使用GLUT的OpenGL:固体对象是transperent

时间:2015-08-10 20:05:17

标签: opengl glut freeglut

我正在尝试使用glutSolidCube()绘制一个具有立方体状头部的机器人,并使用glutSolidSphere()绘制其眼睛。

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

如何防止这种透明度?


前视:

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();
}

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,所以我会在这里公开发布:

  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);