Hi Ethan, I did this a little while back using information from that page as
well. I'm guessing "real" depth here just means that it's in whatever units
you've modeled your world in. For instance, if the near plane is at 1.0, what
are the units of 1.0?
My purpose was to make sure that clouds fade out before they hit the far plane
so that it isn't obvious that my clouds are just a flat plane.
Here are my shaders:
.vert
Code:
varying float eyeDistance;
//This vertex shader is meant to perform the per vertex operations of per pixel
lighting
//using a single directional light source.
void main()
{
//Pass the texture coordinate on through.
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
eyeDistance = -(gl_ModelViewMatrix * gl_Vertex).z;
//Compute the final vertex position in clip space.
gl_Position = ftransform();
}
.frag
Code:
uniform sampler2D baseTexture;
varying float eyeDistance;
void main(void)
{
vec4 alphaColor = texture2D(baseTexture, gl_TexCoord[0].st);
vec4 color = gl_Fog.color;
color.a = gl_Color.a * alphaColor.a;
float A = gl_ProjectionMatrix[2].z;
float B = gl_ProjectionMatrix[3].z;
float zFar = B / (1.0 + A);
float zNear = - B / (1.0 - A);
A = -(zFar + zNear) / (zFar - zNear);
B = -2.0 * zFar * zNear / (zFar - zNear);
// scale eyeDistance to a value in [0, 1]
float normDepth = (eyeDistance - zNear) / (zFar - zNear);
// Start fading out at 70% of the way there
normDepth = max(normDepth - 0.7, 0.0) / 0.3;
normDepth = clamp(normDepth, 0.0, 1.0);
gl_FragColor = vec4(color.rgb, (1.0 - normDepth) * color.a);//color;
}
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=44565#44565
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org