3

Front face on the left; right face on the right

Cube Front

Back face on the left; left face on the right

Cube Back

After some debugging, I've come to a couple conclusions:

  • Face culling (turned off in the photos) appears to make it worse.

  • Depth Buffering (turned on in the photos) appears to do little to help.

The face culls used glFrontFace( GL_CW ) | glFrontFace( GL_CCW ) in combination with glCullFace( GL_BACK_FACE ).

Update

Here is the result from the code snippet offered by Need4Sleep:

(note: the GL_LESS depth comparison didn't appear to change anything on its own)

Front face on left; right face on right - face culling turned on

withCulling


Code Overview

The vertices and colors are ordered as vertex, color in a struct, known as simdColorVertex_t, where the both the color and the vertex components of the struct consist of 4 floats, each, in their own respective arrays:

typedef float simdVec4_t[ 4 ];

typedef struct simdColorVert4_s
{
    simdVec4_t position;
    simdVec4_t color;
}
simdColorVert4_t;

ColorCube class

The constructor creates its respective program. Then the vertex and index data are specified, and bound to their respective buffers:

(shader program creation omitted for brevity)

    const float S = 0.5f;

    const simdColorVert4_t vertices[] =
    {
        /*! Positions */                    /*! Colors */           /*! Indices */
        { {  S,  S,  S, 1.0f },     { 0.0f, 0.0f, 1.0f, 1.0f } },   //! 0

        { { -S,  S,  S, 1.0f },     { 1.0f, 0.0f, 0.0f, 1.0f } },   //! 1

        { { -S, -S,  S, 1.0f },     { 0.0f, 1.0f, 0.0f, 1.0f } },   //! 2

        { {  S, -S,  S, 1.0f },     { 1.0f, 1.0f, 0.0f, 1.0f } },   //! 3

        { {  S,  S, -S, 1.0f },     { 1.0f, 1.0f, 1.0f, 1.0f } },   //! 4

        { { -S,  S, -S, 1.0f },     { 1.0f, 0.0f, 0.0f, 1.0f } },   //! 5

        { { -S, -S, -S, 1.0f },     { 1.0f, 0.0f, 1.0f, 1.0f } },   //! 6

        { {  S, -S, -S, 1.0f },     { 0.0f, 0.0f, 1.0f, 1.0f } }    //! 7
    };


    const GLubyte indices[] =
    {
        1, 0, 2,    2, 0, 3,    //! Front Face

        3, 6, 4,    4, 0, 3,    //! Right Face

        3, 6, 2,    2, 6, 7,    //! Bottom Face

        7, 6, 4,    4, 5, 7,    //! Back Face

        7, 5, 2,    2, 1, 5,    //! Left Face

        5, 1, 4,    4, 0, 1,    //! Top Face
    };

    //! The prefix BI_* denotes an enum, standing for "Buffer Index"

    {
        glBindBuffer( GL_ARRAY_BUFFER, mBuffers[ BI_ARRAY_BUFFER ] );
        glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_DYNAMIC_DRAW );
        glBindBuffer( GL_ARRAY_BUFFER, 0 );

        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mBuffers[ BI_ELEMENT_BUFFER ] );
        glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( indices ), indices, GL_DYNAMIC_DRAW );
        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
    }

From there (still in the constructor), the vertex array is created and bound, along with its respective attribute and buffer data:

    glBindVertexArray( mVertexArray );

    glBindBuffer( GL_ARRAY_BUFFER, mBuffers[ BI_ARRAY_BUFFER ] );

    glEnableVertexAttribArray( 0 );
    glEnableVertexAttribArray( 1 );

    glVertexAttribPointer( 0, 4, GL_FLOAT, GL_FALSE, sizeof( float ) * 8, ( void* ) offsetof( simdColorVert4_t, simdColorVert4_t::position ) );
    glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, sizeof( float ) * 8, ( void* ) offsetof( simdColorVert4_t, simdColorVert4_t::color ) );

    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mBuffers[ BI_ELEMENT_BUFFER ] );

    glBindVertexArray( 0 );

Misc

Before the cube is initialized, this function is called:

void MainScene::setupGL( void )
{
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glDepthRange( 0.0f, 1.0f );

    glClearDepth( 1.0f );

    int width, height;

    gvGetWindowSize( &width, &height );

    glViewport( 0, 0, width, height );
}

And the depth buffer is cleared before the mCube->draw(...) function is called.

I think it's obvious I'm doing something wrong here, but I'm not sure what it could be. After messing about with back-face culling and moving between counter-clockwise and clockwise winding orders for the front-face, it's only made things worse. Any ideas?

zeboidlund
  • 9,731
  • 31
  • 118
  • 180
  • What does your actual draw call look like? – JasonD Apr 03 '13 at 07:31
  • 1
    Actually, never mind. Your indices are clearly wrong. The 2nd face uses vertices which are at opposite corners of the cube. – JasonD Apr 03 '13 at 07:51
  • I'll re-edit the indices tomorrow. If it works, I'll let you know - feel free to post that comment as an answer if you want. – zeboidlund Apr 03 '13 at 08:29
  • Nevermind - I just edited them and it worked beautifully. Thank you very much. If you post an answer I'll gladly accept it. =) – zeboidlund Apr 03 '13 at 08:43

2 Answers2

1

Try adding this piece of code to the top of your program

//screen cleared as blue
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);

this is from a textured cube program i wrote, you may already have some of these functions implemented, but from what i see you might be missing glDepthFunc(GL_LESS)

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
  • I appreciate the suggestion, unfortunately that appeared to make it worse. At first I tried it via setting the depth function to GL_LESS and that didn't change anything. As soon as I turned on face culling, however, it made things worse. I updated my post with screen shot of the result (with face culling). – zeboidlund Apr 03 '13 at 05:05
  • 1
    could you post your entire code on something like pastebin? I could help further if i saw the entire program. – Syntactic Fructose Apr 03 '13 at 05:18
1

Per @JasonD's notice of the flawed indexed draw order, the indices have been edited as follows:

const GLubyte indices[] =
{
    1, 0, 2,    2, 0, 3,    //! Front Face

    3, 7, 4,    4, 0, 3,    //! Right Face (edited)

    3, 7, 2,    2, 6, 7,    //! Bottom Face (edited)

    7, 6, 4,    4, 5, 6,    //! Back Face

    6, 5, 2,    2, 1, 5,    //! Left Face (edited)

    5, 1, 4,    4, 0, 1,    //! Top Face
};

If one compares the above indices with the original, mentioned in the question, it's not hard to notice the flawed beauty of human error!

(needless to say, the cube now renders properly)

zeboidlund
  • 9,731
  • 31
  • 118
  • 180