-
Notifications
You must be signed in to change notification settings - Fork 0
/
fshader.glsl
108 lines (90 loc) · 2.55 KB
/
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#version 150
//gouraud
in vec4 color;
//phong, cel
in vec3 N;
in vec3 pos;
in vec4 Color;
out vec4 fColor;
uniform int mode;
uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct;
uniform mat4 ModelView;
uniform vec4 LightPosition;
uniform float Shininess;
//phong, cel
uniform vec4 idColor;
uniform int colorFlag;
//cel
uniform int Shades;
uniform int outlineMode;
void drawOutline(){
fColor = vec4(0.0, 0.0, 0.0, 1.0);
}
void cel(){
if(outlineMode == 1){
drawOutline();
} else {
int i;
vec4 temp_diffuse;
float fraction, diffuse_sum;
diffuse_sum = DiffuseProduct.r + DiffuseProduct.g + DiffuseProduct.b;
vec4 ambient, diffuse, specular;
vec3 NN = normalize(N);
vec3 E = normalize(-pos);
vec3 L = normalize((LightPosition).xyz - pos);
ambient = AmbientProduct;
float dr = max(dot(L, NN), 0.0);
diffuse = dr * DiffuseProduct;
for(i = 0; i < Shades; i++){
if(diffuse.r + diffuse.g + diffuse.b >= diffuse_sum * (1.0 * i / Shades) - 0.4){
fraction = (1.0 * i / Shades) + 0.05;
temp_diffuse.r = DiffuseProduct.r * fraction;
temp_diffuse.g = DiffuseProduct.g * fraction;
temp_diffuse.b = DiffuseProduct.b * fraction;
}
}
diffuse.r = temp_diffuse.r;
diffuse.g = temp_diffuse.g;
diffuse.b = temp_diffuse.b;
if(idColor.r + idColor.g + idColor.b > 0.0){
fColor = vec4(idColor.r, idColor.g, idColor.b, 1.0);
} else if(colorFlag == 1){
fColor = vec4(Color.r, Color.g, Color.b, 1.0);
} else {
fColor = vec4((diffuse + ambient).xyz, 1.0);
}
}
}
void phong(){
vec4 ambient, diffuse, specular;
vec3 NN = normalize(N);
vec3 E = normalize(-pos);
vec3 L = normalize((LightPosition).xyz - pos);
vec3 R = normalize(-reflect(L, NN));
ambient = AmbientProduct;
float dr = max(dot(L, NN), 0.0);
diffuse = dr * DiffuseProduct;
float sr = pow(max(dot(E, R), 0.0), Shininess);
specular = sr * SpecularProduct;
specular = clamp(specular, 0.0, 1.0);
if(idColor.r + idColor.g + idColor.b > 0.0){
fColor = vec4(idColor.r, idColor.g, idColor.b, 1.0);
} else if(colorFlag == 1){
fColor = vec4(Color.r, Color.g, Color.b, 1.0);
} else {
fColor = vec4((ambient + diffuse + specular).xyz, 1.0);
}
}
void gouraud(){
fColor = color;
}
void main()
{
if(mode == 0){
gouraud();
} else if(mode == 1){
phong();
} else if(mode == 2){
cel();
}
}