text: var canvas;
  var gl;
  
  var numTimesToSubdivide = 3;
  var index = 0;
  var pointsArray = [];
  var normalsArray=[];
  
  var rendered = false;
  
  var va = vec4(0.0, 0.0, 1.0, 1);
  var vb = vec4(0.0, 0.942809, -0.333333, 1);
  var vc = vec4(-0.816497, -0.471405, -0.333333, 1);
  var vd = vec4(0.816497, -0.471405, -0.333333, 1);
  
  var lightPosition = vec4(0.0, 0.0, 1.0, 0.0);
  var lightDiffuse = vec4(0.5, 0.5, 0.5, 0.0);
  var materialDiffuse = vec4(1.0, 1.0, 1.0, 0.0);
  
  var modelViewMatrixLoc, projectionMatrixLoc;
  var modelViewMatrix, projectionMatrix;
  var normalMatrix, normalMatrixLoc;
  
  var near = 3;
  var far = 10;
  var fov = 45;
  var aspect;
  var radius = 6.0;
  var theta  = 0.0;
  var phi    = 0.0;
  var eye;
  var at = vec3(0.0, 0.0, 0);
  var up = vec3(0.0, 1.0, 0.0);
  
  function triangle(a, b, c){
      pointsArray.push(a);
      pointsArray.push(b);
      pointsArray.push(c);
  
  
      normalsArray.push(vec4(a[0], a[1], a[2], 0.0));
      normalsArray.push(vec4(b[0], b[1], b[2], 0.0));
      normalsArray.push(vec4(c[0], c[1], c[2], 0.0));
  
      index += 3;
  }
  
  function divideTriangle(a, b, c, count){
      if (count > 0) {
          var ab = normalize(mix(a, b, 0.5), true);
          var ac = normalize(mix(a, c, 0.5), true);
          var bc = normalize(mix(b, c, 0.5), true);
  
          divideTriangle(a, ab, ac, count - 1);
          divideTriangle(ab, b, bc, count - 1);
          divideTriangle(bc, c, ac, count - 1);
          divideTriangle(ab, bc, ac, count - 1);
      }
      else {
          triangle(a, b, c);
      }
  }
  
  function tetrahedron(a, b, c, d, n){
      divideTriangle(a, b, c, n);
      divideTriangle(d, c, b, n);
      divideTriangle(a, d, b, n);
      divideTriangle(a, c, d, n);
  }
  
  var init = function(){
      canvas = document.getElementById( "gl_canvas" );
      canvas.width = 512;
      canvas.height = 512;
  
      gl = WebGLUtils.setupWebGL( canvas );
      if ( !gl ) alert( "WebGL isn't available" );
  
      gl.viewport( 0, 0, canvas.width, canvas.height );
      aspect = canvas.width/canvas.height;
      gl.clearColor(0.3921, 0.5843, 0.9294, 1.0);
  
      gl.enable(gl.DEPTH_TEST);
      gl.enable(gl.CULL_FACE);
      gl.cullFace(gl.BACK);
  
      var program = initShaders( gl, "vertex-shader", "fragment-shader" );
      gl.useProgram( program );
  
      var diffuseProduct = mult(lightDiffuse, materialDiffuse);
  
      tetrahedron(va, vb, vc, vd, numTimesToSubdivide);
  
      var nBuffer = gl.createBuffer();
      gl.bindBuffer( gl.ARRAY_BUFFER, nBuffer);
      gl.bufferData( gl.ARRAY_BUFFER, flatten(normalsArray), gl.STATIC_DRAW );
  
      var vNormal = gl.getAttribLocation( program, "vNormal" );
      gl.vertexAttribPointer( vNormal, 4, gl.FLOAT, false, 0, 0 );
      gl.enableVertexAttribArray( vNormal);
  
      var vBuffer = gl.createBuffer();
      gl.bindBuffer( gl.ARRAY_BUFFER, vBuffer );
      gl.bufferData(gl.ARRAY_BUFFER, flatten(pointsArray), gl.STATIC_DRAW);
  
      var vPosition = gl.getAttribLocation( program, "vPosition" );
      gl.vertexAttribPointer( vPosition, 4, gl.FLOAT, false, 0, 0 );
      gl.enableVertexAttribArray( vPosition );
  
      document.getElementById("Button1").onclick = function(){
          numTimesToSubdivide++;
          index = 0;
          pointsArray = [];
          normalsArray = [];
          init();
      };
      document.getElementById("Button2").onclick = function(){
          if(numTimesToSubdivide) numTimesToSubdivide--;
          index = 0;
          pointsArray = [];
          normalsArray = [];
          init();
      };
  
      modelViewMatrixLoc = gl.getUniformLocation(program, "modelViewMatrix");
      projectionMatrixLoc = gl.getUniformLocation(program, "projectionMatrix");
      normalMatrixLoc = gl.getUniformLocation( program, "normalMatrix" );
  
      gl.uniform4fv( gl.getUniformLocation(program,
         "diffuseProduct"),flatten(diffuseProduct) );
      gl.uniform4fv( gl.getUniformLocation(program,
         "lightPosition"),flatten(lightPosition) );
  
      if(!rendered) render();
      rendered = true;
  }
  
  function render(){
      gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
      eye = vec3(radius*Math.sin(phi), radius*Math.sin(theta), radius*Math.cos(phi));
      modelViewMatrix = lookAt(eye, at , up);
      projectionMatrix = perspective(fov, aspect, near, far);
  
      phi += 0.005;
  
      var normalM = [
          vec3(modelViewMatrix[0][0], modelViewMatrix[0][1], modelViewMatrix[0][2]),
          vec3(modelViewMatrix[1][0], modelViewMatrix[1][1], modelViewMatrix[1][2]),
          vec3(modelViewMatrix[2][0], modelViewMatrix[2][1], modelViewMatrix[2][2])
      ];
      gl.uniformMatrix4fv(projectionMatrixLoc, false, flatten(projectionMatrix));
      gl.uniformMatrix4fv(modelViewMatrixLoc, false, flatten(modelViewMatrix));
      gl.uniformMatrix3fv(normalMatrixLoc, false, flatten(normalM));
  
      for( var i=0; i<index; i+=3) {
          gl.drawArrays( gl.TRIANGLES, i, 3 );
      }
      
      if(!interrupted) requestAnimFrame(render);
  }
  
  init();
  