Followers 0

# Problem with glEnable(GL_DEPTH_TEST)

## 9 posts in this topic

Hello guys,

I have a .obj, and a rectangle, but even with glEnable(GL_DEPTH_TEST), obj is always behind the rectangle

I translated my obj to a point very close to the screen, but even though it is behind the rectangle.

main():

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

ChangeSize():

void ChangeSize(GLsizei w, GLsizei h){
GLfloat fAspect;
if (h == 0){
h = 1;
}
glViewport(0,0,w,h);

fAspect = (GLfloat)w/(GLfloat)h;
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, fAspect, 0.1, 425.0);

glMatrixMode(GL_MODELVIEW);
}

RenderScene():

void RenderScene(void){
glEnable(GL_COLOR_MATERIAL);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

glPushMatrix();

glTranslatef(0.0f, 0.0f, -300.0f);
glEnable(GL_DEPTH_TEST);

//Rectangle
glBegin(GL_POLYGON);
glColor4f(0.0,  0.0, 1.0, 1.0);
glVertex3f(-0.8, 0.5, 297.5 );
glVertex3f(0.9, 0.5, 297.5 );
glVertex3f(0.9, -0.2, 297.5 );
glVertex3f(-0.8, -0.2, 297.5 );

glEnd();

//Fish
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
glRotatef(-300.0, 0.0f, 300.0f, 50.0f);
glScalef(0.3, 0.3, 0.3);
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D,texId1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glCallList(displayList1);
glDisable(GL_TEXTURE_2D);
glPopMatrix();

glPopMatrix();

glutSwapBuffers();
}

I would really appreciate some help .

0

##### Share on other sites

Does changing the draw order change which one is on top of the other?

0

No :/

0

##### Share on other sites

I understand what you said, but... here are some screens to help:

This is the situation that made me create a new topic:

Now, if I translate the fish to 297.5 or 298, using glTranslatef(0.0f, 0.0f, 297.5f):

And if I use the rectangles vertex as zero:

0

##### Share on other sites

marc, thanks a lot for the big answer :)

Everything you said makes sense, but could you give me an example of moveFish_Z coordinate?

Because with the glTranslate(0.0f, 0.0f, -300.0f) before the rectangle, shouldn't glTranslate(0.0f, 0.0f, -350.0f) make the fish appear first?

They are already isolated by glPush and glPop matrix.

0

##### Share on other sites

If your code is still as it is up above then they are not isolated.  Everything that you do to the rectangle will also happen to the fish.

Once you get the Push and Pops set up correctly then all you have to do is move them around with a keyboard control.  I think that you should set your z values to something more reasonable.  Right now the rectangles Z values are more than 30x bigger than the model's X and Y positions.  Until you get this straightened out you might want to dial the Z positions down to something like 0.5, or 1.0. so they are proportional with everything else, at least until you've got the setup working properly.

0

##### Share on other sites

When I said "they are already isolated" I meant: I already did what you said (isolate the fish from the rectangle)

Thats why I posted again, because even with this change, the rectangle is still in front of the fish :/

With the glTranslate(0.0f, 0.0f, -300.0f) before the rectangle, shouldn't glTranslate(0.0f, 0.0f, -350.0f) before the fish make it appear first?

And yes, I need to set things correctly. The problem is that I got stuck in this problem... after I manage it, I have a lot of things to rewrite

0

##### Share on other sites

I think that -350 should put the fish behind the rectangle if the rectangle is at -300.  Negative should be away from you and positive should be towards you.

Try adding an increment variable to move things around while the render loop is running.

GLfloat moveFish = 0.0;

void RenderScene(void)

{

moveFish += 0.1;

glPushMatrix();

glColor3f(1.0f, 0.0f, 0.0f); glTranslate(0.0f, 0.0f, moveFish//This will keep moving the fish 'towards you'.

glRotatef(-300, 0.0f, 0.0f, 1.0f);    glScalef(0.3, 0.3, 0.3);    glEnable(GL_TEXTURE_2D);   glBindTexture (GL_TEXTURE_2D,texId1);   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);    glCallList(displayList1);   glDisable(GL_TEXTURE_2D);  glPopMatrix();

}

//=======================================================================================

And also, I see that you are still using gigantic numbers for the Z positions.  Again, try setting the Z's to something at little more reasonable like 1 or 2 for now.  I can't make any sense of why you would use numbers like 300 and 350???  The rectangle is less than 2 units wide and 1 unit high.

0

## Create an account

Register a new account