Results 1 to 5 of 5

Thread: depth buffer and lighting

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2015
    Posts
    20

    depth buffer and lighting

    Hello, I am trying to understand how work enable depth buffer with glDepthMask(false) and glDepthFunc(GL_GREATER).

    I try to render the first frame with a depth buffer enabled and glDepthMask(true)

    this generate my depth mask to apply to successive render frame.

    the successive frame I translate and update offset variable and translate the object along z negative.
    The object translate result behind the initial object (object render at the first frame with zero offset) so I expected that
    cause glDepthFunc(GL_GREATER) it should be rendered. But I saw that lighting is incorrect and I don't understand
    why? The object without lighting is render as I expected while with light enabled I saw it incorrect. Any idea?

    Code :
    void GLApp::initializeGL()
    {
    	GLint alpha_bits;
    	glGetIntegerv(GL_ALPHA_BITS, & alpha_bits);
     
    	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    	GLfloat mat_shininess[] = { 100.0 };
    	GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
    	GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
    	glClearColor (0.0, 0.0, 0.0, 0.0);
    	glShadeModel (GL_SMOOTH);
     
    	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
    	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
     
    	glEnable(GL_LIGHTING);
    	glEnable(GL_LIGHT0);
    	glPixelStorei(GL_PACK_ROW_LENGTH, 0);
    	glPixelStorei(GL_PACK_ALIGNMENT, 1);
    	glEnable(GL_NORMALIZE);
    	glEnable(GL_POINT_SMOOTH);
    	glShadeModel (GL_SMOOTH);
    	glEnable(GL_DEPTH_TEST);
    	GLenum err = glewInit();
    	if (GLEW_OK != err)
    	{
    		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    	}
    	glClear(GL_DEPTH_BUFFER_BIT);
    	makeCurrent();
    }
     
     
    void GLApp::paintGL()
    {
    	static bool first=true;
    	if (lighting) glEnable(GL_LIGHTING);
    	else glDisable(GL_LIGHTING);
     
    	glClearColor(0.0,0.0,0.0,0.0);
    	glEnable(GL_DEPTH_TEST);
     
    	glClear(GL_COLOR_BUFFER_BIT);
    	glViewport(0,0, this->width(),this->height());
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(60.0, (GLfloat) this->width()/(GLfloat) this->height(), 0.1, farPlane);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	glTranslatef(0,0,-5);
    	if (first) 
    	{	
    		glDepthMask(GL_TRUE);
    		first=false;
    		glColor3f(1,1,1);
        }
    	else 
    	{
    		glTranslatef(0,0,offset);
    		glDepthFunc(GL_GREATER);
    		glDepthMask(GL_FALSE);
       }
    	glutSolidTeapot(1);
     
    	renderText(0,height()-20,QString("offset ")+QString::number(offset));
    	renderText(0,height()-10,QString("farPlane ")+QString::number(farPlane));
    }

    Here the result with and without lighting.
    Attached Images Attached Images

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,675
    Hello, I am trying to understand how work enable depth buffer with glDepthMask(false) and glDepthFunc(GL_GREATER).
    By passing GL_FALSE to glDepthMask, you're saying that you don't want the fragment's depth to be written. This won't stop the depth test comparison, but it does mean that, even if the test passes, the fragment's depth will not be updated with new values.

    That's not necessarily wrong (blending being a useful use-case for this). I just want to make sure that this is what you want to do.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2015
    Posts
    20
    ok I use depth in read only when render after the first pass. But I don't understand why the teapot is complete dark, I expected were illuminated.
    GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 }. What's change in light calculation when I use a depth mask calculate at the first render frame?
    Maybe I'm missing something because the calculation of the light on the fragments seems wrong.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,105
    I suspect that the problem is that the back of the teapot (the part farthest from the viewpoint) is being drawn last.

    If you render with glDepthMask(GL_FALSE), whichever faces are drawn last will overwrite those which are drawn first, regardless of depth. If the back faces are drawn last, you're looking at the inside of the teapot. Unless you enable two-sided lighting (with glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1)), the inside of the teapot will be dark because the normals are pointing away from the light.

    If you disable lighting, there's no difference between front and back faces..

    Try glEnable(GL_CULL_FACE) and glCullFace() (the correct argument depends upon how the teapot is constructed) to avoid drawing back faces.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2015
    Posts
    20
    Quote Originally Posted by GClements View Post
    I suspect that the problem is that the back of the teapot (the part farthest from the viewpoint) is being drawn last.

    If you render with glDepthMask(GL_FALSE), whichever faces are drawn last will overwrite those which are drawn first, regardless of depth. If the back faces are drawn last, you're looking at the inside of the teapot. Unless you enable two-sided lighting (with glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1)), the inside of the teapot will be dark because the normals are pointing away from the light.

    If you disable lighting, there's no difference between front and back faces..

    Try glEnable(GL_CULL_FACE) and glCullFace() (the correct argument depends upon how the teapot is constructed) to avoid drawing back faces.
    I think you are right, although I do not understand why the color of teapot continue to be dark instead of white.

    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 100.0 };
    GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_diffuse_back[] = { 0.0, 0.0, 1.0, 1.0 };
    GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    //glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    /*glPixelStorei(GL_PACK_ROW_LENGTH, 0);
    glPixelStorei(GL_PACK_ALIGNMENT, 1);*/
    glEnable(GL_NORMALIZE);
    glEnable(GL_POINT_SMOOTH);
    glShadeModel (GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
    glMaterialfv ( GL_FRONT_AND_BACK , GL_AMBIENT_AND_DIFFUSE , mat_diffuse );
    and this image, left part is with two side model light and the right part I add glEnable(GL_CULL_FACE)double_lighting_and_cull.jpg

Similar Threads

  1. OpenGL: Draw a depth texture to depth buffer
    By Balrog in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 04-25-2012, 04:53 AM
  2. Depth buffer reading and depth buffer test
    By BurningLegion in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-15-2007, 04:09 AM
  3. depth buffer -> depth texture -> depth buffer
    By Codemonkey in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 12-23-2005, 06:27 AM
  4. why depth buffer save to image file can not display depth information
    By victorswan in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-27-2001, 09:19 PM
  5. Problem with the depth buffer and lighting
    By Handkor in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 03-24-2001, 12:34 AM

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