61
Objectives Introduce basic implementation strategies Clipping Scan conversion

Objectives Introduce basic implementation strategies Clipping Scan conversion

Embed Size (px)

Citation preview

Page 1: Objectives Introduce basic implementation strategies Clipping Scan conversion

Objectives

• Introduce basic implementation strategies• Clipping • Scan conversion

Page 2: Objectives Introduce basic implementation strategies Clipping Scan conversion

Major tasks

(1) Modeling: results in sets of vertices that specify a group of geometric objects supported by the rest of the system

E.g. GL_POLYGON in OpenGL

Modeler is usually an application program that may be interactive

Page 3: Objectives Introduce basic implementation strategies Clipping Scan conversion

Major tasks

(2) Geometry Processing: • works with vertices• involves a number of transformations

Page 4: Objectives Introduce basic implementation strategies Clipping Scan conversion

Modeling transformations

Viewing transformation

Projection transformation

Clipping

Perspective division

NDC to viewport (window coordinates)

3D WORLD

3D World

3D camera

3D clip

3D clip

3D NDC

2D window (with depth)

“NormalizedViewVolume”

Page 5: Objectives Introduce basic implementation strategies Clipping Scan conversion

Projection Transformation

• OpenGL transforms vertices by a projection transformation to a normalized view volume (cube centered at origin, with side length 2). These coordinates are referred to as normalized device coordinates

• No matter how the viewing volume was specified by the user (e.g. glOrtho or gluPerspective)

11

11

11

z

y

x

Page 6: Objectives Introduce basic implementation strategies Clipping Scan conversion

Perspective Projection

z

y

Z=-near

Z=-far

z

y+1

+1 (far)

-1

-1(near)

Viewing frustum Normalized view volume

viewer

Page 7: Objectives Introduce basic implementation strategies Clipping Scan conversion

Normalized Device Coordinates

• (x,y) coordinates indicate the projected point on the final viewing window.

Orthogonal projection of the distorted shape in the normalized volume is equivalent to perspective projection of the original shape

• z coordinate is used for depth There is a reversal of z coordinates: the points closest to the

viewer have smaller z coordinates Z coordinates went through a nonlinear shortening, however

relative order of z coordinates are maintained—that is sufficient for hidden surface removal

Page 8: Objectives Introduce basic implementation strategies Clipping Scan conversion

Why normalization?

• To simplify clipping Now primitives are clipped with respect to the same

shape!

In fact, clipping takes place before the perspective division (division by w): still an axis aligned rectangular box.

• To avoid division operation for parts that are not going to be visible in the window at the end!

wzw

wyw

wxw

Clip coordinates

Page 9: Objectives Introduce basic implementation strategies Clipping Scan conversion

Clipping

• 2D against clipping window• 3D against clipping volume• Easy for line segments polygons• Hard for curves and text

Convert to lines and polygons first

Page 10: Objectives Introduce basic implementation strategies Clipping Scan conversion

Clipping 2D Line Segments

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

Inefficient: one division per intersection

Page 11: Objectives Introduce basic implementation strategies Clipping Scan conversion

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 12: Objectives Introduce basic implementation strategies Clipping Scan conversion

The Cases

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

Draw (accept) line segment as is

• Case 2: both endpoints outside all lines and on same side of a line

Discard (reject) the line segment

x = xmaxx = xmin

y = ymax

y = ymin

Page 13: Objectives Introduce basic implementation strategies Clipping Scan conversion

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 14: Objectives Introduce basic implementation strategies Clipping Scan conversion

Defining Outcodes

• For each endpoint, define an outcode

• Outcodes divide space into 9 regions• Computation of outcode requires at most 4 subtractions

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

Page 15: Objectives Introduce basic implementation strategies Clipping Scan conversion

Using Outcodes

• Consider the 5 cases below• AB: outcode(A) = outcode(B) = 0

Accept line segment

Page 16: Objectives Introduce basic implementation strategies Clipping Scan conversion

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

Page 17: Objectives Introduce basic implementation strategies Clipping Scan conversion

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 18: Objectives Introduce basic implementation strategies Clipping Scan conversion

Using Outcodes

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

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

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

• Reexecute algorithm

Page 19: Objectives Introduce basic implementation strategies Clipping Scan conversion

Efficiency

• In many applications, the clipping window is small relative to the size of the whole 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 reexecuted for line segments that must be shortened in more than one step

Page 20: Objectives Introduce basic implementation strategies Clipping Scan conversion

Cohen Sutherland in 3D

