Computer Graphics Tutorial
Learn Computer Graphics step by step: 50 lessons in 13 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Computer Graphics? Learn what computer graphics is: raster vs vector images, 2D vs 3D, real-time vs offline rendering, where it is used and how the graphics pipeline works.
- Pixels, Resolution and the Framebuffer Understand pixels, resolution, aspect ratio and bit depth, and learn how a framebuffer is just an array you can fill using canvas ImageData.
- Coordinate Systems Learn screen vs Cartesian coordinates, the y-axis flip, and how world, view, normalized device and screen spaces fit together, with live demos.
Line and Circle Algorithms
- The DDA Line Algorithm Learn how the DDA (digital differential analyzer) algorithm draws a line on a pixel grid with incremental steps, rounding, and its limitations.
- Bresenham's Line Algorithm Learn Bresenham's line algorithm: the decision parameter, integer-only arithmetic, how to handle all octants, and how it compares with DDA, with a live step-through.
- The Midpoint Circle Algorithm Learn the midpoint circle algorithm: the circle equation, eight-way symmetry and the integer decision parameter, with a live circle plotter and worked example.
- The Midpoint Ellipse Algorithm Learn how the midpoint ellipse algorithm plots an ellipse with only additions: two regions, decision parameters, four-way symmetry and a live pixel plot.
- Aliasing and Antialiasing See why raster lines show jaggies, learn the sampling idea behind them, and fix them with supersampling, pixel coverage and Wu's line algorithm.
- Thick Lines, Dashes and Pen Styles Draw wide lines correctly: pixel replication and its flaws, line caps and joins, miter limits, dash patterns and the Canvas 2D line style properties.
Polygons and Filling
- Polygons: Types and Representation Learn convex, concave and self-intersecting polygons, vertex lists, winding order and signed area, plus the even-odd and nonzero winding inside tests.
- Scanline Polygon Filling Fill any polygon one row at a time with the scanline algorithm: edge table, active edge list, sorted intersections, vertex rules and a live software filler.
- Flood Fill and Boundary Fill Learn flood fill and boundary fill: 4- and 8-connectivity, why recursion overflows, the stack-based version, scanline seed fill and a live paint bucket.
- Triangle Rasterization and Barycentric Coordinates Learn how GPUs fill triangles: bounding boxes, edge functions, barycentric weights, the top-left rule and color interpolation, with live demos.
2D Transformations
- Translation, Scaling and Rotation Learn the three basic 2D transformations with formulas and code: move, scale and rotate points, fixed-point scaling, and why order matters.
- Homogeneous Coordinates and Matrix Transformations Learn why graphics uses homogeneous coordinates, how 3x3 matrices represent translation, scaling and rotation, and how to compose them in order.
- Reflection, Shear and Inverse Transforms Learn reflection and shear matrices, how to invert any affine transform, and how Canvas setTransform uses the same math, with live examples.
- Window-to-Viewport Mapping Learn the window-to-viewport transformation: the mapping formula, scale factors, aspect ratio distortion, and how zoom and pan work, with live demos.
Clipping
- Cohen-Sutherland Line Clipping Learn the Cohen-Sutherland line clipping algorithm: region outcodes, trivial accept and reject, iterative clipping, with a live step-by-step clipper.
- Liang-Barsky Line Clipping Learn the Liang-Barsky algorithm: parametric lines, the p and q values, entering and leaving edges, and how it compares to Cohen-Sutherland, with a live clipper.
- Polygon Clipping: Sutherland-Hodgman Clip polygons against a window one edge at a time with the Sutherland-Hodgman algorithm: the four cases, concave pitfalls, Weiler-Atherton and a live clipper.
Color
- RGB, CMY and CMYK Color Models Understand additive RGB and subtractive CMY/CMYK color, the RGB color cube, gamut and conversion code, with a live color mixer and overlap demos.
- HSV, HSL, Gamma and Perceptual Color Learn HSV and HSL color models, conversion code, gamma correction, sRGB versus linear light, luminance and contrast ratios, with a live converter.
- Gradients, Palettes and Dithering Interpolate colors for gradients and color maps, understand indexed palettes, and compare threshold, ordered (Bayer) and Floyd-Steinberg dithering live.
3D Transformations and Viewing
- 3D Points and Transformation Matrices Learn right- and left-handed 3D systems, 4x4 homogeneous matrices, translation, scaling and composition order, with a live matrix calculator.
- 3D Rotation: Axes, Euler Angles and Quaternions Learn 3D rotation matrices about x, y and z, why Euler angles suffer gimbal lock, and how axis-angle and quaternions with slerp fix it, with live demos.
- The Camera and View Transformation Learn how a virtual camera works in 3D graphics: eye, target and up vector, the look-at view matrix, view space and an orbit camera, with live demos.
- Orthographic and Perspective Projection Learn how 3D points are projected onto a 2D screen: parallel (orthographic) and perspective projection, the perspective divide, FOV and matrices, with demos.
Hidden Surfaces and Drawing 3D Objects
- Polygon Meshes and Wireframe Rendering Learn how 3D objects are stored as polygon meshes: vertices, edges, faces, indexed triangles, winding and normals, plus a live wireframe renderer.
- Back-Face Culling and the Painter's Algorithm Learn two classic hidden-surface techniques: back-face culling with the face-normal test, and the painter's algorithm with depth sorting and its limits.
- The Z-Buffer Algorithm Learn how the z-buffer solves hidden surfaces per pixel: the depth buffer, the depth test, depth interpolation, precision and z-fighting, with live demos.
Lighting and Shading
- Light and Reflection Models Learn how renderers fake light: ambient, Lambert diffuse, Phong and Blinn-Phong specular, and attenuation, with a live sphere and sliders.
- Flat, Gouraud and Phong Shading Compare flat, Gouraud and Phong shading: per-face, per-vertex and per-pixel lighting, normal interpolation and Mach bands, with a live renderer.
- Texture Mapping Learn texture mapping: UV coordinates, nearest and bilinear filtering, mipmaps, perspective-correct interpolation and procedural textures, with live demos.
Curves and Surfaces
- Parametric Curves and Interpolation Learn parametric curves: Hermite and Catmull-Rom splines, tangents, and C0, C1 and G1 continuity, with a live curve editor you can drag around.
- Bezier Curves Understand Bezier curves: Bernstein polynomials, the de Casteljau algorithm, cubic curves, subdivision and Canvas bezierCurveTo, with live editors.
- B-Splines and NURBS Learn B-splines and NURBS: knot vectors, Cox-de Boor basis functions, local control, clamped curves and weights that draw exact circles, with live editors.
- Bezier Surfaces and Fractals Extend curves to surfaces with bicubic Bezier patches and surfaces of revolution, then explore fractals: the Koch curve, L-systems, Mandelbrot and Julia sets.
Ray Tracing and Rasterization Pipeline
- Ray Tracing: Rays, Spheres and Planes Build your first ray tracer: camera rays, the ray equation, ray-sphere and ray-plane intersection math, surface normals and simple shading, with live demos.
- Shadows, Reflections and Refraction Add shadow rays, recursive mirror reflection and glass refraction with Snell's law and the Fresnel effect to your ray tracer, with a live recursive renderer.
- The Rasterization Pipeline Follow a triangle through the graphics pipeline: vertex shader, clipping, perspective divide, viewport, rasterization, fragment shader and depth test, with a live mini-GPU.
WebGL and Real-Time Graphics
- WebGL Overview Understand how WebGL draws with the GPU: contexts, buffers, shaders, programs, attributes, draw calls and the state machine, ending with a live triangle.
- Shaders and GLSL Basics Learn GLSL for WebGL: vertex and fragment shaders, attributes, uniforms, varyings, vector types, swizzling and built-in functions, with live editable shaders.
- A Rotating 3D Cube in WebGL Build a lit, rotating 3D cube in WebGL: model, view and projection matrices as uniforms, indexed drawing, the depth test, and Lambert lighting in the shader.
Projects, Practice and Review
- Project: A Software 3D Engine Build a tiny 3D engine without a GPU: mesh, rotation, perspective projection, z-buffer and Lambert shading, drawn pixel by pixel in plain JavaScript.
- Project: A Mini Paint Program Build a pixel paint program from scratch: Bresenham pencil, line, rectangle and circle tools, flood-fill bucket, color picker and undo, all in plain JavaScript.
- Project: A Ray-Traced Scene Build a complete ray tracer in JavaScript: spheres, a checkered floor, a point light, hard shadows, recursive reflections and antialiasing, rendered live.
- Project: A Fractal Explorer Build a Mandelbrot and Julia set explorer in JavaScript: escape-time rendering, smooth coloring, cosine palettes, click-to-zoom and speed-ups, rendered live.
- Practice: Exercises with Solutions Twenty graded computer graphics exercises with runnable JavaScript solutions: line and circle algorithms, transforms, clipping, shading, projection and curves.
- Computer Graphics Interview Questions 36 common computer graphics interview questions with concise answers on algorithms, transforms, projection, depth, shading, the pipeline and ray tracing, plus live checks.
- Computer Graphics Cheat Sheet A one-page computer graphics cheat sheet: key formulas, matrices, line and circle algorithms, clipping, shading, ray tracing and pipeline stages, with live checks.