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)); ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.