ch08/LightedCube_ambient
This is an example from the book "WebGL Programming Guide" by Kouichi Matsuda and Rodger Lea
by Ivan Enzhaev
HTML
<canvas id="webgl" width="400" height="400">
Please use a browser that supports "canvas"
</canvas>
<script type="x-shader/x-vertex" id="VertexShader">
attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec4 a_Normal; // Normal
uniform mat4 u_MvpMatrix;
uniform vec3 u_DiffuseLight; // Diffuse light color
uniform vec3 u_LightDirection; // Diffuse light direction (in the
// world coordinate, normalized)
uniform vec3 u_AmbientLight; // Color of an ambient light
varying vec4 v_Color;
void main()
{
gl_Position = u_MvpMatrix * a_Position;
// Make the length of the normal 1.0
vec3 normal = normalize(a_Normal.xyz);
// The dot product of the light direction and
// the normal (the orientation of a surface)
float nDotL = max(dot(u_LightDirection, normal), 0.0);
// Calculate the color due to diffuse reflection
vec3 diffuse = u_DiffuseLight * a_Color.rgb * nDotL;
// Calculate the color due to ambient reflection
vec3 ambient = u_AmbientLight * a_Color.rgb;
// Add the surface colors due to diffuse
// reflection and ambient reflection
v_Color = vec4(diffuse + ambient, a_Color.a);
}
</script>
<script type="x-shader/x-fragment" id="FragmentShader">
precision mediump float;
varying vec4 v_Color;
void main()
{
gl_FragColor = v_Color;
}
</script>
<script src="https://dl.dropboxusercontent.com/s/ylw7v7a44xtrwv0/webgl-utils.js"></script>
<script src="https://dl.dropboxusercontent.com/s/iz1n21c8xafd76n/webgl-debug.js"></script>
<script src="https://dl.dropboxusercontent.com/s/subm1yzylxuao8v/cuon-utils.js"></script>
<script...
JavaScript
// LightedCube_ambient.js (c) 2012 matsuda
// This is an example from the book "WebGL Programming Guide"
// by Kouichi Matsuda and Rodger Lea
main();
//window.onload = main;
function main()
{
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl)
{
console.log('Failed to get the rendering context for WebGL');
return;
}
// Get shader elements
var vShaderElement = document.getElementById("VertexShader");
var fShaderElement = document.getElementById("FragmentShader");
if (vShaderElement == null)
{
console.log("Failed to get the vertex shader element");
return;
}
if (fShaderElement == null)
{
console.log("Failed to get the fragment shader element");
return;
}
// Get shader sources
var vShaderSource = vShaderElement.firstChild.textContent;
var fShaderSource = fShaderElement.firstChild.textContent;
// Initialize shaders
if (!initShaders(gl, vShaderSource, fShaderSource))
{
console.log('Failed to intialize shaders');
return;
}
// Set the vertex information
var n = initVertexBuffers(gl);
if (n < 0)
{
console.log('Failed to set the vertex information');
return;
}
// Set the clear color and enable the depth test
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.DEPTH_TEST);
// Get the storage locations of uniform variables and so on
var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
var u_DiffuseLight = gl.getUniformLocation(gl.program, 'u_DiffuseLight');
var u_LightDirection = gl.getUniformLocation(gl.program, 'u_LightDirection');
var u_AmbientLight = gl.getUniformLocation(gl.program, 'u_AmbientLight');
if (!u_MvpMatrix || !u_DiffuseLight || !u_LightDirection || !u_AmbientLight)
{
console.log('Failed to get the storage...