Once you've successfully created a WebGL context, you can start rendering into it. The simplest thing we can do is draw a simple 2D, untextured object, so let's start there, by building code to draw a square.

Lighting the scene

The most important thing to understand before we get started is that even though we're only rendering a two-dimensional object in this example, we're still drawing in 3D space. As such, we still need to establish the shaders that will light our simple scene as well as draw our object. These will establish how the square is lit.

Initializing the shaders

Shaders are specified using the OpenGL ES Shading Language. In order to make it easier to maintain and update our content, we can actually write our code that loads the shaders so that it finds them in the HTML document, instead of having to build it all in JavaScript. Let's take a look at our initShaders() routine, which handles this task:

There are two shader programs loaded by this routine; the first, the fragment shader, is loaded from the script element with the ID "shader-fs". The second, the vertex shader, is loaded from the script element with the ID "shader-vs". We'll take a look at the getShader() function in the next section; this routine actually handles pulling in the shader programs from the DOM.

Then we create the shader program by calling the WebGL object's createProgram() function, attach the two shaders to it, and link the shader program. After doing that, the gl object's LINK_STATUS parameter is checked to be sure the program linked successfully; if it did, we activate the new shader program.

Loading shaders from the DOM

The getShader() routine fetches a shader program with the specified name from the DOM, returning the compiled shader program to the caller, or null if it couldn't be loaded or compiled.

Once the code for the shader has been read, we take a look at the MIME type of the shader object to determine whether it's a vertex shader (MIME type "x-shader/x-vertex") or a fragment shader (MIME type "x-shader/x-fragment"), then create the appropriate type of shader from the retrieved source code.

Finally, the source is passed into the shader and compiled. If an error occurs while compiling the shader, we display an alert and return null; otherwise, the newly compiled shader is returned to the caller.

The shaders

Then we need to add the shader programs themselves to the HTML describing our document. The details of how shaders work are beyond the scope of this article, as is the shader language syntax.

Fragment shader

Each pixel in a polygon is called a fragment in GL lingo. The fragment shader's job is to establish the color for each pixel. In this case, we're simply assigning white to each pixel.

gl_FragColor is a built-in GL variable that is used for the fragment's color. Setting its value establishes the pixel's color, as seen below.

Creating the object

Before we can render our square, we need to create the buffer that contains its vertices. We'll do that using a function we call initBuffers(); as we explore more advanced WebGL concepts, this routine will be augmented to create more -- and more complex -- 3D objects.

This routine is pretty simplistic given the basic nature of the scene in this example. It starts by calling the gl object's createBuffer() method to obtain a buffer into which we'll store the vertices. This is then bound to the context by calling the bindBuffer() method.

Once that's done, we create a JavaScript array containing the coordinates for each vertex of the square. This is then converted into an array of WebGL floats and passed into the gl object's bufferData() method to establish the vertices for the object.

Drawing the scene

Once the shaders are established and the object constructed, we can actually render the scene. Since we're not animating anything in this example, our drawScene() function is very simple. It uses a few utility routines we'll cover shortly.

The first step is to clear the context to our background color; then we establish the camera's perspective. We set a field of view of 45°, with a width to height ratio of 640/480 (the dimensions of our canvas). We also specify that we only want objects between 0.1 and 100 units from the camera to be rendered.

Then we establish the position of the square by loading the identity position and translating away from the camera by 6 units. After that, we bind the square's vertex buffer to the context, configure it, and draw the object by calling the drawArrays() method.

Matrix utility operations

Matrix operations are complicated enough. Nobody really wants to write all the code needed to handle them on their own. Fortunately, there's Sylvester, a very handy library for handling vector and matrix operations from JavaScript.

The glUtils.js file used by this demo is used by a number of WebGL demos floating around on the Web. Nobody seems entirely clear on where it came from, but it does simplify the use of Sylvester even further by adding methods for building special types of matrices, as well as outputting HTML for displaying them.

In addition, this demo defines a few helpful routines to interface with these libraries for specific tasks. What exactly they do is beyond the scope of this demo, but there are plenty of good references on matrices available online; see the See also section for a list of a few.