Search code examples
openglglutfreeglut

OpenGL with GLUT: Solid objects are transperent


I am trying to paint a robot, which has a cube-like head, using glutSolidCube(), and to paint its eyes using glutSolidSphere().

For some reason, when I look at the robot from the other side, I can still see its eyes on the inner side of the edge of the cube. I.e. it seems that the cube's edges are transparent and that I can see through them.

How can I prevent this transparency?


Front look:

Front



Look from the back and right edges of the head: Back

I attach my code here:

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

Solution

  • I found a solution, so I'll post it here for the public:

    1. In the init() function, need to add:

      glEnable(GL_DEPTH_TEST);

    2. In the painting function, in the glClear() call, add " | GL_DEPTH_BUFFER_BIT":

      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    3. In the main() function, in theglutInitDisplayMode() call, add " | GLUT_DEPTH":

      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);