Graphics Reference
In-Depth Information
Example 14-1
Per-Fragment Lighting Vertex Shader (continued)
// Pass through texture coordinate
v_texcoord = a_texcoord0.xy;
}
Note that the vertex shader inputs and uniforms are set up automatically
by PVRShaman by setting semantics in the PerFragmentLighting.pfx
file. We have two uniform matrices that we need as input to the
vertex shader: u_matViewInverse and u_matViewProjection . The
u_matViewInverse matrix contains the inverse of the view matrix. This
matrix is used to transform the light vector and the eye vector (which
are in view space) into world space. The first four statements in main
perform this transformation and compute the light vector and view vector
in world space. The next step in the shader is to create a tangent matrix.
The tangent space for the vertex is stored in three vertex attributes:
a_normal , a_binormal , and a_tangent . These three vectors define the
three coordinate axes of the tangent space for each vertex. We construct a
3 × 3 matrix out of these vectors to form the tangent matrix tangentMat .
The next step is to transform the view and direction vectors into tangent
space by multiplying them by the tangentMat matrix. Remember,
our purpose here is to get the view and direction vectors into the
same space as the normals in the tangent-space normal map. By doing
this transformation in the vertex shader, we avoid performing any
transformations in the fragment shader. Finally, we compute the final
output position and place it in gl_Position and pass the texture
coordinate along to the fragment shader in v_texcoord .
Now we have the view and direction vector in view space and a texture
coordinate passed as out variables to the fragment shader. The next step
is to actually light the fragments using the fragment shader, as shown in
Example 14-2.
Example 14-2
Per-Fragment Lighting Fragment Shader
#version 300 es
precision mediump float;
uniform vec4 u_ambient;
uniform vec4 u_specular;
uniform vec4 u_diffuse;
uniform float u_specularPower;
(continues)
 
Search WWH ::




Custom Search