1+ #version 100
2+
3+ precision mediump float ;
4+
5+ // Input vertex attributes (from vertex shader)
6+ varying vec3 fragPosition;
7+ varying vec2 fragTexCoord;
8+ varying vec4 fragColor;
9+ varying vec3 fragNormal;
10+
11+ // Input uniform values
12+ uniform sampler2D texture0;
13+ uniform vec4 colDiffuse;
14+
15+ // NOTE: Add here your custom variables
16+
17+ #define MAX_LIGHTS 4
18+ #define LIGHT_DIRECTIONAL 0
19+ #define LIGHT_POINT 1
20+
21+ struct Light {
22+ int enabled;
23+ int type;
24+ vec3 position;
25+ vec3 target;
26+ vec4 color;
27+ };
28+
29+ // Input lighting values
30+ uniform Light lights[MAX_LIGHTS];
31+ uniform vec4 ambient;
32+ uniform vec3 viewPos;
33+
34+ void main()
35+ {
36+ // Texel color fetching from texture sampler
37+ vec4 texelColor = texture2D (texture0, fragTexCoord);
38+ vec3 lightDot = vec3 (0.0 );
39+ vec3 normal = normalize (fragNormal);
40+ vec3 viewD = normalize (viewPos - fragPosition);
41+ vec3 specular = vec3 (0.0 );
42+
43+ // NOTE: Implement here your fragment shader code
44+
45+ for (int i = 0 ; i < MAX_LIGHTS; i++ )
46+ {
47+ if (lights[i].enabled == 1 )
48+ {
49+ vec3 light = vec3 (0.0 );
50+
51+ if (lights[i].type == LIGHT_DIRECTIONAL)
52+ {
53+ light = - normalize (lights[i].target - lights[i].position);
54+ }
55+
56+ if (lights[i].type == LIGHT_POINT)
57+ {
58+ light = normalize (lights[i].position - fragPosition);
59+ }
60+
61+ float NdotL = max (dot (normal, light), 0.0 );
62+ lightDot += lights[i].color.rgb* NdotL;
63+
64+ float specCo = 0.0 ;
65+ if (NdotL > 0.0 ) specCo = pow (max (0.0 , dot (viewD, reflect (- (light), normal))), 16.0 ); // 16 refers to shine
66+ specular += specCo;
67+ }
68+ }
69+
70+ vec4 finalColor = (texelColor* ((colDiffuse + vec4 (specular, 1.0 ))* vec4 (lightDot, 1.0 )));
71+ finalColor += texelColor* (ambient/ 10.0 );
72+
73+ // Gamma correction
74+ gl_FragColor = pow (finalColor, vec4 (1.0 / 2.2 ));
75+ }
0 commit comments