= cutOffAngle)
return 0.0;
else if (spotAngle <= beamWidth)
return 1.0;
return (spotAngle - cutOffAngle) / (beamWidth - cutOffAngle);
}
vec4
getMaterialColor (in x3d_MaterialParameters material)
{
vec3 N = getNormal (normalize (gl_FrontFacing ? vN : -vN));
vec3 V = normalize (-v); // normalized vector from point on geometry to viewer's position
float dV = length (v);
// Calculate diffuseFactor & alpha
vec3 diffuseFactor = vec3 (1.0, 1.0, 1.0);
float alpha = 1.0 - material .transparency;
vec4 T = getTextureColor ();
diffuseFactor = T .rgb * material .diffuseColor;
alpha *= T .a;
vec3 ambientTerm = diffuseFactor * material .ambientIntensity;
// Apply light sources
vec3 finalColor = vec3 (0.0, 0.0, 0.0);
for (int i = 0; i < 1; ++ i)
{
x3d_LightSourceParameters light = x3d_LightSource [i];
vec3 vL = light .location - v;
float dL = length (vL);
bool di = light .type == x3d_DirectionalLight;
if (di || dL <= light .radius)
{
vec3 d = light .direction;
vec3 c = light .attenuation;
vec3 L = di ? -d : normalize (vL); // Normalized vector from point on geometry to light source i position.
vec3 H = normalize (L + V); // Specular term
float lightAngle = dot (N, L); // Angle between normal and light ray.
vec3 diffuseTerm = diffuseFactor * clamp (lightAngle, 0.0, 1.0);
float specularFactor = material .shininess > 0.0 ? pow (max (dot (N, H), 0.0), material .shininess * 128.0) : 1.0;
vec3 specularTerm = material .specularColor * specularFactor;
float attenuationFactor = di ? 1.0 : 1.0 / max (c [0] + c [1] * dL + c [2] * (dL * dL), 1.0);
float spotFactor = light .type == x3d_SpotLight ? getSpotFactor (light .cutOffAngle, light .beamWidth, L, d) : 1.0;
float attenuationSpotFactor = attenuationFactor * spotFactor;
vec3 ambientColor = light .ambientIntensity * ambientTerm;
vec3 ambientDiffuseSpecularColor = ambientColor + light .intensity * (diffuseTerm + specularTerm);
finalColor += attenuationSpotFactor * (light .color * ambientDiffuseSpecularColor);
}
}
finalColor += material .emissiveColor;
return vec4 (finalColor, alpha);
}
void
main ()
{
gl_FragColor = getMaterialColor (x3d_Material);
}
]]>