CHAPTER 1: Computer Graphics: From Then to Now (^27)
What About the Shaders?
Here is not the place to get into a detailed breakdown of shader design and the
language, but let’s remove a little of the mystery by playing with those as well. Listing 1-
3 is the vertex shader.
Listing 1-3. Shader.vsh that preprocesses the vertices.
attribute vec4 position;
attribute vec3 normal;
varying lowp vec4 colorVarying;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
void main()
{
vec3 eyeNormal = normalize(normalMatrix normal);
vec3 lightPosition = vec3(0.0, 0.0, 1.0); //1
vec4 diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);
float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));
colorVarying = diffuseColor nDotVP;
gl_Position = modelViewProjectionMatrix position; //2
}
Here in the vertex shader is where the light is hidden for this particular cube; the values
are the x, y, and z values. Change the middle value to 5.0, which will move it way above
the scene but will affect only the blue cube.
In line 2, gl_Position is predefined object that carries the position of the current vertex.
Add in the following line to the end: gl_Position.x=.5;. Figure 1-11a shows the result.
singke
(singke)
#1