So this is one way to do it:
vertex:
#version 330 core
layout (location = 0) in vec3 pos;
uniform mat4 model;
out float zloc;
void main(){
gl_Position = model * vec4(pos, 1.0f);
zloc = pos.z;
]
fragment:
#version 330 core
in float zloc;
out vec4 color;
void main(){
float revDist = 1 / (1 + tan(clamp(zloc + 1, 0, 1.99999) * 0.39269908169872415480783042290994)); //Value is PI / 8
color = (revDist, revDist, revDist, 1);
}
tunnelman:
What would be the best function to return the depth fed into a shader as a color and
which shader should I put it in
What do you mean? It’s not clear what the motivation is behind your question.
Perhaps it would help if you spelled out what you perceive about the above that’s undesirable, or insufficient.
As an aside. You don’t really need zloc
at all. Check out gl_FragCoord.z
in the GLSL 4.6 Spec .
So what I want is to have depth perceived like it is seen by something with eyes.
What you probably want might be this:
gl_FragCoord.z/gl_FragCoord.w
- gives you the depth value in clipspace non normalized.
And this gives you the linearized depth information:
float z_n = 2.0 * (gl_FragCoord.z/gl_FragCoord.w)- 1.0;
linear_depth = (2.0 * nearPlane * farPlane ) / (farPlane + nearPlane - z_n * (farPlane-nearPlane));
afterwards you just need to colorize this:
gl_FragColor = color * vec4(linear_depth , 1.0);
You might wanna also check this post what-does-gl-fragcoord-z-gl-fragcoord-w-represent
Are you refering to the fog effect?
Distance fog is a technique used in 3D computer graphics to enhance the perception of distance by shading distant objects differently.
Because many of the shapes in graphical environments are relatively simple, and complex shadows are difficult to render, many graphics engines employ a "fog" gradient so objects further from the camera are progressively more obscured by haze and by aerial perspective. This technique simulates the effect of light scattering, which causes more distant objects to ap...