Trang 4 Clipping■ A fundamental task in graphics is to keep those parts of an object that lie outside a selected view from being drawn Trang 6 Line clipping■ Lines are defined by their
Trang 1Lesson 4
Fundamental algorithms
Trinh Thanh Trung School of ICT, HUST
Trang 21 Clipping algorithms
2 Polygon drawing
3 Anti-aliasing
Trang 3Clipping algorithms
Trang 4■ A fundamental task in graphics is to keep those parts of an object that lie outside a selected view from being drawn
■ Clipping is the removal of all objects or part of objects in a modelled scene that are outside the real-world window
Trang 6Line clipping
■ Lines are defined by their endpoints, so it should
be possible just to examine these (in a similar way
to points) and determine whether or not to clip
without considering every pixel on the line
■ We often have windows that are either very
large, i.e nearly the whole scene fits inside, or very small, i.e most of the scene lies inside the window
■ Hence, most lines may be either trivially
accepted or rejected
Trang 7Cohen-Sutherland Outcode
Line clipping
Trang 8Cohen-Sutherland Outcode
■ Each point on all lines are first assigned an
“outcode” defining their position relative to the clipping rectangle
Trang 9■ In case the line crosses the screen, the end
points will be redefined by the intersections of the line and the boundary of the display
Trang 10■ The Cohen-Sutherland line-clipping algorithm is particularly fast for “trivial” cases, i.e lines
completely inside or outside the window
■ Non-trivial lines, i.e ones that cross a boundary
of the window, are clipped by computing the
coordinates of the new boundary endpoint of the line where it crosses the edge of the window
Trang 11Cyrus-beck & Liang-barsky
Line clipping
Trang 12Cyrus-beck & Liang-barsky
■ The Cohen-Sutherland algorithm requires the window to be a rectangle, with edges aligned with the co-ordinate axes
■ It is sometimes necessary to clip to any convex polygonal window, e.g triangular, hexagonal, or
rotated
■ Cyrus-beck and Liang-Barsky line clippers better optimise the intersection calculations for clipping to window boundary
■ Nicholl-Lee-Nicholl reducing redundant
boundary clipping by identifying edge and corner regions
Trang 13Cyrus-beck & Liang-barsky
■ x = x1 + (x2 - x1)u = x1 + uDx
■ y = y1 + (y2 - y1)u = y1 + uDy
■ xmin x1 + Dx.u xmax x [xm, xM]
■ ymin y1 + Dy.u ymax y [ym, yM]
Dy P
Dx P
Dx P
4 3 2 1
1 3
1 2
1 1
y y
q
y y q
x x
q
x x q
M m M m
Trang 14Polygon clipping
Trang 15Sutherland-Hodgman clipping
■ Sutherland-Hodgman basic routine
□ Go around polygon one vertex at a time
□ Current vertex has position p
□ Previous vertex had position s, and it has been added to the output if appropriate
Trang 16i output
p output
Trang 17Sutherland-Hodgman clipping
1 s inside plane and p inside plane
o Add p to output
o Note: s has already been added
2 s inside plane and p outside plane
o Find intersection point i
o Add i to output
3 s outside plane and p outside plane
o Add nothing
4 s outside plane and p inside plane
o Find intersection point i
o Add i to output, followed by p
Trang 18Polygon drawing
Trang 19Polygon scan conversion
■ Polygon scan conversion is a classic general purpose
algorithm.
■ For each scan-line we determine the polygon edges that intersect it, compute spans representing the interior portions of the polygons along this scan-line and fill the
associated pixels.
■ This represents the heart of a scan-line rendering
algorithm used in many commercial products including Renderman and 3D Studio MAX.
Trang 20Polygon scan conversion
■ Use mid-point algorithm
to specify the boundaries
between polygons
■ Basic algorithm:
□ Specify the intersections
between scanline and
polygon boundaries
□ Sort x value incrementally
□ Fill the pixels between
each pair of x
Trang 21Polygon scan conversion
■ Need to handle 4 cases to prevent pixel sharing:
□ if intersection has fractional x value, do we round up or down?
▫ if inside (on left of span) round up, if outside (on right) round down
□ what happens if intersection is at an integer x value?
▫ if on left of span assume its interior otherwise exterior
□ how do we handle shared vertices?
▫ ignore pixel associated with ymax of an edge
□ how do we handle horizontal edges?
▫ handled as a result of previous rule (lower edges not drawn)
Trang 22Polygon scan conversion
rounded down for A rounded up for B
integer x value is on
right = exterior
ymaxnot included horizontal edge
removed
Trang 23Edge walking
■ To fill a polygon:
□ Draw edges horizontally
□ Fill the pixels vertically
□ Interpolate to below edges
□ For each scanline, interpolate the edge’s colour along the lines inside the domain
Trang 24Edge walking
■ Order vertices in x and y
□ 3 cases: break left, break right, no break
■ Walk down left and right edges
□ Fill each span
□ Until breakpoint or bottom vertex is reached
■ Advantage: can be made very fast
■ Disadvantages:
□ Lots of finicky special cases
□ Tough to get right
□ Need to pay attention to fractional offsets
Trang 25Edge walking
■ Fractional offsets:
■ Be careful when interpolating color values!
■ Also: beware gaps between adjacent edges
Trang 26Scanline algorithm
■ Scanline algorithm works by intersecting
scanline with polygon edges and fills the polygon between pairs of intersections
■ An edge table (ET) is created to store a set of
polygon edges in order of their ymin value
■ An active edge table (AET) is created to store a
set of edges intersecting with the scanline
■ During the algorithm, the edges will be moved from ET to AET
■ The edge will be staying in AET until its ymaxvalue reach the scanline’s The edge will then be removed from AET
Trang 27Edge tableNote: line (8,6) → (13,6) has been deleted according to the scan rules
ymax xmin
numerator denominator
Trang 28Active edge table
AET =
ymax current x denominator current numerator round up
round down
Trang 29Active edge table
ymax current x denominator
AET =
current numerator round up
round down
Trang 31Rasterizing triangles
■ Interactive graphics hardware commonly uses edge
walking or edge equation techniques for rasterizing
triangles
■ Two techniques we won’t talk about much:
□ Recursive subdivision of primitive into micropolygons (REYES, Renderman)
□ Recursive subdivision of screen (Warnock)
Trang 32▫ “Above” the line: Ax + By + C > 0
▫ “Below” the line: Ax + By + C < 0
Trang 33Edge equation
■ Edge equations thus define two half-spaces:
■ And a triangle can be defined as the intersection
of three positive half-spaces:
Trang 34Edge equation
■ Solution: turn on those pixels for which all edge equations evaluate to > 0:
+ +
-
Trang 35-Edge equation
■ With (max, min) bounding box
Trang 36Anti-aliasing
Trang 37graphics term for any
unwanted visual artifact
■ Antialiasing: computer graphics term for avoiding unwanted artifacts
Trang 38Problems in signal processing
Trang 39Sampling
Trang 42Jagged, not smooth
Trang 43Loses information
Trang 44■ In raster images – leads to jagged edges with staircase effect
■ We can reduce effects by antialiasing methods
to compensate for undersampling
sampling frequency
Trang 45■ Pre-filtering:
□ Highest quality method, but often impractical.
■ Super-sampling:
□ Use more samples to raise the Nyquist frequency.
□ Simple and widely used.
■ Stochastic sampling:
□ Sample randomly, not uniformly.
□ Relatively simple, usually used in combination with super-sampling.
Trang 46■ Eliminate high frequencies before sampling
(Foley & van Dam p 630)
□ Convert I(x) to F(u)
□ Apply a low-pass filter (e.g., multiply F(u) by a box function)
□ Then sample Result: no aliasing!
■ Problem: most rendering algorithms generate sampled function directly
□ e.g., Z-buffer, ray tracing
Trang 47■ Problem with pre-filtering:
□ Sampling and image generation inextricably linked
in most renderers
▫ Z-buffer algorithm
▫ Ray tracing
■ Still, some approaches try to approximate effect
of convolution in the continuous domain
Trang 48■ Supersampling cons
□ Doesn’t eliminate aliasing, just shifts the Nyquist limit higher
▫ Can’t fix some scenes (e.g., checkerboard)
□ Increase memory storage
■ Supersampling pros
□ Relatively easy
□ Often works all right in practice
□ Can be added to a standard renderer
Trang 49Anti-aliasing by super-sampling
Antialiasing by
supersampling
Trang 505 1
Trang 52Example
Trang 53■ Random: Also known as stochastic sampling, it avoids
the regularity of grid supersampling However, due to the irregularity of the pattern, samples end up being
unnecessary in some areas of the pixel and lacking in
others.
■ Poisson disc: Again an algorithm that places the
samples randomly, but then checks that any two are not too close The end result is even but random distribution of samples Unfortunately, the computational time required for this algorithm is too great to justify its use in real-time
rendering, unless the sampling itself is computationally expensive compared to the positioning the sample points
or the sample points are not repositioned for every single pixel.
Trang 54Super-sampling patterns
■ Jittered: A modification of the grid algorithm to
approximate the Poisson disc A pixel is split into several sub-pixels, but a sample is not taken from the center of each, but from a random point within the sub-pixel
Congregation can still occur, but to a lesser degree.
■ Rotated grid: A 2×2 grid layout is used but the sample
pattern is rotated to avoid samples aligning on the
horizontal or vertical axis greatly improving antialiasing quality for the most commonly encountered cases For an optimal pattern, the rotation angle is arctan(1/2) (about 26.6 degrees) and the square is stretched by a factor of
√5/2
Trang 55▫ Easy to retrofit existing renderers
▫ Works well most of the time
□ Cons:
▫ High storage costs
▫ Doesn’t eliminate aliasing, just shifts Nyquist limit upwards
Trang 56Example
Trang 57Example Example
Trang 58▫ It is just as easy to fire a ray one direction as another
□ Z-buffer: hard, but possible
▫ Notable example: REYES system (?)
▫ Using image jittering is easier (more later)
□ A-buffer: No
▫ Totally built around square pixel filter and sample coherence
Trang 59■ Problem: given a pixel, how to distribute points (samples) within it?
Trang 60Any questions?
Trang 61Lecture notes provided by School of Information and
Communication Technology, Hanoi University of Science and
Technology.
Composed by Huynh Quyet Thang, Le Tan Hung, Trinh Thanh
Trung and others
Edited by Trinh Thanh Trung
Special thanks to all the people who made and released these awesome resources for free:
■ Presentation template by SlidesCarnival
■ Photographs by Death to the Stock Photo ( license )
■ Diverse device hand photos by Facebook Design Resources