+ All Categories
Home > Documents > 3D to 2D Projection - cpb-us-w2.wpmucdn.com

3D to 2D Projection - cpb-us-w2.wpmucdn.com

Date post: 12-Nov-2021
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
62
3D to 2D Projection Prof. Aaron Lanterman (Based on slides by Prof. Hsien-Hsin Sean Lee) School of Electrical and Computer Engineering Georgia Institute of Technology
Transcript
Page 1: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

3D to 2D Projection

Prof. Aaron Lanterman (Based on slides by Prof. Hsien-Hsin Sean Lee) School of Electrical and Computer Engineering

Georgia Institute of Technology

Page 2: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

2

Projection from 3D space

Much discussion adapted from Joe Farrell’s article: http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__1/ )

x

y

z

Page 3: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

3

Canonical view volume •  Projection transforms your geometry into a canonical view

volume in normalized device coordinates (“clip space”) •  Only X- and Y-coordinates will be mapped onto the screen •  Z will be almost useless, but used for depth test

(-1, -1, 0)

(1, 1, 1)

x

y

z

(0, 0, 0)

Canonical view volume (LHS) (-1, -1, 0) to (1,1,1) used by Direct3D

Page 4: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

4

Strange “conventions”

(-1, -1, 0)

(1, 1, 1)

x

y

z

(0, 0, 0)

Canonical “clips space” view volume (LHS) (-1, -1, 0) to (1,1,1) used by

D3D/XNA

Canonical “clip space” view volume (LHS) (-1, -1, 1) to (1,1,1) used by

OpenGL/Unity

(remember unnormalized eye-space coordinates in OpenGL are in a RHS,

but in Unity are in a LHS!)

(-1, -1,-1)

(1, 1, 1)

x

y

z

(0, 0, 0)

(remember unnormalized eye-space coordinates in

Direct3D are in a LHS, but in XNA are in a RHS!!!)

Page 5: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

5

Orthographic (or parallel) projection

Viewer’s position

View plane

• Project from 3D space to the viewer’s 2D space

Page 6: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

6

Style of orthographic projection

•  Same size in 2D and 3D •  No sense of distance •  Parallel lines remain parallel •  Good for tile-based games where camera is in fixed location

(e.g., Mahjong or 3D Tetris)

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

Page 7: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

7

Direct3D orthographic projection

View Volume (an axis-aligned box)

(l, b, n)

(r, t, f)

(0, -1, -1)

(1, 1, 1)

x

y

z

Canonical view volume (Direct3D)

Page 8: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

8

General orthographic math • Derive x’ and y’

]1 ,1[' ] ,[ −∈∈ xrlx

rxl ≤≤

lrlx −≤−≤0

10 ≤−−≤lrlx

2)(20 ≤−−≤lrlx

11)(21 ≤−−−≤−lrlx

1221 ≤−

+−−≤−lr

lrlx

121 ≤−+−

−≤−

lrlr

lrx

lrlr

lrxx

−+−

−=∴ 2'

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

Page 9: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

9

D3D orthographic math for Z (LHS default)

• Derive z’

]1 ,0[' ] ,[ ∈∈ zfnz

fzn ≤≤

nfnz −≤−≤0

10 ≤−−≤nfnz

10 ≤−

−−

≤nfn

nfz

nfn

nfzz

−−

−=∴ '

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

Page 10: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

10

D3D orthographic results (LHS)

x ' = 2xr − l

− r + lr − l

btbt

btyy

−+−

−= 2'

z '= zf − n

− nf − n

Page 11: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

11

D3D orthographic matrix (LHS default) • Direct3D primarily uses LHS, z from 0 to 1, row vectors

[x ', y ', z ',1] = [x, y, z,1]P where P =

2r − l

0 0 0

0 2t − b

0 0

0 0 1f − n

0

l + rl − r

t + bb − t

nn − f

1

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

•  In Direct3D: D3DXMatrixOrthoOffCenterLH(*o,l,r,b,t,n,f)

http://msdn.microsoft.com/en-us/library/bb205347(VS.85).aspx

Page 12: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

12

D3D orthographic math for Z (RHS weird)

• Derive z’

−z∈[n, f ] z '∈[0, 1]

n ≤ −z ≤ f

0 ≤ −z − nf − n

≤1

0 ≤ −zf − n

− nf − n

≤1

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

0 ≤ −z − n ≤ f − n

•  For RHS, in most API calls z clip parameters are positive, and clip space switches to using a LHS

∴ z '= −zf − n

− nf − n

Page 13: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

13

D3D orthographic matrix (RHS weird)

•  In Direct3D: D3DXMatrixOrthoOffCenterRH(*o,l,r,b,t,n,f) •  In XNA: Matrix.CreateOrthographicOffCenter(l,r,b,t,n,f) http://msdn.microsoft.com/en-us/library/bb205348(VS.85).aspx http://www.cs.utk.edu/~vose/c-stuff/opengl/glOrtho.html

[x ', y ', z ',1] = [x, y, z,1]P where P =

2r − l

0 0 0

0 2t − b

0 0

0 0 1n − f

0

l + rl − r

t + bb − t

nn − f

1

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

Page 14: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

14

Simpler D3D ortho matrix (LHS default)

[x ', y ', z ',1] = [x, y, z,1]P where P =

2w

0 0 0

0 2h

0 0

0 0 1f − n

0

0 0 nn − f

1

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

•  In Direct3D: D3DXMatrixOrthoLH(*o,w,h,n,f)

• Most orthographic projection setups –  Z-axis passes through the center of your view volume –  Field of view (FOV) extends equally far

• To the left as to the right (i.e., r = -l) • To the top as to the below (i.e., t=-b)

Page 15: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

15

Simpler D3D ortho matrix (RHS weird)

[x ', y ', z ',1] = [x, y, z,1]P where P =

2w

0 0 0

0 2h

0 0

0 0 1n − f

0

0 0 nn − f

1

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

•  In Direct3D: D3DXMatrixOrthoRH(*o,w,h,n,f) •  In XNA: Matrix.CreateOrthographic(w,h,n,f)

•  For RHS, in most API calls z input parameters are positive, and clip space switches to using a LHS

Page 16: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

16

OpenGL orthographic projection

View Volume (an axis-aligned box)

(l, b, n)

(r, t, f)

(-1, -1, -1)

(1, 1, 1)

x

y

z

Canonical view volume (OpenGL)

Page 17: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

17

OpenGL orthographic math for Z (RHS) • Derive z’

−z∈[n, f ] z '∈[−1, 1]

n ≤ −z ≤ f

0 ≤ −z − n ≤ f − n

0 ≤ −z − nf − n

≤1

0 ≤ 2(−z − n)f − n

≤ 2

−1≤ 2(−z − n)f − n

−1≤1

−1≤ −2z − 2n − f + nf − n

≤1

−1≤ −2zf − n

− f + nf − n

≤1

∴ z ' = −2zf − n

− f + nf − n

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__2/Deriving-Projection-Matrices.htm

Page 18: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

18

OpenGL orthographic results

x ' = 2xr − l

− r + lr − l

btbt

btyy

−+−

−= 2'

z ' = −2zf − n

− f + nf − n

Page 19: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

19

Ortho proj matrix (OpenGL/Unity)

x 'y 'z '1'

⎢⎢⎢⎢

⎥⎥⎥⎥

= P

xyz1

⎢⎢⎢⎢

⎥⎥⎥⎥

where P =

2r − l

0 0 − r + lr − l

0 2t − b

0 − t + bt − b

0 0 −2f − n

− f + nf − n

0 0 0 1

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

See http://www.songho.ca/opengl/gl_projectionmatrix.html#ortho

• In OpenGL: glOrtho(l,r,b,t,n,f) • In Unity: Matrix4x4.Ortho(l,r,b,t,n,f) ????????????

•  For RHS, in most API calls z input parameters are positive, and clip space switches to using a LHS

Page 20: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

20

Perspective projection

Viewer’s position

View plane

Page 21: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

21

Viewing frustum

Far plane

Viewer’s position

Near plane

Think about looking through a window in a dark room

Page 22: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

22

Viewing frustum with furniture

Near plane

Far plane

Viewer’s position

Page 23: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

23

Direct3D perspective projection

(-1, -1, 0)

(1, 1, 1)

x

y

z

Canonical view volume (Direct3D)

(l, b, n)

(r, t, f)

View Frustum (a truncated pyramid)

Page 24: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

24

D3D perspective proj mapping •  Given a point (x,y,z) within the view frustum, project it onto

the near plane z=n •  We will map x from [l,r] to [-1,1] and y from [b,t] to [-1,1]

(-1, -1, 0)

(1, 1, 1)

x

y

z

Canonical view volume (Direct3D)

(l, b, n)

(r, t, f)

View Frustum

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 25: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

25

D3D perspective math (1)

x 'x= nz⇒ x ' = nx

z

x

y

z

(x,y,z)

(x’,y’,n)

n x’

y’

x

y

z

(0,0,0) (0,0,n)

To calculate new coordinates of x’ and y’

zny

znx

xy

xyxy

xy

xy =⋅==⇒= ''''

f

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Next apply our orthographic projection formulas

Page 26: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

26

D3D perspective math (2)

x

y

z

(x,y,z)

(x’,y’,n)

n x’

y’

x

y

z

(0,0,0) (0,0,n)

f

x' = 2r − l

⋅ nxz− r + l

r − l y' = 2n

t − b⋅ ny

z− t + b

t − b

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

x'z = 2nr − l

x − r + lr − l

z y'z = 2nt − b

y − t + bt − b

z

Now let’s tackle the z’ component

Page 27: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

27

D3D perspective math (3)

• We know z (depth) transformation has nothing to do with x and y

x'z = 2nr − l

x − r + lr − l

z

y'z = 2nt − b

y − t + bt − b

z

z 'z = pz + q where p and q are constants

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 28: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

28

D3D perspective math (4)

• We know (boxed equations above) –  z’ = 0 when z=n (near plane) –  z’ = 1 when z=f (far plane)

z 'z = pz + q where p and q are constants

0 = pn + qf = pf + q nf

fnqnffp

−−=

−=∴ and

z 'z = ff − n

z − fnf − n

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 29: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

29

General D3D perspective matrix

x'z = 2nr − l

x − r + lr − l

z

y'z = 2nt − b

y − t + bt − b

z

z 'z = ff − n

z − fnf − n

w 'z = z

[x 'z, y 'z, z 'z,w 'z] = [x, y, z,1]P where P =

2nr − l

0 0 0

0 2nt − b

0 0

− r + lr − l

− t + bt − b

ff − n

1

0 0 − fnf − n

0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 30: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

30

Simpler D3D perspective matrix •  Similar to orthographic projection, if l=-r and t=-b, we can

simplify to

[x 'z, y 'z, z 'z,w 'z] = [x, y, z,1]P where P =

2nw

0 0 0

0 2nh

0 0

0 0 ff − n

1

0 0 − fnf − n

0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

•  In any case, we will have to divide by z to obtain [x’,y’, z’, w’] –  Implemented by dividing by the fourth (w'z) coordinate

Page 31: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

31

Define viewing frustum

Viewer’s position

+z

Parameters: FOV: Field of View Aspect ratio = Width/Height Near z Far z

fov/2

+y

Far (f)

Near (n)

Height

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 32: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

32

Reparameterized D3D matrix

hna 2)

2cot( =

+z

+y

a h/2

h/2

n

Need to replace w and h with FOV and aspect ratio

r = wh

2nw

= 2nrh

= 2n

r 2n

cot(a2)

= 1rcot(a

2)

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Direct3D style

P =

2nw

0 0 0

0 2nh

0 0

0 0 ff − n

1

0 0 − fnf − n

0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

Page 33: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

33

D3D perspective matrix (LHS default)

[x 'z, y 'z, z 'z,w 'z] = [x, y, z,1]P where P =

1r⋅cot(a

2) 0 0 0

0 cot(a2

) 0 0

0 0 ff − n

1

0 0 − fnf − n

0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

a: Field of View (FOV) r: aspect ratio = widthheight

n: near plan f: far plane

http://msdn.microsoft.com/en-us/library/bb205350(VS.85).aspx

•  In Direct3D: D3DXMatrixPerspectiveFovLH(*o,a,r,n,f)

Page 34: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

34

D3D perspective matrix (RHS weird)

http://msdn.microsoft.com/en-us/library/bb205351(VS.85).aspx

•  In Direct3D: D3DXMatrixPerspectiveFovRH(*o,a,r,n,f) •  In XNA: Matrix.CreatePerspectiveFieldOfView(a,r,n,f)

[x 'z, y 'z, z 'z,w 'z] = [x, y, z,1]P where P =

1r

cot(a2

) 0 0 0

0 cot(a2

) 0 0

0 0 fn − f

−1

0 0 fnn − f

0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

a: Field of View (FOV) r: aspect ratio = widthheight

n: near plan f: far plane

Page 35: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

35

OpenGL/LHS perspective projection

(-1, -1, -1)

(1, 1, 1)

x

y

z

Canonical view volume (Unity)

(l, b, n)

(r, t, f)

View Frustum (a truncated pyramid)

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 36: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

36

OpenGL/LHS perspective proj mapping •  Given a point (x,y,z) within the view frustum, project it onto

the near plane z=n •  We will map x from [l,r] to [-1,1] and y from [b,t] to [-1,1]

(-1, -1, -1)

(1, 1, 1)

x

y

z

Canonical view volume (Unity)

(l, b, n)

(r, t, f)

View Frustum

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 37: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

37

OpenGL/LHS perspective projection math

• We know (boxed equations above) –  z’ = -1 when z=n (near plane) –  z’ = 1 when z=f (far plane)

z 'z = pz + q where p and q are constants

−n = pn + qf = pf + q

∴ p = f + nf − n

and q = − 2 fnf − n�

z 'z = f + nf − n

z − 2 fnf − n

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 38: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

38

General OpenGL/LHS perspective matrix

x'z = 2nr − l

x − r + lr − l

z

y'z = 2nt − b

y − t + bt − b

z

z 'z = f + nf − n

z − 2 fnf − n

w 'z = z

x 'zy 'zz 'zw 'z

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

= P

xyz1

⎢⎢⎢⎢

⎥⎥⎥⎥

where P =

2nr − l

0 − r + lr − l

0

0 2nt − b

− t + bt − b

0

0 0 f + nf − n

− 2 fnf − n

0 0 1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

Page 39: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

39

Simpler OpenGL/LHS perspective matrix •  Similar to orthographic projection, if l=-r and t=-b, we can

simplify to

See http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__3/Deriving-Projection-Matrices.htm

•  In any case, we will have to divide by z to obtain [x’,y’, z’, w’] –  Implemented by dividing by the fourth (w'z) coordinate

x 'zy 'zz 'zw 'z

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

= P

xyz1

⎢⎢⎢⎢

⎥⎥⎥⎥

where P =

2nw

0 0 0

0 2nh

0 0

0 0 f + nf − n

− 2 fnf − n

0 0 1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

Page 40: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

40

Simpler OpenGL/RHS perspective matrix

x 'zy 'zz 'zw 'z

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

= P

xyz1

⎢⎢⎢⎢

⎥⎥⎥⎥

where P =

1r

cot(a2

) 0 0 0

0 cot(a2

) 0 0

0 0 f + nn − f

2 fnn − f

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

a: Field of View (FOV) r: aspect ratio = widthheight

n: near plan f: far plane

http://msdn.microsoft.com/en-us/library/bb205350(VS.85).aspx

•  In OpenGL: gluPerspective(a,r,n,f)

Page 41: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

41

Unity perspective matrix

x 'zy 'zz 'zw 'z

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

= P

xyz1

⎢⎢⎢⎢

⎥⎥⎥⎥

where P =

1r

cot(a2

) 0 0 0

0 cot(a2

) 0 0

0 0 f + nf − n

2 fnf − n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

a: Field of View (FOV) r: aspect ratio = widthheight

n: near plan f: far plane

http://msdn.microsoft.com/en-us/library/bb205350(VS.85).aspx

•  In Unity: Matrix4x4.Perspective(a,r,n,f) ?????????????

Page 42: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

42

Custom projections in Unity

•  From Camera.projectionMatrix documentation:

“Use a custom projection only if you really need a non-standard projection. This property is used by Unity's water rendering to setup an oblique projection matrix. Using custom projections requires good knowledge of transformation and projection matrices.”

Page 43: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

Unity’s 2-D coordinate systems •  Viewport space:

–  (0,0) is bottom-left –  (1,1) is top-right

•  Screen space coordinates: –  z “is in world units from the camera” –  (0,0) is bottom-left –  (Camera.pixelWidth,Camera.pixelHeight) is top-right

•  GUI space coordinates: –  (0,0) is upper-left –  (Camera.pixelWidth,Camera.pixelHeight) is bottom-right

43

Page 44: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

44

Viewport transformation

•  The actual 2D projection to the viewer • Copy to your back buffer (frame buffer) • Can be programmed, scaled, ...

Page 45: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

45

Backface culling •  Determine “facing direction” •  Triangle order matters •  How to compute a normal vector for 2 given vectors?

–  Using cross product of 2 given vectors

(c1, c2, c3) (b1, b2, b3)

(a1, a2, a3)

kji

kji

kji

)()()(21

2

1

122131132332

321

321

yxyxyxyxyxyxVV

yyyV

xxxV

−+−+−=×

++=

++=Cross product

kji

kji

)33()22()11(2

)33()22()11(1

acacacV

abababV

−+−+−=

−+−+−=

2 Vectors

Page 46: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

46

Compute the surface normal for a triangle

(0, 0, 0) (4, 0, 0)

(3, 3, 0)

kji

kji

004v2

033v1

++=

++=

v2 x

z

y

(0, 0, 0) (4, 0, 0)

(3, 3, 0)

kji

kji

033v2

004v1

++=

++=

v1 x

z

•  Clockwise normals, LHS

Page 47: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

47

Backface culling method (1) •  Check if the normal is facing the camera •  How to determine that?

–  Use Dot Product

Surface vectors

Eye vectors

Page 48: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

48

Backface culling method (2) •  Check if the normal is facing the camera •  How to determine that?

–  Use Dot Product

Surface vectors

Eye vectors

Page 49: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

49

Dot product method (1)

B

θcosBABA =•

22

0 πθπ <<−⇒>•BA

Page 50: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

50

Dot product method (2)

B

θcosBABA =•

22

0 πθπ <<−⇒>•BA

Page 51: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

51

Dot product method (3)

22

0 πθπ <<−⇒>•BA

Page 52: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

52

Caution!

22

0 πθπ <<−⇒>•BA

Page 53: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

53

When to perform backface culling?

Clipp

ing

Persp

ectiv

e Divi

de

View

port

Tran

sform

Raste

rizati

on

Proje

ction

Tran

sform

Wor

ld Tr

ansfo

rm

View

Tran

sform

Verte

x Ligh

ting

Back

face C

ulling

Clipp

ing

Persp

ectiv

e Divi

de

View

port

Tran

sform

Raste

rizati

on

Proje

ction

Tran

sform

Wor

ld Tr

ansfo

rm

View

Tran

sform

Verte

x Ligh

ting

Back

face C

ulling

Make sure camera is in correct coordinates!

Page 54: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

54

How about before you even start?

Clipp

ing

Persp

ectiv

e Divi

de

View

port

Tran

sform

Raste

rizati

on

Proje

ction

Tran

sform

Wor

ld Tr

ansfo

rm

View

Tran

sform

Verte

x Ligh

ting

Back

face C

ulling

Transform camera vectors into object spaces

Clipp

ing

Persp

ectiv

e Divi

de

View

port

Tran

sform

Raste

rizati

on

Proje

ction

Tran

sform

Wor

ld Tr

ansfo

rm

View

Tran

sform

Verte

x Ligh

ting

Back

face C

ulling

Page 55: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

55

Or how about at the very end?

Clipp

ing

Persp

ectiv

e Divi

de

View

port

Tran

sform

Raste

rizati

on

Proje

ction

Tran

sform

Wor

ld Tr

ansfo

rm

View

Tran

sform

Ver

tex Li

ghtin

g

Back

face C

ulling

Clipp

ing

View

port

Tran

sform

Raste

rizati

on

Proje

ction

Tran

sform

Wor

ld Tr

ansfo

rm

View

Tran

sform

Verte

x Ligh

ting

Back

face C

ulling

•  Now you can just check “winding order” in 2D

Persp

ectiv

e Divi

de

•  As “officially” done by OpenGL

Page 56: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

56

3D clipping

•  Test 6 planes if a triangle is inside, outside, or partially inside the view frustum

•  Clipping creates new triangles (triangulation) –  Interpolate new vertices info

Page 57: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

57

Appendix

Page 58: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

58

Clipping against a plane • Test each vertex of a triangle

– Outside –  Inside – Partially inside

• Incurred computation overhead • Save unnecessary computation (and

bandwidth) later • Need to know how to determine a plane • Need to know how to determine a vertex is

inside or outside a plane

Page 59: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

59

Specifying a plane

•  You need two things to specify a plane –  A point on the plane (p0, p1, p2) –  A vector (normal) perpendicular to the plane (a, b, c) –  Plane à a*(x – p0) + b*(y – p1) + c*(z - p2) = 0

V=(5, 6, 7)

P=(1, 2, 3) K=(x, y, z)

Plane equation 5*(x-1)+6*(y-2)+7*(z-3)=0

Page 60: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

60

Distance calculation from a plane (1)

•  Given a point R, calculate the distance –  Distance > 0 inside the plane –  Distance = 0 on the plane –  Distance < 0 outside the plane

Normal R � �

d

d �

υυ

υυθ )()(|| cos|| d PR

PRPRPRPR −•=

−⋅

−•⋅−=⋅−=

P

Page 61: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

61

Distance calculation from a plane (2)

R

� �

d �

)180cos(|| d θ−⋅−= PR

P 180-�

Page 62: 3D to 2D Projection - cpb-us-w2.wpmucdn.com

62

Triangulation using interpolation

(a2, b2, c2)

(a1, b1, c1)

(x, y, z)

d1 d2

)12(1)12(1)12(1

211

ccsczbbsbyaasax

ddds

−⋅+=−⋅+=−⋅+=

+=


Recommended