Game Development Reference
In-Depth Information
indices = getIndicesFromFaces(faces);
if(data.materials.length>0){
diffuseColor=data.materials[0].colorDiffuse;
diffuseColor.push(1.0);//Added alpha channel
}
initScene();
});
}
function initScene(){
initBuffers();
gl.clearColor(0.6, 0.6, 0.6, 1.0);
gl.enable(gl.DEPTH_TEST);
drawScene();
}
The
start()
function invokes the
loadModel()
function. The
loadModel()
function
invokes the jQuery library's
$.getJSON()
function. The jQuery response handler
converts the response string to a JSON object. From the JSON object, we directly
assign vertices to the
vertices
array and faces to our
indices
array. Then, we
check the length of the
materials
array (as explained earlier, an object can have
multiple materials, but in our case, we have a single material) and then assign the
colorDiffuse
variable from the first material object to the
diffuseColor
global
variable. Then, the
start()
function invokes the
initScene()
function that
initializes the vertex buffer and the index buffer.
function initShaders() {
...
shaderProgram.materialDiffuseColor =
gl.getUniformLocation(shaderProgram, "materialDiffuseColor");
...
}
In the preceding code, in the
initShaders()
function, we added a new variable,
shaderProgram.materialDiffuseColor
, that holds the reference to the
materialDiffuseColor
uniform in the fragment shader.
function setMatrixUniforms() {
...
gl.uniform4f(shaderProgram.materialDiffuseColor,diffuseColor[0],
diffuseColor[1],diffuseColor[2],diffuseColor[3]);
}