]> Dogcows Code - chaz/rasterize/blob - fragment.glsl
add opengl support
[chaz/rasterize] / fragment.glsl
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #define MAX_LIGHTS 2
9
10 uniform int numLights;
11 uniform vec4 ambient;
12 uniform vec4 mspecular;
13 uniform float shininess;
14 uniform vec4 diffuse[MAX_LIGHTS];
15 uniform vec4 specular[MAX_LIGHTS];
16 uniform sampler2D sampler;
17 uniform bool isTexture;
18
19 varying vec3 normal;
20 varying vec2 textureCoord;
21 varying vec4 color;
22 varying vec3 lightDir[MAX_LIGHTS];
23 varying vec3 eyeVec;
24
25 void main()
26 {
27 gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
28
29 vec4 c = color;
30 if (isTexture) {
31 c *= texture2D(sampler, vec2(textureCoord.x, 1.0 - textureCoord.y));
32 }
33
34 vec3 N = normalize(normal);
35 int i;
36 for (i = 0; i < numLights; ++i) {
37 vec3 L = normalize(lightDir[i]);
38 float kd = dot(N, L);
39 if (0.0 < kd) {
40 gl_FragColor += diffuse[i] * c * kd;
41 vec3 E = normalize(eyeVec);
42 vec3 R = reflect(-L, N);
43 float ks = pow(max(dot(R, E), 0.0), shininess);
44 gl_FragColor += specular[i] * mspecular * ks;
45 }
46 }
47 gl_FragColor += ambient * c;
48
49 gl_FragColor.a = color.a; // preserve the model's alpha
50 }
51
This page took 0.032454 seconds and 4 git commands to generate.