Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

4.6 Painter Algorithm and Comparison

Lesson 32 of 32 in the free Computer Graphics notes on Siksha Sarovar, written by Rohit Jangra.

Idea

Paint farther polygons first, closer polygons over them - exactly how an oil painter layers brush strokes (background sky first, foreground figures last). Sort polygons by depth, then draw back to front. The closer overwrites the farther.

Algorithm

  1. Compute a depth value (centroid z, max z, etc.) for each polygon.
  2. Sort polygons in decreasing depth order.
  3. Render in that order; later polygons naturally cover earlier ones in the framebuffer.

Tests Before Drawing (Newell, Newell, Sancha refinement)

Before drawing polygon Q in front of P, verify:

  1. Z-extents of P and Q do not overlap, OR
  2. Bounding rectangles do not overlap, OR
  3. P is entirely on the far side of Q's plane, OR
  4. Q is entirely on the near side of P's plane, OR
  5. Their projections do not overlap.
  6. If any test passes, ordering P then Q is safe.

Failure Cases

  • Cyclic overlap: three polygons A, B, C where A is in front of B, B in front of C, and C in front of A. No linear order is correct -> must split at least one polygon along another's plane.
  • Intersecting polygons: two polygons cross each other -> single ordering cannot work; must split.
  • Long thin polygons: depth ordering of centroids is misleading; per-pixel correct only after splitting.

Comparison with Z-Buffer

FeaturePainter'sZ-Buffer
Memory overheadNone beyond framebufferDepth buffer
Polygon orderingRequiredIndependent
Handles intersectionsNo - must splitYes - per fragment
ParallelismHard - depends on orderTrivial
Transparent objectsNatural (back to front)Requires sort or OIT
Cost per polygonSort + drawRasterize + per-pixel test

Where Painter's Survives

  • 2D / Sprite engines: layered drawing of sprites where Z is implicit (UI, classic 2D games, SVG rendering).
  • Transparent geometry: even with z-buffer, transparent polygons need painter-style sorted draw because alpha blending is non-commutative.
  • Software rasterizers with very tight memory budgets.

Worked Sketch

Three triangles A (depth 30), B (depth 20), C (depth 10).

  • Sort -> A, B, C.
  • Draw A first - paints background portion.
  • Draw B next - covers part of A.
  • Draw C last - covers parts of A and B.
  • Result: closest geometry visible on top, just like a stack of paintings.

Hybrid Practice

Modern engines run opaque geometry through z-buffer (any order) and then transparent geometry sorted back to front, blending on top of the depth-tested opaque framebuffer. That hybrid keeps both algorithms relevant in 2026.

Other HSR Techniques (Brief Mention)

  • Backface Culling: discard polygons whose normal points away from the viewer (cheap pre-test).
  • BSP Trees: build a binary space partition tree once, then traverse it in viewer-dependent order for guaranteed correct painter draw without runtime sort. Used in early 3D shooters (Doom, Quake).
  • Scan-line HSR: incrementally maintain active polygons per scanline; lower memory than z-buffer but more complex.
  • Ray Casting / Ray Tracing: per-pixel ray finds nearest surface; the gold standard for correctness, increasingly real-time on RTX-class GPUs.