How to do it...

To create a shader program that uses the ADS shading model with a spotlight, use the following code for the fragment shader:

in vec3 Position; 
in vec3 Normal; 
 
uniform struct SpotLightInfo {
vec3 Position; // Position in cam coords
vec3 L; // Diffuse/spec intensity
vec3 La; // Amb intensity
vec3 Direction; // Direction of the spotlight in cam coords.
float Exponent; // Angular attenuation exponent
float Cutoff; // Cutoff angle (between 0 and pi/2)
} Spot;

// Material uniforms... layout( location = 0 ) out vec4 FragColor; vec3 blinnPhongSpot( vec3 position, vec3 n ) {
vec3 ambient = Spot.La * Material.Ka,
diffuse = vec3(0), spec = vec3(0);
vec3 s = normalize( Spot.Position - position );
float cosAng = dot(-s, normalize(Spot.Direction));
float angle = acos( cosAng );
float spotScale = 0.0;
if(angle < Spot.Cutoff ) {
spotScale = pow( cosAng, Spot.Exponent );
float sDotN = max( dot(s,n), 0.0 );
diffuse = Material.Kd * sDotN;
if( sDotN > 0.0 ) {
vec3 v = normalize(-position.xyz);
vec3 h = normalize( v + s );
spec = Material.Ks *
pow( max( dot(h,n), 0.0 ), Material.Shininess );
}
}
return ambient + spotScale * Spot.L * (diffuse + spec);
}

void main() {
FragColor = vec4(blinnPhongSpot(Position, normalize(Normal)), 1);
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.126.5