Sign in to follow this  
WiredCat

OpenGL Render Depth to texture

Recommended Posts

Fairly simple topic, but i can not make it work, the thing is that i don't know how to copy it to a texture.

I dont want to use any shaders just pure opengl ; )

The code might be handy:

But i don't know what type of texture use:
should it be? unsigned int Texture; or something else?

i use something like this to initalize:

[CODE]
glEnabled(GL_TEXUTRE_2D);
glGenTextures(1, &shadowMapTexture);
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 512, 512, 0, GL_DEPTH_COMPONENT24, GL_UNSIGNED_BYTE, 0);
glDisable(GL_TEXTURE_2D);
[/CODE]

then i call:

[CODE]
glEnable(GL_TEXTURE_2D);
//Read the depth buffer into the shadow map texture
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
// glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,
0, 0, shadowMapSize, shadowMapSize, 0);
glDisable(GL_TEXTURE_2D);
[/CODE]


and i try to draw it but i see blank white texture (NOTE: i am using 24 bit depth buffer)]

Maybe i shouldn't see anything but how i am supposed to see if my shadowmap is working when i can't see it even on on blank quad (i expected to see a white texture with black (shadow ;P)

like i said working code would come handy without explenations

Share this post


Link to post
Share on other sites
It's very possible that blank white is actually correct.

As the depth buffer is non-linear the tendency is for most of the depth range to be clustered very close to the near clipping plane, with very few bits of precision as it starts to approach the far. What that means in terms of depth textures when they're drawn to screen (and therefore reduced to 8bpp for this purpose) is that nearby objects will occupy the vast majority of the 0-254 greyscale range, and further objects will all be rammed into the 255 pure-white part. The problem is that "further" in this case is really not that much further, so what you're seeing as pure white is just the effect of reducing a 24-bit depth buffer to 8 bits for display.

You say you don't want to use shaders, but shaders are also "pure OpenGL" - much more pure than the fixed pipeline in fact, as all modern hardware will be emulating the fixed pipeline via shaders anyway. Plus they're also very useful for re-linearizing depth in cases such as this.

Share this post


Link to post
Share on other sites
i cant fetch some adresses like: glShaderSourceARB
i did
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB"); but i have NULL Edited by ___

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1348572845' post='4983529']
It's very possible that blank white is actually correct.
[/quote]
Thanks mhagain! I was just doing that. It turns out all my Z data was between 0.998 and 1. I used the following code to see it.
return (1 - Depth.Sample(Sampler, input.cords)) * 500;

Share this post


Link to post
Share on other sites
[quote name='___' timestamp='1348574903' post='4983537']
i cant fetch some adresses like: glShaderSourceARB
i did
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB"); but i have NULL
[/quote]

i believe even internal chip gpus have basic shader support :)
you need to have a context, aka. opengl32.dll must be loaded (or visually: a window open) to get an entry point
also, try different function names, such as simply: "glShaderSource"

Share this post


Link to post
Share on other sites
glShaderSourceARB is from the original GL_ARB_shader_objects extension and should not be used; the core GL2.0 and above versions have some subtle differences and are in general more robust on a wider range of hardware, as well as much more likely to be actually supported (i.e. a GPU vendor could implement GL2.0+ without implementing GL_ARB_shader_objects at all!)

Share this post


Link to post
Share on other sites
Try these:

http://www.arcsynthesis.org/gltut/Basics/Tut01%20Making%20Shaders.html
http://samplecodebank.blogspot.ie/2011/05/glshadersource-example.html

The first one is a more general GL 3.0 tutorial but the procedure for creating shaders is the very same.

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