How do you render a cube with different colored sides in OpenGL?

I am attempting to render a cube with different colors on certain sides as a practice exercise, but the problem is that as I rotate the cube along the y-axis, I can still see the different color side through the sides facing the camera. I've tried splitting up the code into seperate glBegin() blocks for each side and I've tried looking around on google for the answer with no luck. According to the Microsoft documentation on glColor3f, "glcolor3 variants specify new red, green, and blue values explicitly and set the current alpha value to 1.0 (full intensity) implicitly.", so the transparency shouldn't be a problem...

Here is the picture representing the problem

Here is the rendering code for the cube:

@Override public void render() 
{
    glPushMatrix( );
    {
        glTranslatef( 0, 0, -4 );
        glRotatef( x, 0, 1, 0 );

        glColor3f( 0f, 1f, 0f );

        glBegin( GL_QUADS );
        {
            glVertex3f( -1, 1, 1 );
            glVertex3f( -1, -1, 1 );
            glVertex3f( 1, -1, 1 );
            glVertex3f( 1, 1, 1 );

            glVertex3f( -1, 1, -1 );
            glVertex3f( -1, -1, -1 );
            glVertex3f( 1, -1, -1 );
            glVertex3f( 1, 1, -1 );

            glVertex3f( -1, 1, -1 );
            glVertex3f( -1, 1, 1 );
            glVertex3f( 1, 1, 1 );
            glVertex3f( 1, 1, -1 );

            glVertex3f( -1, -1, -1 );
            glVertex3f( -1, -1, 1 );
            glVertex3f( 1, -1, 1 );
            glVertex3f( 1, -1, -1 );

            glVertex3f( -1, 1, -1 );
            glVertex3f( -1, -1, -1 );
            glVertex3f( -1, -1, 1 );
            glVertex3f( -1, 1, 1 );

            glColor3f( 1f, 0f, 0f );
            glVertex3f( 1, 1, -1 );
            glVertex3f( 1, -1, -1 );
            glVertex3f( 1, -1, 1 );
            glVertex3f( 1, 1, 1 );
        }

        glEnd( );
    }

    glPopMatrix( );
}

Here is my render loop:

protected void render( )
{
    glClear( GL_COLOR_BUFFER_BIT );
    glLoadIdentity( );

    for ( IGameObject gameObject : gameObjects )
        gameObject.render( );

    glfwSwapBuffers( window );
}

You need to enable Z-testing:

 glEnable(GL_DEPTH_TEST);

Change your call to glClear to clear the depth buffer also:

 glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

It looks like you're not using depth testing.

OpenGL draws shapes in the order that you tell it to, so if you draw the red face last, its fragments (ie pixels) overwrite the green ones that were drawn earlier. Since you (presumably) want to see the faces that are "in front" to actually appear in front, you have to tell OpenGL not to draw fragments that are "behind" things that have already been drawn.

Replace the glClear( GL_COLOR_BUFFER_BIT ) line with:

glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BIT );

This tells OpenGL that each time it's about to draw a fragment, it should compare its depth against the depth of what's already been drawn, and discard the new fragment if it's farther away than the old one. It also clears the depth buffer so that initially every pixel on the screen is "infinitely" far away, so that the faces of the cube will all be in front of the background. This will prevent the red face from appearing when there's a green one closer to the camera.

链接地址: http://www.djcxy.com/p/81842.html

上一篇: 在OpenGL中围绕它旋转对象

下一篇: 如何在OpenGL中渲染具有不同颜色的边的立方体?