Game Development Reference
In-Depth Information
Attributes
Attribute-qualified variables are used for data that is passed to shaders frequently.
Since every vertex has different attributes, we generally use this qualifier for vertex
data. Now in our case (square example), each vertex has a different position, hence,
we qualify the aVertexPosition vector as the attribute. If each vertex had a different
color value, we would have qualified another vector aVertexColor as an attribute.
The following line of code qualifies a vector as an attribute in the vertex shader code:
attribute vec3 aVertexPosition;
The following code gets the reference/index of the shader variable. The variables
(attributes) that are declared in the shaders are initialized in the main control code.
In order to initialize them, we need to get a reference to them in the control code.
The getAttribLocation function does just that, as shown in the following snippet:
function initShaders() {
shaderProgram.vertexPositionAttribute =
gl.getAttribLocation(shaderProgram, "aVertexPosition");//
getting the reference to the attribute aVertexPosition from the
flow code
}
Only floating point scalars, floating point vectors, or matrices can be qualified as
an attribute.
Uniforms
The uniform qualifier, just like the attribute qualifier, is used to pass data to the
shaders. But the uniform value should not change per fragment/primitive operation.
A primitive can have many vertices, hence, a uniform cannot be used to qualify
per vertex data. However, a primitive might have the same transformation that can
be applied to all vertices, hence, the transformation matrix is stored in a variable
qualified as a uniform.
The uniform qualified variable cannot be modified inside the shader. Also, vertex
shaders and fragment shaders have a shared global namespace for uniforms, so,
uniforms of the same name will be the same in vertex and fragment shaders. All data
types and arrays of all data types are supported for uniform qualified variables.
Let's take a look at how we load uniform data to shaders.
 
Search WWH ::




Custom Search