Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

render depth map

Started by froni Jan 17, 2007 at 4:19 AM 4 replies 2.7k views
Original Post
froni
froni
Hello! Can anybody explain me how I can look on my depth map?? I create .cgfx file and use a technique with two passes. In this moment the first pass is empty only color push through it. After this render pass I take my depth value and save them in a map. pass = cgGetFirstPass(myCgTechnique); while (pass) { cgSetPassState(pass); glutSolidTeapot(5.0f); // copy the z glBindTexture(GL_TEXTURE_RECTANGLE_NV, depth_texture); glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, 0, 0, m_width, m_height); cgResetPassState(pass); pass = cgGetNextPass(pass); } All I want to do is to render this depth map. For that,I make a texture lookup im my second pass. And set my output color value to depth value. But in this point I realy don't know how to transform the depth value into RGBA values?? I just try some possibilities, but the teapot stays white!!!!!!! // final pixel shader void PSfinal(float4 Pos: POSITION, float4 color:COLOR, float4 wpos: WPOS, out float4 out_color:COLOR, uniform samplerRECT depthTex) { float4 depth= texRECT(depthTex,wpos.xyz); out_color=float4(depth.x,depth.y,depth.z,depth.w); //OR //out_color=float4(depth.x,depth.x,depth.x,1); } Thanks for help Froni
Zipster
Zipster
How can you be copying depth to the texture if you only render color in the first pass?

There's no need to use textures at all. When you render the teapot, instead of outputting color, output the depth:
void PSfinal(float4 Pos : POSITION,             float4 color : COLOR,             float4 wpos : WPOS,             out float4 out_color : COLOR, {   float depth = Pos.z / Pos.w;   out_color = float4(depth, depth, depth, 1);}
froni
froni
Thanks for replying!

I can copy the depth to texture cause of saving it in my c++ code with this function:

glBindTexture(GL_TEXTURE_RECTANGLE_NV, depth_texture);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, 0, 0, m_width, m_height);

The function copies the value in my depth_texture and now i can use it as sampler in my fragment program.

I also tried your code and now the modified version looks like:

void PSTest(float4 color:COLOR,
float4 wpos: WPOS,
out float4 out_color:COLOR)
{

float d=wpos.z/wpos.w;
out_color=float4(d,d,d,1);
}
// definition of techniques
technique T0
{
pass p0
{
FragmentProgram = compile arbfp1 PSTest();
}

}

But the result is a white teapot- perhaps the depth values are greater than 1 or the range of the depth values is near by 1 ?????

Thanks
Zipster
Zipster
Quote:
Original post by froni
I can copy the depth to texture cause of saving it in my c++ code with this function:

glBindTexture(GL_TEXTURE_RECTANGLE_NV, depth_texture);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0, 0, 0, 0, 0, m_width, m_height);

The function copies the value in my depth_texture and now i can use it as sampler in my fragment program.

I'm not as familiar with OpenGL as I am with D3D so correct me if I'm wrong, but I'm under the impression that glCopyTexSubImage2D copies data from the current GL_READ_BUFFER, which is basically whatever was last rendered to that buffer. So if you write color data to that buffer in the first pass, then you're going to read color data into the texture before the second pass.

Also note that I use the Pos parameter instead of the wpos parameter. wpos is the 2D screen coordinate of the pixel between [0,width/height], and IIRC z and w aren't defined or at best 0. So it's likely you just divide by 0 and the hardware silently ignores the error and maxes out d. Pos is the clip-space coordinate you want to use.
froni
froni
Hello!

I'm abel to copy the depth value with that function because I'm using an ARB extension for my depth texture:

glGenTextures(1, &depth_texture);//Test CG Texture
glBindTexture(GL_TEXTURE_RECTANGLE_NV, depth_texture);//Test CG Texture
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_COMPARE_FUNC_ARB, GL_GREATER);

That makes copying the depth value out of my framebuffer.

Ok I use wpos because POSITION isn't a valid input for a fragment program. Isn't it???
Thanks

Zipster
Zipster
Pos has to be valid, otherwise you wouldn't be able to do any worthwhile per-pixel effects such as lighting, shadowing, etc. It's just interpolated from the vertices.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.