Skip to content

← All projects

Games & Graphics

WebGL Mesh Renderer

Course project (CS 4600, Project 5) · Fall 2026

A mesh renderer I wrote for CS 4600 (Introduction to Computer Graphics) at the University of Utah. It loads OBJ models and textures, places them with model-view-projection matrices, and shades them with a Blinn-Phong shader in GLSL. The course provided the page and its controls; I wrote the transformation and the renderer.

JavaScriptWebGLGLSL

Loading model…

Drag to rotate

Controls

Light direction
drag the dot

Load your own model

Files stay in your browser; nothing is uploaded.

Model: “Nyra Woman Warrior” by Arion Digital, licensed under CC BY 4.0. The texture was resized and converted to WebP for the web.

How it works

Transform. Each frame, getModelViewMatrix builds the model-view matrix from a translation and rotations about the y and x axes (column-major, ready for WebGL). The projection is multiplied on top. The vertex shader moves each position to clip space, moves its normal into view space with the normal matrix, and passes along the direction toward the camera.

Shading. The fragment shader uses the Blinn-Phong model:

  • Diffuse: brightness is the cosine of the angle between the surface normal and the light direction, times the surface color (from the texture, or white).
  • Specular: a highlight from the half vector between the light and view directions, raised to the shininess exponent. Higher shininess gives a smaller, sharper highlight.

Textures. Textures are uploaded with mipmaps, so a model stays smooth instead of shimmering when it’s zoomed out. Swap Y-Z axes handles models exported with Z as the up axis.

What I wrote

The course provided the page: controls, camera, the light-direction widget, the bounding box, and OBJ loading. I wrote the renderer: the model-view transform, the mesh buffers, texture setup, and both shaders.

The demo above runs my renderer inside a new page harness built for this site. The harness adds touch support and a light-direction pad that replaces the course’s 3D light widget.

Fixes for the site version. I fixed several bugs in my original code:

  • The texture filters were swapped, so mipmaps were never used. Magnification was also set to an invalid value.
  • Lighting determined the output alpha, so unlit areas were transparent. The original page had a black background, which hid it.
  • Specular highlights appeared on surfaces facing away from the light.
  • The renderer could draw before a model had loaded.
  • Textures whose sides weren’t powers of two rendered black.
Show shader code
// Vertex shader
attribute vec3 pos;
attribute vec3 normal;
attribute vec2 texCoord;

uniform mat4 trans;     // model-view-projection
uniform mat3 normMat;   // normal matrix
uniform mat4 posMat;    // model-view
uniform bool swapyz;

varying vec2 vTexCoord;
varying vec3 vNormal;
varying vec3 vViewDir;

void main() {
	vTexCoord = texCoord;
	vec3 p = swapyz ? pos.xzy : pos;
	vec3 n = swapyz ? normal.xzy : normal;
	gl_Position = trans * vec4(p, 1.0);
	vNormal = normalize(normMat * n);
	vViewDir = normalize((-posMat * vec4(p, 1.0)).xyz);
}
// Fragment shader
varying vec2 vTexCoord;
varying vec3 vNormal;
varying vec3 vViewDir;

uniform sampler2D uTexture;
uniform bool uShowTexture;
uniform float uAlpha;      // shininess
uniform vec3 uLightDir;

void main() {
	vec4 kd = uShowTexture ? texture2D(uTexture, vTexCoord) : vec4(1.0);
	vec3 normal = normalize(vNormal);
	vec3 viewDir = normalize(vViewDir);

	float cosTheta = max(dot(uLightDir, normal), 0.0);
	vec3 h = normalize(uLightDir + viewDir);
	float cosPhi = max(dot(normal, h), 0.0);
	float specular = cosTheta > 0.0 ? pow(cosPhi, uAlpha) : 0.0;

	gl_FragColor = vec4((cosTheta * kd + specular).rgb, 1.0);
}