ch08/PointLightedCube_perFragment

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;
        uniform mat4 u_MvpMatrix;
        uniform mat4 u_ModelMatrix;     // Model matrix
        uniform mat4 u_NormalMatrix;    // Transformation matrix of the normal
        varying vec3 v_Position;
        varying vec4 v_Color;
        varying vec3 v_Normal;

        void main()
        {
            gl_Position = u_MvpMatrix * a_Position;

            // Calculate the vertex position in the world coordinate
            v_Position = vec3(u_ModelMatrix * a_Position);

            v_Normal = normalize(vec3(u_NormalMatrix * a_Normal));

            v_Color = a_Color;
        }
    </script>

    <script type="x-shader/x-fragment" id="FragmentShader">
        precision mediump float;

        uniform vec3 u_LightColor;      // Light color
        uniform vec3 u_LightPosition;   // Position of the light source
        uniform vec3 u_AmbientLight;    // Ambient light color
        varying vec3 v_Position;
        varying vec4 v_Color;
        varying vec3 v_Normal;

        void main()
        {
            // Normalize the normal because it is interpolated and
            // not 1.0 in length any more
            vec3 normal = normalize(v_Normal);

            // Calculate the light direction and make its length 1
            vec3 lightDirection = normalize(u_LightPosition - v_Position);

            // The dot product of the light direction and the
            // orientation of a surface (the normal)
            float nDotL = max(dot(lightDirection, normal), 0.0);

            // Calculate the final color from diffuse reflection and
            // ambient reflection
            vec3 diffuse = u_LightColor * v_Color.rgb * nDotL;
            vec3 ambient = u_AmbientLight * v_Color.rgb;
           ...

JavaScript

// PointLightedCube_perFragment.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.0, 0.0, 1.0);
    gl.enable(gl.DEPTH_TEST);

    // Get the storage locations of uniform variables
    var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
    var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
    var u_NormalMatrix = gl.getUniformLocation(gl.program, 'u_NormalMatrix');
    var u_LightColor = gl.getUniformLocation(gl.program, 'u_LightColor');
    var u_LightPosition = gl.getUniformLocation(gl.program, 'u_LightPosition');
    var u_AmbientLight =...