ch10/ProgramObject

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>

    <!-- Vertex shader for single color drawing -->
    <script type="x-shader/x-vertex" id="SolidVertexShader">
        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;

            // Light direction(World coordinate)
            vec3 lightDirection = vec3(0.0, 0.0, 1.0);

            vec4 color = vec4(0.0, 1.0, 1.0, 1.0); // Face color
            vec3 normal = normalize(vec3(u_NormalMatrix * a_Normal));
            float nDotL = max(dot(normal, lightDirection), 0.0);
            v_Color = vec4(color.rgb * nDotL, color.a);
        }
    </script>

    <!-- Fragment shader for single color drawing -->
    <script type="x-shader/x-fragment" id="SolidFragmentShader">
        precision mediump float;

        varying vec4 v_Color;

        void main()
        {
            gl_FragColor = v_Color;
        }
    </script>

    <!-- Vertex shader for texture drawing -->
    <script type="x-shader/x-vertex" id="TextureVertexShader">
        attribute vec4 a_Position;
        attribute vec4 a_Normal;
        attribute vec2 a_TexCoord;
        uniform mat4 u_MvpMatrix;
        uniform mat4 u_NormalMatrix;
        varying float v_NdotL;
        varying vec2 v_TexCoord;

        void main()
        {
            gl_Position = u_MvpMatrix * a_Position;

            // Light direction(World coordinate)
            vec3 lightDirection = vec3(0.0, 0.0, 1.0);

            vec3 normal = normalize(vec3(u_NormalMatrix * a_Normal));
            v_NdotL = max(dot(normal, lightDirection), 0.0);
            v_TexCoord = a_TexCoord;
        }
    </script>

    <!-- Fragment shader for texture drawing -->
    <script type="x-shader/x-fragment" id="TextureFragmentShader">
       ...

JavaScript

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

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

// The increments of rotation angle (degrees)
var ANGLE_STEP = 30;

// Last time that this function was called
var last = Date.now();

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 vSolidShaderElement = document.getElementById("SolidVertexShader");
    var fSolidShaderElement = document.getElementById("SolidFragmentShader");
    var vTextureShaderElement = document.getElementById("TextureVertexShader");
    var fTextureShaderElement = document.getElementById("TextureFragmentShader");
    if (vSolidShaderElement == null || fSolidShaderElement == null ||
        vTextureShaderElement == null || fTextureShaderElement == null)
    {
        console.log("Failed to get a shader element");
        return;
    }

    // Get shader sources
    var vSolidShaderSource = vSolidShaderElement.firstChild.textContent;
    var fSolidShaderSource = fSolidShaderElement.firstChild.textContent;
    var vTextureShaderSource = vTextureShaderElement.firstChild.textContent;
    var fTextureShaderSource = fTextureShaderElement.firstChild.textContent;

    // Initialize shaders
    var solidProgram = createProgram(gl, vSolidShaderSource, fSolidShaderSource);
    var texProgram = createProgram(gl, vTextureShaderSource, fTextureShaderSource);
    if (!solidProgram || !texProgram)
    {
        console.log('Failed to intialize shaders');
        return;
    }

    // Get storage locations of...