Jump to content


Depth map


5 replies to this topic

#1 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 31 August 2007 - 03:03 PM

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

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5308 posts
  • LocationSanta Clara, CA

Posted 31 August 2007 - 03:41 PM

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).
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 31 August 2007 - 04:16 PM

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

#4 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 31 August 2007 - 05:18 PM

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

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 5308 posts
  • LocationSanta Clara, CA

Posted 31 August 2007 - 06:07 PM

me_here_me said:

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).
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 me_here_me

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 01 September 2007 - 04:33 PM

thanks a lot for your help

greetings





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users