File: deferred-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 (210 lines) | stat: -rw-r--r-- 7,425 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//? #version 150
#include "lighting.sdr" //! #include "lighting.sdr"

#include "shadows.sdr" //! #include "shadows.sdr"

out vec4 fragOut0;

uniform sampler2D ColorBuffer;
uniform sampler2D NormalBuffer;
uniform sampler2D PositionBuffer;
uniform sampler2D SpecBuffer;
uniform sampler2DArray shadow_map;

layout (std140) uniform globalDeferredData {
	mat4 shadow_mv_matrix;
	mat4 shadow_proj_matrix[4];

	mat4 inv_view_matrix;

	float veryneardist;
	float neardist;
	float middist;
	float fardist;

	float invScreenWidth;
	float invScreenHeight;

	float nearPlane;
};

layout (std140) uniform matrixData {
	mat4 modelViewMatrix;
	mat4 projMatrix;
};

layout (std140) uniform lightData {
	vec3 diffuseLightColor;
	float coneAngle;

	vec3 lightDir;
	float coneInnerAngle;

	vec3 coneDir;
	bool dualCone;

	vec3 scale;
	float lightRadius;

	int lightType;
	bool enable_shadows;

	float sourceRadius;
};

// Nearest point sphere and tube light calculations taken from 
// "Real Shading in Unreal Engine 4" by Brian Karis, Epic Games
// Part of SIGGRAPH 2013 Course: Physically Based Shading in Theory and Practice

vec3 ExpandLightSize(in vec3 lightDir, in vec3 reflectDir) {
	// There's an extra max(...,sourceRadius) call here vs the version in the paper.
	// This prevents the centerToRay calculation from choosing a point behind
	// the reflection ray's origin (i.e. underneath the surface).
	// this is necessary for situations where the fragment being shaded lies inside 
	// the sourceRadius of the light.
	// Instead, we choose a point suffciently far away from the reflection origin (hence max(...,sourceRadius))
	// so that we have a gradual transition as the shaded fragments along the surface
	// shift from being inside to outside the light.
	vec3 centerToRay = max(dot(lightDir, reflectDir),sourceRadius) * reflectDir - lightDir;
	return lightDir + centerToRay * clamp(sourceRadius/length(centerToRay), 0.0, 1.0);
}
void GetLightInfo(vec3 position, in float alpha, in vec3 reflectDir, out vec3 lightDirOut, out float attenuation, out float area_normalisation)
{
	if (lightType == LT_DIRECTIONAL) {
		lightDirOut = normalize(lightDir);
		attenuation = 1.0;
		area_normalisation = 1.0;
	} else {
		vec3 lightPosition = modelViewMatrix[3].xyz;
		// Positional light source
		if (lightType == LT_POINT) {
			lightDirOut = lightPosition - position.xyz;
			float dist = length(lightDirOut);

			// this chunk is unnecessary if sourceRadius= 0.0, but let's avoid a branch.
			// given a sphere of radius sourceRadius centered at lightDirOut,
			// move lightDirOut towards the ray defined by reflectDir
			lightDirOut = ExpandLightSize(lightDirOut, reflectDir);
			dist = length(lightDirOut);
			// Energy conservation term
			float alpha_adjust = clamp(alpha + (sourceRadius/(2*dist)), 0.0, 1.0);
			area_normalisation = alpha/alpha_adjust;
			area_normalisation *= area_normalisation;
			//end chunk


			if(dist > lightRadius) {
				discard;
			}
			attenuation = 1.0 - clamp(sqrt(dist / lightRadius), 0.0, 1.0);
		} 
		else if (lightType == LT_TUBE) {  // Tube light
			vec3 beamVec = vec3(modelViewMatrix * vec4(0.0, 0.0, -scale.z, 0.0));
			vec3 beamDir = normalize(beamVec);
			//The actual 'lighting element' is shorter than the light volume cylinder
			//To compensate the light is moved forward along the beam one radius and the length shortened
			//this allows room for clean falloff of the light past the ends of beams.
			vec3 adjustedLightPos = lightPosition - (beamDir * lightRadius);
			vec3 adjustedbeamVec = beamVec - 2.0 * lightRadius * beamDir;
			float beamLength = length(adjustedbeamVec);
			//adjustments having been made, sourceDir needs recalculating
			vec3 sourceDir = adjustedLightPos - position.xyz;

			// Get point on beam nearest to the reflection ray.
			vec3 a_t = reflectDir;
			vec3 b_t = beamDir;
			vec3 b_0 = sourceDir;
			vec3 c = cross(a_t, b_t);
			vec3 d = b_0;
			vec3 r = d - a_t * dot(d, a_t) - c * dot(d,c);
			float neardist = dot(r, r)/dot(b_t, r);
			// Move along the beam by the distance we calculated
			lightDirOut = sourceDir - beamDir * clamp(neardist, 0.0, beamLength);
			// Somebody with a symbolic expression simplifier or a wrinklier brain than me
			// should figure out how to optimise these calcs - qaz


			// this chunk is unnecessary if sourceRadius = 0.0, but let's avoid a branch.
			// Same principle as in LT_POINT, treat chosen location as a spherelight.
			lightDirOut = ExpandLightSize(lightDirOut, reflectDir);
			float dist = length(lightDirOut);
			// Energy conservation term
			float alpha_adjust = min(alpha + (sourceRadius/(2*dist)), 1.0);
			area_normalisation = alpha/alpha_adjust;
			// don't need to square as it's a line rather than a sphere.
			//end chunk

			if(dist > lightRadius) {
				discard;
			}
			attenuation = 1.0 - clamp(sqrt(dist / lightRadius), 0.0, 1.0);
		} 
		else if (lightType == LT_CONE) {
			lightDirOut = lightPosition - position.xyz;
			float coneDot = dot(normalize(-lightDirOut), coneDir);
			float dist = length(lightDirOut);
			attenuation = 1.0 - clamp(sqrt(dist / lightRadius), 0.0, 1.0);
			area_normalisation = 1.0;

			if(dualCone) {
				if(abs(coneDot) < coneAngle) {
					discard;
				} else {
					attenuation *= smoothstep(coneAngle, coneInnerAngle, abs(coneDot));
				}
			} else {
				if (coneDot < coneAngle) {
					discard;
				} else {
					attenuation *= smoothstep(coneAngle, coneInnerAngle, coneDot);
				}
			}
		}
		attenuation *= attenuation;
		lightDirOut = normalize(lightDirOut);
	}
}

