Quads with no depth sorting?

I’ve got 4 cubes made of quads rotating around 0,0,0. The problem is, that the quads appear in the correct order when I render them, but it looks like the cubes are rendered one after the other in a way, that the newly drawn cubes are drawn in front of the old ones.
It’s like no depth sorting was used - but only when rendering the cubes (and not the quads of a cube).

I’ll post a piece of my code (as short as possible), maybe someone will find something I haven’t.

[i]
In “MAIN”:

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();
glMatrixMode( GL_PROJECTION );glPushMatrix();glLoadIdentity();
gluPerspective( 80, 1.3, 0,1000.0 );
gluLookAt(0, 0, 5, 0,0,0, 0,1,0);

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glTranslated(0,0,0);
glRotated(rot,1,0,0);
draw_cubes();

glMatrixMode(GL_PROJECTION);glPopMatrix();
glMatrixMode(GL_MODELVIEW);glPopMatrix();
SwapBuffers(DCvalue);
[/i]

In the init() I also wrote:
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);

I ‘stole’ the normals and the vertices’ data from the NeHe ‘Missile mapping’ lesson, so, they should be correct:
Quad 1:
normal1=0;normal2=0;normal3=1;
texcoord11=0;texcoord12=0;
texcoord21=1;texcoord22=0;
texcoord31=1;texcoord32=1;
texcoord41=0;texcoord42=1;
x1= 0+x; y1= 0+y; z1=5;
x2= 5+x; y2= 0+y; z2=5;
x3= 5+x; y3= 5+y; z3=5;
x4= 0+x; y4= 5+y; z4=5;

Quad 2:
normal1=0;normal2=0;normal3=-1;
texcoord11=1;texcoord12=0;
texcoord21=1;texcoord22=1;
texcoord31=0;texcoord32=1;
texcoord41=0;texcoord42=0;
x1= 0+x; y1= 0+y; z1= 0;
x2= 0+x; y2= 5+y; z2= 0;
x3= 5+x; y3= 5+y; z3= 0;
x4= 5+x; y4= 0+y; z4= 0;

And so on…

I know, that drawing 2 cubes is a basic thing to do and I’ve done harder tricks already, but now, I’m really stuck… I think I missed someting.

Please, someone help me :frowning:

0 is not valid for zNear in gluPerspective…

Do you realize that you are building the composite MVP on the projection matrix stack?

You mean, I should change the lines like:

glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
gluPerspective( 80, 1.3, 0,1000.0 );
gluLookAt(0, 0, 20,
0,0,0,
0,1,0);

What does MVP mean?

Try this

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 80, 1.3, 1, 1000.0 );

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

The transforms are applied to the currently active matrix stack.

You don’t need to push/pop the initial matrices, unless you just want to…

MVP = (M)odel(V)iew + §rojection

I mean MVP is the product of the modelview and projection matrices (in case someone mistakes string concatenation with a matrix operation).

Thanks. I tried it. Now it looks like:
[i]
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 80, 1.3, 0,1000.0 );

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

glDisable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glTranslated(0,0,0);
glRotated(rot,1,0,0);
draw_cubes();

glEnable(GL_LIGHTING);

SwapBuffers(DCvalue);
[/i]
But nothing changed. It is the same bad result…

You still have a 0 for zNear!

:rolleyes: :rolleyes: :rolleyes: :rolleyes:

Oops. Missed your gluperspective line change. Now it’s ok. It works.
I rewrote the code in its original state just to see how it should have worked. The problem was really this line…

I knew, that I just missed something. You have better eyes than I do. Thanks again.

Yeah, projection matrix abuse like that works, but you are hosed when it comes to fixed-function lighting and texgen since opengl relies on the contents of the modelview matrix for various things. Just something to keep in mind…