GLSL depth texture and GLSL
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.
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]
// VERTEXuniform 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);...// FRAGMENTvarying 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]
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 integervec4 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 integerfloat 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement