Jump to content

  • Log In with Google      Sign In   
  • Create Account

GLSL depth texture and GLSL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 JPulham   Members   -  Reputation: 120

Like
0Likes
Like

Posted 29 April 2008 - 05:19 AM

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.

Sponsor:

#2 __sprite   Members   -  Reputation: 461

Like
0Likes
Like

Posted 29 April 2008 - 05:54 AM

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]

#3 JPulham   Members   -  Reputation: 120

Like
0Likes
Like

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.

/// 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

#4 JPulham   Members   -  Reputation: 120

Like
0Likes
Like

Posted 30 April 2008 - 06:42 AM

I've tested it now and it does work :D
thanks for your help




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS