-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragment.frag
92 lines (70 loc) · 2.63 KB
/
fragment.frag
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
#version 460 core
out vec4 FragColor;
in vec2 TexCoords;
in vec3 Normal;
in vec3 FragPos;
in vec4 FragPosLightSpace;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;
uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1;
uniform sampler2D shadowMap;
uniform int Switch;
float ShadowCalculation(vec4 fragPosLightSpace)
{
// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r;
// get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// calculate bias (based on depth map resolution and slope)
vec3 normal = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005);
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
for(int x = -10; x <= 10; ++x)
{
for(int y = -10; y <= 10; ++y)
{
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
shadow /= 441.0;
// keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
if(projCoords.z > 1.0)
shadow = 0.0;
return shadow;
}
vec4 normalLight()
{
float ambientStrength = 0.2;
vec3 ambient = ambientStrength * lightColor;
vec3 color = vec3(texture(texture_diffuse1, TexCoords));
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;///look here for the texture_diffuse dumbass
vec3 specularStrength = vec3(texture(texture_specular1,TexCoords));///look here for the texture_specular dumbass
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 2);
vec3 specular = specularStrength * spec * lightColor;
float shadowCol, shadow = ShadowCalculation(FragPosLightSpace);
shadowCol = 1.0 - shadow;
//vec4 theLight = vec4((ambient + shadowCol * (diffuse + specular)), 1.0);
vec4 debugLight = vec4(color, 1.0);
if(Switch == 1)
color = vec3(0.7);
vec4 theLight = vec4((ambient + shadowCol * (diffuse + specular)) * color, 1.0);
return theLight;
}
void main()
{
FragColor = normalLight();
}