Allegro 5 + OpenGL + GL_DEPTH_TEST problem

Hi guys!
Im new to Allegro5 and OpenGL. I was really exicted when i got my first 3D cube to be drawn. but yesterday I got really depressed when I got this problem… I’ve googled it like 3000000 times but it seems nobody else has this horrible problem.

Im uploading a screenshot:

imageshack.us/photo/my-images/194/crapx.png/
opengl forums won’t let me post the picture…

As u can see it must be related with the way I configure the DEPTH BUFFER… but I really dont know what I’m doing wrong.

I’m using Code::Blocks 12.11 C++ with MINGW 4.7 and the I’m not sure what openGL version im using but it’s the one provided with MINGW 4.7

And here is my whole code:


#include <stdio.h>
#include <allegro5/allegro.h>
#include <gl/glu.h>
#include <math.h>
void DrawCube(void);

int main(int argc, char **argv){
    bool finish=false, draw= true;
    const float FPS = 60.0;
    int resX=1280,resY=720;
    double Angle=90, Ratio=float(resX)/float(resY),ClippingBorder1=0,ClippingBorder2=100;
    float x=0,z=0, y=0,rotX=0,incl=0,moveSpeed = 0.1;

    ALLEGRO_DISPLAY *display = NULL;
    if(!al_init()) {
        fprintf(stderr, "failed to initialize allegro!
");
        return -1;
    }


    al_set_new_display_flags(     ALLEGRO_OPENGL    );
    al_set_new_display_option(  ALLEGRO_COLOR_SIZE,  32,  ALLEGRO_REQUIRE);
    al_set_new_display_option(  ALLEGRO_DEPTH_SIZE,  24,  ALLEGRO_REQUIRE);
    al_set_new_display_option(  ALLEGRO_STENCIL_SIZE,  8,  ALLEGRO_REQUIRE);
    al_set_new_display_option(  ALLEGRO_AUX_BUFFERS,  0,  ALLEGRO_REQUIRE);
    al_set_new_display_option(  ALLEGRO_SAMPLE_BUFFERS,  1,  ALLEGRO_SUGGEST);
    al_set_new_display_option(  ALLEGRO_SAMPLES,  4,  ALLEGRO_SUGGEST);

    display = al_create_display(resX, resY);
    if(!display){
        fprintf(stderr, "failed to create display!
");
        return -1;
    }
    al_clear_to_color(al_map_rgb(0,0,0));
    al_hide_mouse_cursor(display);

    al_install_keyboard();
    al_install_mouse();
    ALLEGRO_KEYBOARD_STATE keyState;
    ALLEGRO_MOUSE_STATE mouseState;
    ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
    ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_mouse_event_source());
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_start_timer(timer);

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

//glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
//glDepthRange(0.0f, 1.0f);
glShadeModel(GL_FLAT);

    while(!finish){

        ALLEGRO_EVENT events;
        al_wait_for_event(event_queue, &events);

        if(events.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(events.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                finish = true;
            }
        }
        if(events.type == ALLEGRO_EVENT_TIMER)
        {
            al_get_keyboard_state(&keyState);
            al_get_mouse_state(&mouseState);

            rotX+=(mouseState.x-resX/2)/100.0;
            if(incl>-1.45 && incl<1.45)
                incl-=float(mouseState.y-resY/2)/200.0;
            else{
                if(incl<-1.45)incl=-1.44;
                if(incl>1.45)incl=1.44;

            }
            
            if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
                rotX += 0.1;
            if(al_key_down(&keyState, ALLEGRO_KEY_LEFT))
                rotX -= 0.1;
            if(al_key_down(&keyState, ALLEGRO_KEY_UP)){
                if(incl<1.45)incl += 0.1;//limitamos el angulo max de inclinación a PI/2
            }
            if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)){
                if(incl>-1.45)incl -= 0.1;//limitamos el angulo min de inclinación a -PI/2
            }
            if(al_key_down(&keyState, ALLEGRO_KEY_A)){
                x-=moveSpeed*cos(rotX+1.57);
                z-=moveSpeed*sin(rotX+1.57);
            }
            if(al_key_down(&keyState, ALLEGRO_KEY_D)){
                x+=moveSpeed*cos(rotX+1.57);
                z+=moveSpeed*sin(rotX+1.57);
            }
            if(al_key_down(&keyState, ALLEGRO_KEY_W)){
                x+=moveSpeed*cos(rotX);
                z+=moveSpeed*sin(rotX);
            }
            if(al_key_down(&keyState, ALLEGRO_KEY_S)){
                x-=moveSpeed*cos(rotX);
                z-=moveSpeed*sin(rotX);
            }
            if(al_key_down(&keyState, ALLEGRO_KEY_Z))
                Angle++;
            if(al_key_down(&keyState, ALLEGRO_KEY_X))
                Angle--;

            al_set_mouse_xy(display, resX/2, resY/2);
            draw = true;
        }

        if(draw)
        {
            draw = false;
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);

            glMatrixMode(GL_PROJECTION); //Open the projection matrix as current
            glLoadIdentity(); //clear its contents to an identity matrix (which doesn't change anything when applied)
            gluPerspective( Angle, Ratio, ClippingBorder1, ClippingBorder2);
            glMatrixMode(GL_MODELVIEW); //select the matrix
            glLoadIdentity(); //clear ...
            gluLookAt (x,y,z,x+cos(incl)*cos(rotX),y+sin(incl),z+cos(incl)*sin(rotX),0,1,0);//CameraX , CameraY, CameraZ, SceneX, SceneY, SceneZ, UpVectorApexX, UpVectorApexY, UpVectorApexZ );

            DrawCube();                //Draw CUBE
            glBegin(GL_QUADS);         // Draw SINLGE FACE behing CUBE

            glColor3f(0.0f,1.0f,0.0f);         // Color Red
            glVertex3f( 1.0f, 1.0f, -3.0f);    // Top Right Of The Quad (Front)
            glVertex3f(-1.0f, 1.0f, -3.0f);    // Top Left Of The Quad (Front)
            glVertex3f(-1.0f,-1.0f, -3.0f);    // Bottom Left Of The Quad (Front)
            glVertex3f( 1.0f,-1.0f, -3.0f);    // Bottom Right Of The Quad (Front)
            glEnd();

            glFlush();
            al_wait_for_vsync();
            al_flip_display();
            al_clear_to_color(al_map_rgb(0, 0, 0));
        }
    }

    printf("Finalizado por el usuario.");
    al_rest(1.0);
    al_destroy_display(display);
    al_destroy_timer(timer);
    al_destroy_event_queue(event_queue);
    return 0;
}


