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