Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Simple Depth Problem

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    15

    Simple Depth Problem

    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
    Code :
      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?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jan 2004
    Posts
    314

    Re: Simple Depth Problem

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    15

    Re: Simple Depth Problem

    Can you tell me how? I mean the exact code.. Fortunately I'm in a hurry.

    Thanks

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    15

    Re: Simple Depth Problem

    Here's the code I'm using:
    Code :
    	glFrontFace(GL_CCW);
    	glEnable( GL_DEPTH_TEST );
    	glDepthFunc( GL_LESS );
    	glDepthRange( -1000, 1000 );
    	glDepthMask(GL_TRUE);
    Well, what do you think?

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    15

    Re: Simple Depth Problem

    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:
    Code :
    	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

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: Simple Depth Problem

    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.

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    15

    Re: Simple Depth Problem

    Ok,
    Here it is, it's in my Breifcase at yahoo:
    OpenGL RMF Simulation
    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

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: Simple Depth Problem

    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:

    Code :
    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.

  9. #9
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    15

    Re: Simple Depth Problem

    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

  10. #10
    Senior Member Frequent Contributor ehsan2004's Avatar
    Join Date
    Jun 2004
    Location
    Iran
    Posts
    645

    Re: Simple Depth Problem

    Mehran,
    Take a look at lesson one of the NeHe home page: nehe.gamedev.net
    -Ehsan-

Page 1 of 2 12 LastLast

Similar Threads

  1. Nvidia Simple Depth Float tutorial
    By Mars_999 in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 07-10-2008, 05:15 AM
  2. Simple problem
    By starman in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-04-2007, 11:21 PM
  3. simple depth test question
    By jide in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-15-2005, 09:37 AM
  4. simple problem i cant fix
    By stichedleg in forum OpenGL: Windows
    Replies: 2
    Last Post: 12-12-2004, 12:23 PM
  5. a simple problem
    By thunder2001 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-31-2004, 03:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean