Jump to content
  • Advertisement
Sign in to follow this  

OpenGL depth and Cg shaders

This topic is 3630 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 am writing a Ray tracer in Cg shading language, and want to calculate the depth to the nearest object in the fragment shader. The ray tracer works much like a skybox, where each side in the skybox is raytraced. Adding depth to the output enables me to add other objects (non raytraced) to the sceene. How do I calculate the depth to coorespond with the depth in the openGL depth buffer? For reference: Raytraced only. A sceene with no aditional non raytraced objects. With non-raytraced object. The white box (non raytraced) is added. But since the depth information is wayback the sphere is not excluding most of the white box.

Share this post

Link to post
Share on other sites
Problem solved:

found the solution here:

float zFar = 400;
float zNear = 0.1;

float z = length(ray.position-IN.Eye);
float a = zFar / ( zFar - zNear );
float b = zFar * zNear / ( zNear - zFar );
float depth = ( a + b / z );

Turned out to be quiet a simple solution.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!