PDA

View Full Version : making a series of cubes



whitedragon10101
09-14-2006, 08:49 AM
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

Overmind
09-14-2006, 09:05 AM
Just to rule out the obvious: Does your pixel format have a depth buffer, and have you enabled depth test?

whitedragon10101
09-15-2006, 12:45 AM
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);

Overmind
09-15-2006, 04:15 AM
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?

whitedragon10101
09-15-2006, 06:21 AM
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)

Overmind
09-15-2006, 06:28 AM
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);

whitedragon10101
09-15-2006, 07:12 AM
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://img178.imageshack.us/img178/8167/topfc3.jpg (http://imageshack.us)

http://img115.imageshack.us/img115/9577/sidezx2.jpg (http://imageshack.us)

http://img174.imageshack.us/img174/2746/bottommu3.jpg (http://imageshack.us)

whitedragon10101
09-15-2006, 07:20 AM
I have tried:

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

but the results are the same as above.

Overmind
09-15-2006, 12:49 PM
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.

whitedragon10101
09-18-2006, 12:04 AM
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.

mikael_aronsson
09-18-2006, 12:50 AM
Do you want to use glDepthFunc( GL_LEQUAL) ? and not GL_LESS ?

I would assume GL_LEQUAL would give some weird results.

whitedragon10101
09-18-2006, 01:27 AM
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.

darkrappey
09-26-2006, 09:25 PM
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.