Notes

rodscarletSoftware and s/w Development

Dec 14, 2013 (3 years and 7 months ago)

89 views

Computer

Graphics

Prof. Feng Liu


Fall 2013


http://www.cs.pdx.edu/~fliu/courses/cs447/


10/23/2013


Last time


More 2D Transformations


Homogeneous Coordinates


3D Transformations


The Viewing Pipeline




2

Today


Perspective projection


Clipping


In
-
class Middle
-
Term


Wednesday, Nov. 6


Close
-
book exam


Notes on 1 page of A4 or Letter size paper


To
-
know list available online




3

Graphics Pipeline

4

Local
Coordinate
Space

World
Coordinate
Space

View
Space


Canonical
View
Volume

Display
Space


OpenGL and Transformations


OpenGL internally stores two matrices that control viewing of the
scene


The
GL_MODELVIEW

matrix is intended to capture all the
transformations up to view space


The
GL_PROJECTION

matrix captures the view to canonical
conversion


You also specify the mapping from the canonical view volume into
window space


Directly through a
glViewport

function call


Matrix calls, such as
glRotate
, multiply some matrix M onto the
current matrix C, resulting in CM


Set view transformation first, then set transformations from local to
world space


last one set is first one applied


This is the convenient way for modeling, as we will see


5

Graphics Pipeline

6

Local
Coordinate
Space

World
Coordinate
Space

View
Space


Canonical
View
Volume

Display
Space


GL_PROJECTION

glViewport


GL_MODELVIEW

OpenGL Camera


The
default

OpenGL image plane has u aligned with the x axis, v
aligned with y, and n aligned with z


Means the default camera looks along the negative z axis


Makes it easy to do 2D drawing (no need for any view transformation)


glOrtho
(…)

sets the view
-
>canonical matrix


Modifies the
GL_PROJECTION

matrix


gluLookAt
(…)

sets the world
-
>view matrix


Takes an image center point, a point along the viewing direction and an
up vector


Multiplies a world
-
>view matrix
onto the current
GL_MODELVIEW

matrix


You could do this yourself, using
glMultMatrix
(…)

with the matrix
from the previous slides


7

Typical Usage

8


GLU functions, such as
gluLookAt(…)
, are not part of the
core OpenGL library


They can be implemented with other core OpenGL commands


For example,
gluLookAt(…)
uses
glMultMatrix(…)

with the
matrix from the previous slides


They are not dependent on a particular graphics card


glMatrixMode
(GL_PROJECTION);

glLoadIdentity
();

glOrtho
(l, r, b, t, n, f);

glMatrixMode
(GL_MODELVIEW);

glLoadIdentity
();

gluLookAt
(
ex,ey,ez,cx,cy,cz,ux,uy,uz
);

Left
vs

Right Handed View Space


You can define
u

as right,
v

as up, and
n

as toward the
viewer: a right handed system
u

v
=w


Advantage: Standard mathematical way of doing things


You can also define
u

as right,
v

as up and
n

as into the
scene: a left handed system
v

u
=w


Advantage: Bigger
n

values mean points are further away


OpenGL is right handed


Many older systems, notably the
Renderman

standard
developed by Pixar, are left handed

9

Graphics Pipeline

10

Local
Coordinate
Space

World
Coordinate
Space

View
Space


Canonical
View
Volume

Display
Space


Review


View Space
is a coordinate system with the viewer
looking down the

z

axis, with
x

to the right and
y

up


The World
-
>View transformation takes points in world
space and converts them into points in view space


The Projection matrix, or View
-
>Canonical matrix,
takes points in view space and converts them into
points in Canonical View Space


Canonical View Space
is a coordinate system with the viewer
looking along

z
,
x

to the right,
y

up, and everything to be
drawn inside the cube [
-
1,1]x[
-
1,1]x[
-
1,1] using parallel
projection


11

Perspective Projection

12


Abstract camera model
-

box with a small hole in it


Pinhole cameras work in
practice

Distant Objects Are Smaller

13

Parallel lines meet

14

Vanishing points


Each set of parallel lines
(=direction) meets at a different
point: The vanishing point for
this direction


Classic artistic perspective
is 3
-
point perspective


Sets of parallel lines on the
same plane lead to collinear
vanishing points: the horizon
for that plane


Good way to spot faked images


Basic Perspective Projection


We are going to temporarily ignore canonical view
space, and go straight from view to window


Assume you have transformed to view space, with
x

to
the right,
y

up, and
z

back toward the viewer


Assume the origin of view space is at the center of
projection (the eye)


Define a focal distance,
d
, and put the image plane
there (note
d

is negative)


You can define
d

to control the size of the image


16

Basic Perspective Projection


If you know
P(
x
v
,y
v
,z
v
)
and

d,
what is
P(
x
s
,y
s
)?


Where does a point in view space end up on the screen?


17

x
v

y
v

-
z
v

d

P(x
v
,y
v
,z
v
)

P(x
s
,y
s
)

Basic Case

18

Similar triangles gives:

y
v

