PDA

View Full Version : Simple Depth Problem



mehran20
07-02-2005, 09:06 PM
Well, I think it's a stupid question but it's driving me crazy.
How can I force openGL not to show the objects that are behind another one? I think
glEnable( GL_DEPTH_TEST );is necessary but even though the far objects can be seen through the near ones!!
Can anyone guide me with the necessary commands for preventing this?

T101
07-02-2005, 11:23 PM
You need to actually have a depth buffer.
When the context is created, that needs to be specified.

Also, if you're setting a perspective projection, be sure to have a zNear greater than 0.

mehran20
07-03-2005, 03:02 AM
Can you tell me how? I mean the exact code.. Fortunately I'm in a hurry.

Thanks

mehran20
07-03-2005, 04:19 AM
Here's the code I'm using:
glFrontFace(GL_CCW);
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glDepthRange( -1000, 1000 );
glDepthMask(GL_TRUE);Well, what do you think?

mehran20
07-03-2005, 10:11 PM
Well it seems the information that I gave was not enough, but since I really need help ASAP, I'm gonna ask for help again. And this time with more information:

GLuint PixelFormat;

PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16;
pfd.cDepthBits = 16;

Format[i] = ChoosePixelFormat(hdc[i], &pfd);
SetPixelFormat(hdc[i], Format[i], &pfd);
hrc[i] = wglCreateContext(hdc[i]);
wglMakeCurrent(hdc[i], hrc[i] );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

GLfloat mat_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat mat_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glEnable(GL_COLOR_MATERIAL);
glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );


GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT, GL_SHININESS, 128);


glFrontFace(GL_CCW);
glEnable(GL_POLYGON_SMOOTH);
glEnable( GL_LIGHTING );
glDepthFunc( GL_LESS );
glDepthRange( -1000, 1000 );
glEnable( GL_DEPTH_TEST );
glShadeModel(GL_SMOOTH);


GLfloat diffuse_light0[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position_light0[] = { 0.5f, -0.5f, 0.5f, 0.0f };
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_light0 );
glLightfv( GL_LIGHT0, GL_POSITION, position_light0 );
glEnable(GL_LIGHT0);

GLfloat diffuse_light1[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat position_light1[] = { 1.3f, -0.5f, 0.2f, 0.0f };
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_light1 );
glLightfv( GL_LIGHT1, GL_POSITION, position_light1 );
glEnable(GL_LIGHT1);

GLfloat ambient_lightModel[] = { 0.75f, 0.75f, 0.75f, 1.0f };
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, ambient_lightModel );

GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f };
GLfloat lightPos[] = { 0.0f, 0.0f, -75.0f, 1.0f };
GLfloat angle = 60.0;

glLightfv(GL_LIGHT4, GL_POSITION, lightPos);
glLightfv(GL_LIGHT4, GL_SPOT_DIRECTION, spotDir);
glLightf(GL_LIGHT4, GL_SPOT_EXPONENT, angle);
glLightf(GL_LIGHT4, GL_SPOT_CUTOFF, 60.0);

glPushAttrib(GL_LIGHTING_BIT );I've used array as for some of the variables, since I have a couple of child windows. And I'm using win32API programming, not MFC ot GLUT windows.
The problem is that the Depth Buffer is not working and I can see the objects behind the others!!

For god's sake, tell me what's my problem
Thanks alot,
Mehran

jide
07-03-2005, 11:24 PM
If you use W32 API, then don't use glut functions !!
Can you post your all program so that we can see what's good or wrong. Things you gave cannot help more.

mehran20
07-04-2005, 12:06 AM
Ok,
Here it is, it's in my Breifcase at yahoo:
OpenGL RMF Simulation (http://f1.pg.briefcase.yahoo.com/bc/mehranziadloo/vwp2?.tok=bcruPlVBTS0BPQhL&.dir=/My+Shared+Documents&.dnm=OpenGL+RMF+Simulation.rar&.src=bc)
I know it's messed up code, in fact I did this two years ago and then I didn't know that much of win32 programming (it doesn't mean that I do now).
To help you understand it, there's a function "SetupRC" in file "Sence.cpp" which initializes a win32-Window to work with OpenGL. I think the problem might be there.
Oh I almost forgot, the project is created by Microsoft Developement Enviroment 2003 Version 7.1.3088

Thanks for your help,
Mehran

jide
07-04-2005, 12:51 AM
I'm not a Windows programmer, so I can't test your program. But I have looked at it. And as I said before, the problem is that you use glut display initialization function merged with W32 code for windowing initialization:



void SetupRC(int i)
{
GLuint PixelFormat;

PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16;
pfd.cDepthBits = 16;

Format[i] = ChoosePixelFormat(hdc[i], &pfd);
SetPixelFormat(hdc[i], Format[i], &pfd);
hrc[i] = wglCreateContext(hdc[i]);
wglMakeCurrent(hdc[i], hrc[i] );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
I don't know if deleting this line (glutInitDisplayMode) will be suffisant, but you really should delete it. Also, maybe pfd.dwFlags should contain something about depth use. Using drawing glut functions should be okay anyway.

Your drawing code is well for depth test to work.
Also try to test gl errors wherever you can.

Hope that helps.

mehran20
07-04-2005, 12:58 AM
Thanks but that line is not the problem, I added that to see if it helps, but it didn't.
I think you're right I should check it by glError.

Thanks anyway,
Mehran

Ehsan Kamrani
07-04-2005, 01:51 AM
Mehran,
Take a look at lesson one of the NeHe home page: nehe.gamedev.net
-Ehsan-

mehran20
07-04-2005, 02:12 AM
Well I did take look at it, and according to first lesson of nehe.gamedev.net I did alright. But yet Depth Bufferring is not working and I can see the object behind other ones!!

Relic
07-04-2005, 03:30 AM
You're requesting a depth buffer with 16 bits and set a depth range from -1000 to 1000. If that is not asking for depth fighting then I don't know.
Choose a 24 bit depth buffer, leave the depth range in the default 0.0 to 1.0 range and set your perspective to the minimum size around your geometry. Push the zNear value as far out as you can to get the zFar/zNear ratio small.
Remove the glEnable(GL_POLYGON_SMOOTH), this is giving trouble on many implementations, too.

mehran20
07-04-2005, 04:36 AM
Yeah baby,
That did it. Thanks alot. It worked, the fact is I never read about glDepthRange or what it does. God bless you.

Thanks again,
Mehran