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
| varying vec3 normal ;
varying vec4 coordinate ;//coordinate of the fragment
void main(void)
{
//ambient component
vec4 lightVector = vec4(gl_LightSource[0].position - coordinate) ;
vec4 ambientLight = gl_FrontMaterial.ambient * gl_LightSource[0].ambient ;
//diffuse component
//lightVector = normalize(lightVector) ;
float intensity = max(dot (normal, lightVector.xyz), 0.0);
vec4 diffuseLight = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * intensity ;
//specular component
//lightVector = normalize(lightVector) ;
vec4 eyeVector = vec4(gl_ModelViewMatrixInverse[3][0] - coordinate.x,
gl_ModelViewMatrixInverse[3][1] - coordinate.y,
gl_ModelViewMatrixInverse[3][2] - coordinate.z, 0.0);
//eyeVector.xyz = normalize(eyeVector.xyz);
vec3 eyePlusLight = normalize(eyeVector.xyz + lightVector.xyz) ;
vec4 specularLight=gl_FrontMaterial.specular * /*red*/gl_LightSource[0].specular *pow(max(dot(eyePlusLight, normal), 0.0), gl_FrontMaterial.shininess);
//attenuation
float attenuation = 1.0/(gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation*distance(gl_LightSource[0].position, coordinate)
+ pow(distance(gl_LightSource[0].position, coordinate), 2.0)) ;
gl_FragColor = gl_FrontMaterial.emission + (ambientLight + diffuseLight + specularLight)*attenuation ;
} |
Partager