You are on page 1of 30

Introduction to Computer Graphics

Assignment
Assignment 11
3D
3D Primitives
Primitives and
and Transformations
Transformations
OpenGL: Front/Back Rendering
Each
Each polygon
polygon has
has two
two sides,
sides, front
front and
and back
back
OpenGL
OpenGL can
can render
render the
the two
two differently
differently
The
The ordering
ordering of
of vertices
vertices in
in the
the list
list determines
determines which
which
is
is the
the front
front side:
side:
•• When
When looking
looking at
at the
the front
front side,
side, the
the vertices
vertices go
go
counterclockwise
counterclockwise
––This
This is
is basically
basically the
the right-hand
right-hand rule
rule
––Note
Note that
that this
this still
still holds
holds after
after perspective
perspective projection
projection
OpenGL: Specifying Geometry
Geometry
Geometry in
in OpenGL
OpenGL consists
consists of
of aa list
list of
of vertices
vertices in
in
between
between calls
calls to glBegin() and
to glBegin() and glEnd()
glEnd()
•• A
A simple
simple example:
example: telling
telling GL
GL to
to render
render aa triangle
triangle
glBegin(GL_POLYGON);
glBegin(GL_POLYGON);
glVertex3f(x1,
glVertex3f(x1, y1,
y1, z1);
z1);
glVertex3f(x2,
glVertex3f(x2, y2,
y2, z2);
z2);
glVertex3f(x3,
glVertex3f(x3, y3,
y3, z3);
z3);
glEnd();
glEnd();
•• Usage: glBegin(geomtype
Usage: glBegin( geomtype)) where
where geomtype
geomtype is:
is:
–– Points,
Points, lines,
lines, polygons,
polygons, triangles,
triangles, quadrilaterals,
quadrilaterals, etc...
etc...
OpenGL: Drawing Triangles
You
You can
can draw
draw multiple
multiple triangles
triangles between
between
glBegin(GL_TRIANGLES) and
glBegin(GL_TRIANGLES) glEnd()::
and glEnd()
float
float v1[3],
v1[3], v2[3],
v2[3], v3[3],
v3[3], v4[3];
v4[3];
...
...
glBegin(GL_TRIANGLES);
glBegin(GL_TRIANGLES);
glVertex3fv(v1);
glVertex3fv(v1); glVertex3fv(v2);
glVertex3fv(v2); glVertex3fv(v3);
glVertex3fv(v3);
glVertex3fv(v1);
glVertex3fv(v1); glVertex3fv(v3);
glVertex3fv(v3); glVertex3fv(v4);
glVertex3fv(v4);
glEnd();
glEnd();

Each
Each set
set of
of 33 vertices
vertices forms
forms aa triangle
triangle
•• How
How much
much redundant
redundant computation
computation is
is happening?
happening?
OpenGL: Triangle Strips
An
An OpenGL
OpenGL triangle
triangle strip
strip primitive
primitive reduces
reduces this
this redundancy
redundancy by
by sharing
sharing vertices:
vertices:
glBegin(GL_TRIANGLE_STRIP);
glBegin(GL_TRIANGLE_STRIP);

glVertex3fv(v0);
glVertex3fv(v0);

glVertex3fv(v1);
glVertex3fv(v1);

glVertex3fv(v2);
glVertex3fv(v2);
v2 v4
glVertex3fv(v3);
glVertex3fv(v3); v0
glVertex3fv(v4);
glVertex3fv(v4);

glVertex3fv(v5);
glVertex3fv(v5);
v5
glEnd();
glEnd();

