-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshader.frag
117 lines (91 loc) · 3.29 KB
/
shader.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#version 400
struct Light
{
vec3 position;
vec3 diffuse_color;
vec3 specular_color;
};
in vec3 frag_position;
in vec3 frag_normal;
in vec4 shadow_coord[2];
in vec2 texture_coord;
in vec3 skybox_texcoords;
out vec4 outputColor;
uniform mat4 view_matrix;
uniform mat4 inv_view_matrix;
uniform vec3 diffuse_color;
uniform sampler2DShadow shadow_text[2];
uniform sampler2D object_texture;
uniform bool texture_enabled;
uniform bool skybox_enabled;
uniform bool shadow_enabled;
uniform bool light_enabled[2];
uniform bool reflection_enabled;
uniform samplerCube cube_texture;
uniform Light lights[2];
void main(void)
{
int light_count = 2;
vec3 diffuse_color_ = diffuse_color;
if (texture_enabled) {
diffuse_color_ = vec3(texture(object_texture, texture_coord));
}
vec3 diffuse = vec3(0);
vec3 specular = vec3(0);
float bias = 0.999;
for (int i = 0; i < light_count; ++i)
{
if (!light_enabled[i])
continue;
vec4 shadow_coord2 = shadow_coord[i];
shadow_coord2.z *= bias;
float shadow = 1;
float shadow_coeff = textureProj(shadow_text[i] , shadow_coord2);
if (shadow_coeff < 1.0)
shadow = 0.5;
vec3 lightPos = lights[i].position;
vec3 diffuseColor = lights[i].diffuse_color;
vec3 specColor = lights[i].specular_color;
lightPos = vec3(view_matrix * vec4(lightPos,1));
vec3 normal = normalize(frag_normal);
vec3 lightDir = normalize(lightPos - frag_position);
if (!shadow_enabled)
shadow = 1.f;
float lambertian = max(dot(lightDir,normal), 0.0);
diffuse += shadow * lambertian * diffuse_color_;
if(lambertian > 0.0) {
vec3 viewDir = normalize(-frag_position);
vec3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, normal), 0.0);
specular += shadow * pow(specAngle, 16.0) * specColor;
}
}
//float bias = 1-(0.001*tan(acos(dot(normal, lightDir))));
vec3 ambient = vec3(0.0);
// EFFETS
vec3 incident_eye = normalize (frag_position);
vec3 normal_eye = normalize (frag_normal);
//REFLECTION
vec3 reflected = reflect (incident_eye, normal_eye);
reflected = vec3 (inv_view_matrix * vec4 (reflected, 0.0));
//REFRACTION
float ratio = 1.0 /1.3333;
vec3 refracted = refract (incident_eye, normal_eye, ratio);
refracted = vec3 (inv_view_matrix * vec4 (refracted, 0.0));
if (skybox_enabled) {
outputColor = vec4(texture (cube_texture, skybox_texcoords));
} else if (texture_enabled) {
int x_row = int((texture_coord.x - 0.703) / 0.036);
int y_row = int((texture_coord.y - 0.709) / 0.0353);
if(reflection_enabled && (x_row <8 && x_row >=0 && y_row <8 && y_row >=0)
&&(((x_row %2 == 0)&&( y_row %2 ==0))
||((x_row %2 == 1)&&( y_row %2 ==1)))){
outputColor = vec4(ambient + diffuse + specular, 1.0) + 0.6*texture (cube_texture, reflected);
}else{
outputColor = vec4(ambient + diffuse + 0.7*specular, 1.0);
}
} else {
//outputColor = vec4(ambient + diffuse + specular, 1.0) + 0.9*texture (cube_texture, refracted);
outputColor = vec4(ambient + diffuse + specular, 1.0);
}
}