Depth Texture Problem

i want to keep the depth information of the scene in a texture.i use FBO to keep the depth information.However nothing appeared. Can someone help me figure out why?
CG code:

uniform float4x4 WorldViewProj : state.matrix.mvp;
void main_v(float4 position:POSITION,
out float4 oPosition: POSITION,
out float2 depth:TEXCOORD0,
uniform float4x4 WorldViewProj)
{

oPosition=mul(WorldViewProj,position);
depth.x=oPosition.z;
depth.y=oPosition.w;

}

void main_f(float2 depth:TEXCOORD0,
uniform float pNear ,
uniform float pFar,
out float4 result:COLOR)
{
float depthNum=0;
depthNum = (depth.x - pNear) / (pFar - pNear);

depthNum += depthOffset;  
 
result.xyz = depthNum.xxx; 
result.w = 1.0;

}

FBO code:

void FrameBuffer()
{

glGenTextures(1, &tex);
glBindTexture(texTarget, tex);

glTexImage2D(texTarget,0,GL_RGBA,texWidth,texHeight,0,GL_RGBA,GL_FLOAT,0);

glTexParameterf(texTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(texTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glGenFramebuffersEXT(1, &fb);
glGenRenderbuffersEXT(1, &depth_rb);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texWidth, texHeight);

glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);//new
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_rb);//new

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

}

Render code:

void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(float)screenWidth/(float)screenHeight,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// glBindTexture(GL_TEXTURE_2D,0);
glPushMatrix();

cgGLBindProgram(vprog);
cgGLEnableProfile(cg_vprofile);
cgGLBindProgram(fprog);
cgGLEnableProfile(cg_fprofile);

DrawScene();

glPopMatrix();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
DetachShader();

// DrawScene();

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glViewport(0,0,screenWidth,screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// gluPerspective(45.0,(float)w/(float)h,0.1,100);
if(screenWidth<screenHeight)
glOrtho(0, 1, 0, 1.0 * (GLfloat)screenHeight/(GLfloat)screenWidth, -20.0, 20.0);

else

 glOrtho(0, 1.0*(GLfloat)screenWidth/(GLfloat)screenHeight, 0, 1.0, -20.0, 20.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,tex);

DrawScreenQuad();
glDisable(GL_TEXTURE_2D);

glutSwapBuffers();
}

The depth buffer has a very non linear stretch, so it will look like it has nothing in it even though there may be (everthing is just scrunched towards, i forget, 0 or 1). The trick to display it is to write a shader that undoes the stretch. This topic has come up previously, and i’m pretty sure that calculation is on one of the forum posts…

Try:
depth stretch discussion

Yeah, I’m sure this is in a number of forum posts. Here’s one:

There is also this
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html
and
http://www.opengl.org/wiki/Common_Mistakes#Depth_Buffer_Precision

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.