Problem with glEnable(GL_DEPTH_TEST)

Started by
8 comments, last by marcClintDion 10 years, 9 months ago

Hello guys,

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

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);
    glLoadIdentity();
    gluPerspective(45.0f, fAspect, 0.1, 425.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

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 .

Advertisement

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

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

No :/

No :/

Then depth testing is working fine, and you're rectangle really is in front of your .obj. Note that your rectangle has a depth of -2.5 (translate -300.0f + vertex depth of 297.5 = -2.5). That means your .obj needs be somewhere between 0 and -2.5 if it's supposed to be in front of the rect (that is, each one of its vertices needs to have a depth >= 297.5), which isn't a lot of room to work with. Try setting the rectangle's vertex depth to 0 (i.e. in glVertex3f make the last parameter 0).

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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

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

initial_Situation.png

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

fish_Coordinates.png

And if I use the rectangles vertex as zero:

vertices_Zero.png

You don't have both models isolated with the glPushMatrix(); and glPopMatrix calls. Because of this, your cube transformations are being applied to the fish as well. If you move the cube back, the fish moves back as well. I'm not sure why you define the cube's Z-axis to be 297.5 but this is a huge number compared to the other sizes and will draw your cube roughly 300 units towards the screen, this is kind of like setting the cube's position to be in your neighbors house behind you. Also the fish has no glTranslate call so this also prevents it from moving independent of the cube. If you want to move them separately, they should look like the following.

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

glPushMatrix(); glTranslatef(0.0f, 0.0f, -300.0f); //Now this call will move only the cube. 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();

glPopMatrix(); This isolates the cube transforms from the fish

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

//Fish glPushMatrix(); glTranslatef(0.0f, 0.0f, moveFish_Z); This call will allow you to move the fish glRotatef(-300.0, 0.0f, 1.0, 0.166); //_Because the X,Y,Z axis values are proportional to one another, what I've entered here is equivalent. I divided 50/300 to get this. glScalef(0.3, 0.3, 0.3); glColor3f(1.0f, 0.0f, 0.0f);

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(); This has been moved up before the fish draw routines and highlighted blue

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

Also, the values entered as following will cause you problems-> glRotatef(-300.0, 0.0f, 300.0f, 50.0f); The values that I've highlighted with red should be within the 0.0 to 1.0 range, at least until you fully understand them. Making one of these values much bigger has the same effect as making the other ones much smaller. They are weighted values that control which axis the rotation takes place about, they are proportional to one another. It might help to think of them as having behavior similar to booleans even though they are floats. For example.

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this first example: glRotatef(-300.0, 1.0, 0.0, 0.0); , The angle is 300 degrees. The X_AXIS is 1.0 so it is 'ON'. But there is a catch here, this rotate function is not perfect. And these values are weighted against one another. The X_Axis would still be 'ON' even if the X value were only 0.001, so long as the others are 0.0.

The following would be equivalent: glRotatef(-300.0, 0.0001, 0.0, 0.0); You will still have a rotation of -300 degrees about the X_Axis, but only until you change the values of Y and Z.

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this second example: glRotatef(-300.0, 1.0, 0.5, 0.0);

Here you would expect that the rotation should be

X_Axis: 300 degrees. //Since the X_Axis is at 1.0, we'd expect this to be 100% rotation about the X_axis

Y_Axis 150 degrees. //Since the Y_Axis is at 0.5, we'd expect this to be 50% rotation about the Y_axis

Unfortunately it does not work like this. This is why I say that these values are 'weighted'. Increasing one of them, decreases the power of the others.

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Setting these values much higher than 1.0 should be avoided until you fully grasp what's taking place here. You will introduce 'bugs', you won't damage anything but you will now get odd behavior from the other axis values.

For example, if you set the rotate function as follows glRotatef(-300.0, 10000.0, 10.0, 0.0); All of the rotation will be around the X_Value, The Y_Axis value will have absolutely no visual effect, it may as well be zero since the X_Value is so high. It would be equivalent to having glRotatef(-300.0, 1.0, 0.001, 0.0);

Now if you try to add rotation about another axis, you will have to increase the other axis values to a super large number as well to see any effect. This is why I said to keep them around 0.0 to 1.0.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

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.

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.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

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

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.

Consider it pure joy, my brothers and sisters, whenever you face trials of many kinds, 3 because you know that the testing of your faith produces perseverance. 4 Let perseverance finish its work so that you may be mature and complete, not lacking anything.

This topic is closed to new replies.

Advertisement