Game Development Reference
In-Depth Information
The following lines of code qualify a matrix as a uniform in the fragment shader code:
uniform mat4 mvMatrix;
uniform mat4 pMatrix;
The following is the code to get the reference of the shader uniform variable:
function initShaders() {
…………………………………
………………………
shaderProgram.pMatrixUniform =
gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform =
gl.getUniformLocation(shaderProgram, "uMVMatrix");
……………………………
……………………………
}
The following is the code to load data to a uniform:
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false,
pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false,
mvMatrix);
}
Please note that we chose to qualify the transformation matrix as a uniform because
we know that the transformation matrix will not change for one particular primitive
rendering operation.
The varying qualifier
The varying qualifier is the only way a vertex shader can communicate results to a
fragment shader. These variables form the dynamic interface between vertex and
fragment shaders. Let's consider a case where each vertex of a primitive, such as a
triangle, has a different color and we add light to our scene. Lights might change the
color at each vertex. Now, in our vertex shader, we will calculate the new color value
of each vertex and share the new color value to the fragment shader using a varying
qualified vector; the fragment shader interpolates the color between vertices and sets
the color of each fragment/pixel. A vertex shader writes to a varying variable and
that value is read in the fragment shader.
 
Search WWH ::




Custom Search