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 );
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments