Prog 2, Kangaroo Hall of Fame, Midterm Review, (Interpolation if - - PowerPoint PPT Presentation

prog 2 kangaroo hall of fame midterm review interpolation
SMART_READER_LITE
LIVE PREVIEW

Prog 2, Kangaroo Hall of Fame, Midterm Review, (Interpolation if - - PowerPoint PPT Presentation

University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Prog 2, Kangaroo Hall of Fame, Midterm Review, (Interpolation if time) Week 6, Wed Feb 9 http://www.ugrad.cs.ubc.ca/~cs314/Vjan2005 Program 2


slide-1
SLIDE 1

University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/Vjan2005

Prog 2, Kangaroo Hall of Fame, Midterm Review, (Interpolation if time) Week 6, Wed Feb 9

slide-2
SLIDE 2

2

Program 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

slide-3
SLIDE 3

3

Program 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

slide-4
SLIDE 4

4

Program 2 Quick Demo

slide-5
SLIDE 5

5

Review: Midpoint Algorithm

 moving incrementally along x direction

 draw at current y value, or move up to y+1?

 check if midpoint between two possible pixel centers

above or below line

 candidates

 top pixel: (x+1,y+1),  bottom pixel: (x+1, y)

 midpoint: (x+1, y+.5)  check if midpoint above or below line

 below: top pixel  above: bottom pixel

 assume , slope

2 1

x x < 0 < dy dx <1

slide-6
SLIDE 6

6

Review: Bresenham Algorithm

 all integer arithmetic  cumulative error function

y=y0; e=0; for (x=x0; x <= x1; x++) { draw(x,y); if (2(e+dy) < dx) { e = e+dy; } else { y=y+1; e=e+dy-dx; }} y=y0; eps=0 for ( int x = x0; x <= x1; x++ ){ draw(x,y); eps += dy; if ( (eps << 1) >= dx ){ y++; eps -= dx; } }

slide-7
SLIDE 7

7

P

Review: 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

slide-8
SLIDE 8

1 2 3 4 5=0 P

Review: Scanline Algorithms

 set pixels inside polygon boundary along

horizontal lines one pixel apart

 use bounding box to speed up

slide-9
SLIDE 9

9

Review: 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

slide-10
SLIDE 10

10

Review: General Polygon Rasterization

 idea: use a parity test

for each scanline edgeCnt = 0; for each pixel on scanline (l to r) if (oldpixel->newpixel crosses edge) edgeCnt ++; // draw the pixel if edgeCnt odd if (edgeCnt % 2) setPixel(pixel);

slide-11
SLIDE 11

11

Hall of Fame

slide-12
SLIDE 12

12

But Wait, There’s More!

 nice comment :)

 “this project is fun, only one of the very few

that I actually enjoyed. Too bad there's only

  • ne CG course in UBC =(

 two fourth year CG courses await you!

 424 Geometric Modelling  426 Animation

slide-13
SLIDE 13

13

Midterm Review

slide-14
SLIDE 14

14

Midterm Exam

 Friday Feb 11 10am-10:50am

 you may use one handwritten 8.5”x11” 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

slide-15
SLIDE 15

15

What’s Covered

 transformations  viewing and projections  coordinate systems of rendering pipeline  lighting and shading  not scan conversion

slide-16
SLIDE 16

16

Reading

 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

slide-17
SLIDE 17

17

Old Exams Posted

 see course web page

slide-18
SLIDE 18

18

The Rendering Pipeline

 pros and cons of pipeline approach

Geometry Database Geometry Geometry Database Database Model/View Transform. Model/View Model/View Transform. Transform. Lighting Lighting Lighting Perspective Transform. Perspective Perspective Transform. Transform. Clipping Clipping Clipping Scan Conversion Scan Scan Conversion Conversion Depth Test Depth Depth Test Test Texturing Texturing Texturing Blending Blending Blending Frame- buffer Frame- Frame- buffer buffer

slide-19
SLIDE 19

19

Transformations

                        =             1 1 1 1 1 1 ' ' ' z y x c b a z y x

translate(a,b,c) translate(a,b,c)

                        − =             1 1 cos sin sin cos 1 1 ' ' ' z y x z y x θ θ θ θ

) , ( Rotate θ x

                        =             1 1 1 ' ' ' z y x c b a z y x

scale(a,b,c) scale(a,b,c)

            − 1 cos sin 1 sin cos θ θ θ θ

) , ( Rotate θ y

            − 1 1 cos sin sin cos θ θ θ θ

) , ( Rotate θ z

slide-20
SLIDE 20

20

Homogeneous Coordinates

x x y y w w w= w=1 1           ⋅ ⋅ w w y w x           ⋅ ⋅ w w y w x           1 y x           1 y x

slide-21
SLIDE 21

21

Composing Transformations

Ta Tb = Tb Ta, but Ra Ta Tb = Tb Ta, but Ra Rb Rb != != Rb Rb Ra and Ta Ra and Ta Rb Rb != != Rb Rb Ta Ta

slide-22
SLIDE 22

22

Composing Transformations

 example: rotation around arbitrary center

slide-23
SLIDE 23

23

Composing Transformations

 example: rotation around arbitrary center

 step 1: translate coordinate system to rotation

center

slide-24
SLIDE 24

24

Composing Transformations

 example: rotation around arbitrary center

 step 2: perform rotation

slide-25
SLIDE 25

25

Composing Transformations

 example: rotation around arbitrary center

 step 3: back to original coordinate system

slide-26
SLIDE 26

26

Composing Transformations

 rotation about a fixed point

p’ = TRT-1p

 rotation around an arbitrary axis  considering frame vs. object

 p’ = DCBAp

  • bject

frame

OpenGL:

D C B A draw p

slide-27
SLIDE 27

27

Transformation Hierarchies

 hierarchies don’t fall apart when changed  transforms apply to graph nodes beneath

slide-28
SLIDE 28

28

Matrix Stacks

 push and pop matrix stack

 avoid computing inverses or incremental

xforms

 avoid numerical error

World coordinates World coordinates

T T1

1(x)

(x) T T2

2(x)

(x) T T3

3(x)

(x)

slide-29
SLIDE 29

29

Matrix Stacks

glPushMatrix glPushMatrix() () glPopMatrix glPopMatrix() () A A B B C C A A B B C C A A B B C C C C glScale3f(2,2,2) glScale3f(2,2,2) D = C scale(2,2,2) trans(1,0,0) D = C scale(2,2,2) trans(1,0,0) A A B B C C D D DrawSquare DrawSquare() () glTranslate3f(1,0,0) glTranslate3f(1,0,0) DrawSquare DrawSquare() () glPushMatrix glPushMatrix() () glPopMatrix glPopMatrix() ()

slide-30
SLIDE 30

30

Transformation Hierarchies

 example

4

θ

1

θ

5

θ

3

θ

2

θ

x x y y glTranslate3f(x,y,0); glTranslate3f(x,y,0); glRotatef glRotatef( ,0,0,1); ( ,0,0,1); DrawBody DrawBody(); (); glPushMatrix glPushMatrix(); (); glTranslate3f(0,7,0); glTranslate3f(0,7,0); DrawHead DrawHead(); (); glPopMatrix glPopMatrix(); (); glPushMatrix glPushMatrix(); (); glTranslate glTranslate(2.5,5.5,0); (2.5,5.5,0); glRotatef glRotatef( ,0,0,1); ( ,0,0,1); DrawUArm DrawUArm(); (); glTranslate glTranslate(0,-3.5,0); (0,-3.5,0); glRotatef glRotatef( ,0,0,1); ( ,0,0,1); DrawLArm DrawLArm(); (); glPopMatrix glPopMatrix(); (); ... (draw other arm) ... (draw other arm)

1

θ

2

θ

3

θ

slide-31
SLIDE 31

31

Display Lists

 reuse block of OpenGL code  more efficient than immediate mode

 code reuse, driver optimization

 good for static objects redrawn often

 can’t change contents  not just for multiple instances

 interactive graphics: objects redrawn every

frame

 nest when possible for efficiency

slide-32
SLIDE 32

32

Double Buffering

 two buffers, front and back

 while front is on display, draw into back  when drawing finished, swap the two

 avoid flicker

slide-33
SLIDE 33

33

Projective Rendering Pipeline

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

OCS OCS WCS WCS VCS VCS CCS CCS NDCS NDCS DCS DCS

modeling modeling transformation transformation viewing viewing transformation transformation projection projection transformation transformation viewport viewport transformation transformation alter w alter w / w / w

  • bject

world viewing/ camera device normalized device clipping

perspective perspective division division glVertex3f(x,y,z) glVertex3f(x,y,z) glTranslatef glTranslatef(x,y,z) (x,y,z) glRotatef glRotatef( (th th,x,y,z) ,x,y,z) .... .... gluLookAt gluLookAt(...) (...) glFrustum glFrustum(...) (...) glutInitWindowSize glutInitWindowSize(w,h) (w,h) glViewport glViewport(x,y,a,b) (x,y,a,b)

slide-34
SLIDE 34

34

Projection

 theoretical pinhole camera

image image plane plane eye eye point point

– image inverted, more convenient equivalent

image image plane plane eye eye point point

slide-35
SLIDE 35

35

Projection Taxonomy

planar planar projections projections perspective: perspective: 1,2,3-point 1,2,3-point parallel parallel

  • blique
  • blique
  • rthographic
  • rthographic

cabinet cabinet cavalier cavalier top, top, front, front, side side axonometric: axonometric: isometric isometric dimetric dimetric trimetric trimetric

slide-36
SLIDE 36

36

Projective Transformations

 transformation of space

 center of projection moves to infinity  viewing frustum transformed into a

parallelpiped

  • z
  • z

x x

  • z
  • z

x x Frustum Frustum

slide-37
SLIDE 37

37

Normalized Device Coordinates

left/right x =+/- 1, top/bottom y =+/- 1, near/far z =+/- 1

  • z
  • z

x x Frustum Frustum z=-n z=-n z=-f z=-f

right right left left

z z x x x= -1 x= -1 z=1 z=1 x=1 x=1 Camera coordinates Camera coordinates NDC NDC z= -1 z= -1

slide-38
SLIDE 38

38

Projection Normalization

 distort such that orthographic projection of

distorted objects is desired persp projection

  • z
  • z

x x z z x x

slide-39
SLIDE 39

39

Transforming View Volumes

x z

NDCS

y

(-1,-1,-1) (1,1,1)

  • rthographic view volume
  • rthographic view volume

x z VCS y x=left y=top x=right z=-far z=-near y=bottom perspective view volume perspective view volume x=left x=right y=top y=bottom z=-near z=-far x VCS y

slide-40
SLIDE 40

40

Basic Perspective Projection

similar triangles similar triangles

→ = z y d y'

z d y y ⋅ = '

z z P(x,y,z) P(x,y,z) P( P(x x’ ’, ,y y’ ’,d) ,d) z=d z=d y y also also

z d x x ⋅ = '

 nonuniform foreshortening

 not affine

d z = '

but but

slide-41
SLIDE 41

41

Basic Perspective Projection

 can express as homogenous 4x4 matrix!

                        =             1 / 1 1 1 1 / z y x d d z z y x

          ⋅ ⋅ d z d y z d x / /

            d z z y x / w /

slide-42
SLIDE 42

42

Projective Transformations

 determining the matrix representation

 need to observe 5 points in general position,

e.g.

 [left,0,0,1]T→[-1,0,0,1]T  [0,top,0,1]T→[0,1,0,1]T  [0,0,-f,1]T→[0,0,1,1]T  [0,0,-n,1]T→[0,0,-1,1]T  [left*f/n,top*f/n,-f,1]T→[-1,1,1,1]T

 solve resulting equation system to obtain matrix

slide-43
SLIDE 43

43

OpenGL Orthographic Matrix

 scale, translate, reflect for new coord sys

 understand derivation from VCS!

P near far near far near far bot top bot top bot top left right left right left right P                         − + − − − − + − − − + − − = 1 2 2 2 '

slide-44
SLIDE 44

44

OpenGL Perspective Matrix

 shear, scale, reflect for new coord sys

 understand derivation from VCS!

                  − − − − + − − + − − + − 1 2 ) ( 2 2 n f fn n f n f b t b t b t n l r l r l r n

slide-45
SLIDE 45

45

Viewport Transformation

x x (0,0) (0,0) (w,h) (w,h) DCS DCS a a b b y y (0,0) (0,0) (w,h) (w,h) DCS DCS a a b b x x y y

(-1,-1)

(1,1) (1,1) NDCS NDCS

x x y y display display viewport viewport x x y y

  • nscreen pixels: map from [-1,1] to [0, displaywidth]

DCS DCS

confusion on hw

 pixel locations should

not be negative or huge!

slide-46
SLIDE 46

46

Light 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

slide-47
SLIDE 47

47

Reflectance

 specular: perfect mirror with no scattering  gloss: mixed, partial specularity  diffuse: all directions with equal energy

+ + =

specular + glossy + diffuse = reflectance distribution

slide-48
SLIDE 48

48

Review: Reflection Equations

Idiffuse = kd Ilight (n • l)

n l θ 2 ( N (N · L)) – L = R

Ispecular = ksIlight(v•r)nshiny

slide-49
SLIDE 49

49

Review: Reflection Equations 2

 Blinn improvement  full Phong lighting model

 combine ambient, diffuse, specular components

l l n n v v h h

Itotal = ksIambient + Ii(

i=1 #lights

kd(n•li) + ks(v•ri)nshiny ) Ispecular = ksIlight(h•n)nshiny h = (l + v)/2

slide-50
SLIDE 50

50

Lighting vs. Shading

 lighting

 simulating the interaction of light with surface

 shading

 deciding pixel color  continuum of realism: when do we do lighting

calculation?

slide-51
SLIDE 51

51

Shading 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

slide-52
SLIDE 52

52

 apply nonuniform scale: stretch along x by 2

 can’t transform normal

by modelling matrix

 solution:

Transforming Normals

MP P = '

P N

QN N = '

( )

T

M Q

1 −

=

normal to any surface transformed by inverse transpose of modelling transformation

slide-53
SLIDE 53

53

Interpolation

slide-54
SLIDE 54

54

Scan 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

slide-55
SLIDE 55

55

z y x

N N N , ,

Interpolation 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

slide-56
SLIDE 56

56

Bilinear Interpolation

 interpolate quantity along L and R edges,

as a function of y

 then interpolate quantity as a function of x

y y P(x,y) P(x,y) P P1

1

P P2

2

P P3

3

P PL

L

P PR

R

slide-57
SLIDE 57

57

  • 3. Barycentric Coordinates

 weighted combination of vertices

3 2 1

P P P P ⋅ + ⋅ + ⋅ = γ β α

1

P

3

P

2

P P

(1,0,0) (1,0,0) (0,1,0) (0,1,0) (0,0,1) (0,0,1)

5 . = β 1 = β = β 1 , , 1 ≤ ≤ = + + γ β α γ β α

“ “convex combination convex combination

  • f points
  • f points”

slide-58
SLIDE 58

58

P P2

2

P P3

3

P P1

1

P PL

L

P PR

R

P P d d2

2 : d

: d1

1

3 2 1 1 2 2 1 2 3 2 1 1 2 2 1 1 2 3 2 1 1 2

) 1 ( ) ( P d d d P d d d P d d d P d d d P P d d d P P

L

+ + + = = + + + − = − + + =

Computing Barycentric Coordinates

 for point P on scanline

slide-59
SLIDE 59

59

Computing Barycentric Coords

 similarly

b b1

1 : b

: b2

2

P P2

2

P P3

3

P P1

1

P PL

L

P PR

R

P P d d2

2 : d

: d1

1

1 2 1 1 2 2 1 2 1 2 1 1 2 2 1 1 2 1 2 1 1 2

) 1 ( ) ( P b b b P b b b P b b b P b b b P P b b b P P

R

+ + + = = + + + − = − + + =

slide-60
SLIDE 60

60

R L

P c c c P c c c P ⋅ + + ⋅ + =

2 1 1 2 1 2

b b1

1 : b

: b2

2

P P2

2

P P3

3

P P1

1

P PL

L

P PR

R

P P d d2

2 : d

: d1

1

3 2 1 1 2 2 1 2

P d d d P d d d P

L

+ + + =

1 2 1 1 2 2 1 2

P b b b P b b b P

R

+ + + =

c c1

1: c

: c2

2

        + + + + +         + + + + =

1 2 1 1 2 2 1 2 2 1 1 3 2 1 1 2 2 1 2 2 1 2

P b b b P b b b c c c P d d d P d d d c c c P

Computing Barycentric Coords

 combining  gives

slide-61
SLIDE 61

61

Computing Barycentric Coords

 thus

with

3 3 2 2 1 1

P a P a P a P ⋅ + ⋅ + ⋅ =

2 1 1 2 1 2 3 2 1 2 2 1 1 2 1 2 2 1 2 2 2 1 1 2 1 1 1

d d d c c c a b b b c c c d d d c c c a b b b c c c a + + = + + + + + = + + =

slide-62
SLIDE 62

62

Computing Barycentric Coords

 can verify barycentric properties

1 , , 1

3 2 1 3 2 1

≤ ≤ = + + a a a a a a