Skip to content

Commit c4dd593

Browse files
committed
Move lighing functions into one file
There was a slight difference between the GGX function from the main shader and the deferred shader. The deferred shader didn't use the fresnel value stored in the G-buffer for some reason.
1 parent 30404d4 commit c4dd593

4 files changed

Lines changed: 51 additions & 87 deletions

File tree

code/def_files/data/effects/deferred-f.sdr

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
#include "lighting.sdr"
3+
14
in vec3 beamVec;
25
in vec3 lightPosition;
36
out vec4 fragOut0;
@@ -27,49 +30,6 @@ layout (std140) uniform lightData {
2730
int lightType;
2831
};
2932

30-
#define SPEC_INTENSITY_POINT 5.3 // Point light
31-
#define SPEC_INTENSITY_DIRECTIONAL 3.0 // Directional light
32-
#define SPECULAR_FACTOR 1.75
33-
#define SPECULAR_ALPHA 0.1
34-
#define SPEC_FACTOR_NO_SPEC_MAP 0.6
35-
#define ENV_ALPHA_FACTOR 0.3
36-
#define GLOW_MAP_INTENSITY 1.5
37-
#define AMBIENT_LIGHT_BOOST 1.0
38-
vec3 FresnelSchlick(vec3 specColor, vec3 light, vec3 halfVec)
39-
{
40-
return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(light, halfVec), 0.0, 1.0), 5.0);
41-
}
42-
vec3 SpecularBlinnPhong(vec3 specColor, vec3 light, vec3 normal, vec3 halfVec, float specPower, float fresnel, float dotNL)
43-
{
44-
return mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnel) * ((specPower + 2.0) / 8.0 ) * pow(clamp(dot(normal, halfVec), 0.0, 1.0), specPower) * dotNL;
45-
}
46-
vec3 SpecularGGX(vec3 specColor, vec3 light, vec3 normal, vec3 halfVec, vec3 view, float gloss, float dotNL)
47-
{
48-
float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
49-
float alpha = roughness * roughness;
50-
51-
float dotNH = clamp(dot(normal, halfVec), 0.0f, 1.0f);
52-
float dotNV = clamp(dot(normal, view), 0.0f, 1.0f);
53-
54-
float alphaSqr = alpha * alpha;
55-
float pi = 3.14159f;
56-
float denom = dotNH * dotNH * (alphaSqr - 1.0f) + 1.0f;
57-
float distribution = alphaSqr / (pi * denom * denom);
58-
59-
vec3 fresnel = FresnelSchlick(specColor, light, halfVec);
60-
61-
float alphaPrime = roughness + 1.0f;
62-
float k = alphaPrime * alphaPrime / 8.0f;
63-
float g1vNL = 1.0f / (dotNL * (1.0f - k) + k);
64-
float g1vNV = 1.0f / (dotNV * (1.0f - k) + k);
65-
float visibility = g1vNL * g1vNV;
66-
67-
return distribution * fresnel * visibility * dotNL;
68-
}
69-
vec4 SpecularLegacy(vec4 specColor, vec3 normal, vec3 halfVec, float specPower)
70-
{
71-
return specColor * pow(clamp(dot(normal, halfVec), 0.0, 1.0), specPower);
72-
}
7333
void main()
7434
{
7535
vec2 screenPos = gl_FragCoord.xy * vec2(invScreenWidth, invScreenHeight);
@@ -124,6 +84,6 @@ void main()
12484
vec3 halfVec = normalize(lightDir + eyeDir);
12585
float NdotL = clamp(dot(normal.xyz, lightDir), 0.0, 1.0);
12686
vec4 fragmentColor = vec4(color * (diffuseLightColor * NdotL * attenuation), 1.0);
127-
fragmentColor.rgb += SpecularGGX(specColor.rgb, lightDir, normal.xyz, halfVec, eyeDir, gloss, NdotL).rgb * specLightColor * attenuation;
87+
fragmentColor.rgb += SpecularGGX(specColor.rgb, lightDir, normal.xyz, halfVec, eyeDir, gloss, fresnel, NdotL).rgb * specLightColor * attenuation;
12888
fragOut0 = max(fragmentColor, vec4(0.0));
12989
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
const float SPEC_INTENSITY_POINT = 5.3 ;// Point light
3+
const float SPEC_INTENSITY_DIRECTIONAL = 3.0 ;// Directional light
4+
const float SPECULAR_FACTOR = 1.75;
5+
const float SPECULAR_ALPHA = 0.1;
6+
const float SPEC_FACTOR_NO_SPEC_MAP = 0.6;
7+
const float ENV_ALPHA_FACTOR = 0.3;
8+
const float GLOW_MAP_INTENSITY = 1.5;
9+
const float AMBIENT_LIGHT_BOOST = 1.0;
10+
11+
vec3 FresnelSchlick(vec3 specColor, vec3 light, vec3 halfVec)
12+
{
13+
return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(light, halfVec), 0.0, 1.0), 5.0);
14+
}
15+
16+
vec3 SpecularBlinnPhong(vec3 specColor, vec3 light, vec3 normal, vec3 halfVec, float specPower, float fresnel, float dotNL)
17+
{
18+
return mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnel) * ((specPower + 2.0) / 8.0 ) * pow(clamp(dot(normal, halfVec), 0.0, 1.0), specPower) * dotNL;
19+
}
20+
21+
vec3 SpecularGGX(vec3 specColor, vec3 light, vec3 normal, vec3 halfVec, vec3 view, float gloss, float fresnelFactor, float dotNL)
22+
{
23+
float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
24+
float alpha = roughness * roughness;
25+
26+
float dotNH = clamp(dot(normal, halfVec), 0.0f, 1.0f);
27+
float dotNV = clamp(dot(normal, view), 0.0f, 1.0f);
28+
29+
float alphaSqr = alpha * alpha;
30+
float pi = 3.14159f;
31+
float denom = dotNH * dotNH * (alphaSqr - 1.0f) + 1.0f;
32+
float distribution = alphaSqr / (pi * denom * denom);
33+
34+
vec3 fresnel = mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnelFactor);
35+
36+
float alphaPrime = roughness + 1.0f;
37+
float k = alphaPrime * alphaPrime / 8.0f;
38+
float g1vNL = 1.0f / (dotNL * (1.0f - k) + k);
39+
float g1vNV = 1.0f / (dotNV * (1.0f - k) + k);
40+
float visibility = g1vNL * g1vNV;
41+
42+
return distribution * fresnel * visibility * dotNL;
43+
}

