Sign in to follow this  

render z-value to texture

This topic is 4589 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

Hi, I want to render the depth value to texture using vertex shader, how can I read from the depth buffer and render to texture in CG? Thanks a lot. helen

Share this post


Link to post
Share on other sites
Do you really need to do it in Cg? How about this:


// create texture
unsigned int id;
glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,id);
glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,width,height,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,NULL);

// to save the depth values to texture
glBindTexture(GL_TEXTURE_2D,id);
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,width,height);

Share this post


Link to post
Share on other sites
AxoDosS: Your method isn't quite as fast.

helenj: It needs to be done in the pixel shader, not the vertex shader. Just return the fragment depth.

Share this post


Link to post
Share on other sites
I also think it should be implemented by a pixel shader,but as I read in a siggraph 2002 sketch paper by Jason.L Mitshell "Real-Time Image-Space Outlining for Non-Photorealistic Rendering",
it says:
For the cases of silhouette and crease outlining, we render the full
scene’s world-space normals and eye-space depths into an RGBA
(nxworld, nyworld, nzworld, deptheye) texture using a vertex shader to
populate the color channels.

I don't quite understand why they used the vertex shader, could someone explain it to me?
Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by zedzeek
i fail to see why u *need* a vertex or fragment shader at all


You don't need a fragment shader, but you need a vertex shader since the goal is to encode the values (normx,normy,normz,eyedepth) into the RGBA channels of the buffer. Those values will be outputed into the primary color per-vertex, and be interpolated across the triangles, filling the buffer with correct values. You need either a vertex shader for that, or calculate those values yourself in software and use glColor4f. Obviously it's best if you use vertex shaders.

Notice that the eyedepth is not the value used in the depth-buffer, it is just the z coordinate of the vertex position in eyespace(properly scaled down I assume in order to be packed into the [0,1] range).

Anyway, you can use render-to-texture extensions to copy the buffer into a texture, but they're a pain in the ass. glCopyTexSubImage2D will work fine unless your program is too performance demanding.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
glCopyTexSubImage2D will work fine unless your program is too performance demanding.


As I understand it, the copy call absolutely massacres the X800s and other cards on the same chipset series.

Share this post


Link to post
Share on other sites
mikeman: Thanks so much for your detailed explanation
Promit: I am using nVidia 5700 card, will the glCopyTexSubImage2D call work on that? Anyway, I'll try it later.

Share this post


Link to post
Share on other sites
Quote:
Original post by helenj
I am using nVidia 5700 card, will the glCopyTexSubImage2D call work on that? Anyway, I'll try it later.


Yes it will work (I have the same card). There is nothing wrong in using glCopyTexSubImage2D.

Share this post


Link to post
Share on other sites
Won't you get a precision loss if you encode normal and depth in the same color? You get 8 bits for depth, instead of 24 or 32 bits with a depth texture?

Maybe you should use a pbuffer with render to texture. You can output a color and depth texture at the same time in a single pbuffer.

Share this post


Link to post
Share on other sites

This topic is 4589 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.

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