Game Development Reference
In-Depth Information
We also have modified the
setLightUniform()
function to initialize our light
objects and added each light to our
lights
object. We then initialized our uniforms'
variable, which is returned from
lights.getDataByType
, with single dimensional
arrays. In the following code, the WebGL function we used to initialize the uniforms
is
gl.uniform3fv
. The following is the code for the
setLightUniform()
function:
function setLightUniform(){
var light=new Light();
light.ambientColor=[0.1,0.1,0.1];
light.diffuseColor=[0.5,0.5,0.5];
light.specularColor=[1.0,1.0,1.0];
light.direction=[0,-1.25,-1.25];
var light1=new Light();
light1.diffuseColor=[lightColor[0],lightColor[1],
lightColor[2]];
light1.specularColor=[0.0,0.0,0.0];
light1.position=[20.0,5.0,-100.0];
var light2=new Light();
light2.diffuseColor= [1.0,1.0,1.0];
light2.specularColor=[0.0,0.0,0.0];
light2.position=[20.0,5.0,200.0];
var lights=new Lights();
lights.addLight(light);
lights.addLight(light1);
lights.addLight(light2);
gl.uniform3fv(shaderProgram.uAmbientColor,
lights.getDataByType("ambientColor"));
gl.uniform3fv(shaderProgram.uDirectionalDiffuseColor,
lights.getDataByType("diffuseColor","direction"));
gl.uniform3fv(shaderProgram.uDirectionalSpecularColor,
lights.getDataByType("specularColor","direction"));
gl.uniform3fv(shaderProgram.uPositionalDiffuseColor,
lights.getDataByType("diffuseColor","position"));
gl.uniform3fv(shaderProgram.uPositionalSpecularColor,
lights.getDataByType("specularColor","position"));
gl.uniform3fv(shaderProgram.uLightPosition,
lights.getDataByType("position"));
gl.uniform3fv(shaderProgram.uLightDirection,
lights.getDataByType("direction"));
}