Documente Academic
Documente Profesional
Documente Cultură
Computer Graphics
The Basics - Matrix Math and OpenGL
Problem Sessions, Papers and Final Projects
Additional Reading:
Computer Graphics Using OpenGL by F.S. Hill
Chapters 4, 5, and 7
Graphics Applications
Entertainment: Cinema
Graphics Applications
Entertainment: Games
id: Quake II
Cyan: Riven
Graphics Applications
Medical Visualization
Graphics Applications
Computer Aided Design (CAD)
Graphics Applications
Scientific Visualization
2D and 3D Transformations
Transformations
What are they?
changing something to something else via rules
mathematics: mapping between values in a range set
and domain set (function/relation)
geometric: translate, rotate, scale, shear,
2D & 3D Transformations
Matrix Notation
a1
b1
a2 v1
b2 v2
a1
b1
a2 v1
b2 v2
a1 * v1 a2 * v2
b1 * v1 b2 * v2
w1
w2
a1 * v1 a2 * v2
b1 * v1 b2 * v2
a1 * w1 a2 * w2
b1 * w1 b2 * w2
Matrix Notation
Ask the teaching assistant for help with this if its
the first time you have seen it.
a1
b1
a2
b2
a3 v1
b3 v2
a1 * v1 a2 * v2 a3 * v3
b1 * v1 b2 * v2 b3 * v3
c1
c2
c3
c1 * v1 c2 * v2 c3 * v3
v3
Affine Transformations
An affine transformation is defined as y = Mx+b and can be
represented as a composition of translations, rotations, and
scalings (in some order)
Translation
Translation displaces points by a fixed distance in a given
direction
Only need to specify a displacement vector d
Transformed points are given by
P =P+d
x = x + dx; y = y+ dy
dy
dx
dx
dy
x
y
x dx
y dy
2D Rotations
Every 2D rotation has a fixed point
r cos(
r cos cos
r sin sin
x cos
y sin
r sin(
r cos sin
r sin cos
x sin
y cos
(x , y )
( x, y )
x
y
cos
sin
sin
cos
x
y
Scaling
Changing the size of an object. We scale an object by
scaling the x and y coordinates of each vertex in the
object.
h
w
sx= w / w
x = sx x
w
sx
0
x'
y'
sy= h/ h
y = sy y
0
sy
x
y
sx x
sy y
Transformations as matrices
Scale:
sx
0
x2 = sxx
y2 = syy
Rotation:
x2 = x cos - y sin
y2 = x sin + y cos
Translation:
x2 = x + dx
y2 = y + dy
0
sy
x
y
sx x
sy y
cos
sin
x cos
y sin
sin
cos
x sin
y cos
dx
dy
x
y
x dx
y dy
Homogeneous Coordinates
In order to represent a transformation as a matrix multiplication
operation we use 3 x 3 matrices and pad our points to become
3 x 1 matrices. This coordinate system (using three values to
represent a 2D point) is called homogeneous coordinate
system.
x
P( x , y )
S x, y
y
1
sx
0
0
0
sy
0
cos
sin
0
0
0
1
Tx , y
sin
cos
0
0
1 0 dx
0 1 dy
0 0 1
Composite Transformations
Suppose we wish to perform multiple transformations on a point:
P2
T3,1 P1
P3
S2, 2 P2
P4
R30 P3
P4 MP1
Remember:
Matrix multiplication is associative, not commutative!
Transform matrices must be pre-multiplied
The first transformation you want to perform will be at the far right, just
before the point
0 1 1 0 1 0 0 1
1
0 0 1 0 0 1 0 0 1
T2,1
S 2,1
T 2, 1
0 1
C
2 0
2 0
0 1
0 0 1
C
2 4
9 10
9 10
After
0
0 1 0
0 0 1
C
2 2
Before
(x,y)
1 0
cos
sin
0 1 0
0 1
sin
cos
0 0 1
0 0 1
Tx , y
1 0 0 1
T x, y
Shears
Shear = a perturbation of one coordinate by an amount proportional to
another
Original Data
y Shear
x Shear
1 0 0
1 b 0
0 1 0
a 1 0
0 0 1
Reflections
0 0 1
Reflections
Reflection about the y-axis
1 0 0
0 1 0
0
0 1
0
0
1 0
0 1
More Reflections
Reflection about the origin
1
0
0 0
1 0
?
?
1 0 dx
0 1 dy
0 0 1
cos
sin
0
sin
cos
0
0
sx
0
0
0
sy
0
0
0
1
Transformations as a change in
coordinate system
All transformations we have looked at involve
transforming points in a fixed coordinate system
(CS)
One can also think of them as a transformation
of the CS itself
Translate(4,4)
Rotate(180)
Basic 3D Transformations
Translation
Scale
Rotation
Shear
As in 2D, we use homogeneous coordinates (x,y,z,w), so
that transformations may be composited together via matrix
multiplication
Mu,
11
12
13
14
21
22
23
24
31
32
33
34
A d
0
, A 3 3 , d 3 1, 01 3 ,
3D Translation
TP = (x + dx, y + dy, z + dz)
1 0 0 dx
0 1 0 dy
0 0 1 dz
0 0 0
x
y
z
1
3D Scaling
SP = (sxx, syy, szz)
sx
0
0
sy
0
0
0
0
0
0
0
0
sz
0
0
1
x
y
z
1
sin
0 0
sin
cos
0 0
1 0
0 1
cos
sin
0 0 x
sin
cos
0 0 y
1 0 z
0 1 1
P' RP
3D Rotations
About y-axis: Ry()P
cos
0
0 sin
1
0
0
0
sin
0 cos
z
1
1
0
0 cos
0
sin
0
0
0 sin
cos
z
1
M T(p f )R z ( )T( p f )
Concatenation of Transformations
More Terminology
World Coordinate System (Object Space) - The
space in which the application model is defined. The
representation of an object is measured in some
physical or abstract units
Screen Coordinate System (Image Space) - The
space in which the image is displayed. Usually
measured in pixels, but could use any units
Window - The rectangle defining the part of the
world we wish to display
Projections
Definitions
Projection: a transformation that maps from a
higher dimensional space to a lower dimensional
space (e.g. 3D
2D)
Center of projection (CoP): the position of the
eye or camera with respect to which the
projection is performed (also eyepoint, camera
point, proj. reference point)
Projection plane: in a 3D
2D projection, the
plane to which the projection is performed (also
viewplane)
Perspective projection
Parallel Projections
Projectors parallel.
COP at infinity.
Orthographic Projections
Perspective Projection
Most natural for people
In human vision, perspective projection of the world is
created on the retina (back of the eye)
Used in CG for creating realistic images
Perspective projection images carry depth cues
Foreshortening causes distant objects to appear smaller
Perspective Projection
Relative lengths and angles are not preserved
A perspective image cannot be used for metric
measurements of the 3D world
Parallel lines not parallel to the image plane converge at a
vanishing point
An axis (principal) vanishing point is a point of convergence
for lines parallel to a principal axis of the object. We
distinguish one-, two-, three-point projections.)
Perspective Projections
One-point:
One principal axis cut by projection plane
One axis vanishing point
Two-point:
Two principal axes cut by projection plane
Two axis vanishing points
Three-point:
Three principal axes cut by projection plane
Three axis vanishing points
Perspective Projections
Perspective Viewing
Perspective Viewing
frustum
From a programming
perspective, how can we
model and display a
virtual world?
Polygonal Modelling
Natural Scenery
fur
liquids
clouds
fog
OpenGL
In the early 90s, the top computer graphics company
worldwide was SGI.
They had all of the top 3D programmers and had
developed an API/library called IrisGL
In 1991, SGI developed an open API/library called
OpenGL which was released in 1992 and based heavily
on IrisGL
The main difference was that OpenGL purposely
removed all local system calls so that it could be crossplatform
OpenGL Methodology
Suppose you were to create an API for 3D, you might want to express
models and actions in the 3D world as:
OpenGL
OpenGL is the underlying API for altering the state of the 3D world. It
does not let you
You could use the native methods for handling windows, etc. -> for
example, using the win32 API on MS Windows.
GLUT
Rapid application development for OpenGL.
Portability - exists on Windows, Linux, Apple, and Android
GLUT is intended for OpenGL 1.x
);
glTranslatef(X, Y, Z) - translate by X, Y, Z
glutSwapBuffers() - in double buffering, swap the buffers
gluPerspective(v, a, n, f);
setup our camera with
v = field of view; a = aspect ratio,
n = near clipping plane; f = far clipping plane
#include "GL/glut.h"
Example 1
void update()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // red
glVertex3f(-0.7, 0.0, 0.0); // vertex to the left
glVertex3f(0.7, 0.0, 0.0); // vertex to the right
glVertex3f(0.0, 0.7, 0.0); // vertex to the top
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutCreateWindow("Triangle");
glutDisplayFunc(&update);
glutMainLoop();
return 0;
}
Grading
Homework, Student Presentations: 30% (-1 penalty/day late)
Problem Session Workshops: 20%
These are like open-book exams where you can ask the teaching
assistant or other students for advice. There is a strict time deadline at
the end of the workshop.
Homework 1
Note for Homework 1: Students are encouraged to discuss the solutions with other students.
./output
Go into room 302, compile the program from Example 1 and run. Take a screenshot of the result. In the
later problem session workshops, you will be doing OpenGL programming. Please email the teaching
assistant if it takes more than an hour to determine the compiler settings or run the program.
Put all of your answers in a "zip" file and submit to the LML Course Manager in one week.