1
- // Fragment Shader – file "minimal .frag"
1
+ // Fragment Shader – file "mesh_pointlight .frag"
2
2
3
3
#version 140
4
4
5
5
precision highp float ; // needed only for version 1.30
6
6
7
7
// Light vars
8
- uniform vec3 lightPositionW = vec3 (0 .0f, 1 .5f, 0 .0f);
9
- uniform vec4 lightColor = vec4 (1 .0f, 1 .0f, 1 .0f, 1 .0f);
10
- uniform float lightIntensity = 1 .0f;
8
+ uniform vec4 ambLightColor = vec4 (1 .0f, 1 .0f, 1 .0f, 1 .0f);
9
+ uniform float ambLightIntensity = 0 .15f;
10
+
11
+ uniform vec4 diffuseLightColor = vec4 (1 .0f, 1 .0f, 1 .0f, 1 .0f);
12
+ uniform float diffuseLightIntensity = 1 .0f;
11
13
uniform float cAttenuation = 1 .0f;
12
14
uniform float lAttenuation = 0 .08f;
13
15
uniform float qAttenuation = 0 .01f;
14
16
15
- // Viewer position
16
- uniform vec3 eyePositionW;
17
+ uniform float specularHardness = 32 .0f;
18
+ uniform vec4 specularLightColor = vec4 (1 .0f, 1 .0f, 1 .0f, 1 .0f);
19
+ uniform float specularLightIntensity = 1 .0f;
17
20
18
21
// Texture vars
19
22
uniform sampler2D texDiff;
20
23
uniform sampler2D texAmb;
21
24
uniform int diffuseTexEnabled;
22
25
uniform int ambientTexEnabled;
23
- uniform vec4 ambLightColor = vec4 (1 .0f, 1 .0f, 1 .0f, 1 .0f);
24
- uniform float ambLightIntensity = 0 .15f;
25
26
26
27
in vec3 ex_Color;
27
28
in vec3 ex_Normal;
28
29
in vec2 ex_UV;
29
- in vec3 ex_PositionW;
30
+ in vec3 ex_LightDirW;
31
+ in vec3 ex_ViewDirW;
30
32
31
33
out vec4 out_Color;
32
34
33
35
void main(void )
34
36
{
35
- vec4 diffuseLight;
36
- vec4 ambientLight;
37
- vec3 lightDir = lightPositionW - ex_PositionW;
38
- float distance = length (lightDir);
39
- lightDir = normalize (lightDir);
37
+ vec4 ambientLight, diffuseLight, specularLight;
38
+
39
+ specularLight = vec4 (0 .0f, 0 .0f, 0 .0f, 0 .0f);
40
40
41
- ambientLight = ambLightIntensity* ambLightColor;
41
+ vec3 viewDirW = normalize (ex_ViewDirW);
42
+ vec3 normal = normalize (ex_Normal);
43
+
44
+ float distance = length (ex_LightDirW);
45
+ vec3 lightDirW = normalize (ex_LightDirW);
46
+
47
+ ambientLight = vec4 (ambLightIntensity* ambLightColor.xyz, ambLightColor.w);
42
48
43
- float NdL = clamp (dot (normalize (ex_Normal), lightDir), 0 .0f, 1 .0f);
44
49
float attenuation = 1 .0f / (cAttenuation + lAttenuation* distance + qAttenuation* distance * distance );
45
- diffuseLight = vec4 (lightIntensity* lightColor.xyz* NdL* attenuation, lightColor.w);
50
+
51
+ float NdL = clamp (dot (normal, lightDirW), 0 .0f, 1 .0f);
52
+ diffuseLight = vec4 (diffuseLightIntensity* diffuseLightColor.xyz* NdL* attenuation, diffuseLightColor.w);
53
+
54
+ if (NdL > 0 .0f) {
55
+ vec3 H = normalize (lightDirW + viewDirW);
56
+ float NdH = clamp (dot (normal, H), 0 .0f, 1 .0f);
57
+ specularLight = vec4 (specularLightIntensity* diffuseLightColor.xyz* pow (NdH, specularHardness), diffuseLightColor.w);
58
+ }
46
59
47
60
if (ambientTexEnabled >= 1 ) {
48
61
vec4 ambTextureColor = texture(texAmb,ex_UV);
@@ -54,5 +67,5 @@ void main(void)
54
67
diffuseLight *= diffTextureColor;
55
68
}
56
69
57
- out_Color = vec4 (ex_Color, 1 .0f)* (clamp (diffuseLight+ ambientLight, 0 .0f, 1 .0f));
70
+ out_Color = vec4 (ex_Color, 1 .0f)* (clamp (( diffuseLight+ specularLight) + ambientLight, 0 .0f, 1 .0f));
58
71
}
0 commit comments