-
Notifications
You must be signed in to change notification settings - Fork 8
/
TextureFragmentShader.fragmentshader
executable file
·74 lines (59 loc) · 2.15 KB
/
TextureFragmentShader.fragmentshader
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
#version 330 core
struct Light {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
// Interpolated values from the vertex shaders
in vec2 UV;
in vec3 view_normal;
in vec3 fragpos;
in mat4 viewMat;
in vec4 FragPosLightSpace;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
uniform Light light;
uniform sampler2D shadowMap;
uniform int object_id;
// Ouput data
layout(location = 0) out vec3 color;
layout(location = 1) out vec3 out_uv;
layout(location = 2) out vec3 out_view_normal;
float ShadowCalculation(vec4 fragPosLightSpace)
{
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
projCoords = projCoords * 0.5 + 0.5;
float closestDepth = texture(shadowMap, projCoords.xy).r;
float currentDepth = projCoords.z;
float shadow;
if (object_id == 0){
shadow = currentDepth - 0.075 > closestDepth ? 1.0 : 0.0;
}else if (object_id == 1){
shadow = currentDepth - 0.01 > closestDepth ? 1.0 : 0.0;
}
return shadow;
}
void main(){
// Output color = color of the texture at the specified UV
float gamma = 2.2;
color = texture( myTextureSampler, UV ).rgb;
color = pow(color, vec3(gamma));
out_uv = vec3(UV,0);
out_view_normal = normalize(view_normal);
//PHONE light calculated in world space
vec3 ambient = light.ambient;
vec3 view_light_direction = normalize((viewMat * vec4(light.direction, 0.0)).xyz);
float diff = max(dot(out_view_normal,-view_light_direction),0);
vec3 diffuse = light.diffuse * diff;
vec3 viewDir = -normalize(fragpos);
vec3 halfwayDir = normalize(viewDir - view_light_direction);
vec3 reflectDir = reflect(view_light_direction, out_view_normal);
float spec = pow(max(dot(halfwayDir, out_view_normal), 0.0), 32);
vec3 specular = light.specular * spec;
float shadow = ShadowCalculation(FragPosLightSpace);
color = (ambient + (specular + diffuse) * (1.0 - shadow)) * color;
//color = (ambient + diffuse + specular) * color;
//Gamma
color = pow(color, vec3(1.0/gamma));
}