making a series of cubes

I have made a stack of cubes each one high with dimensions 50xAxis 50zAxis stacked 5 high (yAxis). However when I rotate the whole thing with the mouse and view it from the sides and underneath I can see through to the other side with rather unaesthetic results.

I have tried to find a solution but I can’t figure out the problem. Any help would be much appreciated.

Thanks

Just to rule out the obvious: Does your pixel format have a depth buffer, and have you enabled depth test?

Posted: Fri Sep 15, 2006 7:43 am Post subject:


I have tried to following but it has drastic consequences. The whole image suddenly looks like its made out of sand with no solid sufaces just dots of colour.

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);

Hmm… could be Z-fighting.

How big are your cubes, and at what distance are your near and far planes?

Can you post a screenshot?

the cubes are 1.0f big and my viewing details are

glViewport(0, 0, (GLsizei) w, (GLsizei) h);
gluPerspective(60.0, 1.0, 0.0, 40.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 200, 20, 0.0, 0.0, 0.0, 0.0, 1, 0.0);

(will just get my pics uploaded to post screenshot)

You have to set a value greater than zero as near plane for gluPerspective. Otherwise you’ll get a division by zero and infinite Z values.

Try for example:
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.1, 40.0);

Here are some screenshots. One from the front which is fine then one from the side where you can see through the layers of cubes, and one from underneath where you can see through to the top.

](http://imageshack.us) [/IMG]

I have tried:

gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.1, 40.0);

but the results are the same as above.

You still have to enable depth test with the code you posted in the third post, and set the near clip plane to something greater than 0.

Yep, I have done both but strangely as I said above when I enable depth test:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);

and set the viewing details to:

glViewport(0, 0, (GLsizei) w, (GLsizei) h);
gluPerspective(60.0, 1.0, 1.0, 40.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 200, 200, 0.0, 0.0, 0.0, 0.0, 1, 0.0);

The picture drawing becomes completely eratic.

Do you want to use glDepthFunc( GL_LEQUAL) ? and not GL_LESS ?

I would assume GL_LEQUAL would give some weird results.

hmm for some reason it has decided to start working. It could be changing the near clipping plane from 0.1 to 1.0.

thanks for your help

The next challenge is making the cubes transparent without all the colours mixing together and turning white.

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

I don’t know why it started working. But i think what was going on, were the normals of the face having an inverted direction.

Example: If you placed the camera inside the cube you would not see any of the walls because the inside faces are not being rendered. OpenGl is under the impression that the inside faces are the out side faces. (atleast on the bottum face)

Anyway, to fix the normals, reverse the order in which you order the verticesfor the buttom face. Or simply tell OpenGl to render both sides using the obove call.