Jump to content
  • Advertisement
Sign in to follow this  
zurekx

Shadow mapping: rendering depth to texture

This topic is 3786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been doing some shadows using shadow volumes, and now I wanna try shadow mapping. I know the theory, but I need help with the depth pass. How do I render the depth of the scene to a (float)-texture? I would like to do as much as possible by myself (i.e. using a shader to set the output to the depth). I know there is some extension for rendering depth-to-texture, but I would like to avoid that as much as possible. I've never done any rendering to texture before.

Share this post


Link to post
Share on other sites
Advertisement
IIRC depth is something like pos.z / pos.w, where "pos" is vertex position in clip space (after ModelViewProjection transform).

Share this post


Link to post
Share on other sites
Basically, from fragment shader you can output only to one of the draw buffers (by default color buffer and depth buffer). The best way to do depth render (or any render to texture) would be to use FBO (framebuffer objects) with depth texture attachment, or pixel buffer (older, mostly superseded by FBO), but if you want only core functions without any extensions, you'll have to just render the scene as usual and use glCopyTexImage with GL_DEPTH_COMPONENT (24,32..) format to copy current draw depth buffer to a texture, after that clear the buffers and then do any other passes you want.

Share this post


Link to post
Share on other sites
Thanks!

How do I use glCopyTexImage? I've never rendered to texture before, so I kinda dont know what to do at all.

Share this post


Link to post
Share on other sites
glCopyTexImage simply copies whatever is in color or depth buffer to current bound texture - pseudocode example:


depth_tex = glGenTextures(...
...
loop do
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT) // clear buffers

render_your_scene();

glBindTexture(GL_TEXTURE_2D,depth_tex);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, screen_width, screen_height, 0);
// depth_tex now contains depth buffer of the rendered scene

glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT); // clear buffers

do_second_pass_and_other_rendering();
...
end




Note that screen_width and screen_height are usually not power-of-two, and so for this to work you'll need either hardware which supports non-power-of-two textures, or instead create power-of-two texture (for example 1024x1024 for rendering to 1024x768 window) and then use glCopyTexSubImage instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!