Painter's algorithm
Template:Short description Template:Distinguish
The painter's algorithm (also depth-sort algorithm and priority fill) is an algorithm for visible surface determination in 3D computer graphics that works on a polygon-by-polygon basis rather than a pixel-by-pixel, row by row, or area by area basis of other Hidden-Surface Removal algorithms.<ref>Template:Cite journal</ref><ref>Template:Cite journal</ref><ref>Gary Scott Watkins. 1970. "A real time visible surface algorithm. Ph.D. Dissertation." The University of Utah. Order Number: AAI7023061.</ref> The painter's algorithm creates images by sorting the polygons within the image by their depth and placing each polygon in order from the farthest to the closest object.<ref name=":0" /><ref>Template:Cite journal</ref>
The painter's algorithm was initially proposed as a basic method to address the Hidden-surface determination problem by Martin Newell, Richard Newell, and Tom Sancha in 1972, while all three were working at CADCentre.<ref name=":0">Template:Cite book</ref> The name "painter's algorithm" refers to the technique employed by many painters where they begin by painting distant parts of a scene before parts that are nearer, thereby covering some areas of distant parts.<ref>Template:Cite book</ref><ref>Template:Cite book</ref> Similarly, the painter's algorithm sorts all the polygons in a scene by their depth and then paints them in this order, farthest to closest.<ref name=":2">Template:Cite book</ref> It will paint over the parts that are normally not visible — thus solving the visibility problem — at the cost of having painted invisible areas of distant objects.<ref name=":1">Template:Cite book</ref> The ordering used by the algorithm is called a 'depth order' and does not have to respect the numerical distances to the parts of the scene: the essential property of this ordering is, rather, that if one object obscures part of another, then the first object is painted after the object that it obscures.<ref name=":1" /> Thus, a valid ordering can be described as a topological ordering of a directed acyclic graph representing occlusions between objects.<ref>Template:Cite book.</ref>Template:Wide image
AlgorithmEdit
Conceptually Painter's Algorithm works as follows:
- Sort each polygon by depth
- Place each polygon from the farthest polygon to the closest polygon
PseudocodeEdit
sort polygons by depth for each polygon p: for each pixel that p covers: paint p.color on pixel
Time complexityEdit
The painter's algorithm's time-complexity depends on the sorting algorithm used to order the polygons. Assuming an optimal sorting algorithm, painter's algorithm has a worst-case complexity of O(n log n + m*n), where n is the number of polygons and m is the number of pixels to be filled.
Space complexityEdit
The painter's algorithm's worst-case space-complexity is O(n+m), where n is the number of polygons and m is the number of pixels to be filled.
AdvantagesEdit
There are two primary technical requisites that favor the use of the painter's algorithm.
Basic graphical structureEdit
The painter's algorithm is not as complex in structure as its other depth sorting algorithm counterparts.<ref name=":1" /><ref>Template:Cite journal</ref> Components such as the depth-based rendering order, as employed by the painter's algorithm, are one of the simplest ways to designate the order of graphical production.<ref name=":2" /> This simplicity makes it useful in basic computer graphics output scenarios where an unsophisticated render will need to be made with little struggle.<ref name=":1" />
Memory efficiencyEdit
In the early 70s, when the painter's algorithm was developed, physical memory was relatively small.<ref>Template:Cite journal</ref> This required programs to manage memory as efficiently as possible to conduct large tasks without crashing. The painter's algorithm prioritizes the efficient use of memory but at the expense of higher processing power since all parts of all images must be rendered.<ref name=":1" />
LimitationsEdit
The algorithm can fail in some cases, including cyclic overlap or piercing polygons.
Cyclical overlappingEdit
In the case of cyclic overlap, as shown in the figure to the right, Polygons A, B, and C overlap each other in such a way that it is impossible to determine which polygon is above the others. In this case, the offending polygons must be cut to allow sorting.<ref name=":0" />
Piercing polygonsEdit
The case of piercing polygons arises when one polygon intersects another. Similar to cyclic overlap, this problem may be resolved by cutting the offending polygons.<ref name=":0" />
EfficiencyEdit
In basic implementations, the painter's algorithm can be inefficient. It forces the system to render each point on every polygon in the visible set, even if that polygon is occluded in the finished scene. This means that, for detailed scenes, the painter's algorithm can overly tax the computer hardware.
Reducing visual errorsEdit
There are a few ways to reduce the visual errors that can happen with sorting:
Binary Space PartitioningEdit
BSP is a method that involves making a BSP tree, and splitting triangles where they intersect. It can be extremely hard to implement, but it fixes most visual errors.
Backface cullingEdit
Backface culling involves calculations to see if a triangles points will appear clockwise or counter-clockwise once projected to the screen, and doesn't draw triangles that shouldn't be visible anyway. It reduces some visual errors, as well as reducing the total triangles drawn.
VariantsEdit
Extended painter's algorithmEdit
Newell's algorithm, proposed as the extended algorithm to painter's algorithm, provides a method for cutting cyclical and piercing polygons.<ref name=":0" />
Reverse painter's algorithmEdit
Another variant of painter's algorithm includes reverse painter's algorithm. Reverse painter's algorithm paints objects nearest to the viewer first — with the rule that paint must never be applied to parts of the image that are already painted (unless they are partially transparent). In a computer graphic system, this can be very efficient since it is not necessary to calculate the colors (using lighting, texturing, and such) for parts of a distant scene that are hidden by nearby objects. However, the reverse algorithm suffers from many of the same problems as the standard version.
Other computer graphics algorithmsEdit
The flaws of painter's algorithm led to the development of Z-buffer techniques, which can be viewed as a development of the painter's algorithm by resolving depth conflicts on a pixel-by-pixel basis, reducing the need for a depth-based rendering order.<ref>Template:Cite book</ref> Even in such systems, a variant of the painter's algorithm is sometimes employed. As Z-buffer implementations generally rely on fixed-precision depth-buffer registers implemented in hardware, there is scope for visibility problems due to rounding error. These are overlaps or gaps at joints between polygons. To avoid this, some graphics engines implement "over-rendering",Template:Citation needed drawing the affected edges of both polygons in the order given by the painter's algorithm. This means that some pixels are actually drawn twice (as in the full painter's algorithm), but this happens on only small parts of the image and has a negligible performance effect.
ReferencesEdit
<references /> Template:Sister project