rect is not seen behind the other

i am using a perspective projection.
now i am drawing a white rectangle(using LINE_LOOP) in the middle of the screen.
and then i am translating the z axis 1 point back
( glTranslate(0,0,-1)) and then i am drawing the same rectangle using the red color(again , in the middle of the screen).

now , Logicly , i am not supposed to see the
second rectangle , because the white
rectangle has to cover it , but what i see
infront of me is the red rect smaller then the
white rect and the red is like above the white one.

i know that the red Rect should be smaller
because it is more far from the point of view.
but but why the white rect is apearing behind
the red one, the red one should be covered.

Help please, i don’t know what to do.
I am new to Opengl , and this is a basic thing.

ensure you’ve enabled depth testing and writes to the depth buffer.


Also, make sure you’ve requested a Pixel Format Descriptor with a depth buffer.

I did not understand a word of everything u just said

OpenGL doesn’t do any depth testing without you saying it to do so. Depth test will enable GL to automatically ‘avoid’ the pixels that are more far from others. So, with this way, you’ll see only what’s visible.

If you use glut, initialize glut with GLUT_DEPTH, then follow what’s Aeluned said.

If you still don’t understand, I just can advise you to read some docs about GL (there are plenty on this website, or check Nehe’s tuts or also buy a book).

You are using GL_LINE_LOOP, so you are not drawing the full rect, but only its border. If you want the rect to obscure things behind it, you have to draw a filled rect, using GL_QUADS for example.