Sign in to follow this  
shirsoft

Saving a depth texture

Recommended Posts

shirsoft    106
I am having problems in reading from the depth texture using glGetTexImage. Iam using the following code. However glGetTexImage gives invalid operation error. The documentation suggests that this happens when glGetTexImage is called between glBegin and glEnd. But this is not happening in my case. I even put an extra glEnd before it but no luck.
glReadBuffer(GL_BACK);
glBindTexture(GL_TEXTURE_2D, iDepthTex);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, w, h);
glGetTexImage(GL_TEXTURE_2D,0,GL_RED,GL_INT,depthbuffer);

Please help

Share this post


Link to post
Share on other sites
zedz    291
im not to sure what u need for glGetTexImage with a depth texture (check the spec) but im pretty sure its not GL_RED.
though with what youre doing youre prolly best off using glReadPixels( .. GL_DEPTH_COMPONENT ... )

Share this post


Link to post
Share on other sites
V-man    813
You actual do need GL_DEPTH_COMPONENT instead of GL_RED else you get GL_INVALID_OPERATION according to the spec.

Share this post


Link to post
Share on other sites
Treesong    170

The complete picture would look something like this:

// Prepare a texture object to hold depth values.
// Can be done once at an 'init' stage of the application
GLint l_DepthmapWidth = 512;
GLint l_DepthmapHeight = 512;
GLuint l_DepthMap = 0;
glGenTextures(1, &l_DepthMap);
glBindTexture(GL_TEXTURE_2D, l_ShadowMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, l_DepthmapWidth, l_DepthmapWidth, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);

...

// Render to the framebuffer. Issue your render call.
// If this is a special depth value generated stage and you're not
// interested in color values, you can do some optimizations like disabling
// writing colors with glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// or disabling lighting etc..

...

// Retrieve depth values
glBindTexture(GL_TEXTURE_2D, l_DepthMap);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, l_DepthmapWidth, l_DepthmapHeight);

...

// Do what ever it is you want to do with the texture now containing depth values


Hope this helps.


Serge van Keulen

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this