ch3_Wall.html
in your HTML5 browse. You will see something similar to the following screenshot:<script id="shader-vs" type="x-shader/x-vertex">)
. Make sure that you identify the attributes uniforms and varyings that are declared there. runWebGLApp
function. Verify that we are calling initProgram
and initLights
there. initProgram
. Make sure you understand how the program is built and how we obtain references to attributes and uniforms. initLights
. Update the values of the uniforms, as shown here.gl.uniform3fv(prg.uLightDirection, [0.0, 0.0, -1.0]); gl.uniform4fv(prg.uLightAmbient, [0.1,0.1,0.1,1.0]); gl.uniform4fv(prg.uLightDiffuse, [0.6,0.6,0.6,1.0]); gl.uniform4fv(prg.uMaterialDiffuse, [0.6,0.15,0.15,1.0]);
uniform4f
to uniform4fv
for the uniform uMaterialDiffuse
. initLights
function, write the following code:var azimuth = 0; var elevation = 0; document.onkeypress = processKey; function processKey(ev){ var lightDirection = gl.getUniform(prg,prg.uLightDirection); var incrAzimuth = 10; var incrElevation = 10; switch(ev.keyCode){ case 37:{ // left arrow azimuth -= incrAzimuth; break; } case 38:{ //up arrow elevation += incrElevation; break; } case 39:{ // right arrow azimuth += incrAzimuth; break; } case 40:{ //down arrow elevation -= incrElevation; break; } } azimuth %= 360; elevation %=360; var theta = elevation * Math.PI / 180; var phi = azimuth * Math.PI / 180; //Spherical to Cartesian coordinate transformation lightDirection[0] = Math.cos(theta)* Math.sin(phi); lightDirection[1] = Math.sin(theta); lightDirection[2] = Math.cos(theta)* -Math.cos(phi); gl.uniform3fv(prg.uLightDirection, lightDirection); }
This function processes the arrow keys and changes the light direction accordingly. There is a bit of trigonometry (Math.cos, Math.sin) Mat.sin
) there but do not worry. We are just converting the angles (azimuth and elevation) calculated by the entered arrow keys into Cartesian coordinates.
Please notice that we are getting the current light direction using the function:
var lightDirection = gl.getUniform(prg,prg.uLightDirection);
After processing the key strokes, we can save the updated light direction with:
gl.uniform3fv(prg.uLightDirection, lightDirection);
ch3_Wall_Final.html
that contains the completed exercise.In this exercise, we have created a keyboard listener that allows us to update the light orientation so we can move it around the wall and see how it reacts to surface normals. We have also seen how the vertex shader and fragment shader input variables are declared and used. We understood how to build a program by reviewing the initProgram
function. We also learned about initializing uniforms on the initLights
function. We also studied the getUniform
function to retrieve the current value of a uniform.
18.218.55.14