You are on page 1of 186

Fundamentals of

Computer Graphics
COMP 557
9 January 2018

Paul Kry
“Computers are useless. They can
only give you answers”
Pablo Picasso
Art

Meats Meier
Visualization

[Li et al., 2007] NASA


Training

CM Labs, crane simulator

Flight simulation Appendectomy simulation (CAE LapVR )


Games
Movies
[R&H Demo Reel]
What is Computer Graphics?
• A vast field that encompasses pretty
much anything related to computer
generated images
• Modeling, rendering, animation, image
processing, visualization, interactive
techniques, etc.

[example assignments and projects from 557 and 559]


Computer Graphics
Mathematics made visible
Problems in Computer Graphics
• 2D imaging
• Compositing and layering
• Digital filtering
• Colour transformations
• 2D drawing
• Illustration, drafting
• Text
• GUIs
Problems in Computer Graphics
• 3D Modeling
• Representing 3D shapes
• Polygons, curved surfaces, …
• Procedural modeling
Problems in Computer Graphics
• 3D Rendering
• 2D views of 3D geometry
• Projection and perspective
• Removing hidden surfaces
• Lighting simulation
Problems in Computer Graphics
• User Interaction
• 2D graphical user interfaces
• 3D modeling interfaces
Problems in Computer Graphics
• Animation
• Keyframe animation
• Physical simulation

[Thürey et al. 2010] [Bridson et al. 2002]


Evolution of computing
environments
• Graphics has been a key to technology growth
• Graphical user interfaces
• Desktop publishing
• Visualization
• Gaming consoles

• Hardware revolution drives everything


• price/performance
improving exponentially
(Moore’s Law)
• Graphics processors on
even faster exponentials

[van Dam]
Original Macintosh New iMac 27”
Date 1984 2018 +34

Price $2500 $2500 x 1.0

x 425
CPU 8 MHz 3.4 GHz (Quad) (x 1700)

Memory
128KB RAM 8.0GB DDR3 SDRAM x 65536
(2 GB on GPU)
Storage 400KB Floppy 1TB Hard Disk x 2500000

Monitor 9” Black&White 27” Color x3


512 x 342 5120 x 2880 x 84
218 dpi x 3.2
68 dpi

Devices Mouse Mouse same


Keyboard Keyboard same

GUI Desktop WIMP Desktop WIMP same


[van Dam]
Graphics Processing Unit (GPU)
Not part of the previous comparison
CPU Transistor Counts from 1971 and Moore’s Law
10,000,000,000
Tesla K40

10-Core Xeon GTX680

1,000,000,000

100,000,000
Transistor count

Curve shows “Moore’s Law”:


10,000,000 transistor count doubling
every two years
1,000,000

100,000
GPUs now part of every machine,
10,000
arguably more powerful than CPUs
and programmable
2,300

1971 1980 1990 2000 2010 2020

Date of introduction
Today
• Research overview
• Introduction
• Course details
• Announcements
• Transforms
Who are you?
• 31 MSc Computer Science
• 41 BSc Physics Earth Math Computer Science
• 8 BEng Software, Computer, Mechanical
• 4 BA Computer Science
• 2 BSc Biol, Biomed & Life Sci CS & Bio
• 2 MEng Electrical
• 1 MA Digital Humanitites
• 2 PhD
Comp 557
• You will:
• explore fundamental ideas
• learn math essential to graphics
• implement key algorithms
• write cool programs
• You will not:
• Learn Photoshop, AutoCad, Maya, Renderman, etc.
• Become experts at OpenGL or DirectX
(but you will become comfortable with OpenGL)
• write huge programs
Prerequisites
(COMP206, COMP251, MATH223)
• Programming
• ability to read (understand), write, and debug small Java
programs (10s of classes)
• understanding of basic data structures
• no serious software design required
• Mathematics
• vector geometry (dot/cross products, etc.)
• linear algebra (matrices in 2D to 4D, linear systems)
• basic calculus (derivatives and integrals)
Topics
• Coordinates, Transformations, Projections
• Meshes, Curves, Smooth Surfaces, Subdivision
• Lighting, Texturing, Rendering
• Images, Sampling
• Color science
Textbook and Resources
• Computer Graphics Principles and Practice 3rd ed
• Other good resources
• OpenGL Programming Guide: The official guide to
learning OpenGL
• Fundamentals of Computer Graphics, 3rd ed., Peter
Shirley and Steve Marschner
• The Graphics Codex (mostly rendering focused)
• Additional papers will be posted to MyCourses
throughout the term
Textbook and Resources
• CGPP Chapter 10 Transforms in 2D
• CGPP Chapter 11 Transforms in 3D
• CGPP Chapter 13 Viewing and Projection

• RedBook Chapter 1 Introduction to OpenGL


• Useful to review… older versions will show immediate
mode, while more recent versions will use buffers and
drawArray calls
Evaluation
• Assignments 50%
• Midterm 20%
• In February, during class time
• Final 30%
In case you didn't already know...
McGill University values academic integrity. Therefore, all students must understand the
meaning and consequences of cheating, plagiarism and other academic offences under the
Code of Student Conduct and Disciplinary Procedures. See www.mcgill.ca/integrity for more
information, as well as www.mcgill.ca/integrity/studentguide, the Student Guide to Avoid
Plagiarism.

It should be noted that, in accordance with article 15 of the Charter of Students' Rights,
students may submit examination answers in either French or English.

According to Senate regulations, instructors are not permitted to make special arrangements
for final exams. Please consult the Calendar, section 4.7.2.1, General University Information
and Regulations at www.mcgill.ca. Special arrangements in emergencies may be requested
at your Student Affairs Office. If you have a disability, please advise the Office for Students
with Disabilities (398-6009) as early in the term as possible so that we can provide
appropriate accommodation to support your success.

In the event of circumstances beyond the instructor's control, the evaluation scheme as set
out in this document might require change. In such a case, every effort will be made to obtain
consensus agreement from the class.

Additional policies governing academic issues which affect students can be found in
the Handbook on Student Rights and Responsibilities, Charter of Students' Rights.
Assignments (four)
• Late Policy: 10% penalty, three days max
• Try “getting started” sample code now!
• Assignments, probably covering:
• Rotations and transform hierarchies
• Projections
• Mesh subdivision or simplification
• Ray tracing (with competition)
Getting started!
• Get started with Eclipse, JOGL
• http://cs.mcgill.ca/~kry/comp557W18/asetup/
• We will use vecmath, and some other jars too
• In class, will provide some introduction to OpenGL, java
bindings, and general graphics debugging strategies
• Need more help?
• TA contact info and office hours on My Courses
• Prof office hours 2-4 pm Wednesdays, or any other time
by email appointment
Today
• Research overview
• Introduction
• Course details
• Announcements
• Transforms
COMP 559 Fundamentals of Computer Animation
Computational techniques for generating animation

