ch09/MultiJointModel

This is an example from the book "WebGL Programming Guide" by Kouichi Matsuda and Rodger Lea

by Ivan Enzhaev

HTML

<p>&larr;&rarr;: arm1 rotation,&uarr;&darr;: joint1 rotation, xz: joint2(wrist) rotation, cv: finger rotation</p>
    <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_Normal;
        uniform mat4 u_MvpMatrix;
        uniform mat4 u_NormalMatrix;
        varying vec4 v_Color;

        void main()
        {
            gl_Position = u_MvpMatrix * a_Position;

            // Shading calculation to make the arm look three-dimensional
            vec3 lightDirection = normalize(vec3(0.0, 0.5, 0.7)); // Light direction
            vec4 color = vec4(1.0, 0.4, 0.0, 1.0);  // Robot color
            vec3 normal = normalize((u_NormalMatrix * a_Normal).xyz);
            float nDotL = max(dot(normal, lightDirection), 0.0);
            v_Color = vec4(color.rgb * nDotL + vec3(0.1), 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 src="https://dl.dropboxusercontent.com/s/jw3p82lq1cjwekg/cuon-matrix.js"></script>

CSS

body {
  overflow: hidden;
}

JavaScript

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

var ANGLE_STEP = 3.0;     // The increments of rotation angle (degrees)
var g_arm1Angle = 90.0;   // The rotation angle of arm1 (degrees)
var g_joint1Angle = 45.0; // The rotation angle of joint1 (degrees)
var g_joint2Angle = 0.0;  // The rotation angle of joint2 (degrees)
var g_joint3Angle = 0.0;  // The rotation angle of joint3 (degrees)

// Coordinate transformation matrix
var g_modelMatrix = new Matrix4(), g_mvpMatrix = new Matrix4();

// Coordinate transformation matrix for normals
var g_normalMatrix = new Matrix4();

// Array for storing a matrix
var g_matrixStack = [];

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...