Skip to content

Repository files navigation

Computer Graphics: A Shortcut

Rhetorical Design

Purpose

Most introductions to computer graphics begin with triangular meshes and a graphics API — a steep entry cost even for legacy APIs like OpenGL, and steeper still for modern ones. This demo is intended for those with no prior graphics experience who want to understand 3D surface rendering without that overhead. We illustrate the core principles through direct manipulation of real geospatial data.

Strategy

Most graphics courses build rendering on top of triangular meshes, but the lighting model itself requires only a surface normal. A digital elevation model (DEM) supplies normals directly, bypassing mesh construction entirely. As a single-valued height function $f(x,y)$, a DEM also has no backfaces — every surface point faces upward and is always visible from above, eliminating visibility and occlusion checks. This reduces 3D surface rendering to an image filter: iteration over pixels, a local stencil to approximate the surface normal from neighboring height values, and basic arithmetic for the lighting equation.

We implement Phong shading from scratch in Python over a real LiDAR DEM (Illinois statewide survey). An interactive widget exposes the shading parameters — light direction, ambient, diffuse, specular coefficients — so the reader can feel the effect of each before its formula is introduced.

Technical Challenges

Normal estimation

Problem. The DEM is a discrete grid; the surface normal at each point must be approximated.

Model. A height map $f(x, y)$ defines a surface $\vec{r}(x, y) = (x,\, y,\, f(x, y))^\top$. The tangent vectors are its partial derivatives:

$$\frac{\partial \vec{r}}{\partial x} = \left(1,\, 0,\, \frac{\partial f}{\partial x}\right)^\top$$ $$\frac{\partial \vec{r}}{\partial y} = \left(0,\, 1,\, \frac{\partial f}{\partial y}\right)^\top$$

The surface normal is their cross product:

$$\vec{n} = \frac{\partial \vec{r}}{\partial x} \times \frac{\partial \vec{r}}{\partial y} = \left(-\frac{\partial f}{\partial x},\, -\frac{\partial f}{\partial y},\, 1\right)^\top$$

Algorithm. Approximating the partial derivatives by central finite differences:

$$\vec{n}[i,j] = \Bigl(f[i,j-1] - f[i,j+1],\; f[i+1,j] - f[i-1,j],\; 2\Bigr)^\top$$

About

3D rendering without a mesh or graphics API

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages