Title: http://www.ugrad.cs.ubc.ca/~cs314/Vjan2005
1Prog 2, Kangaroo Hall of Fame, Midterm Review,
(Interpolation if time)Week 6, Wed Feb 9
- http//www.ugrad.cs.ubc.ca/cs314/Vjan2005
2Program 2 Corrections/Clarifications
- handin 314 proj2 (not 414)
- f not s to toggle flat/smooth shading
- s already in use for camera
- add t to toggle between randomly colored and
grey terrain - makes it easier to check if lighting correct
- add u to replace terrain with new randomly
generated geometry - consider adding for a bit of extra credit
- /- toggle to increment/decrement abs cam speed
3Program 2 Corrections/Clarifications
- roll/yaw confusion
- first para. correct, second para. wrong
- left horiz drag yaw, right horiz drag roll
- image flying expertise courtesy of Matt Baumann
4Program 2 Quick Demo
5Review Midpoint Algorithm
- moving incrementally along x direction
- draw at current y value, or move up to y1?
- check if midpoint between two possible pixel
centers above or below line - candidates
- top pixel (x1,y1),
- bottom pixel (x1, y)
- midpoint (x1, y.5)
- check if midpoint above or below line
- below top pixel
- above bottom pixel
- assume , slope
6Review Bresenham Algorithm
- yy0 e0
- for (xx0 x lt x1 x)
- draw(x,y)
- if (2(edy) lt dx)
- e edy
- else
- yy1
- eedy-dx
- all integer arithmetic
- cumulative error function
yy0 eps0 for ( int x x0 x lt x1 x )
draw(x,y)eps dy if ( (eps ltlt 1)
gt dx ) y eps - dx
7Review Flood Fill
- draw polygon edges, seed point, recursively set
all neighbors until boundary is hit to fill
interior - drawbacks visit pixels up to 4x, per-pixel
memory storage needed
P
8Review Scanline Algorithms
- set pixels inside polygon boundary along
horizontal lines one pixel apart - use bounding box to speed up
9Review Edge Walking
- basic idea
- draw edges vertically
- interpolate colors down edges
- fill in horizontal spans for each scanline
- at each scanline, interpolate edge colors across
span
10Review General Polygon Rasterization
- idea use a parity test
- for each scanline
- edgeCnt 0
- for each pixel on scanline (l to r)
- if (oldpixel-gtnewpixel crosses edge)
- edgeCnt
- // draw the pixel if edgeCnt odd
- if (edgeCnt 2)
- setPixel(pixel)
11Hall of Fame
12But Wait, Theres More!
- nice comment )
- this project is fun, only one of the very few
that I actually enjoyed. Too bad there's only one
CG course in UBC ( - two fourth year CG courses await you!
- 424 Geometric Modelling
- 426 Animation
13Midterm Review
14Midterm Exam
- Friday Feb 11 10am-1050am
- you may use one handwritten 8.5x11 sheet
- one side of page
- no other notes, no books
- nonprogrammable calculators OK
- arrive on time!
- sit every other seat, ID out in front of you
- coats and bags in front of room
15Whats Covered
- transformations
- viewing and projections
- coordinate systems of rendering pipeline
- lighting and shading
- not scan conversion
16Reading
- FCS book, Red book
- see web page for details
- you can be tested on material in book but not
covered in lecture - you can be tested on material covered in lecture
but not covered in book
17Old Exams Posted
18The Rendering Pipeline
- pros and cons of pipeline approach
19Transformations
20Homogeneous Coordinates
w
w1
y
x
21Composing Transformations
Ta Tb Tb Ta, but Ra Rb ! Rb Ra and Ta Rb !
Rb Ta
22Composing Transformations
- example rotation around arbitrary center
23Composing Transformations
- example rotation around arbitrary center
- step 1 translate coordinate system to rotation
center
24Composing Transformations
- example rotation around arbitrary center
- step 2 perform rotation
25Composing Transformations
- example rotation around arbitrary center
- step 3 back to original coordinate system
26Composing Transformations
- rotation about a fixed point
- p TRT-1p
- rotation around an arbitrary axis
- considering frame vs. object
- p DCBAp
OpenGL D C B A draw p
frame
object
27Transformation Hierarchies
- hierarchies dont fall apart when changed
- transforms apply to graph nodes beneath
28Matrix Stacks
- push and pop matrix stack
- avoid computing inverses or incremental xforms
- avoid numerical error
29Matrix Stacks
glPushMatrix()
glPopMatrix()
DrawSquare()
glPushMatrix()
glScale3f(2,2,2)
glTranslate3f(1,0,0)
DrawSquare()
glPopMatrix()
30Transformation Hierarchies
glTranslate3f(x,y,0) glRotatef(
,0,0,1) DrawBody() glPushMatrix()
glTranslate3f(0,7,0) DrawHead() glPopMatrix()
glPushMatrix() glTranslate(2.5,5.5,0)
glRotatef( ,0,0,1) DrawUArm()
glTranslate(0,-3.5,0) glRotatef( ,0,0,1)
DrawLArm() glPopMatrix() ... (draw other
arm)
y
x
31Display Lists
- reuse block of OpenGL code
- more efficient than immediate mode
- code reuse, driver optimization
- good for static objects redrawn often
- cant change contents
- not just for multiple instances
- interactive graphics objects redrawn every frame
- nest when possible for efficiency
32Double Buffering
- two buffers, front and back
- while front is on display, draw into back
- when drawing finished, swap the two
- avoid flicker
33Projective Rendering Pipeline
glVertex3f(x,y,z)
viewing/camera
object
world
alter w
WCS
VCS
OCS
glFrustum(...)
projection transformation
clipping
glTranslatef(x,y,z) glRotatef(th,x,y,z) ....
gluLookAt(...)
/ w
CCS
perspective division
normalized device
- OCS - object coordinate system
- WCS - world coordinate system
- VCS - viewing coordinate system
- CCS - clipping coordinate system
- NDCS - normalized device coordinate system
- DCS - device coordinate system
glutInitWindowSize(w,h) glViewport(x,y,a,b)
NDCS
device
DCS
34Projection
- theoretical pinhole camera
eye point
image plane
- image inverted, more convenient equivalent
eye point
image plane
35Projection Taxonomy
planar projections
parallel
perspective 1,2,3-point
orthographic
oblique
cavalier
cabinet
axonometric isometric dimetric trimetric
top, front, side
36Projective Transformations
- transformation of space
- center of projection moves to infinity
- viewing frustum transformed into a parallelpiped
x
x
Frustum
-z
-z
37Normalized Device Coordinates
- left/right x /- 1, top/bottom y /- 1,
near/far z /- 1
NDC
Camera coordinates
x
x
x1
right
Frustum
-z
z
left
x -1
z1
z -1
z-n
z-f
38Projection Normalization
- distort such that orthographic projection of
distorted objects is desired persp projection
39Transforming View Volumes
NDCS
y
(1,1,1)
z
(-1,-1,-1)
x
40Basic Perspective Projection
P(x,y,z)
y
similar triangles
P(x,y,d)
z
zd
but
also
- nonuniform foreshortening
- not affine
41Basic Perspective Projection
- can express as homogenous 4x4 matrix!
42Projective Transformations
- determining the matrix representation
- need to observe 5 points in general position,
e.g. - left,0,0,1T?-1,0,0,1T
- 0,top,0,1T?0,1,0,1T
- 0,0,-f,1T?0,0,1,1T
- 0,0,-n,1T?0,0,-1,1T
- leftf/n,topf/n,-f,1T?-1,1,1,1T
- solve resulting equation system to obtain matrix
43OpenGL Orthographic Matrix
- scale, translate, reflect for new coord sys
- understand derivation from VCS!
44OpenGL Perspective Matrix
- shear, scale, reflect for new coord sys
- understand derivation from VCS!
45Viewport Transformation
onscreen pixels map from -1,1 to 0,
displaywidth
DCS
- confusion on hw
- pixel locations should not be negative or huge!
46Light Sources
- directional/parallel lights
- point at infinity (x,y,z,0)T
- point lights
- finite position (x,y,z,1)T
- spotlights
- position, direction, angle
- ambient lights
47Reflectance
- specular perfect mirror with no scattering
- gloss mixed, partial specularity
- diffuse all directions with equal energy
-
-
- specular glossy diffuse
- reflectance distribution
48Review Reflection Equations
2 ( N (N L)) L R
49Review Reflection Equations 2
- Blinn improvement
- full Phong lighting model
- combine ambient, diffuse, specular components
50Lighting vs. Shading
- lighting
- simulating the interaction of light with surface
- shading
- deciding pixel color
- continuum of realism when do we do lighting
calculation?
51Shading Models
- flat shading
- compute Phong lighting once for entire polygon
- Gouraud shading
- compute Phong lighting at the vertices and
interpolate lighting values across polygon - Phong shading
- compute averaged vertex normals
- interpolate normals across polygon and perform
Phong lighting across polygon
52Transforming Normals
- apply nonuniform scale stretch along x by 2
- cant transform normal
- by modelling matrix
- solution
normal to any surface transformed by inverse
transpose of modelling transformation
53Interpolation
54Scan Conversion
- done
- how to determine pixels covered by a primitive
- next
- how to assign pixel colors
- interpolation of colors across triangles
- interpolation of other properties
55Interpolation During Scan Conversion
- interpolate values between vertices
- z values
- r,g,b colour components
- use for Gouraud shading
- u,v texture coordinates
- surface normals
- equivalent methods (for triangles)
- bilinear interpolation
- barycentric coordinates
56Bilinear Interpolation
- interpolate quantity along L and R edges, as a
function of y - then interpolate quantity as a function of x
P1
P3
P(x,y)
PL
PR
y
P2
573. Barycentric Coordinates
- weighted combination of vertices
(1,0,0)
(0,0,1)
(0,1,0)
58Computing Barycentric Coordinates
P1
P3
PL
P
PR
d2 d1
P2
59Computing Barycentric Coords
P1
P3
PL
P
PR
b1 b2
d2 d1
P2
60Computing Barycentric Coords
P1
P3
PL
P
PR
b1 b2
c1 c2
d2 d1
P2
61Computing Barycentric Coords
62Computing Barycentric Coords
- can verify barycentric properties