How to do it...

To implement a shader pair that uses the Phong reflection model with two-sided lighting, take the following steps:

  1. The vertex shader is similar to the one in the previous recipe, except that it computes the Phong equation twice. First, without any change to the normal vector, and again with the normal inverted. The results are stored in output variables FrontColor and BackColor, respectively:
// Uniforms and attributes...out vec3 FrontColor;out vec3 BackColor;vec3 phongModel( vec3 position, vec3 n ) { 
    // The Phong model calculations go here...
} 
 
void main() {    vec3 tnorm = normalize( NormalMatrix * VertexNormal);    vec3 camCoords = (ModelViewMatrix *     vec4(VertexPosition,1.0)).xyz; FrontColor = phongModel( camCoords, tnorm ...

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.