PDA

View Full Version : Depth to texture for compositing



abetts
10-23-2003, 07:54 AM
Hello --

I want to render a pass, storing the the resulting color and depth to texture maps for subsequent compositing with things that are drawn in the framebuffer later. I'm using an NVidia QuadroFX 500 in Linux (latest driver, 44.96).

In short the problem I'm having is that when I read the depth texture the image is completely white. I've used glReadPixels to examine the contents of the depth buffer more closely and the result is the same - the values are all 1.0. At this point I'm trying to just store the depth as a texture and then rendering it as luminance so I can see what's inside the depth buffer. Storing the color buffer doesn't seem to be a problem.

Here's what my code looks like:




unsigned int m_width = 1024, m_height = 768;
GLuint ztex,ctex;

void init()
{
glGenTextures(1,&ztex);
glBindTexture(GL_TEXTURE_RECTANGLE_NV,ztex);
glTexImage2D(GL_TEXTURE_RECTANAGLE_NV,0,GL_DEPTH_C OMPONENT,m_width,m_height,0,GL_DEPTH_COMPONENT,GL_ UNSIGNED_INT,0);

glGenTextures(1,&ctex);
glBindTexture(GL_TEXTURE_RECTANGLE_NV,ctex);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV,0,GL_RGBA,m_w idth,m_height,0,GL_RGBA,GL_UNSIGNED_BYTE,0);
}

void render_pass()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_scene();

// store depth
glBindTexture(GL_TEXTURE_RECTANGLE_NV,ztex);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV,0, 0,0,0,0, m_width,m_height);

// store color
glBindTexture(GL_TEXTURE_RECTANGLE_NV,ctex);
glCopyTexSubImage2d(GL_TEXTURE_RECTANGLE_NV,0, 0,0,0,0, m_width,m_height);

glBindTexture(GL_TEXTURE_RECTANGLE_NV,0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void composite()
{
setup_ortho_view();
glDisable(GL_DEPTH_TEST);

glEnable(GL_TEXTURE_RECTANGLE_NV);
glBindTexture(GL_TEXTURE_RECTANGLE_NV,ztex);

glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);

glTexCoord2f(m_width,0);
glVertex2f(m_width,0);

glTexCoord2f(m_width,m_height);
glVertex2f(m_width,m_height);

glTexCoord2f(0,m_height);
glVertex2f(0,m_height);
glEnd();
}

void frame_function()
{
render_pass();
composite();
}


I'm expecing at this point that the quad will display a greyscale depth map, but instead it's all white. If I draw the color map texture on the quad, then I see the image that was created in render_pass() without any problem. It's my intention to use a fragment program to compare the depth texture with the framebuffer depth to do the compositing.

I've used a program similar to this to do depth map shadows in the past and didn't have any problems, so it seemed like in that cast the depth buffer was being read fine.

thanks for any help!

- Alex

[This message has been edited by abetts (edited 10-24-2003).]

roffe
10-23-2003, 10:05 AM
unsigned int m_width = 1024, m_height = 768;
GLuint ztex,ctex;

void init()
{
glGenTextures(1,&ztex);
glBindTexture(GL_TEXTURE_RECTANGLE_NV,m_ztex);
glTexImage2D(GL_TEXTURE_RECTANAGLE_NV,0,GL_DEPTH_C OMPONENT,m_width,m_height,0,GL_DEPTH_COMPONENT,GL_ UNSIGNED_INT,0);

glGenTextures(1,&ctex);
glBindTexture(GL_TEXTURE_RECTANGLE_NV,0,GL_RGBA,m_ width,m_height,0,GL_RGBA,GL_UNSIGNED_BYTE,0);
}

[/B]<HR></BLOCKQUOTE>

What does glGetError return?
In the above init function you initialize m_ztex but use ztex to bind, typo?
And I hope the second bind is a result of a to quick copy and paste.


[This message has been edited by roffe (edited 10-23-2003).]

SirKnight
10-24-2003, 05:17 PM
Here is a demo that may help somehow. http://cvs1.nvidia.com/DEMOS/OpenGL/src/oit_3x/order_independent_transparency.cpp

In there they use ARB_depth_texture so they can have GL_DEPTH_COMPONENT16_ARB or GL_DEPTH_COMPONENT24_ARB in the glTexImage2D call. Maybe that will help.


-SirKnight

SirKnight
10-24-2003, 05:29 PM
What's weird is that I don't see anything wrong with your code. Then again, I'm about half asleep right so I may be missing something. http://www.opengl.org/discussion_boards/ubb/biggrin.gif


-SirKnight

abetts
10-24-2003, 07:24 PM
So, I've built both the oit and oit_3x examples (for Linux). Strange thing is that oit renders both depth-peeled & bad transparency, as expected, while oit_3x renders the depth-peeled teapot opaque (the bad teapot is transparent though).

I have gotten depth-mapped shadows to work before, so I wonder why I'm having trouble with this...

- Alex


Originally posted by SirKnight:
What's weird is that I don't see anything wrong with your code. Then again, I'm about half asleep right so I may be missing something. http://www.opengl.org/discussion_boards/ubb/biggrin.gif


-SirKnight

Relic
10-27-2003, 02:10 AM
Is this a copy from actual code or a typo?
This shouldn't compile.

// store color
glBindTexture(GL_TEXTURE_RECTANGLE_NV,ctex);
glCopyTexSubImage2d(GL_TEXTURE_RECTANGLE_NV,0, 0,0,0,0, m_width,m_height);

Texture rectangles do not support mipmaps, but default OpenGL min filter is GL_LINEAR_MIPMAP_NEAREST. Make sure to have set a suitable filter per texobject.

[This message has been edited by Relic (edited 10-27-2003).]