How to do it...

To create a shader program that can be used for implementing per-fragment (or Phong) shading using the Phong reflection model, use the following steps:

  1. The vertex shader simply converts the position and normal to camera coordinates and passes them to the fragment shader:
layout (location = 0) in vec3 VertexPosition;layout (location = 1) in vec3 VertexNormal;out vec3 Position;out vec3 Normal;uniform mat4 ModelViewMatrix, NormalMatrix, ProjectionMatrix, MVP;void main() {  Normal = normalize( NormalMatrix * VertexNormal);  Position = ( ModelViewMatrix * vec4(VertexPosition,1.0) ).xyz;  gl_Position = MVP * vec4(VertexPosition,1.0);}
  1. The fragment shader evaluates the Phong reflection model using the values passed from the vertex ...

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.