clipping lines lecture 7 wed, sep 10, 2003. the graphics pipeline from time to time we will discuss...

28
Clipping Lines Lecture 7 Wed, Sep 10, 2003

Upload: joseph-strickland

Post on 17-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

Clipping Lines

Lecture 7Wed, Sep 10, 2003

Page 2: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Graphics Pipeline

From time to time we will discuss the graphics pipeline.The graphics pipeline is the sequence of operations that are performed on primitives to render them on the screen as collections of colored pixels.We have discussed the framebuffer.Now we will discuss 2-dimensional clipping of lines.

Page 3: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

Clipping

Any object that is not entirely within the viewport must be clipped.That is, the part that is outside the viewport must be eliminated before the object is drawn.The graphics pipeline, does this “automatically.”Why is it necessary to clip?

Page 4: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

Clipping Lines

If a line is partially in the viewport, then we need to recalculate its endpoints.In general, there are four possible cases. The line is entirely within the viewport. The line is entirely outside the viewport. One endpoint is in and the other is out. Both endpoints are out, but the middle

part is in.

Page 5: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

Clipping Lines

Before clipping

F

A

B

C

D

E

G

H

Page 6: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

Clipping Lines

After clipping

F

A

B

E

G

H

F'

G'

H'

C

D

Page 7: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

Let the x-coordinates of the window boundaries be xmin and xmax and let the y-coordinates be ymin and ymax.

xmin xmax

ymin

ymax

Page 8: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

For each endpoint p of a line segment we will define a codeword c3c2c1c0 consisting of 4 true/false values. c3 = true, if p is left of the window. c2 = true, if p is above the window. c1 = true, if p is right of the window. c0 = true, if p is below the window.

How many different values are possible for a codeword?

Page 9: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

How many different combinations of values are possible for the codewords of the two endpoints of a segment?How do we compute a codeword?

Page 10: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

Consider the vertical edge x = xmin. (You can do the other edges.)Compare the x-coordinate of p to xmin.If it is less, then “set” bit 3 of the codeword.if (p.x < xmin)

c = 1 << 3;

Page 11: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

After the codewords for both endpoints are computed, we divide the possibilities into three cases: Trivial accept – both codewords are

FFFF. Trivial reject – both codewords have T

in the same position. Indeterminate so far – Investigate

further.

Page 12: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

What is the quickest way to separate the cases? Use bitwise “and” and “or.”

If ((codeword1 | codeword2) == 0) then we trivially accept. Why?

If ((codeword1 & codeword2) != 0) then we trivially reject. Why?

Page 13: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

What about the third case?We clip against each edge of the window, in sequence, as necessary.After clipping against an edge, we may test again for trivial acceptance or trivial rejection before moving on to the next edge.

Page 14: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

Page 15: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

clip

Page 16: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

clip

Page 17: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

clip

Page 18: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

clip

Page 19: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

Page 20: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

This raises some questions? What is the worst case? What is the best case? How do we decide whether to clip

against a particular edge? If we do clip, how to we determine the

new endpoint?

Page 21: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

Consider again the vertical edge x = xmin. (You can do the other edges.)Compute codeword1 ^ codeword2.This shows where they disagree. Why?

“And” this with (1 << 3). If result = 1, then clip. Else do not clip.

Page 22: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

If we clip, then the new point will be on the line x = xmin.So its x-coordinate will be xmin.We must calculate its y-coordinate.

Page 23: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

p

q

r

x = xmin

Page 24: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

p

r

x = xminq

Page 25: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

p

r

xmin – p.x q.x – p.x

q.y – p.y

r.y – p.y

x = xminq

Page 26: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

Then(r.y – p.y)/(xmin – p.x) = (q.y – p.y)/(q.x –

p.x)

Sor.y = p.y + (xmin – p.x)(q.y – p.y)/(q.x –

p.x)

Page 27: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

The Cohen-Sutherland Clipping Algorithm

This used to be done in software.Now it is done in hardware, in the graphics pipeline.Should it be done before or after the line is rasterized?We will discuss clipping polygons and clipping in 3D later.

Page 28: Clipping Lines Lecture 7 Wed, Sep 10, 2003. The Graphics Pipeline From time to time we will discuss the graphics pipeline. The graphics pipeline is the

Example: Clip a Line

LineClipper.cpp