Skip to main content
GameDev.net gamedev.net
🔒 Locked

Render Depth to texture

Started by _WeirdCat_ Sep 25, 2012 at 10:31 AM 6 replies 2k views
Original Post
_WeirdCat_
_WeirdCat_
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:


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);


then i call:


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);



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
21st Century Moose
21st Century Moose
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.
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
gradbot
gradbot

It's very possible that blank white is actually correct.

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;
Kaptein
Kaptein

i cant fetch some adresses like: glShaderSourceARB
i did
glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB"); but i have NULL


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"
21st Century Moose
21st Century Moose
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!)
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
21st Century Moose
21st Century Moose
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.
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 

Topic Locked

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

Sign in to reply to this topic.