How it works...

Within the vertex shader, the fourth coordinate of the uniform variable Light.Position is used to determine whether or not the light is to be treated as a directional light. Inside the phongModel function, which is responsible for computing the shading equation, the value of the vector s is determined based on whether or not the fourth coordinate of Light.Position is zero. If the value is zero, Light.Position is normalized and used as the direction toward the light source. Otherwise, Light.Position is treated as a location in eye coordinates, and we compute the direction toward the light source by subtracting the vertex position from Light.Position and normalizing the result.

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.