Sei sulla pagina 1di 19

Computer Graphics

Computer Graphics
Lecture 7
Arbitrary Viewing I:
2D Clipping and Projections

Computer Graphics

Viewing in 2D
In 2D, a world consists of an infinite plane, defined in
world coordinates, i.e metres, angstroms etc.
We need to pick an area of the 2D plane to view, referred
to as the window.
On our display device, need to allocate an area for display,
referred to as the viewport in device specific coordinates.
Clip objects outside of window.
Translate to fit viewport.
Scale to device coordinates.

17/10/2008

Lecture 7

Computer Graphics

Viewing in 2D
250

45

Window in world coordinates.

Viewport in
Device coords

17/10/2008

250 x 250
Pixels.

Lecture 7

Computer Graphics

Clipping in 2D.
Need to clip primitives against the sides of the
window.
e.g lines

17/10/2008

Lecture 7

Computer Graphics

Trivial acceptance.
All line vertices lie inside box accept.

17/10/2008

Lecture 7

Computer Graphics

Trivial rejection.
All line vertices lie outside and on same side reject.

17/10/2008

Lecture 7

Computer Graphics

Cohen-Sutherland algorithm
Efficient method of accepting or rejecting
lines that dont intersect the window edges.
Assign a binary 4 bit code to each vertex :

First bit : above top of window, y > ymax


Second bit : below bottom, y < ymin
Third bit : to right of right edge, x > xmax
Fourth bit : to left of left edge, x < xmin
4-bit code called: Outcode

17/10/2008

Lecture 7

Computer Graphics

Cohen-Sutherland 2D outcodes
1001

17/10/2008

1000

1010

0001

0000

0010

0101

0100

0110

Lecture 7

Computer Graphics

Cohen-Sutherland algorithm
1001

1000

0001

0000

0010

0101

0100

0110

1010

Both endpoint codes 0000, trivial acceptance, else:


Do logical AND of outcodes
17/10/2008

Lecture 7

Computer Graphics

Cohen-Sutherland algorithm
1001

1010

1000
1000

0001
0000

0001

0010

0000
0000
0101

0100

0110

Logical AND between codes for 2 endpoints,


Reject line if non-zero trivial rejection.
17/10/2008

Lecture 7

10

Computer Graphics

Line Intersection.
Now need to intersect line segments with edges of
clip rectangle.
Select any clip edge, trivial line-splitting, feed two
new lines back into algorithm - reentrant
Alternatively express line in parametric form to
handle vertical lines.
Substitute for x or y.
Solve for t.
Need to perform 4 intersection checks for each line.
17/10/2008

Lecture 7

11

Computer Graphics

Cyrus & Beck algorithm


Treat line in parametric form.
Note line will cross all clip boundaries
somewhere :

17/10/2008

Lecture 7

12

Computer Graphics

Cyrus & Beck algorithm


Treat line in parametric form.
Note line will cross all clip boundaries
somewhere :
Find all intersections, test afterwards to determine if
they are on the clip rectangle.
Define a point on the clipping plane and its normal.
Notice the dot product between a vector from this point
and the intersection is zero.
17/10/2008

Lecture 7

13

Computer Graphics

Cyrus & Beck algorithm


P(t ) P0 ( P1 P0 )t

PEJ
P1

N j [ P(t ) PEJ ] 0
P0

N j [ P(t ) PEJ ] 0

N j [ P(t ) PEJ ] 0
Edge Ej

Nj

17/10/2008

Lecture 7

14

Computer Graphics

Cyrus & Beck algorithm


P (t ) P0 ( P1 P0 )t
N j [ P (t ) PEJ ] 0

Sign of denominator is important.


Must 0 ( ignore parallel lines ).

N j [ P0 ( P1 P0 )t PEJ ] 0

Parametric form implies direction.

N j [ P0 PEJ ] N j [ P1 P0 ]t 0

Denominator < 0 point entering clip


region.

Let D ( P1 P0 ), solve for t

Denominator > 0 point leaving clip


region.

N j [ P0 PEJ ]
Nj D
17/10/2008

Lecture 7

15

Computer Graphics

Cyrus & Beck algorithm


Nj

N j [ P0 PEJ ]

Edge Ej

Nj D

Denominator < 0 point entering clip region, classify as PE


Denominator > 0 point leaving clip region, classify as PL
17/10/2008

Lecture 7

16

Computer Graphics

Cyrus & Beck algorithm


Sort PEs and PLs according to t.
t

PL < PE no intersection
t

PE
PL

PL
PE

Draw from a PE PL pair.


17/10/2008

Lecture 7

17

Computer Graphics

Clipping polygons.
Clip polygons by clipping successively
against 4 sides.

17/10/2008

Lecture 7

18

Computer Graphics

Clipping polygons.
Clip polygons by clipping successively
against 4 sides.
Can implement as pipelined algorithm.
Recursively test each edge.
Form new edge with next vertex.
Call with new edge.

17/10/2008

Lecture 7

