user202051
user202051

Reputation: 173

OpenGL & Xcode Colour Issue

Moving from Visual Studio to Xcode to code in OpenGL and C++ I have come across an issue.

Trying to draw a simple cube in Xcode there is an issue (possibly with the depth buffer) that means the rendering of the cube is p

I have created a function that draws the cube panels using quads. My code runs and on Visual Studio displays the cube correctly. In Xcode, however, it seems to give priority to the latest drawn quad so that a panel of the cube appears to be in front of a panel it is behind. Attached is an example of the display I am getting in Xcode. Also, the code I have written.

The question I am asking is if anyone has had this issue before? Further, if anyone knows what is causing this issue and if there is a solution?

#include <stdlib.h>
#include <OpenGL/gl.h> 
#include <OpenGL/glu.h>
#include <GLUT/GLUT.h>
#include <math.h>

char rendermode; // global variable for current rendering mode

/*
 * Scene initialisation
 */
void InitGL(GLvoid)
{
    glShadeModel(GL_SMOOTH);                        // Enable smooth shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);           // Black background
    glClearDepth(1.0f);                             // Depth buffer setup
    glEnable(GL_DEPTH_TEST);                        // Enables depth testing

    glDepthFunc(GL_LEQUAL);                         // The type of depth testing to do
    glEnable(GL_COLOR_MATERIAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void drawCube(float size){
    glBegin(GL_QUADS);
    //Front Panel (Black)
    glColor3f(0.0, 0.0, 0.0);
    glVertex3f(size, size, size);
    glVertex3f(-size, size, size);
    glVertex3f(-size, -size, size);
    glVertex3f(size, -size, size);

    //Back Panel (Blue)
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(size, size, -size);
    glVertex3f(-size, size, -size);
    glVertex3f(-size, -size, -size);
    glVertex3f(size, -size, -size);

    //Left Panel (Yellow)
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(-size, size, size);
    glVertex3f(-size, -size, size);
    glVertex3f(-size, -size, -size);
    glVertex3f(-size, size, -size);

    //Right Panel (Green)
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(size, size, size);
    glVertex3f(size, -size, size);
    glVertex3f(size, -size, -size);
    glVertex3f(size, size, -size);

    //Bottom Panel (Light Blue)
    glColor3f(0.0, 1.0, 1.0);
    glVertex3f(size, -size, size);
    glVertex3f(-size, -size, size);
    glVertex3f(-size, -size, -size);
    glVertex3f(size, -size, -size);

    //Top Panel (Pink)
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(size, size, size);
    glVertex3f(-size, size, size);
    glVertex3f(-size, size, -size);
    glVertex3f(size, size, -size);

    glEnd();
}

void idle (void)
{
    glutPostRedisplay();   // trigger display callback
}

void display (void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    // set the camera
    gluLookAt(5.0f, 5.0f, 10.0f,
              0.0f, 0.0f, 0.0f,
              0.0f, 1.0f, 0.0f);

    //TO DO: Draw a cube rather than a square

    // different render mode
    switch ( rendermode ) {

        case 'f': // to display faces
            drawCube(2);
            break;

        case 'v': // to display points
            glBegin(GL_POINTS);
            glColor3f(0.0f,1.0f,0.0f);
            glVertex3f( 1.0f, 1.0f, 1.0f);
            glVertex3f(-1.0f, 1.0f, 1.0f);
            glVertex3f(-1.0f,-1.0f, 1.0f);
            glVertex3f( 1.0f,-1.0f, 1.0f);
            glEnd();

            glPointSize(5);
            break;

        case 'e': // to display edges
            glBegin(GL_LINES);
            glColor3f(0.0f,0.0f,1.0f);

            glVertex3f( -1.0f, 1.0f,1.0f);
            glVertex3f( -1.0f, -1.0f,1.0f);

            glVertex3f( -1.0f, 1.0f,1.0f);
            glVertex3f( 1.0f, 1.0f,1.0f);

            glVertex3f( -1.0f, -1.0f,1.0f);
            glVertex3f( 1.0f, -1.0f,1.0f);

            glVertex3f( 1.0f, -1.0f,1.0f);
            glVertex3f( 1.0f, 1.0f,1.0f);

            glEnd();
            break;
    }



    glutSwapBuffers();
}

/*
 * The reshape function sets up the viewport and projection
 */
void reshape ( int width, int height )
{
    // Prevent a divide by zero error by making height equal 1
    if (height==0)
    {
        height=1;
    }

    glViewport(0,0,width,height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Need to calculate the aspect ratio of the window for gluPerspective
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

    // Return to ModelView mode for future operations
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

/*
 * Callback for standard keyboard presses
 */
void keyboard ( unsigned char key, int x, int y )
{
    switch(key) {
        // Exit the program when escape is pressed
        case 27:
            exit(0);
            break;

        // Switch render mode for v,e,f
        case 'v':
            rendermode='v';
            break;

        case 'e':
            rendermode='e';
            break;

        case 'f':
            rendermode='f';
            break;

        default:
            break;
    }

    glutPostRedisplay();
}



// Arrow keys need to be handled in a separate function from other keyboard presses
void arrow_keys ( int a_keys, int x, int y )
{
    switch ( a_keys ) {
        case GLUT_KEY_UP:
            glutFullScreen();
            break;
        case GLUT_KEY_DOWN:
            glutReshapeWindow(500, 500);
            break;
        default:
            break;
    }
}

void mouseButton(int button, int state, int x, int y)
{
}

void mouseMove(int x, int y)
{
}




/*
 * Entry point to the application
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutCreateWindow("CW1 OpenGL Framework");
//    glutFullScreen();          // Uncomment to start in full screen
    InitGL();
    rendermode='f';

    // Callback functions
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(arrow_keys);  // For special keys
    glutMouseFunc(mouseButton);
    glutMotionFunc(mouseMove);
    glutIdleFunc(idle);

    glutMainLoop();
}

enter image description here

Upvotes: 1

Views: 225

Answers (1)

genpfault
genpfault

Reputation: 52083

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
...
glEnable(GL_DEPTH_TEST);

You kinda need a depth buffer for GL_DEPTH_TEST to work. The OS isn't required to give you a depth buffer if you don't ask for one.

If you capital-N Need a depth buffer make sure explicitly request one:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
                                              ^^^^^^^^^^

Upvotes: 1

Related Questions