Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Computer Graphics
Subodh Kumar
Dept of Computer Sc. & Engg. IIT Delhi
Model Representation
Model Representation
! List of triangles ! eg, Begin(TRI ); Vertex(v0);.. End(); ! Or even, Begin(TRISTRIP ); Vertex(v0);.. End();
Model Representation
! List of triangles ! eg, Begin(TRI ); Vertex(v0);.. End(); ! Or even, Begin(TRISTRIP ); Vertex(v0);.. End();
! Mesh ! Vertices & Connectivity ! eg, InterleavedArrays(); DrawElements(); ! Displaylists, VBO
Model Representation
! List of triangles ! eg, Begin(TRI ); Vertex(v0);.. End(); ! Or even, Begin(TRISTRIP ); Vertex(v0);.. End();
! Mesh ! Vertices & Connectivity ! eg, InterleavedArrays(); DrawElements(); ! Displaylists, VBO
! Instanced ! aka Scene graph
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
Sphere
Face Mesh
Cylinder
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
T
Sphere
Face Mesh
Cylinder
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
T
Sphere
Face Mesh
Cylinder
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
T
Sphere
Face Mesh
Cylinder
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
T
Sphere
Face Mesh
Cylinder
T’
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
T
Sphere
Face Mesh
Cylinder
T’
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
T
Sphere
Face Mesh
BotArmy
Cylinder
T’
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
World
T
Sphere
Face Mesh
BotArmy
Cylinder
T’
Scene Graph
Robot
BodyHead
ArmTrunk Mesh
Leg Mesh
EyeMouth Mesh
World
T
Sphere
Face Mesh
BotArmy
Cylinder
T0
T1
T2
T3
T4
Scene Graph Draw
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
! Un-multiply Head->Face Matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
! Un-multiply Head->Face Matrix! Un-multiply Robot->Head matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
! Un-multiply Head->Face Matrix! Un-multiply Robot->Head matrix! Multiply Robot->Body matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
! Un-multiply Head->Face Matrix! Un-multiply Robot->Head matrix! Multiply Robot->Body matrix! … Robot
BodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
! Un-multiply Head->Face Matrix! Un-multiply Robot->Head matrix! Multiply Robot->Body matrix! …! Un-multiply Robot->Body matrix
RobotBodyHead
Arm
World
BotArmy
Scene Graph Draw
! DFS Traversal! Multiply World->Army matrix! For each robot:
! Multiply Army->Robot matrix! Multiply Robot->Head matrix
! Multiply Head->Face Matrix! Draw Face! …
! Un-multiply Head->Face Matrix! Un-multiply Robot->Head matrix! Multiply Robot->Body matrix! …! Un-multiply Robot->Body matrix! …
RobotBodyHead
Arm
World
BotArmy
OpenGL Matrix Stack
! glPushMatrix() ! glMultMatrix(M1)
! glPopMatrix() ! glLoadMatrix()
OpenGL Matrix Stack
! glPushMatrix() ! glMultMatrix(M1)
! glPopMatrix() ! glLoadMatrix()
M0
OpenGL Matrix Stack
! glPushMatrix() ! glMultMatrix(M1)
! glPopMatrix() ! glLoadMatrix()
M0
M0 M0
OpenGL Matrix Stack
! glPushMatrix() ! glMultMatrix(M1)
! glPopMatrix() ! glLoadMatrix()
M0
M0 M0
M0 M0*M1
OpenGL Matrix Stack
! glPushMatrix() ! glMultMatrix(M1)
! glPopMatrix() ! glLoadMatrix()
M0
M0
M0 M0
M0 M0*M1
Transformation
! Change an object to another ! Transform all points of an object ! E.g.,
! Move it, Translate ! Re-orient it, Rotate ! Re-size it, Scale ! Skew, etc.
! Want to compose a sequence of transformations
Interior of Triangle
V0
V1
V2
Interior of Triangle
V0
V1
V2
Interior of Triangle
V0
V1
V2
Interior of Triangle
V0
V1
V2
Interior of Triangle
V0
V1
V2
V
Interior of Triangle
V0
V1
V2
(1-t ) V1 + t V2
t =(V-V1)/(V2-V1)V
Interior of Triangle
V1
V2
aV0+bV1+cV2
V0
V
a =Area(V2V V1)/ Area(V2V0V1)
. .
Simple Xform Types
! Rigid body ! No length changes
! Translation, Rotation
! Linear ! aT(P) + bT(Q) = T(aP+bQ)
! Rotation, Scale, nxn Matrix-multiplication
! Affine ! aT(P) + bT(Q) = T(aP+bQ) + K ! Preserves parallel lines ! Translation, Rotation, Scale ! Linear in projective space, n+1xn+1 matrix
multiplication ! Projective
Translation
Translation
Translation
Translation
Translation
tx, ty, tz
Rotation
Rotation
θ
Rotation
θ
Vector, θ
Rotation
θ
Vector, θ
Rotation
r
rθ
Vector, θ
Rotation
r
rθ
α
Vector, θ
Rotation
r
r
x = r cos α
θ
α
Vector, θ
Rotation
r
r
x = r cos αr cos(α+θ)
θ
α
Vector, θ
Rotation
r
r
x = r cos αr cos(α+θ)
θ
α
Vector, θ
x´= r cosαcosθ - r sinαsinθ
Rotation
r
r
x = r cos αr cos(α+θ)
θ
α
Vector, θ
x´= r cosαcosθ - r sinαsinθ = xcosθ - ysinθ
Rotation
r
r
x = r cos αr cos(α+θ)
θ
cos θ -sin θ sin θ cos θ
α
Vector, θ
x´= r cosαcosθ - r sinαsinθ = xcosθ - ysinθ
x y
Rotation
r
r
x = r cos αr cos(α+θ)
θ
cos θ -sin θ sin θ cos θ
cos θ -sin θ 0 sin θ cos θ 0 0 0 1
3D
α
Vector, θ
x´= r cosαcosθ - r sinαsinθ = xcosθ - ysinθ
x y
Rotation
Rotation
Rotation
Rotation
Rotation
Rotation
Rotation
Rotation
1.Translate to origin 2.Rotate 3.Translate back
Rotation
1.Translate to origin 2.Rotate 3.Translate back
Rotation
1.Translate to origin 2.Rotate 3.Translate back
Rotation
1.Translate to origin 2.Rotate 3.Translate back
Rotation
1.Translate to origin 2.Rotate 3.Translate back
Scale
Scale
Scale
Scale
Sx 0 0 0 0 Sy 0 0 0 0 Sz 0 0 0 0 1
Scale
Sx 0 0 0 0 Sy 0 0 0 0 Sz 0 0 0 0 1
Scale
Sx 0 0 0 0 Sy 0 0 0 0 Sz 0 0 0 0 1
Scale
Sx 0 0 0 0 Sy 0 0 0 0 Sz 0 0 0 0 1
Point, Sx, Sy, Sz
Translation
1 0 0 tx
0 1 0 ty
0 0 1 tz
0 0 0 1
x y z w
Homogeneous Coordinates
Projective Space
! n-dimensional projective space is constructed from an n+1-dimensional vector space ! Where, aP = P ! (0,0,0,…) is undefined
! The original space is a projection of this n+1-dimensional vector space ! x, y, z, w = x/w, y/w, z/w, 1 = affine space ! Except when w=0: x, y, z, 0 = x/z, y/z, 1, 0
! Except when z = 0 … ! w is called the homogeneous coordinate
Projective Plane
x
yw
w=1
x = uw, y = vw, ie, uw u vw = v w 1
Projective Plane
x
yw
w=1
x = uw, y = vw, ie, uw u vw = v w 1
2
664
1 0 0 dx
0 1 0 dy
0 0 1 dz
0 0 0 1
3
775
2
664
x
y
z
1
3
775=
2
664
x+ dx
x+ dy
z + dz
1
3
775
2
664
sx 0 0 0
0 sy 0 0
0 0 sz 0
0 0 0 1
3
775
2
664
x
y
z
1
3
775=
2
664
x.sx
x.sy
z.sz
1
3
775
2
664
cos↵ sin↵ 0 0
� sin↵ cos↵ 0 0
0 0 1 0
0 0 0 1
3
775
2
664
x
y
z
1
3
775=
2
664
x cos↵+ y sin↵
�x sin↵+ y cos↵
z
1
3
775
Examples of Xform Matrices
Translation by dx, dy, dz Scaling by sx, sy, sz
Rotation by α (about Z)
Change of Basis
! New basis: ! Orthonormal axes:
V1, V2, V3 ! Origin: O
2
664
— V1 — 0— V2 — 0— V3 — 0
0 0 0 1
3
775
2
664
1 0 0 �Ox
0 1 0 �Oy
0 0 1 �Oz
0 0 0 1
3
775
Rotation about Axis
19
u,v,w
ɵ
2
4x
0
y
0
z
0
3
5=
2
4u.u(1� cos ✓) + cos ✓ w.u(1� cos ✓)� w sin ✓ u.w(1� cos ✓)� v sin ✓
v.u(1� cos ✓) + w sin ✓ w.u(1� cos ✓) + cos ✓ v.w(1� cos ✓)� u sin ✓
w.u(1� cos ✓)� v sin ✓ w.u(1� cos ✓)� u sin ✓ w.w(1� cos ✓) + cos ✓
3
5
2
4x
y
z
3
5
Transformation Recap
! General transformation = ! Composition of known special transformations
! Transformation is equivalent to an inverse change of basis
! Be mindful of space in which ! the transformation is specified ! the vertex/vector is specified
! [M1 M2 M3 … Mk] V = ! Mk then Mk-1 until M1 in world-coordinates
! M1, then M2, until Mk in object-coordinates
Composition: Fixed frame
! T(1,0) ! R(90) ! R T p
Composition: Fixed frame
! T(1,0) ! R(90) ! R T p
Composition: Fixed frame
! T(1,0) ! R(90) ! R T p
Composition: Fixed frame
! T(1,0) ! R(90) ! R T p
Composition: Fixed frame
! T(1,0) ! R(90) ! R T p
Composition: Fixed frame
! T(1,0) ! R(90) ! R T p
Composition: Local frame
! T(1,0) ! R(90) ! R T p
Composition: Local frame
! T(1,0) ! R(90) ! R T p
Composition: Local frame
! T(1,0) ! R(90) ! R T p
Composition: Local frame
! T(1,0) ! R(90) ! R T p
Projection
! Projection = dimension reduction ! 3D " 2D in our case ! 3D lines map to 2D points ! Parallel
! Orthographic ! Oblique
! Projective
Perspective
! Transform plane of projection to Z = -1
Z = -1
Y
Z O
yy'
z
y'/-1 = y/z, or y' = -y/z and x' = -x/z
2
664
1 0 0 00 1 0 00 0 1 00 0 �1 1
3
7750
Perspective II
! Also normalize the image space ! Image X, Y lie in [-1:1]
Z = zv
y
ZO
zv tan(θ)
z
y' = zv y/z. Normalized y', call it y'' = y'/(zv tanθ) = y cotθ/z
θ FOV/2
Image Plane
y'
Perspective III
! What if we also want Z to lie in [0:1] ! Znear = 0
! Zfar = 1
Viewport
! What if we want ! X to lie in [0:Screen-Width] ! Y to lie in [0:Screen-Height]
! Xscreen = (Ximage + 1)/2 * Width
! Yscreen = (Yimage + 1)/2 * Height
Transformation of Normals
n
d
Transformation of Normals
n.d = 0
n
d
Transformation of Normals
n.d = 0nTd = 0
n
d
Transformation of Normals
n.d = 0nTd = 0=> n’T Md must be 0
n
d
Transformation of Normals
n.d = 0nTd = 0=> n’T Md must be 0=> (M’n)T Md = 0
n
d
Transformation of Normals
n.d = 0nTd = 0=> n’T Md must be 0=> (M’n)T Md = 0=> nTM’T Md = 0
n
d
Transformation of Normals
n.d = 0nTd = 0=> n’T Md must be 0=> (M’n)T Md = 0=> nTM’T Md = 0=> M’T M = k I
n
d
Transformation of Normals
n.d = 0nTd = 0=> n’T Md must be 0=> (M’n)T Md = 0=> nTM’T Md = 0=> M’T M = k I=> M’T = k M-1
n
d
Transformation of Normals
n.d = 0nTd = 0=> n’T Md must be 0=> (M’n)T Md = 0=> nTM’T Md = 0=> M’T M = k I=> M’T = k M-1
=> M’ = (M-1)T k = 1
n
d
3D Interaction
! Specify point (3 DOF) ! Specify orientation (3 DOF) ! Navigate (6 DOF)
! Manipulate object ! Manipulate eye/walk ! Specify path
! Select an object or region in space ! Find out its location and orientation
30
3D Interface! Mouse provide a 2D input (+ buttons)
! 2D location ! Up-down, Left-right motion ! 2D rotation
! 3D navigation has 6 DOF ! Up / Down ! Forward / Backward ! Left / Right ! Pitch (transverse axis) ! Yaw (normal axis) ! Roll (longitudinal axis)
31
cf Hoffman
Yaw
Pitch
Roll
Trackball Interface
! Inspect an object ! Imagine rotating a sphere by a handle
! Map planar 2D motion onto unit sphere ! Raise old position <x0, z0> to <x0, y0, z0>
! Raise new position <x1, z1> to <x1, y1, z1> ! Find rotation that moves handle from old to
new
Trackball
Trackball
! p1: x12+y1
2+z12 = 1
Trackball
! p1: x12+y1
2+z12 = 1
! p2: x22+y2
2+z22 = 1
Trackball
! p1: x12+y1
2+z12 = 1
! p2: x22+y2
2+z22 = 1
! Ball rotates about the axis formed by p1 and p2
! p1 X p2
Trackball
! p1: x12+y1
2+z12 = 1
! p2: x22+y2
2+z22 = 1
! Ball rotates about the axis formed by p1 and p2
! p1 X p2
! Rotation angle proportional to the distance p1p2
! Or simply ||p1 X p2||
Trackball
! p1: x12+y1
2+z12 = 1
! p2: x22+y2
2+z22 = 1
! Ball rotates about the axis formed by p1 and p2
! p1 X p2
! Rotation angle proportional to the distance p1p2
! Or simply ||p1 X p2||
! What happens if the object is not at the origin?
Rotation by Euler angles
34
Yaw
Pitch
Roll
Rotation by Euler angles
34
Yaw
Pitch
Roll
Gimbal Lock
Rotation by Euler angles
34
Yaw
Pitch
Roll
Gimbal Lock
! Interpolation, Composition is non-intuitive
Quaternions
! Relates two vectors ! Like a vector relates two points
! q = <s, x, y, z> = <s,v> = s + xi + yj + zk ! Scalar, Vector: relates length and orientation ! cos(θ/2) + U sin (θ/2)
Quaternion multiplication: q1q2 = ( s1s2 – v1.v2, s1v1 + s2v2 + v1 x v2) Quaternion inverse: If q = (s,v), then q-1 = (s,-v)/||q||2 Apply quaternion to p: P' = q P q-1
Quaternions
! Relates two vectors ! Like a vector relates two points
! q = <s, x, y, z> = <s,v> = s + xi + yj + zk ! Scalar, Vector: relates length and orientation ! cos(θ/2) + U sin (θ/2)
Quaternion multiplication: q1q2 = ( s1s2 – v1.v2, s1v1 + s2v2 + v1 x v2) Quaternion inverse: If q = (s,v), then q-1 = (s,-v)/||q||2 Apply quaternion to p: P' = q P q-1 , P = (0, p)
Quaternions
! Relates two vectors ! Like a vector relates two points
! q = <s, x, y, z> = <s,v> = s + xi + yj + zk ! Scalar, Vector: relates length and orientation ! cos(θ/2) + U sin (θ/2)
Quaternion multiplication: q1q2 = ( s1s2 – v1.v2, s1v1 + s2v2 + v1 x v2) Quaternion inverse: If q = (s,v), then q-1 = (s,-v)/||q||2 Apply quaternion to p: P' = q P q-1 (0, p') = , P = (0, p)
Quaternion Rotation
p' = s2p + v(p.v) + 2s(v x p) + v x (v x p)
36
Linear Interpolation
37
Lerp(t,a, b) = (1� t)a+ tb, 0 t 1
Linear
(Point a and b)
! Euclidean space interpolation:
Linear Interpolation
37
Lerp(t,a, b) = (1� t)a+ tb, 0 t 1
Linear
(Point a and b)
! Euclidean space interpolation:
! Spherical interpolation
Linear Interpolation
37
Lerp(t,a, b) = (1� t)a+ tb, 0 t 1
Slerp(t,a, b) =sin((1� t)✓)
sin ✓a+
sin(t✓)
sin ✓b, 0 t 1
LinearSpherical
(Point a and b)
(Vector a and b) θ = arccos(a.b)