Game Development Reference
In-Depth Information
For example, in Listing 4-14 a uniform variable that has a value of type float is set using the
glUniform1f()
function.
Listing 4-14. Setting a Float Uniform Shader Variable
void SetShaderUniformVariableValue(String variable, float value)
{
int loc = GLES20.glGetUniformLocation(m_ShaderProgram,variable);
GLES20.glUniform1f(loc, value);
}
The
SetShaderUniformVariableValue()
function in Listing 4-15 that follows sets a uniform shader
vec3
variable, taking as input a
Vector3
object and using a
glUniform3f()
function to set the actual
shader variable. As before, the
glGetUniformLocation()
function gets the index of the desired
variable from the shader program.
Listing 4-15. Setting a
Vector3
Uniform Shader Variable Using a
Vector3
Object
void SetShaderUniformVariableValue(String variable, Vector3 value)
{
int loc = GLES20.glGetUniformLocation(m_ShaderProgram,variable);
GLES20.glUniform3f(loc, value.x, value.y, value.z);
}
The following
SetShaderUniformVariableValue()
function in Listing 4-16 sets a
vec3
shader variable,
taking as input a float array. The function sets the
vec3
shader variable using the first three values of
the float array.
Listing 4-16. Setting a
vec3
Uniform Shader Variable Using a Float Array
void SetShaderUniformVariableValue(String variable, float[] value)
{
int loc = GLES20.glGetUniformLocation(m_ShaderProgram,variable);
GLES20.glUniform3f(loc, value[0], value[1], value[2]);
}
Function
SetShaderVariableValueFloatMatrix4Array()
in Listing 4-17 sets a uniform 4-by-4 matrix
shader variable or array. The following code sets a mat4 shader variable called
uModelViewMatrix
to
the data from the float array
ModelViewMatrix
. The count variable is set to 1, because there is only
one 4-by-4 matrix. Transpose is set to false, to indicate that the default OpenGL matrix format will be
used. Offset is the offset into
ModelViewMatrix
, where the matrix data starts.
m_Shader.SetShaderVariableValueFloatMatrix4Array("uModelViewMatrix", 1, false, ModelViewMatrix, 0);
Listing 4-17. Setting a Uniform Mat4 Shader Variable
void SetShaderVariableValueFloatMatrix4Array(String variable,
int count,
boolean transpose,
float[] value,
int offset)