•• triangle
triangle 00 is
is v0,
v0, v1,
v1, v2
v2 v1
•• triangle
triangle 11 is
is v2,
v2, v1,
v1, v3
v3 ((why
why not
not v1,
v1, v2,
v2, v3?
v3?)) v3
•• triangle
triangle 22 is
is v2,
v2, v3,
v3, v4
v4
•• triangle
triangle 33 is
is v4,
v4, v3,
v3, v5
v5 ((again,
again, why
why not
not v3,
v3, v4,
v4, v5
v5))
OpenGL: More Examples
Example:
Example: GL
GL supports
supports quadrilaterals:
quadrilaterals:
glBegin(GL_QUADS);
glBegin(GL_QUADS);
glVertex3f(-1,
glVertex3f(-1, 1,
1, 0);
0);
glVertex3f(-1,
glVertex3f(-1, -1,
-1, 0);
0);
glVertex3f(1,
glVertex3f(1, -1,
-1, 0);
0);
glVertex3f(1,
glVertex3f(1, 1,
1, 0);
0);
glEnd();
glEnd();
•• This
This type
type of
of operation
operation is
is called
called immediate-mode
immediate-mode rendering
rendering;;
each
each command
command happens
happens immediately
immediately
Primitive Types
GL_POINTS
GL_POINTS
GL_LINE
GL_LINE
•• {S
{S || _STRIP
_STRIP || _LOOP}
_LOOP}
GL_TRIANGLE
GL_TRIANGLE
•• {S
{S || _STRIP
_STRIP || _FAN}
_FAN}
GL_QUAD
GL_QUAD
•• {S
{S || _STRIP}
_STRIP}
GL_POLYGON
GL_POLYGON
GL_POLYGON
List
List of
of vertices
vertices defines
defines polygon
polygon edges
edges
Polygon
Polygon must
must be
be convex
convex
Non-planar Polygons
Imagine
Imagine polygon
polygon with
with non-planar
non-planar vertices
vertices
Some
Some perspectives
perspectives will
will be
be rendered
rendered as
as concave
concave
polygons
polygons
These
These concave
concave polygons
polygons may
may not
not rasterize
rasterize
correctly
correctly
Single Buffering
•• ‘‘Buffer’
Buffer’ corresponds
corresponds to
to frame
frame buffer
buffer
•• ‘‘Single’
Single’ corresponds
corresponds to
to there
there being
being just
just one
one
•• Place
Place where
where you
you write
write pixel
pixel color
color values
values is
is same
same place
place
graphics
graphics hardware
hardware goes
goes to
to read
read color
color values
values for
for display
display
•• Can
Can draw
draw on
on top
top of
of existing
existing image
image
–– Might
Might be
be faster
faster than
than redrawing
redrawing whole
whole image
image
•• Can
Can also
also lead
lead to
to flickering
flickering and
and half-drawn
half-drawn images
images
Double Buffering
•• OpenGL
OpenGL writes
writes raster
raster image
image to
to separate
separate buffer
buffer from
from that
that read
read for
for
display
display
•• ‘‘front’
front’ buffer
buffer is
is for
for video
video signal
signal
•• ‘‘back’
back’ buffer
buffer is
is the
the drawing
drawing target
target
•• After
After each
each frame
frame is
is drawn,
drawn, the
the front
front and
and back
back buffers
buffers swap
swap
–– glxSwapBuffers
glxSwapBuffers (Display
(Display *dpy,
*dpy, Window,
Window, w)
w)
–– glutSwapBuffers
glutSwapBuffers (void)
(void)
•• Avoids
Avoids displaying
displaying partially
partially rendered
rendered frame
frame buffer
buffer
•• Avoids
Avoids flickering
flickering
Setting up the OpenGL Window
Must
Must clear
clear the
the ‘canvas’
‘canvas’ before
before drawing
drawing
•• glClearColor
glClearColor (r,
(r, g, b, ))
g, b,
–– Specify
Specify the
the color
color to
to initialize
initialize canvas
canvas to
to
–– Typically set  to
Typically set to 0.0
0.0 to
to make
make fully
fully transparent
transparent
–– This
This is
is aa state
state variable
variable and
and can
can only
only be
be called
called once
once
•• glClear(GL_COLOR_BUFFER_BIT)
glClear(GL_COLOR_BUFFER_BIT)
–– Actually
Actually clear
clear the
the screen
screen
–– glClear
glClear could
could also
also clear
clear the
the GL_DEPTH_BUFFER_BIT
GL_DEPTH_BUFFER_BIT
–– We’re
We’re not
not worried
worried about
about that
that now
now
Scaling and Translation in 3-D
r 0 0 0
0 s 0 0

0 0 t 0
 
0 0 0 1
Pretty
Pretty much
much just
just like
like 2-D
2-D
•• Just
Just add
add on
on the
the zz dimension
dimension to
to
Scaling
Scaling
everything
everything 0 x 
1 0
0 1 0 y 

0 0 1 z 
 
0 0 0 1

Translation
Translation
Rotation in 3-D
1 0 0 0
Looks
Looks similar
similar to
to 2-D
2-D Case
Case 0 cos   sin  0
Rotationx  
•• Specify
Specify arbitrray
arbitrray rotation
rotation as
as 0 sin  cos  0
 
three
three angles
angles 0 0 0 1

–– One
One per
per coordinate
coordinate axis
axis  cos 0 sin  0
 0 1 0 0
