Graphics Programs Reference
In-Depth Information
▪ Inside the vertex shader, store the current (transformed) normal in
a variable of type
vec3
using the following code. This normal
corresponds to the
N
vector, shown in
Figure 5-8
.
vec3 normal
= normalize(vec3(uNormal * aNormal));
▪ Here,
uNormal
is the uniform variable (of type
mat3
) to store
the normal matrix, and
aNormal
is an attribute variable that re-
ceives vertex normals. Similar to how we pass vertices using
glVertexAttribPointer
, we also pass the vertex normals.
Recall from
Chapter 4
(“Parsing Objects for OpenGL ES”) that
you can access the vertex normals from the “normal:” block in the
parser's output text file.
▪ The
normalize
function, as the name suggests, is a built-in
function in ES 2.0 that helps normalize the given vector. Normal-
ization is necessary to avoid any scaling of the vectors.
▪ To pass the 3x3 normal matrix to the vertex shader, use the ES 2.0
function
glUniformMatrix3fv
, and call it as:
GLES20.glUniformMatrix3fv(_tankUNormalLocation,
1, false, _tankNormalMatrix, 0);
▪ Here,
_tankNormalMatrix
(
float[9]
) is the normal mat-
rix, obtained by copying the upper-left 3x3 portion of the
MV
matrix corresponding to the object (
Listing 5-14
)
. You do not
need to create a separate
MV
matrix to copy the required values;
instead, copy the values from the
MVP
matrix, before it is com-
bined with the projection transformation, as shown in the follow-
ing code.
Listing 5-14.
VERTEX POINT LIGHTING/src/com/apress/android/vertexpoint-
lighting/GLES20Renderer.java
_tankNormalMatrix[0] = _tankMVPMatrix[0];
_tankNormalMatrix[1] = _tankMVPMatrix[1];
_tankNormalMatrix[2] = _tankMVPMatrix[2]; // from 1st
column, ending at [3]
_tankNormalMatrix[3] = _tankMVPMatrix[4];
_tankNormalMatrix[4] = _tankMVPMatrix[5];
Search WWH ::

Custom Search