Depth map

3216e03e8ddff73a6b3aaa14ddd7830c
0
me_here_me 101 Aug 31, 2007 at 15:03

Hi
I am trying to store the depth may of my mesh into a texture. Unfortunately I am not able to get a depth map out of it.

The code in my display method to produce mesh is:

    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CW);

    glEnable( GL_DEPTH );
    glShadeModel( GL_SMOOTH );

    glColor3f( 1., 0., 0. );
    glUseProgram(meshLight);

    for( unsigned int i=0; i< stlMesh.size(); i++)
    {
        //  dummy = stlMesh[i]->getNormal();
        stlMesh[i]->getVertex( 1, dummy1 );
        stlMesh[i]->getVertex( 2, dummy2 );
        stlMesh[i]->getVertex( 3, dummy3 );
        
        v1.x = dummy2.x - dummy1.x;
        v1.y = dummy2.y - dummy1.y;
        v1.z = dummy2.z - dummy1.z;

        v2.x = dummy3.x - dummy1.x;
        v2.y = dummy3.y - dummy1.y;
        v2.z = dummy3.z - dummy1.z;

        v1 = v1.cross(v2);
        v1 = v1.normalize();
    
        glBegin( GL_TRIANGLES );    
            glNormal3f( -v1.x, -v1.y, -v1.z );

            // vertex 1
            glVertex3f( dummy1.x, dummy1.y, dummy1.z );
            // vertex 2
            glVertex3f( dummy2.x, dummy2.y, dummy2.z );
            // vertex 3
            glVertex3f( dummy3.x, dummy3.y, dummy3.z );
        glEnd();
    }

This works wonderful.

Now, I am trying to store the depth map using the following code but i fail:

glBindTexture(GL_TEXTURE_2D, txDepth0);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 
            fbWidth, fbHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer0);
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, 
            GL_TEXTURE_2D, txDepth0, 0);

        //----------------------------------
        // render depth projection

        glViewport(0, 0, fbWidth, fbHeight);
        glUseProgram(0);


        // render front depth into framebuffer 1
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer0);
        glDepthFunc(GL_GREATER);
        glClearDepth(0.0f);

        glClear(GL_DEPTH_BUFFER_BIT);

        for( unsigned int i=0; i< stlMesh.size(); i++)
        {
            //  dummy = stlMesh[i]->getNormal();
            stlMesh[i]->getVertex( 1, dummy1 );
            stlMesh[i]->getVertex( 2, dummy2 );
            stlMesh[i]->getVertex( 3, dummy3 );
            
            v1.x = dummy2.x - dummy1.x;
            v1.y = dummy2.y - dummy1.y;
            v1.z = dummy2.z - dummy1.z;

            v2.x = dummy3.x - dummy1.x;
            v2.y = dummy3.y - dummy1.y;
            v2.z = dummy3.z - dummy1.z;

            v1 = v1.cross(v2);
            v1 = v1.normalize();
        
            glBegin( GL_TRIANGLES );    
                glNormal3f( -v1.x, -v1.y, -v1.z );

                // vertex 1
                glVertex3f( dummy1.x, dummy1.y, dummy1.z );
                // vertex 2
                glVertex3f( dummy2.x, dummy2.y, dummy2.z );
                // vertex 3
                glVertex3f( dummy3.x, dummy3.y, dummy3.z );
            glEnd();
        }

        glColor3f(1, 1, 1);

the txDepth0 remains filled with 0 :(.

Can someone point out some error that i am making in my code

regards and best wishes

5 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 31, 2007 at 15:41

Why are you using ClearDepth = 0.0 and DepthFunc = GL_GREATER? That doesn’t make sense to me. It should be ClearDepth = 1.0 and DepthFunc = GL_LESS or GL_LEQUAL.

Also, you’ve provided no color buffer attachment for your FBO, so you should probably do glColorMask(false, false, false, false).

3216e03e8ddff73a6b3aaa14ddd7830c
0
me_here_me 101 Aug 31, 2007 at 16:16

thanks for the reply.

You were right. I attached the color buffer with the FBO and also made the changes you proposed and it works.

But just a query if u can answer to me.

The program works with ClearDepth = 1.0 and DepthFunc = GL_LESS but I am not sure if it is doing it right. When I display the txDepth0 to the screen, I get a NOT fully black screen (but its quite dark) with the object in the middle, which is completely black.

I was expecting to get a white screen with a semi black object?

regards

3216e03e8ddff73a6b3aaa14ddd7830c
0
me_here_me 101 Aug 31, 2007 at 17:18

Another question?

If I have enabled depth Test, Do I need to create a depth map as i tried above?

or can i retrieve the depth map by a simple call:

glReadPixels(0,0,fbWidth,fbHeight,GL_DEPTH_COMPONENT,GL_FLOAT,tempdata);

thanks in advance

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Aug 31, 2007 at 18:07

@me_here_me

I was expecting to get a white screen with a semi black object?

Yes that’s what I would expect too. Maybe it is that you are declaring the depth texture as being 32 bits per pixel but it is actually being used as 24/depth + 8/stencil.

It would be interesting to try rebinding that depth-32 texture as an RGBA 8-8-8-8 texture and displaying it to see what is in each channel. I am not sure if OpenGL lets you do this though.

As for your second question, using an FBO is a better way to get the depth in a texture than using glReadPixels. You should only use that if you need to get the data back on the CPU for some reason (e.g. for saving out to a file).

3216e03e8ddff73a6b3aaa14ddd7830c
0
me_here_me 101 Sep 01, 2007 at 16:33

thanks a lot for your help

greetings