Game Development Reference
In-Depth Information
The following code gets the reference to the aVertexColor attribute and enables
it. The variables are declared in shaders but are initialized in the control flow code.
Hence, we obtain a reference to them, as shown in the following code:
function initShaders() {
shaderProgram.vertexColorAttribute =
gl.getAttribLocation(shaderProgram, "aVertexColor");// Getting
the reference to the attribute aVertexColor from the flow code
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
//Enabling the vertex color attribute.
}
We just added two lines. The first line activates the buffer object and the second
line associates the buffer object to the vertex shader attribute aVertexColor
through the reference vertexColorAttribute. The following is the code for the
drawScene() function:
function drawScene() {
//Activate the buffer object colorBuffer for operations
gl.bindBuffer(gl.ARRAY_BUFFER,colorBuffer);
//Associating buffer object(colorBuffer) with shader attribute
aVertexColor whose refrence is in vertexColorAttribute
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, 4,
gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.drawElements(gl.TRIANGLES, indices.length,
gl.UNSIGNED_SHORT,0);
}
 
Search WWH ::




Custom Search