Graphics Programs Reference

In-Depth Information

basis, we can pass data to uniform variables at run time using the ES 2.0 function

GLES20.glUniformMatrix4fv
.

Listing 3-27.
GL CUBE/src/com/apress/android/glcube/GLES20Renderer.java

private final String _cubeVertexShaderCode =

"attribute vec3 aPosition;"

+ "attribute vec4 aColor;"

+ "varying vec4 vColor;"

+ "uniform mat4 uMVP;"

+ "void main() {"

+ " vColor = aColor;"

+ " vec4 vertex =

vec4(aPosition[0],aPosition[1],aPosition[2],1.0);"

+ " gl_Position = uMVP * vertex;"

+ "}";

Variable
gl_Position
is finally assigned the value
uMVP * vertex
. In this

multiplication, the matrix comes before per-vertex position data, because matrices in

OpenGL ES (
mat2
,
mat3
, and
mat4
) are stored in column major order (the meth-

ods of
Matrix
class also operate on column-vector matrices because of this). Please

note that, if
vertex
variable inside
main
was of type
vec3
, the result of multiplic-

ation would have been invalid, as the uniform variable
uMVP
is of type
mat4
(recall,

that
mat4
is a square matrix).

Like
glGetAttribLocation
,
glGetUniformLocation
is used to obtain

the location of uniform variable. Renderer class has field
_cubeUMVPLocation

to store this location, and location of uniform is stored using
_cubeUMVPLoca-

tion

=

GLES20.glGetUniformLocation(_cubeProgram,

"uMVP");
.

Finally, this value is loaded to the uniform variable by calling

GLES20.glUniformMatrix4fv(_cubeUMVPLocation, 1, false,

_MVPMatrix, 0)
. As shown in
Figure 3-22
,
the rendered cube is incomplete. It

is an exercise for you to complete this cube.

Search WWH ::

Custom Search