CS 551 / 645: Introductory Computer Graphics David Luebke

advertisement
CS 551 / 645:
Introductory Computer Graphics
David Luebke
cs551@cs.virginia.edu
http://www.cs.virginia.edu/~cs551
David Luebke
7/27/2016
Administrivia

Office hours today: 4:30-5:30
David Luebke
7/27/2016
Recap: Rigid-Body Transforms


Goal: object coordinatesworld coordinates
Rigid-body transforms
–
–
–
–
David Luebke
Translation
Rotation
Scale
Shear
7/27/2016
Recap: Transformation Matrices

Represent these transformation using
matrices
– Rotation, scale, shear: 3x3 matrices suffice
– Would be nice to work translation and projection
into the same system

Solution: homogeneous coordinates
– A point in homogeneous coordinates: [x, y, z, w]T
– Corresponding point in 3-D: [x/w, y/w, z/w]T
– Now transformation matrices are 4x4
David Luebke
7/27/2016
Recap: Homogeneous
Coordinates

4x4 matrix for rotation about the X axis:
0
0
1
0 cos()  sin( )
Rx  
0 sin( ) cos()

0
0
0
David Luebke
0
0
0

1
7/27/2016
Recap: Homogeneous
Coordinates

4x4 matrix for rotation about the Y axis:
 cos()
 0
Ry  
 sin( )

 0
David Luebke
0 sin( ) 0
1
0
0
0 cos() 0

0
0
1
7/27/2016
Recap: Homogeneous
Coordinates

4x4 matrix for rotation about the Z axis:
cos()  sin( )
 sin( ) cos()
Rz  
 0
0

0
 0
David Luebke
0 0
0 0
1 0

0 1
7/27/2016
Recap: Homogeneous
Coordinates

4x4 matrix for scaling by Sx, Sy, Sz:
 Sx 0
 0 Sy
S
0 0

0 0
David Luebke
0 0
0 0
Sz 0 

0 1
7/27/2016
Recap: Homogeneous
Coordinates

4x4 matrix for translating by Tx, Ty, Tz:
1
0
T
0

0
David Luebke
0 0 Tx 
1 0 Ty 
0 1 Tz 

0 0 1
7/27/2016
Recap: Compositing Transforms


We can composite the effect of multiple
transforms by multiplying their matrices:
Ex: rotate 90° about X, then 10 units down Z:
 x'  1
 y '  0
 
 z '  0
  
 w' 0
David Luebke
0 0 0  1
0
0
1 0 0  0 cos(90)  sin( 90)
0 1 10 0 sin( 90) cos(90)

0 0 1  0
0
0
0  x 
0  y 
0  z 
 
1   w
7/27/2016
Recap: Compositing Transforms


Now that we can represent translation as a
matrix, we can composite it with other
transformations
Ex: rotate 90° about X, then 10 units down Z:
 x'  1
 y '  0
 
 z '  0
  
 w' 0
David Luebke
0 0 0  1
1 0 0  0
0 1 10 0

0 0 1  0
0 0 0  x 
0  1 0  y 
1 0 0  z 
 
0 0 1   w
7/27/2016
Recap: Compositing Transforms


Now that we can represent translation as a
matrix, we can composite it with other
transformations
Ex: rotate 90° about X, then 10 units down Z:
 x'  1
 y '  0
 
 z '  0
  
 w' 0
David Luebke
0 0 0  x 
0  1 0   y 
1 0 10  z 
 
0 0 1   w
7/27/2016
Recap: Compositing Transforms

Rigid-body transformations, in general, do
not commute
– Translate then rotate very different from rotate
then translate

Write transforms down from right to left in the
order in which they take place
– Example: p’ = Ry-1 Rx  -1 Rz Rx  Ry p
David Luebke
7/27/2016
More On Homogeneous Coords



The w coordinate of a homogeneous point is
typically 1
Decreasing w makes the point “bigger”,
meaning further from the origin
Homogeneous points with w = 0 are thus
“points at infinity”, meaning infinitely far away
in some particular direction.
– (What direction?)

To help illustrate this, imagine subtracting two
homogeneous points
David Luebke
7/27/2016
Perspective Projection


In the real world, objects exhibit perspective
foreshortening: distant objects appear smaller
The basic situation:
David Luebke
7/27/2016
Perspective Projection

When we do 3-D graphics, we think of the
screen as a 2-D window onto the 3-D world:
How tall should
this bunny be?
David Luebke
7/27/2016
Perspective Projection

The geometry of the situation is that of
similar triangles. View from above:
View
plane
X
P (x, y, z)
x’ = ?
(0,0,0)
Z
d

What is x’?
David Luebke
7/27/2016
Perspective Projection

Desired result for a point [x, y, z, 1]T projected
onto the view plane:
x' x
 ,
d z
dx
x
x' 

,
z
z d

y' y

d z
dy
y
y' 

, zd
z
z d
What should a matrix look like to do this?
David Luebke
7/27/2016
A Perspective Projection Matrix

