Vertices and Fragments I
CS4395: Computer Graphics 1
Mohan SridharanBased on slides created by Edward Angel
Objectives
• Chapter 7.
• Implementation of rendering pipeline.– Clipping: viewing volume.– Rasterization: fragments from valid objects.– Hidden-surface removal: occlusions, blocked surfaces.
• This lecture:– Clipping.– Scan conversion.
CS4395: Computer Graphics 2
Overview
• At end of the geometric pipeline, vertices have been assembled into primitives.
• Must clip out primitives that are outside the view frustum:– Algorithms represent primitives by lists of vertices.
• Must compute pixels that can be affected by each primitive:– Fragment generation.– Rasterization or scan conversion.
CS4395: Computer Graphics 3
Required Tasks
• Clipping.• Rasterization or scan conversion.• Transformations.
• Some tasks deferred until fragment processing:– Hidden surface removal .– Anti-aliasing.
CS4395: Computer Graphics 4
Rasterization Meta Algorithms
• Consider two approaches to render a scene with opaque objects.
• For every pixel, determine object that projects on the pixel and is closest to the viewer. Compute the shade of this pixel.– Ray tracing paradigm.
• For every object, determine pixels it covers and shade them:– Pipeline approach.– Must keep track of depths.
CS4395: Computer Graphics 5
Clipping• 2D against clipping window.• 3D against clipping volume.• Easy for line segments and polygons.• Hard for curves and text:
– Convert to lines and polygons first
CS4395: Computer Graphics 6
Clipping 2D Line Segments
• Brute force approach: compute intersections with all sides of clipping window.– Inefficient: one division per intersection
CS4395: Computer Graphics 7
Cohen-Sutherland Algorithm
• Idea: eliminate as many cases as possible without computing intersections.
• Start with four lines that determine the sides of the clipping window.
CS4395: Computer Graphics 8
x = xmaxx = xmin
y = ymax
y = ymin
The Cases
• Case 1: endpoints of line segment inside all four lines.– Draw (accept) the line segment.
• Case 2: endpoints outside all lines and on same side of a line.– Discard (reject) the line segment.
CS4395: Computer Graphics 9
x = xmaxx = xmin
y = ymax
y = ymin
The Cases
• Case 3: one endpoint inside, one outside.– Must do at least one intersection.
• Case 4: both outside.– May have part inside.– Must do at least one intersection.
CS4395: Computer Graphics 10
x = xmaxx = xmin
y = ymax
Defining Outcodes• For each endpoint, define an outcode:
• Outcodes divide space into 9 regions.• Computation of outcode requires at most 8 subtractions per
line segment.
CS4395: Computer Graphics 11
b0b1b2b3
b0 = 1 if y > ymax, 0 otherwiseb1 = 1 if y < ymin, 0 otherwiseb2 = 1 if x > xmax, 0 otherwiseb3 = 1 if x < xmin, 0 otherwise
Using Outcodes
• Consider the 5 cases below.
• AB: outcode(A) = outcode(B) = 0000.– Accept line segment.
CS4395: Computer Graphics 12
Using Outcodes
• CD: outcode (C) = 0000, outcode(D) 0 (0010).– Compute intersection.– Location of 1 in outcode(D) determines which edge to intersect with.– Note: if there were a segment from A to a point in a region with 2
ones in outcode, we might have to do two intersections.
CS4395: Computer Graphics 13
Using Outcodes
• EF: outcode(E) logically ANDed with outcode(F) (bitwise) 0.– Both outcodes have a 1 bit in the same place (0010, 0010).– Line segment is outside of corresponding side of clipping window.– Reject.
CS4395: Computer Graphics 14
Using Outcodes
• GH, IJ have similar outcodes: (1001, 1000) and (0001, 1000).– Reject GH but what about IJ (logical AND returns zero) ?
• Shorten line segment by intersecting with sides of window.• Compute outcode of intersection (new endpoint of shortened
line segment).• Re-execute algorithm.
CS4395: Computer Graphics 15
Efficiency
• In many applications, the clipping window is small relative to the size of the entire data base:– Most line segments are outside one or more side of the window and
can be eliminated based on their outcodes.
• Inefficiency when code has to be re-executed for line segments that must be shortened in more than one step.
CS4395: Computer Graphics 16
Cohen Sutherland in 3D
• Use 6-bit outcodes.• When needed, clip line segment against planes.
CS4395: Computer Graphics 17
Liang-Barsky Clipping
• Consider the parametric form of a line segment:
• As parameter changes from 0 to 1, move from p1 to p2.
• We can distinguish between the cases by looking at the values of where the extended line segment crosses the lines that determine the window.
CS4395: Computer Graphics 18
p1
p2
21
21
21
)1()(
)1()(
10)1()(
yyy
xxx
ppp
Liang-Barsky Clipping
• In (a): 4 > 3 > 2 > 1
– Intersect right, top, left, bottom: shorten.• In (b): 4 > 2 > 3 > 1
– Intersect right, left, top, bottom: reject.
CS4395: Computer Graphics 19
Advantages
• Can accept or reject as easily as with Cohen-Sutherland.
• Using values of , we do not have to use algorithm recursively as with C-S.
• Extends to 3D.
CS4395: Computer Graphics 20
Clipping and Normalization
• General clipping in 3D requires intersection of line segments against arbitrary plane.
• Example: oblique view.
CS4395: Computer Graphics 21
Plane-Line Intersections
CS4395: Computer Graphics 22
)(
)(
12
1
ppn
ppna o
Normalized Form
CS4395: Computer Graphics 23
before normalization after normalization
Normalization is part of viewing (pre-clipping), but after normalization clip against sides of right parallelepiped.
Typical intersection calculation now requires only a floating point subtraction, e.g. is x > xmax ?
top view
Polygon Clipping
• Not as simple as line segment clipping:– Clipping a line segment yields at most one line segment.– Clipping a polygon can yield multiple polygons.
• However, clipping a convex polygon can yield at most one other polygon.
CS4395: Computer Graphics 24
Tessellation and Convexity
• Replace non-convex (concave) polygons with a set of triangular polygons (a tessellation).
• Also makes fill easier.• Tessellation code in GLU library.
CS4395: Computer Graphics 25
Line Clipping as a Black Box
• Consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment.
CS4395: Computer Graphics 26
Pipeline Clipping of Line Segments
• Clipping against each side of window is independent of other sides.
• Can use four independent clippers in a pipeline.
CS4395: Computer Graphics 27
Pipeline Clipping of Polygons
• Three dimensions: add front and back clippers!• Strategy used in SGI Geometry Engine.• Small increase in latency.
CS4395: Computer Graphics 28
Bounding Boxes
• Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent.– Smallest rectangle aligned with axes that encloses the polygon.– Simple to compute: max and min of x and y.
CS4395: Computer Graphics 29
Bounding Boxes
• Can usually determine accept or reject decisions based only on bounding box.
CS4395: Computer Graphics 30
reject
accept
requires detailed clipping
Clipping and Visibility
• Clipping has much in common with hidden-surface removal.
• In both cases, we are trying to remove objects that are not visible to the camera.
• Often we can use visibility or occlusion testing early in the process to eliminate as many polygons as possible before going through the entire pipeline.
CS4395: Computer Graphics 31
Summary
• More information: Sections 7.4-7.7.
• What next:– Rasterization.– Hidden surface removal.
CS4395: Computer Graphics 32