CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x...

18
CS475 / CS675 Computer Graphics Lecture 2 : Clipping

Transcript of CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x...

Page 1: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS475 / CS675Computer Graphics

Lecture 2 : Clipping

Page 2: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Image Formation

Camera Model

Image

World

Reflected Ray

Incident Ray

Transmitted Ray

Point Light Source

Page 3: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Camera Model

Pinhole Camera

Y

X

Z

O

P(X, Y, Z)

p(x, y)

f

x = ­ f X/Zy = ­ f Y/Z

Page 4: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Synthetic Camera ModelY

X

Z

P(X, Y, Z) p(x, y)

f

x = f X/Zy = f Y/Z

Eye

Lookat

Up Vector

Is the Eye, Lookat and Up Vector is enough to define the camera?

Page 5: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Synthetic Camera ModelY

X

Z

P(X, Y, Z) p(x, y)

w

h

Eye

Lookat

Up Vector

The field of view (Ө) is also needed alongwith the window aspect ratio (w/h).

Ө

Page 6: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Synthetic Camera ModelY

Z

Eye

Lookat

Up Vector

What if the window is shifted?

Page 7: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Synthetic Camera ModelY

Z

Eye

Lookat

Up Vector

If the window is shifted the the scene gets clipped at the window edges.

Camera Demo

Page 8: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Line Clipping● Divide the plane into 9 regions.

● Each region has its own 4 bit outcode.

● Compute the outcodes OC0 and

OC1 for the vertices of the line

segment.

● Trivially accept if OC0 ∨ OC

1 = 0 (TA)

● Trivially reject if OC0 ∧ OC

1 = 1 (TR)

● If cannot TA/TR, subdivide line into two segments at a clip edge and TA/TR one or both segments.

● Repeat until entire line has been processed.

Cohen – Sutherland Algorithm

0000

1001 1000 1010

0001 0010

011001000101

Page 9: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Line Clipping

Cohen – Sutherland Algorithm

clipline(x0, y0,x1, y1){ ComputeOutcode(x0, y0, OC0);

ComputeOutcode(x1, y1, OC1); repeat Check for TA and TR. If either happens then done.

Choose a vertex that is outside the clip rectangle.

If (vertex lies over TOP edge) then

x = x0 + 1/slope * (ymax – y0)y = ymax

....

0000

1001 1000 1010

0001 0010

011001000101

Page 10: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Line Clipping

Cohen – Sutherland Algorithm

else if (vertex lies below BOTTOM edge) then

x = x0 + 1/slope * (ymin – y0)y = ymin

else if (vertex lies to right of RIGHT edge) then

y = y0 + slope * (xmax – x0)x = xmax

else if (vertex lies to left of LEFT edge) then

y = y0 + slope * (xmin – x0)x = xmin

0000

1001 1000 1010

0001 0010

011001000101

Page 11: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Line Clipping

Cohen – Sutherland Algorithm

else if (vertex lies below BOTTOM edge) then

x = x0 + 1/slope * (ymin – y0)y = ymin

else if (vertex lies to right of RIGHT edge) then

y = y0 + slope * (xmax – x0)x = xmax

else if (vertex lies to left of LEFT edge) then

y = y0 + slope * (xmin – x0)x = xmin

0000

1001 1000 1010

0001 0010

011001000101

Page 12: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Line Clipping

Cohen – Sutherland Algorithm

if (x0, y0) was the outer point then

x0= x, y0 = yComputeOutcode(x0, y0, OC0)

elsex1= x, y1 = yComputeOutcode(x1, y1, OC1)

until (done)

}Issues in­ Clipping­ Scan Conversion and Clipping

Read notes on Cyrus­Beck Parametric Line Clipping Algorithm.

0000

1001 1000 1010

0001 0010

011001000101

Page 13: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Polygon Clipping

Page 14: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Polygon Clipping

Sutherland – Hodgman Algorithm

Page 15: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Polygon Clipping

Sutherland – Hodgman Algorithm

Page 16: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Polygon Clipping

Sutherland – Hodgman Algorithm

Page 17: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Polygon Clipping

Sutherland – Hodgman Algorithm

Page 18: CS475 / CS675 Computer Graphics · Line Clipping Cohen – Sutherland Algorithm clipline(x 0, y 0,x 1, y 1) { ComputeOutcode(x 0, y 0, OC 0); ComputeOutcode(x 1, y 1, OC 1); repeat

CS 475/CS 675: Lecture 3 Parag Chaudhuri, 2015

Polygon Clipping

Sutherland – Hodgman Algorithm