Objects get overlayed incorrectly

Started by
1 comment, last by Janison 13 years, 7 months ago
Hello,

i have encountered a problem followingly.
First here the projection settings etc.

glMatrixMode(GL_PROJECTION); // select the projection matrix
glLoadIdentity(); // reset the projection matrix

gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,-1.0f,1.0f);

glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
gluLookAt(0, 0, 120, 0, 0, 0, 0, 1, 0);

then I make two triangle which intersect.(here the code)

glPushMatrix();

glTranslatef(0,0,0);
glColor3f(0,0,0);
glBegin(GL_TRIANGLES);

glVertex3f(0.0,15.0,10);
glVertex3f(15.0,0.0,10);
glVertex3f(15.0,15.0,10);
glEnd();

glPopMatrix();
glPushMatrix();
glTranslatef(0,0,0);
glColor3f(1,1,1);
glBegin(GL_TRIANGLES);
glVertex3f(0.0,40.0,0);
glVertex3f(0.0,0.0,0);
glVertex3f(40.0,40.0,0);
glEnd();
glPopMatrix();

But now if I change the z-value of one triangle to lets say 10.(s.code)
The black triangle should be displayed on top of the white one, thus should be moved towards me.

But the thing what happens is, that I will see the white one on top, although when I turn the model around I will see that the black one was correctly moved towards me. (I have a function for turning)

But for some reason the color of objects underneath dominate upper objects' colors.Why?

I am also not really sure about the gluperspective thing.
But Everything is displayed correctly so far except for this z-axis movement things.


Advertisement
Try adding glEnable(GL_DEPTH_TEST); to your initilization, and make sure you specified a depth buffer and bit depth when you created your opengl window/instance.

Ok wait.
I already have that included.

Here the rest of my initialisation:


glClearDepth(1.0f); // depth buffer setup
glEnable(GL_DEPTH_TEST); // enables depth testing
glDepthFunc(GL_LEQUAL); // the type of depth test to do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); /

glEnable(GL_CULL_FACE);
glEnable(GL_LINE_SMOOTH);


glShadeModel(GL_SMOOTH); // enables smooth shading


The problem remains the same.

This topic is closed to new replies.

Advertisement