-
z
v

P(x
v
,y
v
,z
v
)

P(x
s
,y
s
)

View Plane

d

Simple Perspective Transformation


Using homogeneous coordinates we can write:


Our next big advantage to homogeneous coordinates


19

Parallel Lines Meet?


Parallel lines are of the form:


Parametric form:
x
0

is a point on the line,
t

is a scalar (distance
along the line from
x
0
) and
d

is the direction of the line (unit vector)


Different
x
0

give different parallel lines


Transform and go from homogeneous to regular:






Limit as t


is

General Perspective


The basic equations we have seen give a flavor of
what happens, but they are insufficient for all
applications


They do not get us to a Canonical View Volume


They make assumptions about the viewing conditions


To get to a Canonical Volume, we need a Perspective
Volume …


21

Perspective View Volume


Recall the orthographic view volume, defined by a near,
far, left, right, top and bottom plane


The perspective view volume is also defined by near,
far, left, right, top and bottom planes


the
clip planes


Near and far planes are parallel to the image plane
:

z
v
=n,
z
v
=f


Other planes all pass through the center of projection (the origin
of view space)


The left and right planes intersect the image plane in vertical
lines


The top and bottom planes intersect in horizontal lines


22

Clipping Planes

23

x
v

-
z
v

Near Clip
Plane

Far Clip
Plane

View
Volume

Left Clip

Plane

Right Clip

Plane

f

n

l

r

Where is the Image Plane?


Notice that it doesn’t really matter where the image
plane is located, once you define the view volume


You can move it forward and backward along the
z

axis and
still get the same image, only scaled


The left/right/top/bottom planes are defined
according
to where they cut the near clip plane


Or, define the left/right and top/bottom clip planes by
the
field of view


24

Field of View

25

x
v

-
z
v

Near Clip
Plane

Far Clip
Plane

View
Volume

Left Clip

Plane

Right Clip

Plane

f

FOV

Assumes a
symmetric

view volume

Perspective Parameters


We have seen several different ways to describe a
perspective camera


Focal distance, Field of View, Clipping planes


The most general is clipping planes


they directly
describe the region of space you are viewing


For most graphics applications, field of view is the
most convenient


It is
image size invariant


having specified the field of view,
what you see does not depend on the image size


You can convert one thing to another

26

Focal Distance to FOV


You must have the image size to do this conversion


Why? Same
d
, different image size, different FOV


27

d

d

FOV/2

height
/2

OpenGL


gluPerspective
(…)


Field of view in the
y

direction,
FOV
, (vertical field
-
of
-
view)


Aspect ratio,
a,

should match window aspect ratio


Near and far clipping planes,
n

and
f


Defines a symmetric view volume


glFrustum
(…)


Give the near and far clip plane, and places where the other
clip planes cross the near plane


Defines the general case


Used for stereo viewing, mostly


28

gluPerspective

to
glFrustum


As noted previously,
glu

functions don’t add basic
functionality, they are just more convenient


So how does
gluPerspective

convert to
glFrustum
?


Symmetric, so only need
t

and
l


29

y

z

FOV / 2

n

t

?

Perspective Projection Matrices


We want a matrix that will take points in our perspective
view volume and transform them into the orthographic
view volume


This matrix will go in our pipeline before an orthographic
projection matrix


30

(l,b,n)

(r,t,n)

(l,b,n)

(r,t,n)

(
r,t,f
)

(r,t,f)

Mapping Lines


We want to map all the lines through the
center of projection to parallel lines


This converts the perspective case to the
orthographic case, we can use all our existing
methods


The relative intersection points of lines
with the near clip plane should not change


The matrix that does this looks like the
matrix for our simple perspective case


31

General Perspective

32


This matrix leaves points with
z=n

unchanged


It is just like the simple projection matrix, but it does some extra
things to
z

to map the depth properly


We can multiply a homogenous matrix by any number without
changing the final point, so the two matrices above have the
same effect

Complete Perspective Projection

33


After applying the perspective matrix, we map the orthographic
view volume to the canonical view volume:

Near/Far and Depth Resolution


It may seem sensible to specify a very near clipping
plane and a very far clipping plane


Sure to contain entire scene


But, a bad idea:


OpenGL only has a finite number of bits to store screen depth


Too large a range reduces resolution in depth
-

wrong thing
may be considered “in front”


See Shirley for a more complete explanation


Always place the near plane as far from the viewer as
possible, and the far plane as close as possible


34

OpenGL Perspective Projection

35


For OpenGL you give the distance to the near and far clipping
planes


The total perspective projection matrix resulting from a
glFrustum

call is:

Clipping


Parts of the geometry to be rendered may lie outside the
view volume


Clipping

removes parts of the geometry that are outside
the view


Best done in canonical space
before perspective divide


Before dividing out the homogeneous coordinate


36

Clipping Terminology


Clip region: the region we wish to
restrict the output to


Geometry: the thing we are clipping


Only those parts of the geometry that
lie inside the clip region will be output


Clipping edge/plane: an infinite line
or plane and we want to output only
the geometry on one side of it


