Introduction To Computer Graphics

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Introduction to Computer Graphics

Lecture
Lecture 33
Mathematical
Mathematical Primitives
Primitives
Introduction
Introduction to
to Transformations
Transformations
Triangle Arithmetic
Consider
Consider aa triangle,
triangle, (a,
(a, b,
b, c)
c)
b
•• a,b,c
a,b,c == (x,y,z)
(x,y,z) tuples
tuples
a c

Surface
Surface area
area == ssaa == ½
½ ** ||(b
||(b –a)
–a) X
X (c-a)||
(c-a)||

Unit
Unit normal
normal == (1/2s
(1/2saa)) ** (b-a)
(b-a) X
X (c-a)
(c-a)
Vector Spaces: Basis Vectors
Given
Given aa basis
basis for
for aa vector
vector space:
space:
•• Each
Each vector
vector in
in the
the space
space is
is aa unique
unique linear
linear combination
combination of
of the
the basis
basis
vectors
vectors
•• The
The coordinates
coordinates of
of aa vector
vector are
are the
the scalars
scalars from
from this
this linear
linear
combination
combination
•• IfIf basis
basis vectors
vectors are
are orthogonal
orthogonal and
and unit
unit length:
length:
–– Vectors
Vectors comprise
comprise orthonormal
orthonormal basis
basis
•• Best-known
Best-known example:
example: Cartesian
Cartesian coordinates
coordinates
•• Note
Note that
that aa given
given vector
vector vv will
will have
have different
different coordinates
coordinates for
for different
different
bases
bases
Matrices
By
By convention,
convention, matrixmatrix  M11 M12  M1n 
 M21 M2n 
element
element M Mrcrc is
is located
located at
at M22 
M
row
row rr and
and column
column c: c:      
 
Mm1 Mm2  Mmn 

 v1  By By (OpenGL)
(OpenGL)

v   v 2  convention,
convention, vectors
vectors
 v 3  are
are columns
columns
Matrices
Matrix-vector
Matrix-vector multiplication
multiplication applies
applies aa linear
linear
transformation
transformation to
to aa vector:
vector:
 M11 M12 M13   vx   a 
M  v  M 21 M 22 M 23  vy   b 
M 31 M 32 M 33  vz   c 

•• Why
Why do
do we
we call
call this
this aa linear
linear transformation?
transformation?
Recall
Recall how
how to
to do
do matrix
matrix multiplication?
multiplication?
Matrix Transformations
A
A sequence
sequence or
or composition
composition of of linear
linear transformations
transformations
corresponds
corresponds to
to the
the product
product of of the
the corresponding
corresponding
matrices
matrices M 1M 2 M 3vold  vnew
•• Note:
Note: the
the matrices
matrices to
to the
the right
right affect
affect vector
vector first
first
•• Note:
Note: order
order of
of matrices
matrices matters!
matters!
The
The identity
identity matrix
matrix II has
has no
no effect
effect in
in multiplication
multiplication
Some
Some (not
(not all)
all) matrices
matrices have
have an an inverse:
inverse:
M 1  M  v    v
Matrix Transformations
A
A linear
linear transformation
transformation::
•• Maps
Maps one
one vector
vector to
to another
another
•• Preserves
Preserves linear
linear combinations
combinations
Thus
Thus behavior
behavior of
of linear
linear transformation
transformation is is completely
completely
determined
determined by
by what
what itit does
does to
to aa basis
basis
Turns
Turns out
out any
any linear
linear transform
transform can
can be
be represented
represented by
by
aa matrix
matrix
Matrix Transformations
•• We
We hypothesize
hypothesize that
that all
all necessary
necessary linear
linear transformations
transformations can
can
be
be accomplished
accomplished with
with matrix
matrix multiplication
multiplication
•• Let’s
Let’s look
look at
at aa few
few
–– Scaling
Scaling
–– Rotation
Rotation
–– Translation?
Translation?
Scaling
Scaling
Scaling aa coordinate
coordinate means
means multiplying
multiplying each
each of
of its
its
components
components by by aa scalar
scalar
Uniform
Uniform scaling
scaling means
means this
this scalar
scalar is
is the
the same
same for
for all
all
components:
components:

2
Scaling
Non-uniform
Non-uniform scaling
scaling:: different
different scalars
scalars per
per component:
component:

X  2,
Y  0.5

How
How can
can we
we represent
represent this
this in
in matrix
matrix form?
form?
Scaling

Scaling
Scaling operation:
operation:  x ' ax 
 y '  by 
   

Or,
Or, in
in matrix
matrix form:
 x '   a 0  x 
 y '   0
form:
  
b  y 
  
scaling matrix
2-D Rotation
(x’, y’)

(x, y)

x’ = x cos() - y sin()
 y’ = x sin() + y cos()
2-D Rotation
x = r cos ()
y = r sin ()
x’ = r cos ( + )
y’ = r sin ( + )
(x’, y’)
Trig Identity…
(x, y) x’ = r cos() cos() – r sin() sin()
y’ = r sin() sin() + r cos() cos()

 
Substitute…
x’ = x cos() - y sin()
y’ = x sin() + y cos()
2-D Rotation
This
This is
is easy
easy to
to capture
capture in
in matrix
matrix form:
form:

 x' cos   sin     x 


 y '   sin   cos    y 
  