• Physically based simulation of Rigid


bodies, cloth, deformation, contact…
• Motion capture, reuse, retargeting,
motion graphs, control…
• Learn by doing! Four assignments
including a mini project.
• First class this afternoon!
Today
• Research overview
• Introduction
• Course details
• Announcements
• Transforms
Some math background
• Notation for sets, functions, mappings
• Linear transformations
• Matrices
• Matrix-vector multiplication
• Matrix-matrix multiplication
• Geometry of curves in 2D
• Implicit representation
• Explicit representation
Implicit Representations
• Equation to tell if we are on the curve
𝐯 𝑓(𝐯) = 0}
• Set always defines the boundary of a region,
assuming f is continuous
• Example: line (orthogonal to 𝐮, distance 𝑘 from 0)
𝐯 𝐯 ⋅ 𝐮 + 𝑘 = 0}
• Example: circle (center 𝐩, radius 𝑟)
𝐯 𝐯 − 𝐩 ⋅ 𝐯 − 𝐩 − 𝐫 2 = 0}
Explicit Representations
• Also called parametric
• Equation maps a domain into the plane
𝑓(𝑡) 𝑡 ∈ 𝐷}
• Variable 𝑡 is the “parameter”, a scalar
• Like tracing out the path of a particle over time
• Example: line (containing 𝐩, parallel to 𝐮)
𝐩 + 𝑡𝐮 𝑡 ∈ ℝ}
• Example: circle (center 𝐩, radius 𝑟)
cos 𝑡
𝐩+r | 𝑡 ∈ [0,2𝜋)
sin 𝑡
Transforming Geometry
• Move a set of points using a mapping 𝑇 from the
plane to itself
𝑆 → {𝑇(𝐯)|𝐯 ∈ 𝑆}
• Parametric representation:
𝑓(𝑡) 𝑡 ∈ 𝐷} → 𝑇 𝑓 𝑡 | 𝑡 ∈ 𝐷
• Implicit representation:
𝐯 𝑓(𝐯) = 0} → {𝑇 𝐯 𝑓 𝐯 = 0
= 𝐯 | 𝑓 𝑇 −1 𝐯 =0
Translation
• Simplest transformation: 𝑇(𝐯) = 𝐯 + 𝐮
• Inverse: 𝑇 −1 𝐯 = 𝐯 − 𝐮
• Consider transforming a circle
• Explicit form
• Implicit form
Linear Transformations
• One way to define a transformation is by matrix
multiplication:
𝑇(𝐯) = 𝑀𝐯
• Such transformations are linear, which means
𝑇(𝑎𝐮 + 𝐯) = 𝑎𝑇(𝐮) + 𝑇(𝐯)
• All linear transformations can be written by matrix
Geometry of 2D linear
Transformations
• 2x2 matrices have simple geometric interpretations
• Uniform scale
• Non-uniform scale
• Rotation
• Shear
• Reflection
• Let us look at examples of each of these
Composing Transformations
• Want to move an object, and move it some more?
𝐩 → 𝑇 𝐩 → 𝑆 𝑇 𝐩 = (𝑆 ∘ 𝑇)(𝐩)
• We need to represent 𝑆 ∘ 𝑇 (that is, S compose T)
• Would like to use the same representation as for S and T
• Translation is easy
𝑇 𝐩 = 𝐩 + 𝐮𝑇
𝑆 𝐩 = 𝐩 + 𝐮𝑆
𝑆 ∘ 𝑇 𝐩 = 𝐩 + 𝐮 𝑇 + 𝐮𝑆
• Translation by 𝐮 𝑇 then by 𝐮𝑆 is translation by 𝐮 𝑇 + 𝐮𝑆
Composing Transformations
• Linear transformations are also straightforward
𝑇 𝐩 = 𝑀𝑇 𝐩
𝑆 𝐩 = 𝑀𝑆 𝐩
𝑆 ∘ 𝑇 𝐩 = 𝑀𝑆 𝑀𝑇 𝐩
• Transforming first by 𝑀𝑇 then by 𝑀𝑆 is the same as
transforming by 𝑀𝑆 𝑀𝑇
• Note 𝑀𝑆 𝑀𝑇 or 𝑆 ∘ 𝑇 is always 𝑇 first, then 𝑆
• Only sometimes commutative!
• What commutes? What doesn’t?
• rotation with uniform scale commute
• non-uniform scale and non-uniform scale commute
• rotations and translations do not commute
Combining Linear with Translation
• Need to use both in a single framework
• Can represent sequence as 𝑇 𝐩 = 𝑀𝐩 + 𝐮
• Now consider composition
𝑇 𝐩 = 𝑀𝑇 𝐩 + 𝐮 𝑇
𝑆 𝐩 = 𝑀𝑆 𝐩 + 𝐮𝑆
𝑆 ∘ 𝑇 𝐩 = 𝑀𝑆 𝑀𝑇 𝐩 + 𝐮 𝑇 + 𝐮𝑆
= (𝑀𝑆 𝑀𝑇 )𝐩 + (𝑀𝑆 𝐮 𝑇 + 𝐮𝑆 )
• This will work, but it is a bit awkward
Homogeneous Coordinates
• Can representing transformations more elegantly
• Extra component 𝑤 for vectors, and an extra row
and column for matrices
• For points in affine space, we can keep 𝑤 = 1
• Linear transformations do not make use of the
extra row and column
𝑎 𝑏 0 𝑥 𝑎𝑥 + 𝑏𝑦
𝑐 𝑑 0 𝑦 = 𝑐𝑥 + 𝑑𝑦
0 0 1 1 1
Homogeneous Coordinates
• We represent translation using the extra column

1 0 𝑡 𝑥 𝑥+𝑡
0 1 𝑠 𝑦 = 𝑦+𝑠
0 0 1 1 1
Homogeneous Coordinates
• Composition works by 3-by-3 matrix multiplication
(note we use block matrix notation)
𝑀𝑆 𝐮𝑆 𝑀𝑇 𝐮 𝑇 𝐩
0 1 0 1 1
𝑀𝑆 𝑀𝑇 𝑀𝑆 𝐮 𝑇 + 𝐮𝑆 𝐩
=
0 1 1
• This is exactly the same as carrying around 𝑀 and 𝐮
• But cleaner
• And generalizes in useful ways as we will see later!
Review and more information
• Review
• CGPP Chapter 7 up to and not including 7.7
• Essential Math and Geometry of 2D and 3D
• CGPP Chapter 10 up to and including 10.10
• 2D transformations
• Points and Vectors
• Skip the SVD (10.3.8 and 10.3.9)
• Inverses of transformation matrices
• Coordinate Transformations
Transformations
COMP 557
11 January 2018

Paul Kry
Affine transformations
• The set of transformations we have been looking at,
linear transformations combined with translation,
is known as affine transformations
• Straight lines are preserved
• Parallel lines are preserved
• Ratios of lengths along lines are preserved (e.g., midpoints)
Affine transformation examples
in homogeneous coordinates
1 0 𝑡𝑥 1 0 2.15
0 1 𝑡𝑦 , for example, 0 1 0.85
0 0 1 0 0 1
• Translation
Affine transformation examples
in homogeneous coordinates
𝑠 0 0 1.5 0 0
0 𝑠 0 , for example, 0 1.5 0
0 0 1 0 0 1
• Uniform scale
Affine transformation examples
in homogeneous coordinates
𝑠𝑥 0 0 1.5 0 0
0 𝑠𝑦 0 , for example, 0 0.8 0
0 0 1 0 0 1
• Non-uniform scale
Affine transformation examples
in homogeneous coordinates
cos 𝜃 −sin 𝜃 0 0.866 −0.5 0
sin 𝜃 cos 𝜃 0 , for example, 0.5 0.866 0
0 0 1 0 0 1
• Rotation
Affine transformation examples
in homogeneous coordinates
−1 0 0
0 1 0
0 0 1
• Reflection (about y axis)
Affine transformation examples
in homogeneous coordinates
1 𝑎 0 1 0.5 0
0 1 0 , for example, 0 1 0
0 0 1 0 0 1
• Shear
General affine transformations
• The previous slides show “canonical” examples of
the different types of affine transformations
• In general, an affine transformations can be
different than any single canonical example
• Often define them as a product of canonical transforms
• Sometimes define them more directly based on desired
properties (e.g., creating a change of coordinates)
Composite affine transformation
• In general, non commutative: order matters!

rotate then translate translate then rotate


Rigid motions
• A transformation made up of only translation and
rotation is a rigid motion
• Also called a rigid body transformation
𝑅 𝐮
𝐸=
0 1
• The inverse is easy to write, because the rotation 𝑅
is orthonormal with 𝑅 −1 = 𝑅𝑇 , thus,
𝑇 𝑇
−1
𝐸 = 𝑅 −𝑅 𝐮
0 1
𝑅 𝑇 −𝑅𝑇 𝐮 𝑅 𝐮 𝑅 𝑇𝑅 𝑅𝑇 𝐮 − 𝑅𝑇 𝐮
𝐸 −1 𝐸 = =
0 1 0 1 0 1
Composing to change axes
• Want to rotate about a particular point in space?
• Easily done by composing transformations
• We know how to rotate about the origin
• Translate that point to origin, rotate, and translate back!

𝑀 = 𝑇 −1 𝑅 𝑇
Composing to change axes
• Want to scale along particular axis and point?
• We know how to scale about y axis at origin
• Suppose we want to stretch by 1.5 times in the direction
and at the point shown below

𝑀 = 𝑇 −1 𝑅 −1 𝑆 𝑅 𝑇
What are the contents of 𝑆 𝑅 and 𝑇?
Transforming points and vectors
• Note distinction between points and vectors
• Vectors are offsets (differences between points)
• Points have a location
• Represented by vector offset from a fixed origin
• Points and vectors transform differently
• Points respond to translation; vectors do not
• Consider
𝑣 = 𝑝−𝑞
𝑇 𝑥 = 𝑀𝑥 + 𝑡
Transforming points and vectors
𝑣 = 𝑝−𝑞
𝑇 𝑥 = 𝑀𝑥 + 𝑡
• 𝑇 is an affine transformation
• 𝑇 is not a linear transformation
𝑇 𝑥+𝑦 ≠𝑇 𝑥 + 𝑇 𝑦
=𝑀 𝑥+𝑦 +𝑡+𝑡

𝑇 𝑝 – 𝑇 𝑞 = 𝑀𝑝 + 𝑡 – 𝑀𝑞 + 𝑡
= 𝑀 𝑝−𝑞 + 𝑡−𝑡
= 𝑀𝑣
• Vectors are only transformed by linear part
Transforming points and vectors
• In homogeneous coordinates, vectors have 𝑤 = 0
• Translation of an affine transformation excluded for
vectors in homogeneous coordinates
𝑀 𝐭 𝐩 𝑀𝐩 + 𝐭
=
0 1 1 1
𝑀 𝐭 𝐯 𝑀𝐯
=
0 1 0 0
• Preview of what is to come
• Last coordinate need not always be 0 or 1
• Can change last row in matrix to do perspective projection
Coordinate systems
• Can express vectors with respect to different bases
• Linear transformation is a change of basis
• This leads to an alternate view
• We were previously thinking of modeling
transformations (e.g., “make it bigger”)
• Can instead think of them as a means of using different
reference frames, or different units (e.g., cm to inches)
Affine change of coordinates
• There are 6 degrees of freedom
𝑎1 𝑎2 𝑎3
𝐮 𝐯 𝐩
𝑎4 𝑎5 𝑎6 or
0 0 1
0 0 1
Affine change of coordinates
• Coordinate frame: point and a basis
• Interpretation is that the transformation changes
the representation of point (or vector) from one
basis to another
• Frame to canonical matrix has frame in columns
• Takes points in coordinates of the frame
• Gives points in coordinates of the canonical basis

𝐮 𝐯 𝐩
0 0 1
Affine change of coordinates
• A new way to “read off” the matrix
• e.g., shear from earlier
• Observe how the matrix columns correspond to the
change of shape

1 0.5 0
0 1 0
0 0 1
Affine change of coordinates
• When we move an object to the origin to apply a
transformation, we are really changing coordinates
• Easy to express the transformation in the object’s frame
• Define it there and transform it
𝑇𝑒 = 𝐹 𝑇𝐹 𝐹 −1
• 𝑇𝑒 is the transform expressed wrt 𝐞1 𝐞2 canonical basis
• 𝑇𝐹 is the transformation expressed in the natural frame
• 𝐹 is the frame-to-canonical matrix 𝐮 𝐯 𝐩 , here in
block form with homogeneous representation for basis
vectors 𝐮 and 𝐯 and origin point 𝐩
• This is a similarity transformation
Coordinate frame summary
• Frame is a point plus a basis
• Frame matrix (frame to canonical) is
𝐮 𝐯 𝐩
𝐹=
0 0 1
• Change coordinates of point 𝑎 to and from frame
by multiplying by 𝐹 and 𝐹 −1
𝑎𝑒 = 𝐹𝑎𝐹 𝑎𝐹 = 𝐹 −1 𝑎𝑒
• “Move” transformations using similarity transform
𝑇𝑒 = 𝐹 𝑇𝐹 𝐹 −1 𝑇𝐹 = 𝐹 −1 𝑇𝑒 𝐹
Classes of Transformations
• The set of 3x3 matrices we can use to transform
homogenous points have a variety of properties
• Most generally, they map lines to lines.
• These are called Homographies provided they are invertible
(this set include perspective projection)
• A more restrictive set also preserves parallel lines
• These are called Affine transforms
• A more restrictive set also preserves angles
• These are called Conformal
• A more restrictive set also preserves lengths
• These are called Rigid
• Where do translate, rotate, and scale fit?
Conformal (preserve angles)
uniform scale
Question

Give a homogeneous transformation matrix, or product of


matrices, that will transform triangle ABC to triangle A’B’C’.

25
Review and more information
• Review
• CGPP Chapter 7 up to and not including 7.7
• Essential Math and Geometry of 2D and 3D
• CGPP Chapter 10 up to and including 10.10
• 2D transformations
• Points and Vectors
• Skip the SVD (10.3.8 and 10.3.9)
• Inverses of transformation matrices
• Coordinate Transformations
3D Transformations
COMP557
Paul Kry

McGill COMP557 1
Transformations in OpenGL
Your Code
Geometry OpenGL
(points)

Transformation Matrix
(and many other things)
Window

2
Translation glTranslated(x,y,z)
𝑥′ 1 0 0 𝑡𝑥 𝑥
𝑦′ 0 1 0 𝑡𝑦 𝑦
=
𝑧′ 0 0 1 𝑡𝑧 𝑧
1 0 0 0 1 1

McGill COMP557 3
Based on slides by Steve Marschner
Scaling glScaled(x, y, z)
𝑥′ 𝑠𝑥 0 0 0 𝑥
𝑦′ 0 𝑠𝑦 0 0 𝑦
=
𝑧′ 0 0 𝑠𝑧 0 𝑧
1 0 0 0 1 1

McGill COMP557 4
Based on slides by Steve Marschner
Rotation glRotate(deg, x, y, z)
• For example, rotation about the 𝑧 axis
• glRotate( thetaDegrees, 0, 0, 1 )

𝑥′ cos 𝜃 − sin 𝜃 0 0 𝑥
𝑦′ sin 𝜃 cos 𝜃 0 0 𝑦
=
𝑧′ 0 0 1 0 𝑧
1 0 0 0 1 1

McGill COMP557 5
Based on slides by Steve Marschner
Rotation glRotate(deg, x, y, z)
• For example, rotation about the 𝑥 axis
• glRotate( thetaDegrees, 1, 0, 0 )

𝑥′ 1 0 0 0 𝑥
𝑦′ 0 cos 𝜃 − sin 𝜃 0 𝑦
=
𝑧′ 0 sin 𝜃 cos 𝜃 0 𝑧
1 0 0 0 1 1

McGill COMP557 6
Based on slides by Steve Marschner
Rotation glRotate(deg, x, y, z)
• For example, rotation about the 𝑦 axis
• glRotate( thetaDegrees, 0, 1, 0 )

𝑥′ cos 𝜃 0 sin 𝜃 0 𝑥
𝑦′ 0 1 0 0 𝑦
=
𝑧′ −sin 𝜃 0 cos 𝜃 0 𝑧
1 0 0 0 1 1

McGill COMP557 7
Based on slides by Steve Marschner
General rotations
• A rotation in 2D is around a point
• A rotation in 3D is around an axis
• so 3D rotation is w.r.t a line through origin, not just
about the origin point
• there are many more 3D rotations than 2D
In 3D, one way to specify a
Rotation is via a unit vector
(the axis) and an angle.
Convention: positive rotation
2D is CCW when vector points
3D points toward you.
McGill COMP557 8
Based on slides by Steve Marschner
What are 3D rotations?
More precisely…
3D Rotations are the set of linear transformations
{𝑅 ∈ ℝ3×3 ∶ 𝑅𝑇 = 𝑅−1 , det 𝑅 = +1}
which form a group with matrix multiplication as the binary group
operation (i.e., operation that combines two elements)
• Closed under the group operation
• Exist identity element (the identity matrix)
• Exist an inverse for every group element
• Associative, A(BC) = (AB)C
This group is the “special orthogonal group” SO(3). Each different
possible 3D orientation is specified only once in this group.

9
Based on slides by Steve Marschner
Specifying rotations
• In 2D, a rotation just has an angle
• How many Degrees Of Freedom, i.e., DOFs?
• 𝑅𝑇 = 𝑅−1 , det(𝑅) = +1 does not perhaps give much
intuition, but
• Knowing that an angle about an axis direction lets us
specify any rotation with 3 DOFs
• This is how you specify a rotation with glRotate in OpenGL

McGill COMP557 10
Based on slides by Steve Marschner
Specifying Rotations
Euler Angles
• Can specify a rotation matrix with Euler angles
• Stack up three coordinate axis rotations, for instance
𝑅 = 𝑅𝑥 𝜃𝑥 𝑅𝑦 𝜃𝑦 𝑅𝑧 𝜃𝑧
• Can see z rotation as being about the object frame
• Can see x rotation as being about the canonical frame
• The y rotation is about an intermediate frame
• Why not another order? (more on this shortly)
• Also, problem of gimbal lock !! (more on this shortly)

11
Based on slides by Steve Marschner
Euler’s Theorem
• Any two independent orthonormal coordinate
frames can be related by a sequence of rotations
(not more than three) about coordinate axes,
where no two successive rotations may be about
the same axis.
• Have 12 possible sequences!
XYZ XZY XYX XZX YXZ YZX
YXY YZY ZXY ZYX ZXZ ZYZ

• Given some Euler angles, not knowing the order or


if they map frame A to B or vice versa, then there is
24 possibilities!
12
Based on slides by Steve Marschner
Euler Angles
• There are no conventions, different orders in
different industries, and order is often
customizable (e.g., in software like Maya).
• There may be some practical differences between
orderings and the best sequence may depend on
what you are trying to accomplish.
• In situations where there
is a definite ground plane,
Euler angles can actually
be an intuitive representation
(e.g., roll pitch yaw of a
vehicle)
13
Based on slides by Steve Marschner
http://www.youtube.com/watch?v=zc8b2Jo7mno

Euler Angles – Gimbal Lock

14
Based on slides by Steve Marschner
Interpolating Euler Angles
• One can simply interpolate between the three values
independently
• This will result in the interpolation following a different
path depending on which of the 12 schemes you
choose
• This may or may not be a problem, depending on your
situation
• Interpolating near singularities is problematic
• Note: when interpolating angles, remember to check
for crossing the +180/-180 degree boundaries

Based on slides by Steve Marschner


Euler Angles - Summary
• Euler angles are used in a lot of applications, but they
tend to require some rather arbitrary decisions
• They also do not interpolate in a consistent way (but
this isn’t always bad)
• They suffer from Gimbal lock and related problems
• Two rotations are combined by multiplication, not by
addition of the Euler Angles!
• Conversion to/from a matrix requires several
trigonometry operations
• They are compact (requiring only 3 numbers)

Based on slides by Steve Marschner


Scene Graphs

17
Data structures with transforms
• Representing a drawing (“scene”)
• List of objects
• Transform for each object
• Can use minimal primitives: ellipse is transformed circle
• Transform applies to points of object

COMP557 McGill 18
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Example
• Can represent drawing with flat list
• But editing operations require updating many
transforms

COMP557 McGill 19
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Groups of objects
• Treat a set of objects as one
• Introduce new object type: group
• contains list of references to member objects
• This makes the model into a tree
• Interior nodes = groups
• Leaf nodes = objects
• Edges = membership of object in group

COMP557 McGill 20
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Example
• Add group as a new object type
• Lets the data structure reflect the drawing structure
• Enables high-level editing by changing just one node

COMP557 McGill 21
Based on slides by Steve Marschner

Based on slides by Steve Marschner


The Scene Graph (tree)
• A name given to various kinds of graph structures
(nodes connected together) used to represent
scenes
• Simplest form: tree
• Just saw this
• Every node has one parent
• Leaf nodes are identified
with objects in the scene

COMP557 McGill 22
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Concatenation and hierarchy
• Transforms associated with nodes or edges
• Each transform applies to all geometry below it
• want group transform to transform each member
• members already transformed—concatenate
• Frame transform for object is product of all
matrices along path from root
• Each object’s transform describes relationship between
its local coordinates and its group’s coordinates
• Frame-to-canonical transform is the result of
repeatedly changing coordinates from group to
containing group
COMP557 McGill 23
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Instances
• Simple idea: allow an object to be a member of
more than one group at once
• Transform different in each case
• Leads to linked copies
• Single editing operation changes all instances

COMP557 McGill 24
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Example
• Allow multiple references to nodes
• Reflects more of drawing structure
• Allows editing of repeated parts in one operation

COMP557 McGill 25
Based on slides by Steve Marschner

Based on slides by Steve Marschner


The Scene Graph (with instances)
• With instances, there is no more tree
• An object that is instanced multiple
times has more than one parent
• Transform tree becomes DAG
• Directed Acyclic Graph
• Group is not allowed to contain
itself, even indirectly
• Transforms still accumulate
along path from root
• Now paths from root to leaves
are identified with scene objects

COMP557 McGill 26
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Implementing a hierarchy
• Object-oriented language is convenient
• Define shapes and groups as derived from single class
abstract class Shape { class Square extends Shape {
void draw(); void draw() {
} // draw unit square
}
}

class Circle extends Shape {


void draw() {
// draw unit circle
}
}

COMP557 McGill 27
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Implementing traversal
• Pass a transform down the hierarchy
• before drawing, concatenate

abstract class Shape { class Group extends Shape {


void draw(Transform t_c); Transform t;
} ShapeList members;
void draw(Transform t_c) {
class Circle extends Shape { for (m in members) {
void draw(Transform t_c) { m.draw(t_c * t);
// draw t_c * unit circle }
} }
} }

class Square extends Shape {


void draw(Transform t_c) {
// draw t_c * unit square
}
}

28
COMP557 McGill
Based on slides by Steve Marschner Based on slides by Steve Marschner
Implementing traversal
In OpenGL*, transforms are kept on matrix stack, with push
and pop matrix commands allowing us to easily store copies
before we make modifications in different sub-trees.

abstract class DAGNode { class RotateX extends DAGNode {


List<DAGNode> children; double theta;
void display( GLAutoDrawable d) { void display( GLAutoDrawable d ) {
for ( DAGNode n : children ) { GL2 gl = drawable.getGL().getGL2();
n.display(d); gl.glPushMatrix();
} gl.glRotate( theta, 1, 0, 0 );
} super.display(d);
} gl.glPopMatrix();
}
}
class Cube extends DAGNode {
void display( GLAutoDrawable d ) {
glut.glutSolidCube(1);
super.display(d);
}
}

* Modern OpenGL no longer provides the matrix stack


COMP557 McGill 29
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Basic Scene Graph operations
• Editing a transformation
• Good to present usable UI
• Getting transform of object in canonical (world) frame
• Traverse path from root to leaf
• Grouping and ungrouping
• Can do these operations without moving anything
• Group: insert identity node
• Ungroup: remove node, push transform to children
• Re-parenting
• Move node from one parent to another
• Can do without altering position

COMP557 McGill 30
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Adding more than geometry
• Objects have properties besides shape
• Color, shading parameters
• Approximation parameters (e.g. precision of
subdividing curved surfaces into triangles)
• Behavior in response to user input
•…
• Setting properties for entire groups is useful
• Paint entire window green
• Many systems include some kind of property nodes
• In traversal they are read as, e.g., “set current color”
• Scene graphs also typically include cameras, lights,
background, and other information
COMP557 McGill 31
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Scene Graph variations
• Where transforms go
• in every node?
• on edges?
• in group nodes only?
• in special Transform nodes?
• Tree vs DAG
• Nodes for cameras and lights?

COMP557 McGill 32
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Based on slides by Steve Marschner
COMP557 McGill 34
Based on slides by Steve Marschner

Based on slides by Steve Marschner


COMP557 McGill 35
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Characters Design, Posing,
and Key Frame Animation
• Joints and bones, it is useful to separate joint transforms
from geometry (and geometry transforms)
• Joint types:
• Hinge / Rotary, a given axis about a given point
• Ball / Spherical, XYZ Euler, about a given point
• Free, e.g., both translation and XYZ Euler angles
• Bones / geometry:
• Cube, Sphere, Cylinder, Cone, etc… (see glut methods)
• Translated and scaled with respect to parent node!
• Combine shapes (e.g., capsules), different colours, etc.!

COMP557 McGill 36
Based on slides by Steve Marschner

Based on slides by Steve Marschner


Characters Design, Posing,
and Key Frame Animation
• What should be the root?
• A foot? Hips? Torso? Head?
• Want to expose parameters necessary to pose
• Typically only want to edit joint angles, while geometry
may remain fixed (e.g., bone lengths)
• Reasonable limits on parameters?
• Want meaningful poses produced by interpolation of
parameters (gimbal lock?)
• Many options for organizing DAG and classes!
Discuss!
COMP557 McGill 37
Based on slides by Steve Marschner

Based on slides by Steve Marschner


More on Rotations
Quaternions
Interpolation
Trackball Interaction

38
Group of Unit Quaternions
• Quaternions are like complex numbers, but with three
imaginary parts
𝑖𝑗𝑘 = 𝑖2 = 𝑗2 = 𝑘2 = −1
• Example multiplication:
𝐴 = 𝑎0 + 𝑎1𝑖 + 𝑎2𝑗 + 𝑎3𝑘
𝐵 = 𝑏0 + 𝑏1𝑖 + 𝑏2𝑗 + 𝑏3𝑘
𝐴𝐵 = 𝑎0𝑏0 – 𝑎1𝑏1 – 𝑎2𝑏2 – 𝑎3𝑏3 +
(𝑎0𝑏1 + 𝑎1𝑏0 + 𝑎2𝑏3 – 𝑎3𝑏2)𝑖 + …
The set of unit quaternions, combined with quaternion
multiplication as a binary operation, form a group which is
very similar to the rotation group SO(3), except that each 3D
orientation appears twice! It is a double covering.
39
Based on slides by Steve Marschner
Quaternions as Rotations
• Related to a rotation by q on unit length axis (x,y,z)
(𝑐, 𝑠𝑥, 𝑠𝑦, 𝑠𝑧) ≡ 𝑐 + 𝑠𝑥𝑖 + 𝑠𝑦𝑗 + 𝑠𝑧𝑘
𝑞 𝑞
where 𝑐 = cos and 𝑠 = sin
2 2
• Why q/2?
• This means that q and –q are same rotation
• The angle between two unit quaternions in 4D space is half the
angle between the 3D orientations that they represent
• Composition by multiplication (rules on previous slide)
• Inverse of unit quaternions similar to complex
conjugation
• If 𝑞 = (𝑤, 𝑥, 𝑦, 𝑧) is unit length, then 𝑞 −1 = (𝑤, −𝑥, −𝑦, −𝑧)
• Vector (𝑥, 𝑦, 𝑧) transforms as 𝑞𝑣𝑞 −1 where
𝑣 = 0 + 𝑖𝑥 + 𝑗𝑦 + 𝑘𝑧 and the result is the imaginary part
40
Based on slides by Steve Marschner
Questions

• Cost of composing rotations:


• Matrix? Quaternion?
• Cost of transforming vectors:
• Matrix? Quaternion?

41
Based on slides by Steve Marschner
Comparison of Rotation representations
• Rotation matrix
• 3x3 matrix with RTR = I and det(R) = +1
• Euler angles
• Used widely, often simple and convenient, also problematic
• Quaternion
• Compact storage, efficient and nice mathematical
properties…
• Axis and Angle
• Similar to quaternions
• Axis scaled by angle
• Save storage space!

42
Based on slides by Steve Marschner
Important issues for different
rotation representations
• Storage Numerical problems?
• How much memory? • Suppose we need to
compose hundreds or
• Composition thousands of
• Is it possible? transformations?
• What’s the cost? • Does this ever happen?
• What’s the cost of • What can go wrong?
transforming points and • How can we fix it?
vectors?
• Conversion between • Interpolation
representations? • Smoothly interpolating
between two rotations is
• Easy or Hard? important for animation!

43
Based on slides by Steve Marschner
Aside:
Linear Interpolation
• Linear interpolation between two points a and b
Lerp(t,a,b) = (1-t)a + (t)b
where t ranges from 0 to 1
• Note that the Lerp operation can be thought of as a
weighted average (convex)
• We can also write it in as an additive blend:
Lerp(t,a,b) = a + t(b-a)
• What happens if we use linear interpolation with
rotations in different representations?

44
Based on slides by Steve Marschner
Aside:
Spherical Linear Interpolation
• We define the spherical linear interpolation of two
unit vectors in N dimensional space as
sin 1 t q sin tq
Slerp(t , a, b) a b
sin q sin q
where : q cos 1 a b
• We can use this formula to smoothly interpolate
two arbitrary rotations represented as quaternions.
• Watch out if a dot b is negative!
• What happens in this case? How to fix this?

45
Based on slides by Steve Marschner
Interaction
• What is a good way to rotate an object you are
viewing on screen using a mouse?

46
Based on slides by Steve Marschner
Ken Shoemake, ARCBALL: A User
Interface for Specifying Three-
ArcBall / TrackBall Dimensional Orientation Using a
Mouse, Graphics Interface 1992.

• Imagine: ball centered on


the screen
MOUSE
DOWN • screen is at z=0 with axis
pointing out of the
screen
• Dragging the mouse from
MOUSE UP one screen point to
another rotates the point
on the ball.
• How do we compute the
• Fit specifies size of the ball, rotation?
radius = min screen dim / fit, ▪ Axis?
2 means just touching edges. ▪ Angle?
• Gain specifies a modification to • When mouse not on ball,
the computed arc angle. project onto ball. 4
Based on slides by Steve Marschner
7
Coming up with a rotation matrix
• We have seen matrices for coordinate axis rotations,
and we have seen formulas for quaternion and axis
angle to matrix on previous slides…
• What if we want rotation about some random axis? But
with an intuitive / constructive solution…
• Compute by composing elementary transforms
• transform rotation axis to align with x axis
• apply rotation
• inverse transform back into position
• Just as in 2D this can be interpreted as a similarity
transform

McGill COMP557 48
Based on slides by Steve Marschner
Building general rotations
• Using elementary transforms you need three
• translate axis to pass through origin
• rotate about y to get into x-y plane
• rotate about z to align with x axis
• Alternative: construct frame and change
coordinates
• choose p, u, v, w to be orthonormal frame with p and u
matching the rotation axis
• apply similarity transform T = F Rx(q) F–1

McGill COMP557 49
Based on slides by Steve Marschner
Orthonormal frames in 3D
• Useful tools for constructing transformations
• Recall rigid motions
• affine transforms with pure rotation
• columns (and rows) form right handed orthonormal
basis

McGill COMP557 50
Based on slides by Steve Marschner
Building 3D frames
• Given a vector a and a secondary vector b
• The u axis should be parallel to a; the u–v plane should
contain b
• u = u / ||u||
• w = u x b; w = w / ||w||
• v=wxu
• Given just a vector a
• The u axis should be parallel to a; don’t care about
orientation about that axis
• Same process but choose arbitrary b first
• Good choice is not near a, e.g., set smallest entry to 1

McGill COMP557 51
Based on slides by Steve Marschner
Building general rotations
• Alternative: construct frame and change
coordinates
• choose p, u, v, w to be orthonormal frame with p and u
matching the rotation axis
• apply similarity transform T = F Rx(q) F–1
• interpretation: move to x axis, rotate, move back
• interpretation: rewrite u-axis rotation in new
coordinates
• (each is equally valid)

McGill COMP557 52
Based on slides by Steve Marschner
Building transforms from points
• 2D affine transformations have 6 degrees of freedom (DOFs)
• this is the number of “knobs” we have to set to define one
• Therefore 6 constraints suffice to define the transformation
• Constrain point p maps to point q (2 constraints at once)
• three point constraints add up to constrain all 6 DOFs
(i.e., can map any triangle to any other triangle)
• 3D affine transformation has 12 degrees of freedom
• count them by looking at the matrix entries we’re allowed to
change
• Therefore 12 constraints suffice to define the
transformation
• in 3D, this is 4 point constraints
(i.e., can map any tetrahedron to any other tetrahedron)

McGill COMP557 53
Based on slides by Steve Marschner
Transforming normal vectors
• Transforming surface normals
• Differences of points (e.g., tangents) transform OK
• Normals are covectors, and do not transform the same
way

Use inverse transpose


to transform normals

54
McGill COMP557
Based on slides by Steve Marschner
Question

55
Other Questions

• Derive a matrix for a reflection in the plane with


normal 𝑛 going through the origin
• Derive a matrix for a reflection in the plane with
normal 𝑛 going through point 𝑝0.

56
Based on slides by Steve Marschner
Review and More Information
• Transforming covectors (normals) CGPP 10.12
• Transformations in 3D, Chapter 11, in particular:
• Euler angles and gimbal lock 11.2
• Quaternions 11.2.6
• Trackball and Arcball, 11.6
• CGPP 6.6, Hierarchical modeling using a scene
graph
• glPushMatrix and glPopMatrix
• Convenient but not in the latest spec
• http://www.davidbishop.org/oglmeta#old-vs
57
Based on slides by Steve Marschner
3D Viewing, Orthographic and
Perspective Projection
COMP557
Paul Kry

COMP557 McGill 1
Image order and Object order
• Image order: “backward” approach
– start from pixel
– ask what part of scene projects to pixel
– explicitly construct the ray corresponding to the pixel
• Object order: “forward” approach
– start from a point in 3D
– compute its projection into the image
• Matrix transformations critical for object order
approach (actually important for both)
– combines seamlessly with coordinate transformations
used to position camera and model
– ultimate goal: single matrix operation to map any 3D point
to its correct screen location.
COMP557 McGill 2
Based on slides by Steve Marschner
Image Order Approach to Viewing

[Durand]
• Ray generation produces rays, not points in scene
• Cast a ray at every pixel and find points of intersection
• This is called ray tracing

COMP557 McGill 3
Based on slides by Steve Marschner
Object Order Approach to Viewing

[Durand]
• Inverting the ray tracing process requires division for the perspective case
• Once triangle vertices are known in screen coordinates the triangle can be
filled in (rasterization… more on this later)

COMP557 McGill 4
Based on slides by Steve Marschner
Mathematics of projection
• Always work in eye coordinates
– assume eye point at 0 and plane perpendicular to z
• Orthographic case
– a simple projection: just toss out z
• Perspective case: scale diminishes with z
– and increases with d

COMP557 McGill 5
Based on slides by Steve Marschner
Pipeline of transformations
• Standard sequence of transforms

COMP557 McGill 6
Based on slides by Steve Marschner
Camera / Eye Coordinates
• We have discussed object to world transformations
• Need world to camera transformation
– In viewing, we typically know:
• Where the camera is
• What we want to look at
• Which way is up
– Need a rigid transformation
(rotation and translation)
– How many degrees of freedom?

[Toldo]
– How to compute it?
– In OpenGL we can use gluLookat

COMP557 McGill 7
Based on slides by Steve Marschner
“Lookat” Transformation
• uvn or uvw commonly used for xyz camera axes
• Compute transform from points e, l, and vector Vup
– e is the eye point, or view reference point
– l is the lookat point
• Separate the rotation R and translation T
– What order to compose? What is easiest?
– What is T?
– What is R?
• We want

COMP557 McGill 8
Based on slides by Steve Marschner
Pipeline of transformations
• Standard sequence of transforms

COMP557 McGill 9
Based on slides by Steve Marschner
Projection
• How to form an image by planar perspective projection?
In computer graphics,
projection plane is in front
of center of projection

[Source unknown]
COMP557 McGill 10
Based on slides by Steve Marschner
Parallel projection: orthographic

But lets start with something simpler… orthographic projection


to implement orthographic, just toss out z:

COMP557 McGill 11
Based on slides by Steve Marschner
Pipeline of transformations
• Standard sequence of transforms

COMP557 McGill 13
Based on slides by Steve Marschner
View volume: orthographic

COMP557 McGill 14
Based on slides by Steve Marschner
Viewing a cube of size 2
• Start by looking at a restricted case: the canonical view volume
• It is the cube [-1,1]3, viewed from the z direction
• Matrix to project it into a square image in [-1,1]2 is trivial:

COMP557 McGill 15
Based on slides by Steve Marschner
Viewing a cube of size 2
• To draw in image, need coordinates in pixel units
• Suppose nx pixels in x direction and ny pixels in y direction

1 ny – .5

–1 –.5
–1 1 –.5 nx – .5

Pixel size in canonical view volume is 2/nx by 2/ny


Center is at ½ pixel width and height away from (-1, -1)
i.e., (-1+1/nx, -1+1/ny) maps to (0,0) integer pixel location
(1-1/nx, 1-1/ny) maps to (nx-1,ny-1) integer pixel location

COMP557 McGill 16
Based on slides by Steve Marschner
Windowing transforms
• This transformation is worth generalizing
– take one axis-aligned rectangle or box to another
– a useful transformation chain

[Shirley3e f. 6-16; eq. 6-6]

COMP557 McGill 17
Based on slides by Steve Marschner
Viewport transformation
1 ny – .5

–1 –.5
–1 1 –.5 nx – .5

(-1+1/nx, -1+1/ny) maps to (0,0)


(1-1/nx, 1-1/ny) maps to (nx-1,ny-1)
COMP557 McGill 18
Based on slides by Steve Marschner
Viewport transformation
• In 3D, carry along z for the ride
– one extra row and column

COMP557 McGill 19
Based on slides by Steve Marschner
Orthographic projection
• First generalization: different view rectangle
– retain the minus-z view direction

– specify view by left, right, top, bottom (as in RT)


– also near, far

COMP557 McGill 20
Based on slides by Steve Marschner
Clipping planes
• Recall…
– Object-order rendering considers each object in turn
• i.e., forward rendering, rasterization
– Image-order rendering considers each pixel in turn
• i.e., backward rendering, ray tracing

• In object-order systems we always use at least two


clipping planes that further constrain the view volume
– near plane: parallel to view plane; things between it and the
viewpoint will not be rendered
– far plane: also parallel; things behind it will not be rendered
• These planes are:
– partly to remove unnecessary stuff (e.g., behind the camera)
– but really to constrain the range of depths (we’ll see why later)

COMP557 McGill 21
Based on slides by Steve Marschner
Orthographic projection
• We can implement this by mapping the view volume
to the canonical view volume.
• This is just a 3D windowing transformation!

COMP557 McGill 22
Based on slides by Steve Marschner
Camera and modeling matrices
• We worked out all the preceding transforms starting from
eye coordinates
– before we do any of this we need to transform into that space
• Transform from world (canonical) to eye space is
traditionally called the viewing matrix
– Easy for us to compute the matrix Fc which takes us from eye
space to canonical space, but here we use the inverse Fc–1
• Remember that geometry would originally have been in
the object’s local coordinates; transform into world
coordinates is called the modeling matrix, Mm
• Note some systems (e.g., OpenGL) combine the two into a
modelview matrix and just skip world coordinates

COMP557 McGill 23
Based on slides by Steve Marschner
Viewing transformation

the camera matrix rewrites all coordinates in eye space


COMP557 McGill 24
Based on slides by Steve Marschner
Orthographic transformation chain
• Start with coordinates in object’s local coordinates
• Transform into world coords (modeling transform, Mm)
• Transform into eye coords (camera xf., Mcam = Fc–1)
• Orthographic projection, Morth
• Viewport transform, Mvp

COMP557 McGill 25
Based on slides by Steve Marschner
Planar Perspective projection

With the Projection plane at distance d,


note the similar triangles:
• 𝑦’ is the foreshortened version of 𝑦,
that is, it is smaller by a factor −𝑑/𝑧
• W can think of dividing by negative
𝑧 so that 𝑦’ has the same sign as 𝑦!
COMP557 McGill 26
Based on slides by Steve Marschner
Homogeneous coordinates revisited
• Perspective requires division
– that is not part of affine transformations
– in affine, parallel lines stay parallel
• therefore no vanishing point
• therefore no rays converging on viewpoint
• “True” purpose of homogeneous coords:
projection

COMP557 McGill 27
Based on slides by Steve Marschner
Homogeneous coordinates revisited
• Introduced w = 1 coordinate as a placeholder

– used as a convenience for unifying translation with linear


• Can also allow arbitrary non-zero w

COMP557 McGill 28
Based on slides by Steve Marschner
Implications of w

• All scalar multiples of a 4-vector are equivalent


• When w is not zero, can divide by w
– therefore these points represent “normal” affine points
• When w is zero, it’s a point at infinity, i.e., a direction
– can think of this as the point where parallel lines intersect
– can also think of it as the vanishing point
COMP557 McGill 29
Based on slides by Steve Marschner
Perspective projection

to implement perspective, just move z to w:

COMP557 McGill 30
Based on slides by Steve Marschner
View volume: perspective

COMP557 McGill 31
Based on slides by Steve Marschner
View volume: perspective (clipped)

COMP557 McGill 32
Based on slides by Steve Marschner
Carrying depth through perspective
• Perspective has a varying denominator—can’t preserve depth!
• Compromise: preserve order, and depth on near and far planes

– that is, choose a and b so that z’(n) = n and z’(f) = f.

COMP557 McGill 33
Based on slides by Steve Marschner
Official perspective matrix
• Use near plane distance as the projection distance
• Let 𝑛 be negative and 𝑑 = – 𝑛
• Scale by –1 to have fewer minus signs
– The matrix is different but this does not change the
projective transformation
Potential Confusion:
In OpenGL the near and
far plane are specified as
positive numbers giving
the distance along the
negative z axis!
COMP557 McGill 34
Based on slides by Steve Marschner
Questions
• Questions to better understand the action of a 4x4
homogenous planar perspective transformation matrix
– What happens to lines through the origin ?
• They become parallel
– What happens to vectors (x,y,z,0) ?
• They map to plane z=near+far
– What happens to points on plane with z normal at center of projection ?
• Map to points at infinity (all vectors come from the homogeneous
representations of points on this plane at center of projection)
– What happens to points at z = (near+far)/2 ?
• (n*n+f*f) / (n+f), that is, z is not preserved !! However, order is!
(has implications for z depth precision)
– What happens to points behind the camera ?
• It goes in front of the camera
COMP557 McGill 35
Based on slides by Steve Marschner
Aside: What goes where?

This has implications for clipping (i.e., discarding) geometry!


COMP557 McGill 36
Based on slides by Steve Marschner
Perspective projection matrix
• Need perspective projection to produce points in the
canonical view volume, so combine with an
orthographic projection matrix (windowing transform)

Is n-f a bug?
Is it not f-n ??
Answer:
The convention is that -near
maps to -1 and -far maps to 1.
Subtle detail, confusing, yes!
COMP557 McGill 37
Based on slides by Steve Marschner
Perspective transformation chain
• Transform into world coords (modeling transform, Mm)
• Transform into eye coords (camera xf., Mcam = Fc–1)
• Perspective matrix, P
• Orthographic projection, Morth
• Viewport transform, Mvp

COMP557 McGill 38
Based on slides by Steve Marschner
OpenGL view frustum: orthographic

Note OpenGL puts the near and far planes at –n and –f


so the user should give positive numbers

COMP557 McGill 39
Based on slides by Steve Marschner
OpenGL view frustum: perspective

Note OpenGL puts the near and far planes at –n and –f


so that the user should give positive numbers
gluPerspective(fovy, aspect, near, far)
glFrustum( left, right, bottom, top, near, far )

Defined on near plane


COMP557 McGill 40
Based on slides by Steve Marschner
Frustum applications
• Shifted perspective
• Tiled rendering (e.g., render very high
resolutions)
• 3D viewing (i.e., left eye right eye)
• Depth of field (i.e., accumulating multiple
render passes)

COMP557 McGill 41
Based on slides by Steve Marschner
COMP557 McGill 42
Based on slides by Steve Marschner
COMP557 McGill 43
Based on slides by Steve Marschner
COMP557 McGill 44
Based on slides by Steve Marschner
COMP557 McGill 45
Based on slides by Steve Marschner
Review and More Information
• CAPP Section 10.7
– Windowing Transformations
• CAPP Chapter 13
– View Specification 13.3
– Perspective 13.6
– Orthographic cameras 13.8

COMP557 McGill 46
Based on slides by Steve Marschner
Review and more information
• FCG chapter 7
– Viewing and projection

COMP557 McGill 47
Based on slides by Steve Marschner

You might also like