gifts/Particle

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;
        uniform vec3 u_lightDir;
        
        attribute vec4 a_Position;
        attribute vec2 a_TexCoord;

        varying vec4 v_Color;
        varying vec2 v_TexCoord;

        void main()
        {
            mat4 modelViewMatrix = u_viewMatrix * u_modelMatrix;
            gl_Position = u_perspectiveMatrix * modelViewMatrix * a_Position;

            v_TexCoord = a_TexCoord;
        }
    </script>

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

        uniform sampler2D u_Sampler;
        uniform float u_Alpha;
        varying vec2 v_TexCoord;

        void main()
        {
            gl_FragColor.rgb = texture2D(u_Sampler, vec2(v_TexCoord.s, v_TexCoord.t)).rgb;
            gl_FragColor.a = u_Alpha;
        }
    </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>

JavaScript

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

var g_perspectiveMatrix = new Matrix4();
var g_lightDir = new Vector3([0, 0.4, 0.6]);
var g_modelMatrix = new Matrix4();
var g_viewMatrix = new Matrix4();

var g_quadVertexPositionBuffer;
var g_quadVertexIndexBuffer;
var g_quadVertexTexCoordBuffer;

var TEXTURE;
var IMAGE;

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 texture
    //var texturePath = "../resources/particle.png";
    var texturePath = "https://dl.dropboxusercontent.com/s/enn8mt4dhsxad1u/particle.png";
    if (!initTextures(gl, texturePath))
    {
        console.log('Failed to set texture');
        return;
    }

    var perspectiveMatrixShaderLocation = gl.getUniformLocation(gl.program, 'u_perspectiveMatrix');
    var modelMatrixShaderLocation = gl.getUniformLocation(gl.program, 'u_modelMatrix');
    var viewMatrixShaderLocation =...