03

Table of Contents

Anatomy of a WebGL App

  1. Create a canvas element.
  2. Obtain a drawing context for the canvas.
  3. Initialize the viewport.
  4. Create one or more buffers containing the data to be rendered (typically vertices).
  5. Create one or more matrices to define the transformation from vertex buffers to screen space.
  6. Create one or more shaders to implement the drawing algorithm.
  7. Initialize the shaders with Draw.

Enable webGL in firefox

  1. In firefox, bring up about:config
  2. Make sure webgl.disabled set to false

operations to manipulate WebGL buffers

activate a vertex shader

  1. Bind a VBO (Vertex Buffer Object - contains the data that describes the geometry to be rendered
    gl.bindBuffer(gl.ARRAY_BUFFER, myBuffer)
  2. Point an attribute to currently bound VBO
    gl.vertexAttribPointer(index, size, type, normalize, stride, offset);
  3. Enable the attribute
    gl.enableVertexAttribArray(positionAttributeLocation);
  4. unbind
    gl.bindBuffer(gl.ARRAY_BUFFER, null);

Render

gl.drawArrays(mode, first, count)
gl.drawElements(mode, count, type, offset)

buffers/definitions

There are 2 data types that are fundamental to represent the geometry of a 3d object: vertices and indices

Operations to Manipulate WebGL Buffers

Associating Attributes to VBOs

  1. Bind a VBO
    gl.bindBuffer(gl.ARRAY_BUFFER, myBuffer);
  2. Point an attribute to the currently-bound VBO
    gl.vertexAttribPointer(index, size, type, normalize, stride, offset);
  3. Enable the attribute
    gl.enableVertexAttribArray(positionAttributeLocation);
  4. unbinding a VBO
  5. gl.bindBuffer(gl.ARRAY_BUFFER, null);