Frequently, one edge or face of the clip
region


37

Clip region

Geometry

Output

Clipping


In hardware, clipping is done in canonical space
before
perspective divide


Before dividing out the homogeneous coordinate


Clipping is useful in many other applications


Building BSP trees for visibility and spatial data structures


Hidden surface removal algorithms


Removing hidden lines in line drawings


Finding intersection/union/difference of polygonal regions


2D drawing programs: cropping, arbitrary clipping


We will make explicit assumptions about the geometry and
the clip region


Assumption depend on the algorithm


38

Types of Geometry


Points

are clipped via inside/outside tests


Many algorithms for this task, depending on the clip region


Two main algorithms for clipping polygons exist


Sutherland
-
Hodgman


Weiler

that we will not talk about in our class


39

Clipping Points to View Volume


A point is inside the view volume if it is on the “inside” of all
the clipping planes


The
normals

to the clip planes are considered to point inward,
toward the visible region


Now we see why clipping is done in canonical view space


For instance, to check against the left plane:


X coordinate in 3D

must be >
-
1


In homogeneous screen space, same as:
x
screen
>
-
w
screen


In general, a point,
p
, is “inside” a plane if:


You represent the plane as
n
x
x+n
y
y+n
z
z+d
=0
, with (
n
x
,n
y
,n
z
)
pointing inward


And
n
x
p
x
+n
y
p
y
+n
z
p
z
+d
>0


40

Clipping Point to Line

41

0 1

1

-
x
-
y+1=0

(0.4, 0.4)

Sutherland
-
Hodgman

Clip

42


Clip polygons to convex clip regions


Clip the polygon against each edge of the clip region
in turn


Clip polygon each time to line containing edge


Only works for convex clip regions (Why? Example that
breaks?)

Sutherland
-
Hodgman

Clip (2)

43


To clip a polygon to a line/plane:


Consider the polygon as a list of vertices


One side of the line/plane is considered
inside the clip region, the other side is
outside


We are going to rewrite the polygon one
vertex at a time


the rewritten polygon
will be the polygon clipped to the
line/plane


Check start vertex: if “inside”,

emit

it,
otherwise ignore it


Continue processing vertices as
follows…


p
0

p
4

p
3

p
2

p
1

p
0

p
4

p
3

p
2

p
1

Clipper

point
s in

point
s out

Sutherland
-
Hodgman

(3)

44

Inside

Outside

s

p

Output p

Inside

Outside

s

p

Output i

Inside

Outside

s

p

No output

Inside

Outside

s

p

Output i and p

i

i

Sutherland
-
Hodgman

(4)


Look at the next vertex in the list,
p
, and the edge from
the last vertex,
s
, to
p
. If the…


polygon edge crosses the clip line/plane going from out to in:
emit crossing point,
i
, next vertex,
p


polygon edge crosses clip line/plane going from in to out:
emit crossing,
i


polygon edge goes from out to out: emit nothing


polygon edge goes from in to in: emit next vertex,
p


45

Inside
-
Outside Testing


Lines/planes store a vector
pointing toward the
inside
of
the clip region


the
inward
pointing normal


Could re
-
define for
outward
pointing


Dot products give
inside/outside information


Note that
x

(a vector) is any
point on the clip line/plane

Outside

Inside

n

s

f

i

x

Finding Intersection Pts


Use the parametric form for the edge between two
points,
x
1

and
x
2
:



For planes of the form
x=a
:





Similar forms for
y=a, z=a


Solution for general plane can also be found

Inside/Outside in Screen Space


In canonical view space, clip planes are
x
s
=
±
1
,
y
s
=
±
1
,
z
s
=
±
1


Inside/Outside reduces to comparisons before
perspective divide


48

Clipping Lines


Lines can also be clipped by Sutherland
-
Hodgman


Slower than necessary, unless you already have hardware


Better algorithms exist


Cohen
-
Sutherland


Liang
-
Barsky


Nicholl
-
Lee
-
Nicholl

(we won’t cover this one


only good for
2D)


49

Cohen
-
Sutherland (1)


Works basically the same as Sutherland
-
Hodgman


Was developed earlier


Clip line against each edge of clip region in turn


If both endpoints outside, discard line and stop


If both endpoints in, continue to next edge (or finish)


If one in, one out, chop line at crossing pt and continue


Works in both 2D and 3D for convex clipping regions


50

Cohen
-
Sutherland (2)

51

1

2

3

4

1

2

3

4

3

4

1

2

3

4

1

2

Cohen
-
Sutherland
-

Details


Only need to clip line against edges where one endpoint is out


Use
outcode

to record endpoint in/out
wrt

each edge. One bit per
edge, 1 if out, 0 if in.


Trivial reject:


outcode
(x1) &
outcode
(x2)!=0


Trivial accept:


outcode
(x1) |
outcode
(x2)==0


Which edges to clip against?


outcode
(x1) ^
outcode
(x2)


1

2

3

4

0010

0101

Next Time


Rasterization




53