Game Development Reference
In-Depth Information
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Shaders cannot be initialized");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute =
gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray
(shaderProgram.vertexPositionAttribute);
shaderProgram.pMatrixUniform =
gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform =
gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
The createShader() function parses our HTML document tree; it then loads the
shader code in the string variable before assigning it to context for compiling. The
parent function initShaders() first creates the program object and then attaches
shaders to the program object. The final step is linking. Our two different shaders
need to work together. The link step verifies that they actually match up. If the vertex
shader passes data on to the fragment shader, the link step makes sure that the
fragment shader actually accepts that input. The two shader sources are compiled
into a program, which is passed to the useProgram() function to be used. The
last few lines of the function get references/indexes to the attribute and uniform
variables so that we can later associate attributes to the buffer objects.
Associating buffer objects with shader
attributes
We know how to allocate a buffer object and have understood how to get a
reference to the shader attribute. Now, we need to associate the buffer object to
the vertex shader attribute, so that the following shader code knows where to load
its data from. The first line of the following code makes the buffer that we need
to associate as the current buffer. The second line associates the current buffer
( squareVertexPositionBuffer ) with the shader attribute ( shaderProgram.
vertexPositionAttribute ):
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
The parameters of the function glVertexAttribPointer are defined as follows:
void glVertexAttribPointer(Index,Size,Type,Norm,Stride,Offset)
 
Search WWH ::




Custom Search