Game Development Reference
In-Depth Information
Finally, we are left with our
drawScene
function. Since we are rendering a single
object, we only referenced the first object of the
stageObjects
array. We activated
its buffers using the
vertexAttribPointer
API call for vertices and normals. Then,
we initialized the material's color (diffuse, specular, and ambient) uniforms. Now,
instead of using the global JavaScript variables
vertexBuffer
and
indexBuffer
, we
use the
stageObject
array's buffer objects such as
stageObject.ibo
,
stageObject.
vbo
, and
stageObject.nbo
. The following is the code for the
drawScene
function:
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
mat4.perspective(pMatrix,degToRadian(55), gl.viewportWidth /
gl.viewportHeight, 0.1, 1000.0);
setLightUniform();
var i=0;
mat4.translate(mvMatrix,mvMatrix,
stage.stageObjects[i].location);
mat4.rotateX(mvMatrix,mvMatrix,stage.stageObjects[i].rotationX);
mat4.rotateY(mvMatrix,mvMatrix,stage.stageObjects[i].rotationY);
mat4.rotateZ(mvMatrix,mvMatrix,stage.stageObjects[i].rotationZ);
setMatrixUniforms();
gl.bindBuffer(gl.ARRAY_BUFFER, stage.stageObjects[i].vbo);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,
stage.stageObjects[i].vbo.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER,stage.stageObjects[i].nbo);
gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute,
stage.stageObjects[i].nbo.itemSize, gl.FLOAT, false, 0, 0);
gl.uniform3f(shaderProgram.materialDiffuseColor,
stage.stageObjects[i].diffuseColor[0],
stage.stageObjects[i].diffuseColor[1],
stage.stageObjects[i].diffuseColor[2]);
gl.uniform3f(shaderProgram.materialAmbientColor,
stage.stageObjects[i].ambientColor[0],
stage.stageObjects[i].ambientColor[1],
stage.stageObjects[i].ambientColor[2]);
gl.uniform3f(shaderProgram.materialSpecularColor,
stage.stageObjects[i].specularColor[0],
stage.stageObjects[i].specularColor[1],
stage.stageObjects[i].specularColor[2]);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,
stage.stageObjects[i].ibo);
gl.drawElements(gl.TRIANGLES,
stage.stageObjects[i].geometry.indices.length,
gl.UNSIGNED_SHORT,0);
}