Viewing and Perspective Kurt Akeley CS248 Lecture 9 23 October 2007
Download
Report
Transcript Viewing and Perspective Kurt Akeley CS248 Lecture 9 23 October 2007
Viewing and Perspective
Kurt Akeley
CS248 Lecture 9
23 October 2007
http://graphics.stanford.edu/courses/cs248-07/
Projection
Projection
Transforms points from an n to an n-1 coordinate system
Other definitions? …
Surface of projection
Typically a plane (“planar”)
Curved in special cases
E.g., dome simulator
Image courtesy of Wikipedia
Projectors (connect points through the center of projection)
Typically straight lines (“geometric”)
Curved in special cases
Cartographic (e.g., Mercator)
Fish-eye lens (e.g., Omnimax)
Image courtesy of Wikipedia
CS248 Lecture 9
Kurt Akeley, Fall 2007
Planar geometric projections
Straight lines project to straight lines
p1, p2, and c define a plane
The intersection of this plane and the plane of projection is a line
But distances may be distorted
p1
p2
c
Center of projection
(aka point of projection, viewpoint)
Plane of projection
CS248 Lecture 9
Kurt Akeley, Fall 2007
Taxonomy of planar geometric projections
Planar geometric
Parallel
Orthographic
Top
Front
Perspective
Oblique
Side
1-point
Cabinet
2-point
3-point
Cavalier
True projection type
CS248 Lecture 9
Viewing transformation
Kurt Akeley, Fall 2007
The vertex pipeline
Application
struct {
float x,y,z,w;
float nx,ny,nz;
float r,g,b,a;
} vertex;
Vertex assembly
Vertex operations
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 9
Kurt Akeley, Fall 2007
OpenGL coordinate systems
no
vo
Transform vo by M
Transform no by M-1
Vertex operations
ne
ve
Transform ve by P
vc
CS248 Lecture 9
Object coordinates
“Model-view”
transformation
(non-rigid)
Eye coordinates
(lighting calculations)
Projection transformation
(non-rigid)
Clip coordinates
Kurt Akeley, Fall 2007
The vertex pipeline
struct {
float x,y,z,w;
float nx,ny,nz;
float r,g,b,a;
} vertex;
Application
Vertex assembly
Vertex operations
struct {
float x,y,z,w;
float r,g,b,a;
} clipvertex;
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 9
Kurt Akeley, Fall 2007
OpenGL coordinate systems
no
Vertex operations
vo
Transform vo by M
Transform no by M-1
ne
ve
Transform ve by P
Primitive assembly
vc
Object coordinates
Model-view transformation
Eye coordinates
(lighting calculations)
Projection transformation
Clip coordinates
(clip to unit cube)
Divide by wc
Primitive operations
vd
Map to Window
CS248 Lecture 9
vw
Homogenize
Normalized device
coordinates
Viewport transformation
Window coordinates
Kurt Akeley, Fall 2007
Coordinates systems are convenient
Object coordinates
Defined by modeler
Eye coordinates
Eye is at the origin, looking down the -z axis, +x to the right
Convenient for lighting calculations
Clip coordinates
Unit cube (+/- 1) centered at the origin
Convenient for clipping calculations (plane equations are trivial)
Normalized device coordinates
Homogenized clip coordinates
Convenient for rasterization and frame buffer arithmetic
Window coordinates
Pixels are unit squares, origin at lower-left corner of window
Z values fill available depth-buffer range
CS248 Lecture 9
Kurt Akeley, Fall 2007
Orthographic Projection
(the complete sequence)
CS248 Lecture 9
Kurt Akeley, Fall 2007
Viewing transformation
Puts eye at origin looking down the -z axis, +x to the right
Transformation is rigid, one rotation and one translation
GLU helper routine is available (next slide)
y
жxe ч
ц
жxo ц
зз ч
зз ч
ч
ч
зз ye ч
зз yo ч
ч
ч
ч
зз ч
з
ч
=
M
Ч
з
ч
ч
зз ze ч
з
ч
z
oч
ч
з
зз ч
зз ч
ч
ч
ч
зиwe ч
з
w
ш
и oш
ˆ
M = V ЧM
CS248 Lecture 9
x
z
Kurt Akeley, Fall 2007
gluLookAt
gluLookAt(double ex, double ey, double ez,
double cx, double cy, double cz,
// eye
// center
double ux, double uy, double uz); // up
f = c- e
f ў=
f
f
u ў=
u
u
s = f ўґ u ў
t = sґ f ў
CS248 Lecture 9
жs x s y s z
зз
зз t x t y t z
M ў= M Чззз
ззf x ў f y ў f z ў
зз
зи 0 0 0
ц ж1
0ч
зз
ч
ч
зз0
0ч
ч
ч
Чзз
ч
ч
зз0
0ч
ч
зз
ч
ч
ч зи0
1ш
ц
0 0 - ex ч
ч
ч
1 0 - ey ч
ч
ч
ч
0 1 - ez ч
ч
ч
ч
ч
0 0 1 ш
Kurt Akeley, Fall 2007
Light positions
Specified position is transformed by the modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(ex,ey,ez,cx,cy,cz,ux,uy,uz);
// now in world coordinates
Float vec[4] = {px, py, pz, pw};
glLightfv(GL_POSITION, vec, 4);
// specify in world coordinates
…
glMultMatrixf(…);
glLightfv(GL_POSITION, vec, 4);
CS248 Lecture 9
// specify in object coordinates
Kurt Akeley, Fall 2007
Orthographic projection
Only scaling is required
But the interface supports scaling and translation:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
n and f are signed distances,
not coordinates
glOrtho(l,r,b,t,n,f);
жxc ц
жxe ц
ч
зз ч
зз чч
зз yc чч
зз ye чч
зз ччч= P Чзз ччч
зз zc чч
зз ze чч
зз чч
зз чч
ч
ч
зиwc ш
з we ш
и
CS248 Lecture 9
ж 2
зз
зз r - l
зз
зз 0
P ў= P Чззз
зз
зз 0
зз
зз
зи 0
0
2
t- b
0
0
r+ l ц
ч
0
ч
r- l ч
ч
ч
ч
t+ b ч
ч
ч
0
ч
t- b ч
ч
ч
2
f + nч
ч
ч
ч
f- n
f - nч
ч
ч
ч
ч
0
1 ш
Kurt Akeley, Fall 2007
The orthographic view volume
(r t - f )
ye
(l b - n)
xe
ze
CS248 Lecture 9
Kurt Akeley, Fall 2007
Clip testing
vc is within the unit cube centered at the origin if:
xc
- 1Ј
Ј 1
wc
yc
yc
- 1Ј
Ј 1
wc
zc
- 1Ј
Ј 1
wc
(1 1 - 1)
Assuming wc is positive,
this is equivalent to:
- wc Ј xc Ј wc
- wc Ј yc Ј wc
xc
(- 1 - 1 1)
zc
- wc Ј zc Ј wc
CS248 Lecture 9
Kurt Akeley, Fall 2007
Clipping arithmetic
Points outside the unit cube are discarded
Lines (or edges) that straddle a face of the unit cube are clipped
A new vertex is location is computed at the intersection
Appropriate attribute values are assigned to this vertex
p
p2
p1
Find p (on the clipping plane) and t such that p = t Чp1 + (1- t )Чp 2
Then set the attributes of p to f = t Чf1 + (1- t )Чf 2
CS248 Lecture 9
Kurt Akeley, Fall 2007
Clipping details
The calculation of p must be invariant to the order of p1 and p2
An edge shared by two triangles must not “crack”
Easiest implementation is canonical:
Sort the vertexes into canonical order, then compute p
Line clipping never generates more than two vertexes
Triangle clipping can generate a vertex for each clipping plane
E.g., Sutherland-Hodgman
Later in this lecture
SGI paid royalties on this
CS248 Lecture 9
Kurt Akeley, Fall 2007
Homogenization
Divide by wc:
Discard wd:
жxd чц
зз ч
v d = зз yd ччч
зз чч
зиzd чш
CS248 Lecture 9
ц
чч
чч
чч=
чч
чч
чч
ш
жxc
зз
зз yc
зз
зз zc
зз
зиwc
жxd
зз
зз yd
зз
зз zd
зз
зи 1
wc чц
чч
wc чч
чч
wc чч
чч
wc чш
Kurt Akeley, Fall 2007
Viewport transformation
Transform the [-1,1] range device coordinates to window coordinates:
glViewport(int l, int b, int w, int h);
glDepthRange(double n, double f);
жxw ц
зз ч
ч
зз yw ч
ч=
зз ч
ч
ч
ч
зиzw ш
жw
зз
зз 2
зз
зз 0
зз
зз
зз
зз 0
и
0
0
h
2
0
0
f2
(10 6 1)
ц
ж wц
ч
зз l + ч
ч
ч
ч
ч
з
2
ч
ч
з
ж
ц
x
ч
ч
d
з
ч
ч
зз ч
з
ч
ч
ч
h
ч
з
ч
з
ч
ч
+ зз b + ч
зз yd ч
ч
ч
ч
ч
ч
ч
2
з
з
ч
ч
ч
з
ч
з
ч
ч
z
и
ш
з
d
ч
nч
f
+
n
з
ч
ч
з
ч
ч
ч
ч
зз
ч
ч
ш
и 2 ш
glViewport(0, 0, 10, 6);
glDepthRange(0.0, 1.0);
(0 0 0)
CS248 Lecture 9
One pixel
Kurt Akeley, Fall 2007
The vertex pipeline
struct {
float x,y,z,w;
float nx,ny,nz;
float r,g,b,a;
} vertex;
Application
Vertex assembly
Vertex operations
struct {
float x,y,z,w;
float r,g,b,a;
} clipvertex;
struct {
winvertex v0,v1,v2
} triangle;
struct {
float x,y,z;
float r,g,b,a;
} winvertex;
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 9
Kurt Akeley, Fall 2007
Window coordinates
Window coordinates are:
Continuous, not discrete
Not homogeneous
Can be used directly for rasterization
3-D, not 2-D
Projection did not reduce 3-D to 2-D
3-D homogeneous was reduced to 3-D non-homogeneous
–
But this was done by homogenization, not by the
projection transformation
Where did the parallel “projection” happen?
CS248 Lecture 9
Kurt Akeley, Fall 2007
Oblique projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(l,r,b,t,n,f);
float s = 0.5; // Cabinet
float m[16] = {1,0,0,0,
0,1,0,0,
-s,-s,1,0,
0,0,0,1};
glMultMatrixf(m);
ж1
зз
зз0
m = зз
зз0
зз
зи0
CS248 Lecture 9
0 - s 0ц
ч
ч
ч
1 - s 0ч
ч
ч
ч
0 1 0ч
ч
ч
ч
ч
0 0 1ш
Kurt Akeley, Fall 2007
Perspective Projection
CS248 Lecture 9
Kurt Akeley, Fall 2007
Change only the projection matrix
no
Vertex operations
vo
Transform vo by M
Transform no by M-1
ne
ve
Transform ve by P
Primitive assembly
vc
Object coordinates
Model-view transformation
Eye coordinates
(lighting calculations)
Projection transformation
Clip coordinates
(clip to unit cube)
Divide by wc
Primitive operations
vd
Map to Window
CS248 Lecture 9
vw
Homogenize
Normalized device
coordinates
Viewport transformation
Window coordinates
Kurt Akeley, Fall 2007
Orthographic projection
Only scaling is required
But the interface supports scaling
and translation:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(l,r,b,t,n,f);
жxc ц
жxe ц
ч
зз ч
зз чч
зз yc чч
зз ye чч
зз ччч= P Чзз ччч
зз zc чч
зз ze чч
зз чч
зз чч
ч
ч
зиwc ш
з we ш
и
CS248 Lecture 9
ж 2
зз
зз r - l
зз
зз 0
P ў= P Чззз
зз
зз 0
зз
зз
зи 0
0
2
t- b
0
0
r+ l ц
ч
0
ч
r- l ч
ч
ч
ч
t+ b ч
ч
ч
0
ч
t- b ч
ч
ч
2
f + nч
ч
ч
ч
f- n
f - nч
ч
ч
ч
ч
0
1 ш
Kurt Akeley, Fall 2007
Perspective projection
Specify a frustum:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
n and f are distances, not
coordinates. Both must
be positive.
glFrustum(l,r,b,t,n,f);
жxc ц
жxe ц
ч
зз ч
зз чч
зз yc чч
зз ye чч
зз ччч= P Чзз ччч
зз zc чч
зз ze чч
зз чч
зз чч
ч
ч
зиwc ш
з we ш
и
CS248 Lecture 9
ж 2n
зз
зз r - l
зз
зз 0
P ў= P Чззз
зз
зз 0
зз
зз
зи 0
ц
r+ l
0
0 ч
ч
ч
r- l
ч
ч
ч
ч
2n
t+ b
ч
0 ч
ч
ч
t- b
t- b
ч
ч
f+n
2 fn ч
ч
ч
0
ч
f- n
f - nч
ч
ч
ч
ч
0
- 1
0 ш
Kurt Akeley, Fall 2007
The frustum
wc = - ze
ж f
ззl Ч
зи n
f
tЧ
n
ц
ч
- fч
ч
ш
ye
f+n
2 fn
zc = - ze Ч
- we Ч
f- n
f- n
= c Чze + d
(l b - n)
zd =
zc
wc
c Чze + d
=
- ze
CS248 Lecture 9
xe
(r t - n)
ze
Kurt Akeley, Fall 2007
Where does projection happen?
There are two ways to think about this:
In the projection transformation
“Thinking homogeneously”
Setting wc= –ze warps the eye-coordinate system
–
From a frustum in eye coordinates
–
To the unit cube in clip coordinates
Homogenization doesn’t really change anything further
Hence “projection transformation”
During homogenization and the discarding of the wd
“Thinking geometrically”
Dividing by –ze causes the projection
This is where coordinates are reduced from 4D to 3D
CS248 Lecture 9
Kurt Akeley, Fall 2007
Remaining topics
Perspective-correct attribute parameterization
Sutherland-Hodgman clipping
Near-plane clipping
The “direction” of a projection
Wide field-of-view projection (planar doesn’t work well)
CS248 Lecture 9
Kurt Akeley, Fall 2007
Rasterization revisited
struct {
float x,y,z,w;
float nx,ny,nz;
float r,g,b,a;
} vertex;
Application
Vertex assembly
Vertex operations
struct {
float x,y,z,w;
float r,g,b,a;
} clipvertex;
struct {
winvertex v0,v1,v2
} triangle;
struct {
float x,y,z;
float r,g,b,a;
} winvertex;
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 9
Kurt Akeley, Fall 2007
Planar geometric projection
Straight lines project to straight lines
Only vertexes need to be transformed
That’s why we’re interested in lines and polygons
But distances and parameterizations are warped:
CS248 Lecture 9
Kurt Akeley, Fall 2007
Incorrect attribute interpolation
f1
Linear
interpolation
f1
F
f
f
f2
f2
F№f
CS248 Lecture 9
Kurt Akeley, Fall 2007
Linear (barycentric) attribute evaluation
(x1, y1, f1)
f = c0 f0 + c1 f1 + c2 f 2
(x, y, f )
a2
(x0, y0, f0)
a0
a1
a0 = (yx1 - xy1 )+ (y1x2 - x1 y2 )+ (y2 x - x2 y ), c0 = a0 a
(x2, y2, f2)
a = (y0 x1 - x0 y1 )+ (y1 x2 - x1 y2 )+ (y2 x0 - x2 y0 )
a0 = ( yx1 - xy1 )+ ( y1 x2 - x1 y2 )+ ( y2 x - x2 y ), c0 = a0 a
a1 = ( yx2 - xy2 )+ ( y2 x0 - x2 y0 )+ ( y0 x - x0 y ), c1 = a1 a
a2 = ( yx0 - xy0 )+ ( y0 x1 - x0 y1 )+ ( y1 x - x1 y ), c2 = a2 a
CS248 Lecture 9
Kurt Akeley, Fall 2007
Perspective-correct attribute evaluation
f0
f1
f2
c0
+ c1 + c2
w0
w1
w2
f =
1
1
1
c0
+ c1 + c2
w0
w1
w2
All w’s are wc’s
(x0, y0, w0, f0)
(x1, y1, w1, f1)
(x, y, f )
a2
a0
a1
(x2, y2, w2, f2)
a = (y0 x1 - x0 y1 )+ (y1 x2 - x1 y2 )+ (y2 x0 - x2 y0 )
a0 = ( yx1 - xy1 )+ ( y1 x2 - x1 y2 )+ ( y2 x - x2 y ), c0 = a0 a
a1 = ( yx2 - xy2 )+ ( y2 x0 - x2 y0 )+ ( y0 x - x0 y ), c1 = a1 a
a2 = ( yx0 - xy0 )+ ( y0 x1 - x0 y1 )+ ( y1 x - x1 y ), c2 = a2 a
CS248 Lecture 9
Requires a division for each fragment
Kurt Akeley, Fall 2007
Rasterization requires access to wc
struct {
float x,y,z,w;
float nx,ny,nz;
float r,g,b,a;
} vertex;
Application
Vertex assembly
Vertex operations
struct {
float x,y,z,w;
float r,g,b,a;
} clipvertex;
struct {
winvertex v0,v1,v2
} triangle;
struct {
float x,y,z,w_c;
float r,g,b,a;
} winvertex;
Primitive assembly
Primitive operations
Rasterization
Fragment operations
Framebuffer
Display
CS248 Lecture 9
Kurt Akeley, Fall 2007
OpenGL coordinate systems
no
Vertex operations
vo
Transform vo by M
Transform no by M-1
ne
ve
Transform ve by P
Primitive assembly
vc
Object coordinates
Model-view transformation
Eye coordinates
(lighting calculations)
Projection transformation
Clip coordinates
(clip to unit cube)
Divide by wc
Primitive operations
vd , wc
Map to Window
CS248 Lecture 9
vw , wc
Homogenize
Normalized device
coordinates
Viewport transformation
Window coordinates
Kurt Akeley, Fall 2007
Sutherland-Hodgman clipping
Approach: clip against each plane in sequence
Simplified example clips a triangle to a square, 2-D frustum:
CS248 Lecture 9
Kurt Akeley, Fall 2007
Near-plane clipping
Is required …
To limit zw values to the range of the depth buffer
To avoid division by wc= 0
To avoid rendering objects behind the viewpoint
CS248 Lecture 9
But clipping assuming wc> 0 accomplishes this too
Kurt Akeley, Fall 2007
The “direction” of a projection
Yes, we spoke of a view direction in eye coordinates
Specifically, down the –ze axis, +xe to the right
And yes, the plane of projection is perpendicular to the –ze axis by
convention
But, once the projection has been specified, it is not valid to treat it
as implying a true viewing direction (the orientation of the eye):
Parallel projections have a meaningful direction that is
distinct from the normal to the plane of projection, but this
direction is not the view direction.
Perspective projections have no meaningful direction at all.
Once specified, they are fully characterized by a center of
projection and a plane of projection. The normal to the
plane of projection has no meaning.
Bottom line: once a projection has been specified, it implies nothing
about the orientation of the viewer’s eye. Only the location of
the eye’s optical center is specified (the center of projection).
CS248 Lecture 9
Kurt Akeley, Fall 2007
Projection does not imply viewing direction!
CS248 Lecture 9
Kurt Akeley, Fall 2007
Wide field-of-view display
Assume equal-size pixels
Problem:
Center-field pixels are most important (foveal gaze)
But wide-field pixels have much greater “resolution”
CS248 Lecture 9
Kurt Akeley, Fall 2007
Ratio of perceived pixel sizes
Ratio of perceived size of
center pixel to edge pixel
35
30
25
20
15
10
5
0
0
CS248 Lecture 9
20
40
60
80
100 120 140 160
Field of view (degrees)
Kurt Akeley, Fall 2007
Piece-wise linear projection
Note: there is one view direction,
but three planes of projection
CS248 Lecture 9
Kurt Akeley, Fall 2007
Summary
Perspective projection can be understood two different ways:
Warping of eye coordinates by the projection transformation
Consistent with orthographic transformation
Consistent with 3-D result
Consistent with name “projection transformation”
Division by -ze and discarding of wd
Consistent with attribute evaluation
Perspective projection warps distances and attribute evaluation
Must interpolate only “projected” values
Requires division for attributes of each fragment
Projections do not specify the true orientation of the viewer’s eye
Perspective projections don’t really have a direction
Parallel projections do, but it’s not the viewing direction
CS248 Lecture 9
Kurt Akeley, Fall 2007
Assignments
Reading assignment for Thursday’s class
FvD 17.4
OpenGL chapter 9
Optional: Heckbert, P., A Survey of Texture Mapping,
IEEE Computer Graphics, 6:11, pp. 56-67, 1986.
Project 2:
Due tonight at midnight
Midterm
Monday 7 pm to 9 pm, Gates B01
CS248 Lecture 9
Kurt Akeley, Fall 2007
End
CS248 Lecture 9
Kurt Akeley, Fall 2007