• Use 6-bit outcodes • When needed, clip line segment against planes

Page 21: Objectives Introduce basic implementation strategies Clipping Scan conversion

Liang-Barsky Clipping

• Consider the parametric form of a line segment (works in both 2- and 3-dimensions)

• We can distinguish between the cases by looking at the ordering of the values of where the line determined by the line segment crosses the lines that determine the window

p() = (1-)p1+ p2 1 0

p1

p2

Page 22: Objectives Introduce basic implementation strategies Clipping Scan conversion

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

Page 23: Objectives Introduce basic implementation strategies Clipping Scan conversion

Liang-Barsky Clipping

• Efficient implementation requires avoiding computing intersections until they are needed.

• We also want to avoid divisions when possible.

Page 24: Objectives Introduce basic implementation strategies Clipping Scan conversion

Liang-Barsky Clipping

• The intersection with top line (y= ymax) is at

• Rewrite equation as

• It is possible to restate all the rules in terms of

12

1max

yy

yy

max1max12 )( yyyyyy

.,, max etcyy

Page 25: Objectives Introduce basic implementation strategies Clipping Scan conversion

Liang-Barsky Clipping

• Many lines can be rejected before all 4 intersections are known.

E.g. if , reject0,0 max yy

Page 26: Objectives Introduce basic implementation strategies Clipping Scan conversion

Liang-Barsky Clipping

• Can accept/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 We are now clipping lines against planes (six of

them)

Page 27: Objectives Introduce basic implementation strategies Clipping Scan conversion

Plane-Line Intersections

)(

)(

,0))((

)1()(

12

1

0

21

ppn

ppna

papn

appaap

o

Page 28: Objectives Introduce basic implementation strategies Clipping Scan conversion

Plane-line intersection

• The above equation requires six multiplications and divisions!

• However, the normalized viewing volume has axis aligned planes

Reduces to a single division just like in 2D. E.g.•

12

1max

yy

yya

Page 29: Objectives Introduce basic implementation strategies Clipping Scan conversion

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 30: Objectives Introduce basic implementation strategies Clipping Scan conversion

Tessellation and Convexity

• Either do not allow nonconvex polygons• or replace nonconvex (concave) polygons with a

set of triangular polygons (a tessellation)

Page 31: Objectives Introduce basic implementation strategies Clipping Scan conversion

Clipping as a Black Box

• 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 32: Objectives Introduce basic implementation strategies Clipping Scan conversion

Pipeline Clipping of Line Segments

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

Can use four independent clippers in a pipeline

Page 33: Objectives Introduce basic implementation strategies Clipping Scan conversion

Pipeline Clipping of Polygons

• Clip edges of polygons successively• Three dimensions: add front and back clippers• Strategy used in SGI Geometry Engine• Small increase in latency

Page 34: Objectives Introduce basic implementation strategies Clipping Scan conversion

Therefore clipped polygon is P, Q, R, S.

Clipping to One Boundary

• Consider each polygon edge in turn• Four cases:

ENTER:

STAY IN:

LEAVE:

STAY OUT:

inside

P

Q

Output P, Q R

Output R

S

Output S

(no output)

Page 35: Objectives Introduce basic implementation strategies Clipping Scan conversion

Clipping Example

• Works for more nonconvex shapes.

Input Case Output1 start -2 stay in 23 leave A4 stay out -5 enter B, 56 leave C7 stay out -1 enter D, 1

2 A B 5 C D 1

1

4

6

5

3

2

7

A

D

CB

inside

Page 36: Objectives Introduce basic implementation strategies Clipping Scan conversion

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 37: Objectives Introduce basic implementation strategies Clipping Scan conversion

Bounding boxes

Can usually determine accept/reject based only on bounding box

reject

accept

requires detailed clipping

Page 38: Objectives Introduce basic implementation strategies Clipping Scan conversion

Rasterization

• Rasterization (scan conversion) Shade pixels that are inside an object specified

by a set of vertices• Line segments• Polygons: scan conversion = fill

• Shades determined by color, texture, shading model

• Here we study algorithms for determining the correct pixels starting with the vertices

Page 39: Objectives Introduce basic implementation strategies Clipping Scan conversion

Scan Conversion of Line Segments

• Start with line segment in window coordinates with integer values for endpoints

• Assume implementation has a write_pixel function

y = mx + h

x

ym

Page 40: Objectives Introduce basic implementation strategies Clipping Scan conversion

DDA Algorithm

• Digital Differential Analyzer DDA was a mechanical device for numerical

solution of differential equations

Line y=mx+ h satisfies differential equation dy/dx = m = y/x = y2-y1/x2-x1

• Along scan line x = 1

For(x=x1; x<=x2, x++) { y+=m; write_pixel(x, round(y), line_color)}

Page 41: Objectives Introduce basic implementation strategies Clipping Scan conversion

Problem

• DDA = for each x plot pixel at closest y Problems for steep lines

Page 42: Objectives Introduce basic implementation strategies Clipping Scan conversion

Using Symmetry

• Use for 1 m 0• For m > 1, swap role of x and y

For each y, plot closest x

Page 43: Objectives Introduce basic implementation strategies Clipping Scan conversion

Bresenham’s Algorithm

• DDA requires one floating point addition per step

• We can eliminate all fp through Bresenham’s algorithm

• Consider only 1 m 0 Other cases by symmetry

• Assume pixel centers are at half integers• If we start at a pixel that has been written, there

are only two candidates for the next pixel to be written into the frame buffer

Page 44: Objectives Introduce basic implementation strategies Clipping Scan conversion

Candidate Pixels

1 m 0

last pixel

candidates

Note that line could havepassed through anypart of this pixel

Page 45: Objectives Introduce basic implementation strategies Clipping Scan conversion

Decision Variable

-

d = x(b-a)

d is an integerd > 0 use upper pixeld < 0 use lower pixel

Page 46: Objectives Introduce basic implementation strategies Clipping Scan conversion

Incremental Form

• To compute dk+1

More efficient if we use dk, the value of the decision variable at x = xk

122)1(2

1

)1(

kk

k

k

k

yhxmab

yya

yyb

hxmya

b

1kx

ky

1ky

y

Page 47: Objectives Introduce basic implementation strategies Clipping Scan conversion

Incremental Form

)12(2.2.2)(

122)1(2

hxyyxxyabxd

yhxmab

kkk

kk

a

b

1kx

ky

1ky

y

Constant (c )

Page 48: Objectives Introduce basic implementation strategies Clipping Scan conversion

Incremental Form

).(22

.2)1.(2

11

11

kkkk

kkk

yyxydd

cyxxyd

a

b

1kx

ky

1ky

y

Either 0 or 1 depending on the sign of dk

Page 49: Objectives Introduce basic implementation strategies Clipping Scan conversion

Incremental Form

dk+1= dk +2y, if dk < 0dk+1= dk +2(y- x), otherwise

•For each x, we need do only an integer addition and a test

Page 50: Objectives Introduce basic implementation strategies Clipping Scan conversion

Bresenham’s Algorithm

• (1) Input the two line endpoints and store the left endpoint in(x1, y1)

• (2)set the color for posiiton(x1,y2) and plot the first point• (3) calculate constants y, x, 2(y-x)• (4)calculate d1 = 2y-x• (5) At each xk, starting at k=1 do

if dk < 0, plot (xk +1, yk)dk+1= dk +2y,

Else plot(xk+1, yk+1)dk+1= dk +2(y- x)

Page 51: Objectives Introduce basic implementation strategies Clipping Scan conversion

Example

• Endpoints (20,10) and (30,18)x=10, y=8, d1=62y-2x=-4, 2y=16

Page 52: Objectives Introduce basic implementation strategies Clipping Scan conversion

Example

20 22 25 30

10

15

18

• Endpoints (20,10) and (30,18)

x=10, y=8, d1=6• 2y-2x=-4, 2y=16

Page 53: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

(x1,y1)=(20,10)

P1=6

Next pixel (x2,y2): (21,11)

P2=6-4=2

Page 54: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

(x2,y2)=(21,11)

P2=2

Next pixel (x3,y3): (22,12)

P3=2-4=-2

Page 55: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

(x3,y3)=(22,12)

P3=-2

Next pixel (x4,y4): (23,12)

P4=-2+16=14

Page 56: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

(x4,y4)=(23,12)

P4=14

Next pixel (x5,y5): (24,13)

P5=14-4=10

Page 57: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

Page 58: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

Page 59: Objectives Introduce basic implementation strategies Clipping Scan conversion

20 22 25 30

10

15

18

Page 60: Objectives Introduce basic implementation strategies Clipping Scan conversion

Aliasing

• Ideal rasterized line should be 1 pixel wide

• Choosing best y for each x (or visa versa) produces aliased raster lines

Page 61: Objectives Introduce basic implementation strategies Clipping Scan conversion

Antialiasing by Area Averaging

• Color multiple pixels for each x depending on coverage by ideal line

original antialiased

magnified