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
|
// -*- mode: c -*-
struct Material {
vec3 ambient;
sampler2D ambientMap;
bool useAmbientMap;
vec3 diffuse;
sampler2D diffuseMap;
bool useDiffuseMap;
vec3 specular;
sampler2D specularMap;
bool useSpecularMap;
float shininess;
sampler2D bumpMap;
bool useBumpMap;
};
struct DirectionalLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#ifdef GLSL120
varying vec3 fragNorm;
varying vec2 fragTex;
#else
in vec3 fragNorm;
in vec2 fragTex;
#endif
#ifdef GLSL330
out vec4 fragColor;
#endif
uniform Material material;
uniform DirectionalLight directionalLight;
void main() {
vec3 baseAmbientColor;
vec3 baseDiffuseColor;
vec3 baseSpecularColor;
if(material.useAmbientMap) {
#ifdef GLSL330
baseAmbientColor = texture(material.ambientMap, fragTex).xyz;
#else
baseAmbientColor = texture2D(material.ambientMap, fragTex).xyz;
#endif
} else {
baseAmbientColor = vec3(1.0, 1.0, 1.0);
}
if(material.useDiffuseMap) {
// discard transparent fragments.
#ifdef GLSL330
vec4 color = texture(material.diffuseMap, fragTex);
#else
vec4 color = texture2D(material.diffuseMap, fragTex);
#endif
if(color.a == 0.0) { discard; }
baseDiffuseColor = color.xyz;
} else {
baseDiffuseColor = vec3(1.0, 1.0, 1.0);
}
if(material.useSpecularMap) {
#ifdef GLSL330
baseSpecularColor = texture(material.specularMap, fragTex).xyz;
#else
baseSpecularColor = texture2D(material.specularMap, fragTex).xyz;
#endif
} else {
baseSpecularColor = vec3(1.0, 1.0, 1.0);
}
vec3 ambientColor = material.ambient * baseAmbientColor * baseDiffuseColor;
vec3 lightDir = normalize(-directionalLight.direction);
float diffuseFactor = max(dot(lightDir, fragNorm), 0.0);
vec3 diffuseColor = diffuseFactor * baseDiffuseColor * material.diffuse;
vec3 reflectDir = reflect(-lightDir, fragNorm);
float specularFactor = 0;
if(material.shininess > 0) {
specularFactor = pow(max(dot(lightDir, reflectDir), 0.0), material.shininess);
}
vec3 specularColor = specularFactor * baseSpecularColor * material.specular;
#ifdef GLSL330
fragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
#else
gl_FragColor = vec4(ambientColor + diffuseColor + specularColor, 1.0);
#endif
}
|