-
Notifications
You must be signed in to change notification settings - Fork 0
/
frag.glsl
49 lines (41 loc) · 1.49 KB
/
frag.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
#version 410
// Define INPUTS from fragment shader
uniform mat4 view_mat;
smooth in vec3 lightContrib;
in vec2 texture_coords;
in vec3 frag_Position;
in vec3 normal;
// And from the uniform outputs for the textures setup in main.cpp.
uniform sampler2D texture00;
uniform sampler2D texture01;
uniform vec3 light_position;
uniform vec3 light_color;
uniform float shininess;
uniform bool useText;
uniform bool useSpec;
uniform bool useDiff;
out vec4 fragment_color; //RGBA color
void main () {
float toonLevels = 4.0;
float toonThreshold = 1.0 / toonLevels;
vec3 lightDir = normalize(light_position - frag_Position);
vec3 viewDir = normalize(vec3(0.0, 0.0, 10.0) - frag_Position);
vec3 halfwayDir = normalize(lightDir + viewDir);
float ambientStrength = 0.2;
float specFactor = pow(max(dot(normal, halfwayDir), 0.0), shininess);
float specToonFactor = floor(specFactor * toonLevels) / toonLevels;
vec3 specular = light_color * specToonFactor;
float diffFactor = max(dot(normal, lightDir), 0.0);
float diffToonFactor = floor(diffFactor * toonLevels) / toonLevels;
vec3 diffuse = light_color * diffToonFactor;
vec3 ambient = light_color * ambientStrength;
vec3 lightContrib = ambient;
if(useSpec) {lightContrib += specular;}
if(useDiff) {lightContrib += diffuse;}
if(useText) {
fragment_color = texture2D(texture00, texture_coords) * vec4(lightContrib, 1.0);
}
else{
fragment_color = vec4(1.0, 0.5, 0.0, 1.0) * vec4(lightContrib, 1.0);
}
}