void DrawCube(void)
{
        // Draw The Cube Using quads
        glColor3f(0.0f,1.0f,0.0f);    // Color Blue
        glBegin(GL_QUADS);
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Top)
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Bottom Left Of The Quad (Top)
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Bottom Right Of The Quad (Top)
glEnd();
        glColor3f(1.0f,0.5f,0.0f);    // Color Orange
        glBegin(GL_QUADS);
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Top Right Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Top Left Of The Quad (Bottom)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Bottom)
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Bottom)
glEnd();
        glColor3f(1.0f,0.0f,0.0f);    // Color Red
        glBegin(GL_QUADS);
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Front)
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Front)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Front)
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Front)
glEnd();
        glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
        glBegin(GL_QUADS);
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Top Right Of The Quad (Back)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Top Left Of The Quad (Back)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Bottom Left Of The Quad (Back)
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Bottom Right Of The Quad (Back)
glEnd();

        glColor3f(0.0f,0.0f,1.0f);    // Color Blue
        glBegin(GL_QUADS);
        glVertex3f(-1.0f, 1.0f, 1.0f);    // Top Right Of The Quad (Left)
        glVertex3f(-1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f,-1.0f);    // Bottom Left Of The Quad (Left)
        glVertex3f(-1.0f,-1.0f, 1.0f);    // Bottom Right Of The Quad (Left)
glEnd();
        glColor3f(1.0f,0.0f,1.0f);    // Color Violet
        glBegin(GL_QUADS);
        glVertex3f( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Right)
        glVertex3f( 1.0f, 1.0f, 1.0f);    // Top Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f, 1.0f);    // Bottom Left Of The Quad (Right)
        glVertex3f( 1.0f,-1.0f,-1.0f);    // Bottom Right Of The Quad (Right)
  glEnd();            // End Drawing The Cube

}

THANKYOU so much in advance!! Id be so pleased if you can solve this…

Thiê?t kê? thi công trang tri? nô?i thâ?t, cung câ?p nô?i thâ?t cao câ?p
Liên hê?: 08 220 15128

Im sorry but I cant understand anything you say, dont know if it’s intentional spam or just my pc interprets wrong those characters.

EDIT: SOLVED. I got rid of this. I had a mistake setting zNear to 0, I set it to 0.1 and now it works fine.

Try calling glClearDepth(1.0f); before you clear the screen.