Rotationy  
–– Called
Called an
an Euler
Euler Angle
Angle  sin  0 cos  0
 
 0 0 0 1
–– Common
Common representation
representation
–– But cos   sin  0 0
But order
order of
of rotations
rotations matters
matters  sin  cos  0 0
Rotationz  
 0 0 1 0
 
 0 0 0 1
3-D Rotation
General
General rotations
rotations in in 3-D
3-D require
require rotating
rotating about
about an
an
arbitrary
arbitrary axis
axis ofof rotation
rotation
Deriving
Deriving the
the rotation
rotation matrix
matrix for
for such
such aa rotation
rotation
directly
directly is
is aa good
good exercise
exercise in
in linear
linear algebra
algebra
Standard
Standard approach:
approach: express
express general
general rotation
rotation as
as
composition
composition of of canonical
canonical rotations
rotations
•• Rotations
Rotations about
about X,
X, Y,
Y, ZZ
Composing Canonical Rotations
Goal:
Goal: rotate
rotate about
about arbitrary
arbitrary vector
vector A by 
A by
•• Idea:
Idea: we
we know
know how
how to
to rotate
rotate about
about X
X,,Y
Y,,ZZ

––So,
So, rotate
rotate about
about Y by  until
Y by until A
A lies
lies in
in the
the YZ
YZ plane
plane
––Then
Then rotate
rotate about
about X by  until
X by until A
A coincides
coincides with
with ++ZZ
––Then
Then rotate
rotate about by 
about ZZ by 
––Then
Then reverse
reverse the
the rotation
rotation about
about X (by --))
X (by
––Then
Then reverse
reverse the
the rotation
rotation about
about Y (by --))
Y (by
Composing Canonical Rotations
First:
First: rotating
rotating about
about Y by  until
Y by until A
A lies
lies in
in YZ
YZ

How
How exactly
exactly do
do we calculate ??
we calculate
•• Project
Project A
A onto
onto XZ
XZ plane
plane (Throw
(Throw away
away y-coordinate)
y-coordinate)
•• Find angle  to
Find angle to X
X::
 == -(90°
-(90° -- )) ==  -- 90
90 °°

Second:
Second: rotating
rotating about
about X by  until
X by until A
A lies
lies on
on ZZ
How
How do
do we calculate ??
we calculate
Composing Canonical Rotations
Why
Why are
are we
we slogging
slogging through
through all
all this
this tedium?
tedium?
A1:
A1: Because
Because you’ll
you’ll have
have to
to do
do itit on
on the
the test
test
A2:
A2: Because
Because itit will
will help
help with
with the
the assignment
assignment
•• Computing
Computing intersection
intersection between
between aa parabola
parabola and
and ground
ground
plane
plane
y+

dist = ?
3-D Rotation Matrices
So
So an
an arbitrary
arbitrary rotation
rotation about
about AA composes
composes several
several
canonical
canonical rotations
rotations together
together
We
We can
can express
express each
each rotation
rotation as
as aa matrix
matrix
Composing
Composing transforms
transforms ==
== multiplying
multiplying matrices
matrices
Thus
Thus we
we can
can express
express the
the final
final rotation
rotation as
as the
the product
product of
of
canonical
canonical rotation
rotation matrices
matrices
Thus
Thus we
we can
can express
express the
the final
final rotation
rotation with
with aa single
single
matrix!
matrix!
Composing Matrices
So
So we
we have
have the
the following
following matrices:
matrices:

pp:: The
The point
point to
to be
be rotated
rotated about
about A by 
A by 
R
Ryy :: Rotate
Rotate about
about Y by 
Y by
R
Rxx  :: Rotate
Rotate about
about X by 
X by
R
Rzz :: Rotate
Rotate about by 
about ZZ by 
R
Rxx  -1-1:: Undo
Undo rotation
rotation about
about X by 
X by
R
Ryy-1-1 :: Undo
Undo rotation
rotation about
about Y by 
Y by
In
In what
what orderorder should
should we
we multiply
multiply them?
them?
Compositing Matrices
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.
So
So in
in our
our case:
case:
p’
p’ == R
Ryy-1-1 R
Rxx  -1-1 R
Rzz R
Rxx  R
Ryy pp
Rotation Matrices
Notice
Notice these
these two
two matrices:
matrices:
R
Rxx  :: Rotate
Rotate about
about X by 
X by
R
Rxx  -1-1:: Undo
Undo rotation
rotation about
about X by 
X by
How
How can
can we
we calculate
calculate R
Rxx  -1-1??
Rotation Matrices
Notice
Notice these
these two
two matrices:
matrices:
R
Rxx  :: Rotate
Rotate about
about X by 
X by
R
Rxx  -1-1:: Undo
Undo rotation
rotation about
about X by 
X by
How
How can
can we
we calculate
calculate R
Rxx  -1-1??
•• Obvious
Obvious answer:
answer: calculate
calculate R
Rxx ((--))
•• Clever
Clever answer:
answer: exploit
exploit fact
fact that
that rotation
rotation matrices
matrices are
are
orthonormal
orthonormal
Rotation Matrices
Notice
Notice these
these two two matrices:
matrices:
RRxx  :: Rotate
Rotate about
about X by 
X by
R
Rxx  -1-1:: Undo
Undo rotation
rotation aboutabout X by 
X by
How
How can
can we we calculate
calculate R
Rxx  -1-1??
•• Obvious
Obvious answer:
answer: calculate
calculate R
Rxx((--))
•• Clever
Clever answer:
answer: exploit
exploit fact
fact that
that rotation
rotation matrices
matrices are
are orthonormal
orthonormal
What
What is
is an
an orthonormal
orthonormal matrix?
matrix?
What
What property
property are
are we
we talking
talking about?
about?
Rotation Matrices
Orthonormal
Orthonormal matrix:
matrix:
•• orthogonal
orthogonal (columns/rows
(columns/rows linearly
linearly independent)
independent)
•• normalized
normalized (columns/rows
(columns/rows length
length of
of 1)
1)
The
The inverse
inverse of
of anan orthogonal
orthogonal matrix
matrix is
is just
just its
its
transpose:
transpose:
1 T
a b c  a b c  a d h 
 d e f    d e f   b e i 
     
 h i j   h i j   c f j 
Rotation Matrix

99 DOFs
DOFs must
must reduce
reduce toto 33
Rows
Rows must
must be
be unit
unit length
length (-3
(-3 DOFs)
DOFs)
Rows
Rows must
must be
be orthogonal
orthogonal (-3
(-3 DOFs)
DOFs)
Drifting
Drifting matrices
matrices is
is very
very bad
bad
•• Numerical
Numerical errors
errors results
results when
when trying
trying to
to gradually
gradually rotate
rotate matrix
matrix
by
by adding
adding derivatives
derivatives
•• Resulting
Resulting matrix
matrix may
may scale
scale // shear
shear
•• Gram-Schmidt
Gram-Schmidt algorithm
algorithm will
will re-orthogonalize
re-orthogonalize your
your matrix
matrix
Difficult
Difficult to
to interpolate
interpolate between
between matrices
matrices
Euler Angles
((xx,, yy,, zz)) == R
RzzR
RyyR
Rxx
Rotate xx degrees
•• Rotate degrees about
about x-axis
x-axis
Rotate yy degrees
•• Rotate degrees about
about y-axis
y-axis
Rotate zz degrees
•• Rotate degrees about
about z-axis
z-axis
Axis
Axis order
order is
is not
not defined
defined
•• (y,
(y, z,
z, x),
x), (x,
(x, z,
z, y),
y), (z,
(z, y,
y, x)…
x)…
are
are all
all legal
legal
•• Pick
Pick one
one
Euler Angles
Rotations
Rotations not
not uniquely
uniquely defined
defined
•• ex:
ex: (z,
(z, x,
x, y)
y) == (90,
(90, 45,
45, 45)
45) == (45,
(45, 0,
0, -45)
-45)
takes
takes positive
positive x-axis
x-axis to
to (1,
(1, 1,
1, 1)
1)
•• cartesian
cartesian coordinates
coordinates are
are independent
independent of
of one
one another,
another, but
but
Euler
Euler angles
angles are
are not
not
Gimbal
Gimbal Lock
Lock
•• Term
Term derived
derived from
from mechanical
mechanical problem
problem that
that arises
arises inin
gimbal
gimbal mechanism
mechanism that
that supports
supports aa compass
compass oror aa gyro
gyro
Gimbal Lock
Gimbal Lock
Occurs
Occurs when
when two
two axes
axes are
are
aligned
aligned
Second
Second and
and third
third rotations
rotations
have
have effect
effect of
of transforming
transforming
earlier
earlier rotations
rotations
•• ex:
ex: Rot
Rot x,
x, Rot
Rot y,
y, Rot
Rot zz
––IfIf Rot
Rot yy == 90
90 degrees,
degrees, Rot
Rot zz ==
==
-Rot
-Rot xx

You might also like