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.
GLSL depth texture and GLSL
Started by JPulham, Apr 29 2008 05:19 AM
3 replies to this topic
Ad:
#2 Members - Reputation: 461
Posted 29 April 2008 - 05:54 AM
If my somewhat messily scribbled notes from the orange book are correct, you want something like this:
[edit:] Oh, just read your post more carefully. How are you encoding the float value? [/edit]
// 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]
#3 Members - Reputation: 120
Posted 30 April 2008 - 01:18 AM
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.
I can't test this right now as I'm am at school at the mo.. but thanks for the reply anyway
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






