PDA

View Full Version : Strange Problem GL_DEPTH_TEST



ketenev
03-31-2013, 05:39 AM
Hello there ! My name is Kliment and i came here to ask you a question about the Open GL ...
I have a very strange problem ....So the problem is that i am drawing 4 polygons with different color in the 3d space but i do not know why these polygons appear to be drawn one after another .... For example if we draw polygon at 0,0,0 and after that one at 0,0,-1 in theory when enabling GL_DEPTH_TEST the second polygon must appear behind the first one , but it does not .... Here is my code take a look at it please if you have to compile it ....but please help me solve this ...

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>

void Init (void)
{
glClearColor (0.0,0.0,0.0,1.0);
glShadeModel (GL_SMOOTH);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);

glEnable(GL_BLEND); //enabling or disabling this does not make any difference
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}

void Display (void)
{
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

gluLookAt (7.0,1.0,-7.0, 10.0,0.5,-10.0, 0.0,1.0,0.0);

/////////////////////Start Draw

//The red one is first
glPushMatrix ();
glTranslatef (10.0,0.0,-10.0);
glColor4f (1.0,0.0,0.0,1.0);
glBegin (GL_POLYGON);
glVertex3f (0.0,0.0,0.0);
glVertex3f (1.0,0.0,0.0);
glVertex3f (1.0,1.0,0.0);
glVertex3f (0.0,1.0,0.0);
glEnd ();
glPopMatrix ();

//Second the green one
glPushMatrix ();
glTranslatef (9.0,0.0,-10.0);
glColor4f (0.0,1.0,0.0,1.0);
glBegin (GL_POLYGON);
glVertex3f (0.0,0.0,0.0);
glVertex3f (1.0,0.0,0.0);
glVertex3f (1.0,1.0,0.0);
glVertex3f (0.0,1.0,0.0);
glEnd ();
glPopMatrix ();

//The blue one
glPushMatrix ();
glTranslatef (10.0,0.0,-10.0);
glRotatef (90,0.0,1.0,0.0);
glColor4f (0.0,0.0,1.0,1.0);
glBegin (GL_POLYGON);
glVertex3f (0.0,0.0,0.0);
glVertex3f (1.0,0.0,0.0);
glVertex3f (1.0,1.0,0.0);
glVertex3f (0.0,1.0,0.0);
glEnd ();
glPopMatrix ();

//the yellow one
glPushMatrix ();
glTranslatef (10.0,0.0,-9.0);
glRotatef (90,0.0,1.0,0.0);
glColor4f (1.0,1.0,0.0,1.0);
glBegin (GL_POLYGON);
glVertex3f (0.0,0.0,0.0);
glVertex3f (1.0,0.0,0.0);
glVertex3f (1.0,1.0,0.0);
glVertex3f (0.0,1.0,0.0);
glEnd ();
glPopMatrix ();

glutSwapBuffers ();
glutPostRedisplay ();
}

void Reshape (int w,int h)
{
glViewport (0,0,(GLsizei)w,(GLsizei)h);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (40,1,0,30);
glMatrixMode (GL_MODELVIEW);
}

/////////////////////////////////////////////////////

int main (int argc,char *argv[])
{
glutInit (&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGBA);
glutInitWindowSize (500,500);
glutCreateWindow ("Open GL TEST");
Init ();
glutDisplayFunc (Display);
glutReshapeFunc (Reshape);
glutMainLoop();

return 0;
}

cireneikual
03-31-2013, 06:47 AM
Your depth function is set to GL_ALWAYS, so it will always pass the depth test. Make it GL_LESS or GL_LEQUAL.

ketenev
04-01-2013, 12:41 AM
No GL_LESS...it dosen't work ...neither GL_LEQUAL ...But if i am not wrong if there is no glDepthFunc() with a parameter ..its set to default GL_LESS right ?