Even
Even though sin()) and
though sin( cos()) are
and cos( are nonlinear
nonlinear functions
functions
of ,,
of
•• x’
x’ is
is aa linear
linear combination
combination of
of xx and
and yy
•• y’
y’ is
is aa linear
linear combination
combination of
of xx and
and yy
Translation
What
What can
can you
you do
do with
with aa 2x2
2x2 matrix
matrix transformation?
transformation?

a b   x   x'
 c d   y    y '
    
Nothing
Nothing corresponds
corresponds to
to translation…
translation…
Homogeneous Coordinates
Homogeneous
Homogeneous coordinates
coordinates
 x
•• represent
represent coordinates
coordinates inin 22  x  homogeneous coords  
dimensions
dimensions with
with aa 3-vector
3-vector  y       y 
   1 

Homogeneous
Homogeneous coordinates
coordinates seem
seem unintuitive,
unintuitive, but
but they
they
make
make graphics
graphics operations
operations much
much easier
easier
Homogeneous Coordinates
Our
Our transformation
transformation matrices
matrices are
are now
now 3x3:
3x3:

cos( )  sin( ) 0

Rotation   sin( ) cos( ) 0
 0 0 1

 a 0 0

Scale  0 b 0 
0 0 1
Homogeneous Coordinates
Q:
Q: How
How can
can we
we represent
represent translation
translation as
as aa 3x3
3x3 matrix?
matrix?
A:
A: Using
Using the
the rightmost
rightmost column:
column:
1 0 Tx 
 
Translation  0 1 Ty 
0 0 1 

Translation
Example
Example of
of translation
translation 1 0 Tx   x   x  Tx 
    
 0 1 T y y
   y  T y
0 0 1   1   1 
 

Tx = 2
Ty = 1
Total Picture
Think
Think about
about what
what happens
happens with
with
manipulating
manipulating transformation
transformation a b c
matrix
matrix
d e f 

•• a,
a, ee == negative?
negative?
•• Changing
Changing dd and
and e?
e?
•• Changing
Changing ii !=
!= 1?
1?  g h i 
Composing Transformations
What
What ifif we
we want
want to
to scale
scale and
and rotate
rotate and
and translate?
translate?
•• We
We can
can execute
execute transformations
transformations one
one after
after the
the other
other
•• Ex:
Ex: Rotate
Rotate line
line segment
segment by
by 45
45 degrees
degrees about
about endpoint
endpoint aa

a a
Multiplication Order – Wrong Way
Our
Our line
line is
is defined
defined by
by two
two endpoints
endpoints
•• Applying
Applying aa rotation
rotation of
of 45
45 degrees,
degrees, R(45),
R(45), affects
affects both
both points
points
•• We
We could
could try
try to
to translate
translate both
both endpoints
endpoints to
to return
return endpoint
endpoint aa to
to its
its
original
original position,
position, but
but by
by how
how much?
much?

T(-3), R(45), T(3)


a
a
Wrong Correct
R(45) T(-3) R(45) T(3)
Multuplication Order - Correct
Isolate
Isolate endpoint
endpoint aa from
from rotation
rotation effects
effects a

•• First
First translate
translate line
line so
so aa is
is at
at origin:
origin: TT (-3)
(-3)
a

•• Then
Then rotate
rotate line
line 45
45 degrees:
degrees: R(45)
R(45)
a

•• Then
Then translate
translate back
back so
so aa is
is where
where itit was:
was: T(3)
T(3)
a
Compositing Matrices
Will
Will this
this sequence
sequence of
of operations
operations work?
work?
1 0  3 cos(45)  sin(45) 0 1 0 3  a x   a' x 
0 1 0   sin(45) cos(45) 0 0 1 0 a   a ' 
    y   y 
0 0 1   0 0 1 0 0 1  1   1 

Result
Result of
of first
first multiply
multiply with
with vector
vector
1 0 3  a x  a x  3
0 1 0   a    a 
  y   y 
0 0 1  1   1 
Compositing Matrices
Order
Order of
of multiplication
multiplication matters
matters

Short
Short answer:
answer: the
the transformations,
transformations, in
in order,
order, are
are
written
written from
from right
right to
to left
left
•• In
In other
other words,
words, the
the first
first matrix
matrix to
to affect
affect the
the vector
vector goes
goes next
next
to
to the
the vector,
vector, the
the second
second next
next to
to the
the first,
first, etc.
etc.
Compositing Matrices
After
After correctly
correctly ordering
ordering the
the matrices
matrices
Multiply
Multiply matrices
matrices together
together
What
What results
results is
is one
one matrix
matrix –– store
store it!
it!
Multiply
Multiply this
this matrix
matrix by
by the
the vector
vector of
of each
each vertex
vertex
All
All vertices
vertices easily
easily trasformed
trasformed with
with one
one matrix
matrix
multiply
multiply

You might also like