-
Notifications
You must be signed in to change notification settings - Fork 2
/
draw_world_fshader.glsl
92 lines (76 loc) · 1.77 KB
/
draw_world_fshader.glsl
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 330 core
uniform sampler2D texSampler;
uniform struct Transform
{
mat4 viewProjection;
vec3 viewPosition;
}
transform;
uniform struct PointLight
{
vec3 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 attenuation;
}
pointLight;
uniform struct Material
{
vec4 emission;
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
}
material;
in Vertex
{
flat vec3 normal;
flat vec4 specular;
vec3 position;
vec4 light;
vec2 texCoord;
float under;
}
vertex;
out vec4 color;
// Schlick's approximation a ^ b == a / (b – a * b + a)
// a in [0.0; 1.0].
float approxPow(float a, float b)
{
return a / (b - a * b + a);
}
vec4 getSpecularLight()
{
vec3 to_light = pointLight.position - vertex.position;
vec3 to_light_norm = normalize(to_light);
vec3 to_camera = transform.viewPosition - vertex.position;
vec3 to_camera_norm = normalize(to_camera);
float RdotV = max(dot(reflect(-to_light_norm, vertex.normal),
to_camera_norm), 0.0);
RdotV = approxPow(RdotV, material.shininess);
return vertex.specular * RdotV;
}
float water_color_factor(float water_thickness)
{
const float e = 2.718281828459045235360;
return clamp((exp(water_thickness * e / 25.0) - 1.0), 0.0, 0.5);
}
void main(void)
{
vec4 waterColor = vec4(0.0, 0.20, 0.40, 1.0);
vec4 specular_light = getSpecularLight();
vec4 light = (vertex.light + specular_light);
vec4 objColor = texture(texSampler, vertex.texCoord);
if (vertex.under > 0.0)
{
color = objColor * light;
}
else
{
float dist = distance(vertex.position, transform.viewPosition);
float wf = water_color_factor(dist);
color = light * mix(objColor, waterColor, wf);
}
}