+ All Categories
Home > Documents > ECE478 11 Clipping -...

ECE478 11 Clipping -...

Date post: 24-May-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
39
Clipping Line Segments Cohen-Sutherland Algorithm Gregor Miller gregor{at}ece.ubc.ca
Transcript
Page 1: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Clipping

Line SegmentsCohen-Sutherland Algorithm

Gregor Miller gregor{at}ece.ubc.ca

Page 2: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Overview

• At the end of the geometric pipeline, vertices have been assembled intro primitives

• Must clip primitives that are outside the view frustum

• Algorithms based on representing primitives by lists of vertices

• Must find which pixels can be affected by each primitive

• Fragment generation

• Rasterization of scan conversion

Page 3: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Required Tasks

• Clipping

• Rasterization of scan conversion

• Transformations

• Some tasks deferred until fragment processing

• Hidden surface removal

• Antialiasing

Page 4: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Rasterization Meta Algorithms

• Consider two approaches to rendering a scene with opaque objects

• For every pixel, determine which of the objects which projects onto the pixel is closest to the viewer and compute the shade of this pixel

• Used by ray tracing

• For every object, determine which pixels it covers and shade these pixels

• Pipeline approach

• Must keep track of depths

Page 5: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

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

Page 6: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Clipping 2D Line Segments

• Brute force approach: compute intersections with all sides of clipping window

• Inefficient: one division per intersection

Page 7: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

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

x = xmaxx = xmin

y = ymax

y = ymin

Page 8: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

The Cases

• Case 1: both endpoints of line segment inside all four lines

• Accept (i.e. draw) the line segment with no modification

• Case 2: both endpoints outside on same side of one line

• Reject the line segment for rendering

x = xmaxx = xmin

y = ymax

y = ymin

Page 9: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

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

x = xmaxx = xmin

y = ymax

Page 10: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Defining Outcodes

• For each endpoint, define a 4-bit outcode: b0b1b2b3

!

!

!

!

• Outcodes divide space into 9 regions

• Computation of outcode requires at most 4 subtractions

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

Page 11: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Using Outcodes

• Consider the 5 cases below

• AB: outcode(A) = outcode(B) = 0 - accept!

Page 12: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Using Outcodes

• CD: outcode(C) = 0, outcode(D) ≠ 0

• 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 an outcode, we might have to do two intersections

Page 13: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Using Outcodes

• EF: outcode(E) logically ANDed with outcode(F) (bitwise) ≠ 0

• Both outcodes have a 1 bit in the same place

• Line segment is outside of corresponding side of clipping window

• Reject!

Page 14: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Using Outcodes

• GH and IJ: same outcodes, neither zero but logical AND yields zero

• Shorten line segment by intersecting with one of the sides of the window

• Computer outcode of intersection (new endpoint of shortened line segment)

• Iterate algorithm again...

Page 15: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Efficiency

• In many applications, the clipping window is small relative to the size of the entire world

• Most line segments are outside one or more sides of the window and can be eliminated based on their outcodes

• Inefficiency when code has to be executed again for line segments that must be shortened in more than one step

Page 16: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Cohen-Sutherland in 3D

• Use 6-bit outcodes

• When needed clip line segments against planes

Page 17: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

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

Page 18: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Tessellation and Convexity

• One strategy is to replace non-convex (concave) polygons with a set of triangular polygons (a tessellation)

• This makes filling easier

• Tessellation code is available in the GLU library

Page 19: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Clipping as a Black Box

• We can 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

Page 20: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Pipeline Clipping of Line Segments

• Clipping against each side of the window is independent of the other sides

• So we can use four independent clippers in a pipeline

Page 21: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Pipeline Clipping of Polygons

!

!

!

!

!

• Three dimensions: add front and back clippers

• Small increase in latency

Page 22: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

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

Page 23: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Bounding Boxes

• Can usually determine accept/reject based only on bounding box

Reject

AcceptRequires detailed

clipping

Page 24: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Hidden Surface Removal

Painter’s AlgorithmDepth SortDepth BufferBSP Trees

Gregor Miller gregor{at}ece.ubc.ca

Page 25: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

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

Page 26: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Hidden Surface Removal

• Object-space approach: use pairwise testing between polygons (objects)

!

!

!

!

• Worst complexity if O(n2) for n polygons

Partially obscuring Draw independently

Page 27: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Painter’s Algorithm

• Render polygons in back to front order so that polygons behind others are painted over

B behind A as seen by viewer Fill B then A

Page 28: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Depth Sort

• Requires ordering of polygons first

• O(n log n) calculation of ordering

• Not every polygon is either in front or behind all other polygons

• Order polygons and deal with easy cases first, harder later

Page 29: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Easy Cases

• A lies behind all other polygons, so can render this

!

• Polygons overlap in z but not in either x or y

• Can render these independently

Page 30: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Hard Cases

Cyclic OverlapPenetrationOverlap in all

dimensions (one fully in front of other)

Page 31: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Back Face Removal (Culling)

• Face is visible iff θ < 90

OR cosθ > 0

OR v ⋅ n > 0 (for |v| = |n| = 1)

• Plane of face has form ax + by + cz + d = 0

but after view normalization v = (0 0 1 0)T, so only have to test c >= 0

• In OpenGL we can simply enable culling but may not work correctly if we have non-convex objects

θ

Page 32: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Image Space Approach

• Look at each projector and find closest of the K polygons it intersects (there are NM projectors in an N x M image)

• Complexity O(NMK)

• Ray tracing

• z-buffer

Page 33: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

z-Buffer Algorithm

• Use a buffer called the z or depth buffer to store the depth of the closest object found so far, at each pixel

• As we render each polygon, compare the depth of each pixel to depth in z-buffer

• If less, place shade of pixel in colour buffer and update z-buffer

Page 34: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Efficiency

• If we work on scan lines, as we move across the scan line the depth changes satisfy

a∆x + b∆y + c∆z = 0

• Along a scan line, ∆y = 0, ∆z = -(a/c)∆x and in screen space ∆x = 1

Page 35: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Scan-Line Algorithm

• Can combine shading and HSR through the scan line algorithm

Scan line i: no need for depth information, can only be in one polygon (or none)

Scan line j: need depth information only when inmore than one polygon

Page 36: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Implementation

• Need a data structure to store

• Flag for each polygon (inside/outside)

• Incremental structure for scan lines that stores which edges are encountered

• Parameters for planes

Page 37: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Visibility Testing

• In many real-time applications (e.g. games) we want to eliminate as many objects as possible within the application

• Reduce burden on pipeline

• Reduce traffic on bus

• Partition space with Binary Spatial Partition (BSP) tree

Page 38: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

Simple Example

Consider 6 parallel polygons

Top view

The plane of A separates B and C from D, E and F

Page 39: ECE478 11 Clipping - courses.ece.ubc.cacourses.ece.ubc.ca/478/Lectures/2014_PDF/ECE478_11_Clipping.pdf · Cohen-Sutherland Algorithm • Idea: eliminate as many cases as possible

BSP Tree

• Can continue recursively

• Plane of C separates B from A

• Plane of D separates E and F

• Can put this information in a BSP tree

• Use for visibility and occlusion testing


Recommended