FBO:How to get the depth data only?

Started by
10 comments, last by Neutrinohunter 16 years, 2 months ago
Hello everyone, I'm trying to do shadow maping with FBO in GLSL. I just want to get the depth buffer of the light's view Following is my code.

glGenFramebuffersEXT(1, &fb);// frame buffer
glGenTextures(1, &depth_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0,GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
                          GL_DEPTH_ATTACHMENT_EXT,
                           GL_TEXTURE_2D, depth_tex, 0);

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

draw scene here....

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

//preparing to pass this depth texture to shader

glUseProgramObjectARB(my_program);
int my_shadow=glGetUniformLocationARB(my_program,"shadowMap");

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, depth_tex);
glUniform1i(my_shadow,2);

glActiveTexture(GL_TEXTURE0);
glUseProgramObjectARB(0);

glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);

Above is my code's sructure. Did I make some mistake?? I just want to get this scene's depth informations. But my result is always black.(shadow map test failed) always false..I really don't know whether to get only depth infos correctly. By the way!! In my shader code ,the value's xyz component in shadow map used to do in depth test(shadow maping) gives the same result(black..).And the w comp. gives the anoher(almost no shadow). I guess there may be several possibility 1. did I get the depth data in wrong method? But my code is almost same with the spec:framebuffer_object.txt 2.Did I transfer this depth texture to shader correctly? I think so.. 3. In shader, the accpeting type is sampler2D. I compare the xyz compo. gives same reslut. Which component I should use to do depth test? Please help me!!!plase!! Thanks in advance.
Advertisement
Maybe this line is a problem
glDrawBuffer(GL_NONE);

Instead of sampler2D, try sampler2DShadow
Instead of texture2D(tex, texcoord), try shadow2D(tex, texcoord)
Look in the GLSL lang spec.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
if you were interested in already written code please check
http://dbfinteractive.com/index.php?topic=2682.0
shader code is corrected later by taj.
code is writen mainly for size, so there possibly may be some improvements for speed.

i've read somewhere ( forgot where :) ) that you might need to specify the amount of bits used by the depth map.
Quote:Original post by V-man
Maybe this line is a problem
glDrawBuffer(GL_NONE);

Instead of sampler2D, try sampler2DShadow
Instead of texture2D(tex, texcoord), try shadow2D(tex, texcoord)
Look in the GLSL lang spec.


Thanks for your help.
these several version I've tried. But it still no work..gives same result.
So I guess I got the wrong depth texture. But I can't figure it out

Quote:Original post by frea
if you were interested in already written code please check
http://dbfinteractive.com/index.php?topic=2682.0
shader code is corrected later by taj.
code is writen mainly for size, so there possibly may be some improvements for speed.

i've read somewhere ( forgot where :) ) that you might need to specify the amount of bits used by the depth map.


I will read it , thanks!!
I post my shader code below,
Hope someone can help me.
Because I've completed a shadow mapping(use glReadPixel, but this time use FBO),I didn't change any detail to my shader code.


//vertexuniform mat4 lightModel;uniform mat4 lightProj;uniform mat4 eyeModelInv;varying vec4 ShadowCoord ;void main(){		ShadowCoord	= lightProj * lightModel * eyeModelInv * gl_ModelViewMatrix  * gl_Vertex;											gl_Position	= ftransform();	gl_FrontColor = gl_Color;}//frag//uniform sampler2DShadow shadowMap;uniform sampler2D shadowMap;varying vec4 ShadowCoord ;void main(){	vec4 sdwCoor = ShadowCoord/ ShadowCoord.w;	sdwCoor = sdwCoor/2.0 + 0.5;		vec4 shadow = texture2D(shadowMap,sdwCoor.xy);	//vec4 shadow = shadow2DProj(shadowMap, sdwCoor);	float d = sdwCoor.z;		if(sdwCoor.x >1.0 || sdwCoor.x < 0.0 || sdwCoor.y>1.0 || sdwCoor.y<0.0)	gl_FragColor=gl_Color;	else	{			if(d >= shadow.x)			gl_FragColor=vec4(0.0,0.0,0.0,1.0);		else			gl_FragColor=gl_Color;					/*if(shadow.y!=1.0)			gl_FragColor=vec4(0.0,0.0,0.0,1.0);		else			gl_FragColor=gl_Color;*/	}			//gl_FragColor=shadow;	}


By the way, I discover the shadowMap's all values in shader are 0.0.
Obviously, I use the wrong way the get depth. But where? ~"~ help me..

[Edited by - shaill on January 13, 2008 1:41:05 AM]
i initialize the texture in a little bit different way :
glTexImage2D(GL_TEXTURE_2D, 0,  GL_DEPTH_COMPONENT,  shTexSize, shTexSize, 0,  GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Without the second function i get garbage.

Quote:Original post by frea
i initialize the texture in a little bit different way :
glTexImage2D(GL_TEXTURE_2D, 0,  GL_DEPTH_COMPONENT,  shTexSize, shTexSize, 0,  GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Without the second function i get garbage.


I've tried..But don't work..
Besides I add the other..

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);

But .__.||...same..

oh!!no!!!!!!!!!FBO such a hard thing..
Quote:Original post by frea
i initialize the texture in a little bit different way :
glTexImage2D(GL_TEXTURE_2D, 0,  GL_DEPTH_COMPONENT,  shTexSize, shTexSize, 0,  GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Without the second function i get garbage.


The default state is
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
so if you don't create them...
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by shaill
Quote:Original post by frea
i initialize the texture in a little bit different way :
glTexImage2D(GL_TEXTURE_2D, 0,  GL_DEPTH_COMPONENT,  shTexSize, shTexSize, 0,  GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Without the second function i get garbage.


I've tried..But don't work..
Besides I add the other..

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);

But .__.||...same..

oh!!no!!!!!!!!!FBO such a hard thing..


What video card do you have and what drivers?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
NVIDIA GeForce 6800
Version 6.14.10.9371

above is my video card's information.
Thanks for ur help,V-man.

This topic is closed to new replies.

Advertisement