void main()
{
	vec2 screenPos = gl_FragCoord.xy * vec2(invScreenWidth, invScreenHeight);
	vec3 position = texture(PositionBuffer, screenPos).xyz;

	if(abs(dot(position, position)) < nearPlane * nearPlane)
		discard;

	vec3 diffColor = texture(ColorBuffer, screenPos).rgb;
	vec4 normalData = texture(NormalBuffer, screenPos);
	vec4 specColor = texture(SpecBuffer, screenPos);
	// The vector in the normal buffer could be longer than the unit vector since decal rendering only adds to the normal buffer
	vec3 normal = normalize(normalData.xyz);
	float gloss = normalData.a;
	float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
	float alpha = roughness * roughness;
	float fresnel = specColor.a;
	vec3 eyeDir = normalize(-position);

	vec3 lightDir;
	float attenuation;
	float area_normalisation;
	vec3 reflectDir = reflect(-eyeDir, normal);
	GetLightInfo(position, alpha, reflectDir, lightDir, attenuation, area_normalisation);

	if (enable_shadows) {
		vec4 fragShadowPos = shadow_mv_matrix * inv_view_matrix * vec4(position, 1.0);
		vec4 fragShadowUV[4];
		fragShadowUV[0] = transformToShadowMap(shadow_proj_matrix[0], 0, fragShadowPos);
		fragShadowUV[1] = transformToShadowMap(shadow_proj_matrix[1], 1, fragShadowPos);
		fragShadowUV[2] = transformToShadowMap(shadow_proj_matrix[2], 2, fragShadowPos);
		fragShadowUV[3] = transformToShadowMap(shadow_proj_matrix[3], 3, fragShadowPos);

		attenuation *= getShadowValue(shadow_map, -position.z, fragShadowPos.z, fragShadowUV, fardist, middist,
								neardist, veryneardist);
	}

	vec3 halfVec = normalize(lightDir + eyeDir);
	float NdotL = clamp(dot(normal, lightDir), 0.0, 1.0);
	vec4 fragmentColor = vec4(1.0);
	fragmentColor.rgb = computeLighting(specColor.rgb, diffColor, lightDir, normal.xyz, halfVec, eyeDir, roughness, fresnel, NdotL).rgb * diffuseLightColor * attenuation * area_normalisation;
	fragOut0 = max(fragmentColor, vec4(0.0));
}