Sign in to follow this  
JPulham

GLSL depth texture and GLSL

Recommended Posts

JPulham    120
How do I get the scene depth into a texture using GLSL? I've tried using a varying 'veiwVec' and dividing by far clip plane and I've tried gl_FragCoord.z, but no matter what I seem to get 'banding' or a plain white screen. I have a A8R8G8B8 texture that I'm encoding the float value to. cheers.

Share this post


Link to post
Share on other sites
sprite_hound    461
If my somewhat messily scribbled notes from the orange book are correct, you want something like this:



// VERTEX
uniform vec3 CamPos, CamDir;
uniform float DepthNear, DepthFar;

varying float CameraDepth;

...
vec3 offset = (gl_Vertex.xyz / gl_Vertex.w) - CamPos;
float z = -dot(offset, CamDir);
CameraDepth = (z - DepthNear) / (DepthFar - DepthNear);
...

// FRAGMENT
varying float CameraDepth;

...

gl_FragColor.xyz = vec3(CameraDepth);
// Or whatever you want exactly here...
gl_FragColor.w = 1.0;
...




[edit:] Oh, just read your post more carefully. How are you encoding the float value? [/edit]

Share this post


Link to post
Share on other sites
JPulham    120
I am encoding to float<->RGBA8 using the code in this topic:
http://www.gamedev.net/community/forums/topic.asp?whichpage=1&pagesize=25&topic_id=463075
I think its 15th down, by Ysaneya.

/// Packing a [0-1] float value into a 4D vector where each component will be a 8-bits integer
vec4 packFloatToVec4i(const float value)
{
const vec4 bitSh = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
const vec4 bitMsk = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
vec4 res = fract(value * bitSh);
res -= res.xxyz * bitMsk;
return res;
}

/// Unpacking a [0-1] float value from a 4D vector where each component was a 8-bits integer
float unpackFloatFromVec4i(const vec4 value)
{
const vec4 bitSh = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
return(dot(value, bitSh));
}




I can't test this right now as I'm am at school at the mo.. but thanks for the reply anyway

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