Answer:
1
0
Mperspective  
0

0

0
1
0
0
0
1
0 1d
0

0
0

0
An aside: what fundamental difference do
you notice about this matrix from the others?
David Luebke
7/27/2016
A Perspective Projection Matrix

Example:
 x  1
 y  0


 z  0

 
 z d  0

0
1
0
0
0
1
0 1d
0  x 



0  y 
0  z 
 
0  1 
Or, in 3-D coordinates:
 x

,
z d
David Luebke

y
, d 
zd

7/27/2016
Projection Matrices



Now that we can express perspective
foreshortening as a matrix, we can composite
it onto our other matrices with the usual
matrix multiplication
End result: a single matrix encapsulating
modeling, viewing, and projection transforms
Coming up after the exam: parallel (a.k.a.
orthographic) projections, and the viewport
transformation
David Luebke
7/27/2016
Review for Exam

Quick recap of lecture topics for exam…
– Display technologies




Vector versus raster: what’s a pixel?
CRT (black & white, color): shadow mask, phosphors,
electron guns
LCD: polarizing crystals that line up under an E-field,
losing their polarization and acting as light valves
Pros and cons of different technologies
– Framebuffers


David Luebke
Fast, dual-ported memory bank for holding pixels
True-color (24-bit) vs Pseudocolor (8-bit indexed) vs hicolor (16-bit, 6-6-4 ?)
7/27/2016
Review For Exam

Color
– Basic physiology: retina, rods, cones
– Different types of cones: L, M, S
– Metamers: perceptually identical color senstions
caused by different spectra
– CIE Color Space (X, Y, Z)



Color mix-and-match experiment
Hypothetical light sources X, Y, Z (why?)
Three dimensional shape, often simplified to 2-D gamut
– Other color spaces (RGB, HSV)
– Gamma correction: linearize non-linear response
of display device
David Luebke
7/27/2016
Review For Exam

Rasterizing lines
– A methodology in optimizing code
– Simplest way: solve slope-intercept equation

Check slope and step in X or Y
– DDA: find incremental change per inner loop
– Bresenham: take advantage of rational slope,
endpoints to optimize with integer arithmetic
David Luebke
7/27/2016
Review For Exam

Rasterizing polygons
– Can break into triangles for convenience

Trivial for convex polys, difficult for complex polys
– Edge equations:






David Luebke
Evaluate equation of edge (linear expression) per pixel
If all three edges are positive, light pixel
Can evaulate R,G,B as linear expressions too
Hardware: SIMD array
Software: bounding box
Issues: ensuring consistent edge equations, numerical
stability when evaluating edge equations
7/27/2016
Review For exam

Rasterizing triangles: edge walking
–
–
–
–
–

Find edges (DDA)
Interpolate colors down edges
Interpolate colors across scanlines
Fast: touches only pixels necessary
Difficult: lots of special cases, fractional offsets,
precision worries
Rasterizing triangles: general issues
– Need consistent rules about pixels right on edge
David Luebke
7/27/2016
Review For Test

Can also rasterize general polygons
– Active edge table: sort edges by ymin and ymax,
then by x intersection w/ scanline
– March up scanline by scanline, filling pixels
according to parity rule
– Hard to interpolate color, etc. correctly -- not
planar in color space, in general
David Luebke
7/27/2016
Review For Test

Clipping
– Want only portions of lines, polygons in viewport
– Cohen-Sutherland line clipping: binary outcodes
– Polygon clipping is harder


Triangle in, 7-gon out
Concave polyon in, 2 polygons out
– Sutherland-Hodgman: clip against view planes in
succession


David Luebke
In-out rules
Line-plane intersection
7/27/2016
Review For Test

3-D graphics:
Transform
Illuminate
Transform
Clip
Project
Rasterize
Model & Camera
Parameters
David Luebke
Rendering Pipeline
Framebuffer
Display
7/27/2016
Review For Test

Rendering pipeline:
Scene graph
Object geometry
Result:
Modeling
Transforms
• All vertices of scene in shared 3-D “world” coordinate system
Lighting
Calculations
• Vertices shaded according to lighting model
Viewing
Transform
• Scene vertices in 3-D “view” or “camera” coordinate system
Clipping
Projection
Transform
David Luebke
• Exactly those vertices & portions of polygons in view frustum
• 2-D screen coordinates of clipped vertices
7/27/2016
Review For Test

Transformations
–
–
–
–
–
David Luebke
Shift in coordinate systems (basis sets)
Accomplished via matrix multiplication
Modeling transforms: object->world
Viewing transform: world->view
Projection transform: view->screen
7/27/2016
Review For Test

Rigid-body transforms
–
–
–
–
Rotation: 2-D. 3-D (canonical & arbitrary axis)
Scaling
Translation: homogeneous coords, 4x4 matrices
Composiing transforms



Matrix multiplication
Order from right to left
Projection transforms
– Geometry of perspective projection
– Derive perspective projection matrix
David Luebke
7/27/2016
Download