19

Computer Graphics

Sutherland-Hodgman algorithm.
Four cases of polygon clipping:
Inside Outside

Output
Vertex

Inside Outside

Inside Outside

Output
Intersection
Case 1

17/10/2008

Inside Outside

First
Output

Second
Output

Case 2.

Case 3
No
output.
Lecture 7

Case 4

20

10

Computer Graphics

Sutherland-Hodgman algorithm.
Loop round vertices, test each against 4
clipping planes in sequence.
Call algorithm again with new edge formed
by next vertex -reentrant
No storage requirement between stages
Easy to implement in hardware.

17/10/2008

Lecture 7

21

Computer Graphics

3D viewing.
Inherently more complex than 2D case.
Extra dimension (!)
Many display devices are only 2D.

Need to use a projection to transform 3D


object or scene to 2D display device.
Need to clip against a 3D view volume.
Six planes.
View volume probably truncated pyramid.
17/10/2008

Lecture 7

22

11

Computer Graphics

Coordinate Systems.
Object coordinate systems.

Transform
World coordinates.

Clip
View Volume

Project
Screen coordinates.

Rasterize
Raster
17/10/2008

Lecture 7

23

Computer Graphics

Projections.
Transforms points in a coordinate system of
dimension n into points in one of less than n.
The projection is defined by straight lines called
projectors.
Projectors emanate from a centre of projection,
pass through every point in the object and intersect
a projection surface to form the 2D projection.

17/10/2008

Lecture 7

24

12

Computer Graphics

Projections.
In graphics we only deal with planar projections
where the projection surface is a plane.
Most cameras employ a planar film plane.
But the retina is not a plane.

Only deal with geometric projections the


projectors are straight lines.
Many projections used in cartography are either nongeometric or non-planar.

Exception Image-based rendering.


17/10/2008

Lecture 7

25

Computer Graphics

Projections.
Henceforth refer to planar geometric projections as just:
projections.
Two classes of projections :
Perspective.
Parallel.

Parallel
A

A
A

A
Centre of
Projection.
B

B
B

Perspective
17/10/2008

Lecture 7

B
Centre of
Projection
at infinity
26

13

Computer Graphics

A Taxonomy of projections.
Planar geometric projections.

Parallel

Perspective

Orthographic
Axonometric

Cabinet

Oblique

1 point

Cavalier

2 point

Isometric

3 point

Elevations

17/10/2008

Lecture 7

27

Computer Graphics

Perspective projections.
Defined by projection plane and centre of projection.
Visual effect is termed perspective foreshortening.
The size of the projection of an object varies inversely
with distance from the centre of projection.
Similar to a camera - Looks realistic !
Not useful for metric information.
Parallel lines do not in general project as parallel.
Angles only preserved on faces parallel to the
projection plane.
Distances not preserved.
17/10/2008

Lecture 7

28

14

Computer Graphics

Perspective

The first ever painting


(Trinity with the Virgin,
St. John and Donors)
done in perspective by
Masaccio, in 1427.

17/10/2008

Lecture 7

29

Computer Graphics

Perspective projections.
A set of lines not parallel
to the projection plane
converge at a vanishing
point.
Can be thought of in 3D as
the projection of a point at
infinity.
Homogeneous coordinate
is 0 (x,y,0)

17/10/2008

Lecture 7

30

15

Computer Graphics

Perspective projections.
Lines parallel to a principal axis converge at an axis
vanishing point.
Categorized according to the number of such points
Corresponds to the number of axes cut by the projection plane.
y
y

z
z

Projection plane

17/10/2008

Lecture 7

32

Computer Graphics

1-point projection.

Projection plane cuts 1 axis only.

17/10/2008

Lecture 7

33

16

Computer Graphics

1-point perspective.

A painting (The Piazza


of St. Mark, Venice)
done by Canaletto in
1735-45 in one-point
perspective.

17/10/2008

Lecture 7

34

Computer Graphics

2-point perspective.
y

Projection plane

17/10/2008

Lecture 7

35

17

Computer Graphics

2-point perspective.
Painting in two point
perspective by Edward
Hopper
The Mansard Roof
1923 (240 Kb);
Watercolor on paper, 13
3/4 x 19 inches;
The Brooklyn Museum,
New York

17/10/2008

Lecture 7

36

Computer Graphics

3-point perspective.
Adds little beyond 2-point perspective.
A painting (City
Night, 1926) by
Georgia O'Keefe, that
is approximately in
three-point
perspective.

y
z

Projection plane
17/10/2008

Lecture 7

37

18

Computer Graphics

Arbitrary Viewing I
Read Foley et al. sections 3.12, 3.14, Ch6
intro and 6.1, and Introduction text
sections 3.9, 3.11, 6.1 and 6.2.
Clipping can be done in image space if
more efficient application dependent.
Started to consider 3D viewing and looked
at Perspective projection basics.
Start with Parallel projection next time.
17/10/2008

Lecture 7

38

19

Potrebbero piacerti anche