How to do it...

To create a shader program that uses a subroutine to switch between pure-diffuse and Phong, take the following steps:

  1. Set up the vertex shader with a subroutine uniform variable, and two functions of the subroutine type:
subroutine vec3 shadeModelType( vec3 position, vec3 normal); 
subroutine uniform shadeModelType shadeModel; out vec3 LightIntensity;
 
// Uniform variables and attributes here...  
subroutine( shadeModelType ) 
vec3 phongModel( vec3 position, vec3 norm ) { 
    // The Phong reflection model calculations go here...
} 
 
subroutine( shadeModelType ) 
vec3 diffuseOnly( vec3 position, vec3 norm ) { 
   // Compute diffuse shading only..
} 
 
void main() {   // Compute camPosition and camNorm ... // Evaluate the shading equation, calling ...

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.