glUtils is a bare-bones WebGL library abstracting away the more common boilerplate WebGL code, while still allowing one to use the API directly.
Demonstrations of glUtils functionality can be found in the examples
directory and in most of my WebGL experiments.
The WebGL context is acquired using glUtils.getGL()
, passing a canvas element to it as argument (it is assumed the canvas has already had its width and height set):
var gl = glUtils.getGL(document.getElementById("webgl"));
A program is set up using glUtils.getProgram()
and passing it the WebGL context, and the ids of elements
in the page containing the vertex and fragment shader code:
var program = glUtils.getProgram(gl, "vertex-shader", "fragment-shader");
Any errors in the compilation or linking will be printed to the console.
References to shader attributes and uniforms can be acquired using glUtils.getGLVars()
:
var gl_vars = glUtils.getGLVars(gl, program, {
attributes: ["aPosition", "aColor"],
uniforms: ["uMVP"]
});
glUtils.getGLVars()
returns an object containing the variable names as keys and their references
as values, e.g. gl_vars.aPosition
, gl_vars.uMVP
.
Attribute buffers can be prepared using glUtils.setBuffer()
, passing the WebGL context,
attribute reference, data and item size as arguments. The created buffer object is returned:
var position_buffer = glUtils.setBuffer(gl, gl_vars.aPosition, vertices, 3);
When switching between programs, it might be necessary to rebind buffer objects,
and this can be done using glUtils.enableBuffer()
, passing the WebGL context,
attribute reference, buffer object and item size as arguments:
glUtils.enableBuffer(gl, gl_vars.aPosition, position_buffer, 3);
Textures can be loaded using glUtils.loadTexture()
, which takes the WebGL
context, texture unit and image object as arguments:
glUtils.loadTexture(gl, gl.TEXTURE0, texture_image);
glUtils.loadTexture()
defaults to common options for filtering and wrap modes. These can be overridden by passing it additional options:
glUtils.loadTexture(gl, gl.TEXTURE0, texture_image, {
min_filter: gl.LINEAR,
mag_filter: gl.LINEAR,
wrap_s: gl.CLAMP_TO_EDGE,
wrap_t: gl.CLAMP_TO_EDGE
});
glUtils
also provides two utility methods for creating the geometry of basic shapes:
var sphere = glUtils.createSphere({
long_bands: 32,
lat_bands: 32,
radius: 1
});
var box = glUtils.createBox({
dimensions: [1, 2, 1]
});
Both return objects containing properties vertices
, the vertex positions, normals
, the vertex normals, and
texture_coords
, the texture coordinates.
glUtils provides two modules, glUtils.vec3
and glUtils.mat4
, to help with common mathematical operations on the typed arrays used by WebGL. The API for these modules is heavily influenced by gl-matrix.
glUtils.vec3
provides the following functions:
vec3.create(x, y, z)
: create avec3
(a 3-elementFloat32Array
). Elements will default to 0.vec3.copy(v1, v2)
: copy elements ofv2
intov1
.vec3.clone(v)
: create a clone ofv
.vec3.length(v)
: calculate the length of vectorv
.vec3.scale(v, s)
: scale vectorv
by a factor ofs
.vec3.normalize(v)
: normalize vectorv
.vec3.add(out, v1, v2)
: add vectorsv1
andv2
, store result inout
.vec3.sub(out, v1, v2)
: subtract vectorv2
from vectorv1
, store result inout
.vec3.dot(v1, v2)
: calculate the dot product of vectorsv1
andv2
.vec3.cross(out, v1, v2)
: calculate the cross product of vectorsv1
andv2
, store result inout
.vec3.applyMat4(out, m, v, vector_transform)
: apply transformation represented by matrixm
to the vector or point represented byv
, store result inout
. Ifvector_transform
is set totrue
, the multiplication will occur as ifv
had a fourth element set to0
. Otherwise, it will occur as ifv
had a fourth element set to1
. The matrixm
is represented by a 16-element array in column-major order.vec3.random()
: create avec3
with elements set to random numbers between 0 and 1.
glUtils.mat4
provides the following functions:
mat4.create(m0, m1, m2...)
: create amat4
(a 16-elementFloat32Array
in column-major order). Elements will default to elements of the identity matrix.mat4.copy(m1, m2)
: copy elements ofm2
intom1
.mat4.clone(m)
: create a clone ofm
.mat4.identity(m)
: setm
to the identity matrix.mat4.translation(m, x, y, z)
: setm
to a matrix that translates points by(x, y, z)
.mat4.scaling(m, x, y, z)
: setm
to a matrix that scales points or vectors by(x, y, z)
. If only one scaling factor is given, it will be used for all three axes.mat4.rotationX(m, theta)
: setm
to a matrix that rotates points or vectors around the x-axis bytheta
radians.mat4.rotationY(m, theta)
: setm
to a matrix that rotates points or vectors around the y-axis bytheta
radians.mat4.rotationZ(m, theta)
: setm
to a matrix that rotates points or vectors around the z-axis bytheta
radians.mat4.mult(out, m1, m2)
: multiply matricesm1
andm2
, store result inout
.mat4.transpose(m)
: find the transpose ofm
(occurs in-place).mat4.det(m)
: calculate the determinant ofm
.mat4.invert(m)
: find the inverse ofm
(occurs in-place).mat4.lookAt(m, eye, at, up)
: setm
to a view matrix for a camera described by the pointeye
, and the vectorsat
andup
(all 3-element arrays).mat4.ortho(m, left, right, bottom, top, near, far)
: setm
to an orthographic projection matrix.mat4.perspective(m, yfov, aspect, near, far)
: setm
to a perspective projection matrix.mat4.random()
: create amat4
with elements set to random numbers between 0 and 1.