File: fog-f.sdr

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (40 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include "gamma.sdr"

in vec4 fragTexCoord;

out vec4 fragOut0;

uniform sampler2D tex;
uniform sampler2D depth_tex;

layout (std140) uniform genericData {
	vec3 fog_color;
	float fog_start;

	float fog_density;
	float zNear;
	float zFar;
};

void main()
{
	vec4 color_in = texture(tex, fragTexCoord.xy);

	// If the scene is in fog then we change the color we operate on based on the depth of the pixel
	float depth_val = texture(depth_tex, fragTexCoord.xy).x;
	// Transform depth value from [0, 1] to [-1, 1]
	float depth_normalized = 2 * depth_val - 1;
	// Now we compute the depth value in projection space using the clipping plane information
	float view_depth = 2.0 * zNear * zFar / (zFar + zNear - depth_normalized * (zFar - zNear));

	if (isinf(view_depth)) {
		fragOut0.rgb = color_in.rgb;
	} else {
		float fog_dist = clamp(1 - pow(fog_density, view_depth - fog_start) , 0.0, 1.0);
		vec3 finalFogColor = srgb_to_linear(fog_color);

		fragOut0.rgb = mix(color_in.rgb, finalFogColor, fog_dist);
	}
	fragOut0.a = 1.0;
}