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

CaptainObvious

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

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

(Modern) OpenGL Different Colored Faces on a Cube - Using Shaders

From Dev

How do I make an open cube (missing one surface) without getting effect where sides are mirrored or switched in OpenGL?

From Dev

How do you binarize a colored image?

From Dev

How do you delete an SSAS cube?

From Dev

How should you initialize a cube map array in OpenGL 4.5?

From Dev

How to set different images as faces/sides of a 3D cube in Java 3D API?

From Dev

How to draw a cube in OpenGL?

From Dev

Render to cube map on iPhone using OpenGL ES

From Dev

Can't Get Spinning Cube To Render In OpenGL

From Dev

Can't Get Spinning Cube To Render In OpenGL

From Dev

OpenGL - Render face of cube map to a quad

From Dev

How do i draw a Cube in OpenGL/C++?

From Dev

OPENGL How do I correctly render these 2 objects with different textures/mapping?

From Dev

How do I get different colored prompt depending on server?

From Dev

How do you render a javascript object?

From Dev

OpenGL: How do you create a colormap texture?

From Dev

Modern OpenGL - how to render VBO part in different color

From Dev

How to render Colored Text in Apache PDFBox

From Dev

OpenGL how to render background

From Dev

How do I render multiple textures in modern OpenGL?

From Dev

How to make my OpenGL cube "fade" in and out

From Dev

how to do colored checkbox in javascript

From Dev

Displaying all sides of cube - XNA

From Dev

How do you create a 3D array (data cube) in python?

From Dev

How do you render views in an each loop without container objects?

From Dev

How do you render 2D text in lwjgl?

From Dev

How do you render a partial to a hash value in JBuilder?

From Dev

How do you render a command button through ajax? It has to be dynamic

From Dev

How do you render html and css into an iframe directly from responses

Related Related

  1. 1

    (Modern) OpenGL Different Colored Faces on a Cube - Using Shaders

  2. 2

    How do I make an open cube (missing one surface) without getting effect where sides are mirrored or switched in OpenGL?

  3. 3

    How do you binarize a colored image?

  4. 4

    How do you delete an SSAS cube?

  5. 5

    How should you initialize a cube map array in OpenGL 4.5?

  6. 6

    How to set different images as faces/sides of a 3D cube in Java 3D API?

  7. 7

    How to draw a cube in OpenGL?

  8. 8

    Render to cube map on iPhone using OpenGL ES

  9. 9

    Can't Get Spinning Cube To Render In OpenGL

  10. 10

    Can't Get Spinning Cube To Render In OpenGL

  11. 11

    OpenGL - Render face of cube map to a quad

  12. 12

    How do i draw a Cube in OpenGL/C++?

  13. 13

    OPENGL How do I correctly render these 2 objects with different textures/mapping?

  14. 14

    How do I get different colored prompt depending on server?

  15. 15

    How do you render a javascript object?

  16. 16

    OpenGL: How do you create a colormap texture?

  17. 17

    Modern OpenGL - how to render VBO part in different color

  18. 18

    How to render Colored Text in Apache PDFBox

  19. 19

    OpenGL how to render background

  20. 20

    How do I render multiple textures in modern OpenGL?

  21. 21

    How to make my OpenGL cube "fade" in and out

  22. 22

    how to do colored checkbox in javascript

  23. 23

    Displaying all sides of cube - XNA

  24. 24

    How do you create a 3D array (data cube) in python?

  25. 25

    How do you render views in an each loop without container objects?

  26. 26

    How do you render 2D text in lwjgl?

  27. 27

    How do you render a partial to a hash value in JBuilder?

  28. 28

    How do you render a command button through ajax? It has to be dynamic

  29. 29

    How do you render html and css into an iframe directly from responses

HotTag

Archive