Using functions in shaders
The GLSL supports functions that are syntactically similar to C functions. However, the calling conventions are somewhat different. In the following example, we'll revisit the Phong shader using functions to help provide abstractions for the major steps.
Getting ready
As with previous recipes, provide the vertex position at attribute location 0 and the vertex normal at attribute location 1. Uniform variables for all of the Phong coefficients should be set from the OpenGL side, as well as the light position and the standard matrices.
How to do it...
The vertex shader is nearly identical to the one from the previous recipe, except that the Phong model is evaluated within a function, and we add another function to convert the position and the normal to camera coordinates:
// Uniform variables and attributes omitted... void getCamSpace( out vec3 norm, out vec3 position ) { norm = normalize( NormalMatrix * VertexNormal); position = (ModelViewMatrix * vec4(VertexPosition...