code/def_files/data/effects/main-f.sdr

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#include "shadows.sdr"
33

4+
#include "lighting.sdr"
5+
46
#define MAX_LIGHTS 8
57
#define LT_DIRECTIONAL 0
68
#define LT_POINT 1
@@ -136,13 +138,6 @@ in vec4 fragShadowUV[4];
136138
in vec4 fragShadowPos;
137139
uniform sampler2DArray shadow_map;
138140
#endif
139-
#define SPEC_INTENSITY_POINT 5.3 // Point light
140-
#define SPEC_INTENSITY_DIRECTIONAL 3.0 // Directional light
141-
#define SPECULAR_FACTOR 1.75
142-
#define SPEC_FACTOR_NO_SPEC_MAP 0.6
143-
#define ENV_ALPHA_FACTOR 0.3
144-
#define GLOW_MAP_INTENSITY 1.5
145-
#define AMBIENT_LIGHT_BOOST 1.0
146141
#define SRGB_GAMMA 2.2
147142
in vec4 fragPosition;
148143
in vec3 fragNormal;
@@ -159,41 +154,6 @@ vec3 FresnelLazarovEnv(vec3 specColor, vec3 view, vec3 normal, float gloss)
159154
return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(view, normal), 0.0, 1.0), 5.0) / (4.0 - 3.0 * gloss);
160155
}
161156

162-
vec3 FresnelSchlick(vec3 specColor, vec3 light, vec3 halfVec)
163-
{
164-
return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(light, halfVec), 0.0, 1.0), 5.0);
165-
}
166-
167-
vec3 SpecularBlinnPhong(vec3 specColor, vec3 light, vec3 normal, vec3 halfVec, float specPower, float fresnel, float dotNL)
168-
{
169-
return mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnel) * ((specPower + 2.0) / 8.0 ) * pow(clamp(dot(normal, halfVec), 0.0, 1.0), specPower) * dotNL;
170-
}
171-
172-
vec3 SpecularGGX(vec3 specColor, vec3 light, vec3 normal, vec3 halfVec, vec3 view, float gloss, float fresnelFactor, float dotNL)
173-
{
174-
float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
175-
float alpha = roughness * roughness;
176-
177-
float dotNH = clamp(dot(normal, halfVec), 0.0f, 1.0f);
178-
float dotNV = clamp(dot(normal, view), 0.0f, 1.0f);
179-
180-
float alphaSqr = alpha * alpha;
181-
float pi = 3.14159f;
182-
float denom = dotNH * dotNH * (alphaSqr - 1.0f) + 1.0f;
183-
float distribution = alphaSqr / (pi * denom * denom);
184-
185-
vec3 fresnel = mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnelFactor);
186-
187-
float alphaPrime = roughness + 1.0f;
188-
float k = alphaPrime * alphaPrime / 8.0f;
189-
float g1vNL = 1.0f / (dotNL * (1.0f - k) + k);
190-
float g1vNV = 1.0f / (dotNV * (1.0f - k) + k);
191-
float visibility = g1vNL * g1vNV;
192-
193-
return distribution * fresnel * visibility * dotNL;
194-
}
195-
196-
#ifdef FLAG_LIGHT
197157
void GetLightInfo(int i, out vec3 lightDir, out float attenuation)
198158
{
199159
lightDir = normalize(lights[i].position.xyz);
@@ -242,7 +202,7 @@ vec3 CalculateLighting(vec3 normal, vec3 diffuseMaterial, vec3 specularMaterial,
242202
}
243203
return diffuseMaterial * (lightAmbient + lightDiffuse) + lightSpecular;
244204
}
245-
#endif
205+
246206
void main()
247207
{
248208
#ifdef WORKAROUND_CLIPPING_PLANES

code/source_groups.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ set(file_root_def_files_data_effects
175175
def_files/data/effects/fxaa-f.sdr
176176
def_files/data/effects/fxaa-v.sdr
177177
def_files/data/effects/fxaapre-f.sdr
178+
def_files/data/effects/lighting.sdr
178179
def_files/data/effects/ls-f.sdr
179180
def_files/data/effects/main-f.sdr
180181
def_files/data/effects/main-g.sdr

0 commit comments

Comments
 (0)