gifts/SpecularCube

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">
        uniform mat4 u_perspectiveMatrix;
        uniform mat4 u_modelMatrix;
        uniform mat4 u_viewMatrix;

        attribute vec4 a_Position;
        attribute vec3 a_Normal;

        varying vec4 v_Position;
        varying vec3 v_Normal;

        void main()
        {
            mat4 modelViewMatrix = u_viewMatrix * u_modelMatrix;
            v_Position = modelViewMatrix * a_Position;
            gl_Position = u_perspectiveMatrix * v_Position;
            v_Normal = normalize( mat3(modelViewMatrix) * a_Normal);
        }
    </script>

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

        uniform mat4 u_fViewMatrix;
        uniform vec3 u_lightPosition;

        varying vec4 v_Position;
        varying vec3 v_Normal;

        void main()
        {
            vec3 normal = normalize(v_Normal);
            vec3 lightPosition = vec3(u_fViewMatrix * vec4(u_lightPosition, 1) - v_Position);
            vec3 lightDir = normalize(lightPosition);
            float lightDist = length(lightPosition);

            float specular = 0.0;
            float d = max(dot(v_Normal, lightDir), 0.0);

            if (d > 0.0)
            {
                vec3 viewVec = vec3(0,0,1.0);
                vec3 reflectVec = reflect(-lightDir, normal);
                specular = pow(max(dot(reflectVec, viewVec), 0.0), 120.0);
            }

            gl_FragColor.rgb = vec3(0.1,0.1,0.1) + vec3(0.4, 0.4, 0.4) * d + specular;
            gl_FragColor.a = 1.0;
        }
    </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>
   ...

JavaScript

// SpecularCube.js 2013 (c) matsuda
// This is an example from the book "WebGL Programming Guide"
// by Kouichi Matsuda and Rodger Lea

// Gloval variables
var g_perspectiveMatrix = new Matrix4();
var g_modelMatrix = new Matrix4();
var g_viewMatrix = new Matrix4();

var g_vertexPositionBuffer;
var g_vertexNormalBuffer;
var g_vertexIndexBuffer;

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;
    }

    var perspectiveMatrixShaderLocation = gl.getUniformLocation(gl.program, 'u_perspectiveMatrix');
    var modelMatrixShaderLocation = gl.getUniformLocation(gl.program, 'u_modelMatrix');
    var viewMatrixShaderLocation = gl.getUniformLocation(gl.program, 'u_viewMatrix');
    var lightPositionShaderLocation = gl.getUniformLocation(gl.program, 'u_lightPosition');
    var f_viewMatrixShaderLocation = gl.getUniformLocation(gl.program, 'u_fViewMatrix');

    gl.enable(gl.DEPTH_TEST);

    gl.clearColor(0, 0, 0, 1);
    sendCubeVertexBuffers(gl);

    var angle = 0;

    var tick = function...