Geometric Objects and Transformations

advertisement
Geometric Objects and
Transformations
Coordinate systems and frames
Working with representation
Object transformation
Suriyong L.
Introduction
• Mathematical of Object
– Euclidean vector spaces
• Vector space with measure of size
– Independent of coordinate system
• Parametric form system
Suriyong L.
Scalars, Points, and Vectors
• Geometric object description in space
– By length, angle
– With 3 fundamental types scalars, points and
vector
Suriyong L.
The geometric View
• Point : a location in the space
– Mathematical for point
• Neither a size nor shape
– Properties
• location
– What for :
• Specify an object
Suriyong L.
Scalars
•
•
•
•
Quantity of object or relation objects
Ex. Distance between object
Specify with real / complex number
Useful rule for scalar
– communitivity and Associativity in
multiplicity, additivity
– Ex. a+b = b+a, (a+b)+c = a+(b+c)
Suriyong L.
Vector
• Quantity with direction and
magnitude ex. velocity, force
– Does not have a fixed position in space
– Synonymously to line segment
• Computer graphics often connect
points with directed line segment
Directed line segment
that connects points
– Line segment: a segment of line which
has both magnitude and direction
Suriyong L.
Identical vectors
Vector
properties
Its lengths changed by real number
•
• B = 2A
– B is double in size to vector A with the same direction
• Vector combining: (addition)
– Use head to tail combining, the result is the sum of the vector
– Any vector in space is able to do addition, independent of its location
– Scalar-vector addition make sense: ex. A + 2B – 3C
• Inverse vector:
– The vector that has opposite direction to the original vector
Parallel line segments
Addition of line segments
Suriyong L.
Inverse vectors
Vector operation
• Scalar multiplying
– Result change in length (magnitude)
• Point-vector addition
– Result change in displacement to the new
point position
• Point-point subtraction
– Result is a vector between 2 points
• Note:
Point-vector addition
Some expression involving scalars, vectors and
points make sense ex. P+3v, or P-2Q+3v
while P+3Q-v do not
Suriyong L.
Coordinate-free geometry
• For graphics system let the object relate to
each other but independent of coordinate
system
• Let object relate to an arbitrary location and
orientation of the original axis
Object and coordinate system
Object without coordinate system
Suriyong L.
The mathematical view:
Vector and Affine Spaces
• Scalar operation
– Addition , multiplication
• If operation obey closure, associativity, commutivity
and inverse properties, the element form a scalar
field
– Ex. field . Real number, complex number,
rational function
Suriyong L.
Vector space
• 2 distinct type of entities in vector space
– Scalar and vector
• Scalar-vector multiplication
– Vector and vector
• Vector – vector addition
• Euclidean space
– Extension of vector space
– Add a measure of size or distance to define object
– Ex. Length of line segment
Suriyong L.
Affine space
(space of transformation)
• Extension of vector space
– Include point to vector space
– Have vector-point addition and point-point
operation
Suriyong L.
Computer Science View
• Prefer to see object as
abstract data type (ADT)
• Operations and data are
defined independently
• Fundamental to modern
computer science
• Like C++ language features :
class and overloading
vector u,v;
point p,q;
scalar a, b;
Computational point of view declaration
(Independent of data type declaration)
q = p+a*v;
Operation that independent of data type
Suriyong L.
Geometric ADT
• Learn how to perform geometric operation and
forming geometric object
• Let
Greek letter a, b, g,... : scalars
Upper-case letter P, Q, R,… : points
Bold lower-case letters u, v, w,… : vector
Suriyong L.
• The operation of vector-scalar multiplication
𝛼𝑣 = 𝛼 |𝑣|
• Subtraction of two points, P and Q -> vector v
𝑣 =𝑃−𝑄
• Add vector with point get point
𝑃 =𝑣+𝑄
• Vector-vector operation can be in point form
(P−Q)+ (Q−R) = P−R
Point-point subtraction
Use of the head-to-tail rule
(a) For vectors, (b)For points
Suriyong L.
Lines
•
•
•
•
•
General term definition
Called parametric form of line
Point generating by varying alpha
Line is infinity in length
One we see just line segment
𝑃 𝛼 = 𝑃0 + 𝛼𝑑
Parametric line equation
Suriyong L.
Line in an affine space
Affine sum
If P is a point on line
P=Q+av
v = R – Q;
Thus
P = Q + a(R-Q)
= aR + (1-a)Q
Let
a = a1 and (1-a) = a2
Thus
Affine line addition
a1 + a2 = 1
Then
P=a1R+a2Q
Points on line can be found between point Q and P(a)
Suriyong L.
Convexity
• Convex object
– Any point that lying on
line segment connect any
two points in the object
also in the object
• Affine sum definition
Line segment that connects two points
Objects defined by n points P1, P2,…,Pn. Consider the form
P = a1 P1  a2 P2  ...  an Pn
When
a1  a2  ...  an = 1
Suriyong L.
Convexity
• An object is convex iff for any two points in
the object all points on the line segment
between these points are also in the object
P
P
Q
Q
not convex
convex
Suriyong L.
19
Convex hull
The set of points formed by the affine sum of n points, under the additional restriction
αi ο‚³ 0,
i = 1,2,...,n
Convex hull
is called the convex hull of the set of points
convex hull includes all line segments connecting pairs of points i[P1,P2,…,Pn]
The notion of convexity is extreamly important in the design of curves and
surfaces;
Suriyong L.
Dot and Cross product
• Dot (inner product)
– u.v: result is magnitude of 2 vectors
– If u.v = 0, u and v are orthogonal vector
– Unit product is
𝑒. 𝑣
cos πœƒ =
𝑒 𝑣
𝑒. 𝑣
𝑒 cos πœƒ =
𝑣
Dot product and projection
is the orthogonal projection of u onto v
Suriyong L.
Cross product
• Result is vector
• Forming from right hand coordinate system
𝑒×𝑣
sin πœƒ =
𝑒 𝑣
Cross product
Note:
right-handed coordinate system
u points in the direction of the thumb
v points in the direction of the index finger
n points in the direction of the middle finger
Suriyong L.
Plane
•
•
•
•
•
Infinite flat area
Direct extension of parametric line
Define with 3 non-co-linear points
Suppose P, Q and R are points in plane
The plane equation
𝑆 𝛼 = 𝛼𝑃 + 1 − 𝛼 𝑄 ; 0 ≤ 𝛼 ≤ 1
and
𝑇 𝛼, 𝛽 = 𝛽𝑆 𝛼 + 1 − 𝑏 𝑅 ; 0 ≤ 𝛽 ≤ 1
Suriyong L.
General plane equation
𝑇 − 𝑇0 = 𝛼𝑒 + 𝛽𝑣
The plane can have vector (normal vector)
formed from u and v
Let
n : plane vector
Thus
𝑛 =𝑒×𝑣
Suriyong L.
3D primitives
Curves in three dimension
Surfaces in three dimensions
Object are not lying on plane
Suriyong L.
Volumetric objects
2 problems when incorporate in 3D
• Complex mathematic
– Not all object that has 3D efficient
implementation
• Approximated method may be used
• 3 features characteristic to describe object
– Hollow
– Vertices
– Flat convex polygon composition
Suriyong L.
Coordinate system and frame
• Represents Vector in 3D space with 3 basis vector
𝑀 = 𝛼1 𝑣1 + 𝛼2 𝑣2 + 𝛼3 𝑣3
𝛼1 , 𝛼2 and 𝛼3 : component of 𝑀
𝑣1 , 𝑣2 and 𝑣3 ∢ basic vector
π‘Ž = 𝛼1
thus
𝑣1
𝑀 = π‘Ž 𝑣2
𝑣3
𝛼2
𝛼3
Vector derived from three basis vectors
Suriyong L.
Point and vector representation in affine space
• It is not enough to use only vector to represent
point in space
• Frame: fix point (origin) and basis vector
– Vector: represent with 3 basis
– Point: fix point and 3 basis
Coordinate system (a) with vector
emerging from a common point, (b) with
vector moved
Suriyong L.
A Dangerous representation of vector
Representations and N-tuples
• Any vector v representation
𝑣 = 𝛼1 𝑒1 + 𝛼2 𝑒2 + 𝛼3 𝑒3
𝑒1 = 1,0,0
𝑒2 = 0,1,0
𝑒3 = 0,0,1
𝑇
𝑇
𝑇
Where 𝑒1 𝑒2 and 𝑒3 : basis vector
• In column matrix form
π‘Ž = a1 𝑒1 + a2 𝑒2 + a3 𝑒3
(𝛼1 , 𝛼2 , 𝛼3 ) ∢ called 3−tuple of scalars
• Known as Euclidean 𝑅3 :
Suriyong L.
Coordinate System Changing
World /User frame ↔ Camera frame
– Done by MODELVIEW matrix
– Use 2 basis set of vector 𝑒 and 𝑣
– Representation of 𝑒 by 𝑣
𝑒1
𝛾11 𝑣1 + 𝛾12 𝑣2 + 𝛾13 𝑣3
𝑒2 = 𝛾21 𝑣1 + 𝛾22 𝑣2 + 𝛾13 𝑣3
𝑒3
𝛾31 𝑣1 + 𝛾32 𝑣2 + 𝛾33 𝑣3
Suriyong L.
Let
𝛾11
𝑀 = 𝛾21
𝛾31
𝛾12
𝛾22
𝛾32
𝛾13
𝛾23
𝛾33
and
𝑒 = 𝑀𝑣
𝑀 represent matrix from 𝑒 to 𝑣
Let
Where
𝑀 = 𝛼1 𝑣1 + 𝛼2 𝑣2 + 𝛼3 𝑣3
𝛽1
𝑏 = 𝛽2
𝛽3
Equivalent to
𝑀 = π‘Žπ‘‡
𝑣1
𝑣2
𝑣3
Where
𝛼1
π‘Ž = 𝛼2
𝛼3
Assume 𝑏 : the representation of 𝑀 with
respect to 𝑒1 𝑒2 𝑒3 ;
that is,
Then using our representation of the
second basis in terms of the first, we
find that
𝑒1
𝑣1
𝑣1
𝑀 = 𝑏 𝑇 𝑒2 = π‘Žπ‘‡ 𝑣2 = 𝑀𝑏 𝑇 𝑣2
𝑒3
𝑣3
𝑣3
Thus
π‘Žπ‘‡ = 𝑀𝑏 𝑇
The matrix (MT)-1 takes us from a to b:
𝑀 = 𝛽1 𝑒1 + 𝛽2 𝑒2 + 𝛽3 𝑒3
or
𝑀 = 𝑏𝑇
𝑒1
𝑒2
𝑒3
𝑏 = 𝑀𝑇
−1
π‘Ž
We can transfer coefficient matrix
from one to the other
Suriyong L.
• This changing let us to work with different
coordinate system but origin unchanged
• Able to use them to represent rotation and scaling
but not translation
Translation of a basis
Rotation and scaling of a basis
2 examples 2 different frames
Suriyong L.
Example:
Suppose that we have a vector w whose representation in
some basis is
1 οƒΉ
a = οƒͺοƒͺ 2 οƒΊοƒΊ
οƒͺ 3 
We can denote the three basis vectors and v1 , v2 , and v3 hence,
w=v1 +2v2 +3v3
Now suppose that we want to make a new basis from the
three vectors v1 , v2 , and v3
u1 ο€½ v1
u2 ο€½ v1  v2
u3 ο€½ v1  v2  v3
The matrix M is
1 0 0 οƒΉ
M= οƒͺοƒͺ1 1 0 οƒΊοƒΊ
οƒͺ1 1 1 
Suriyong L.
The matrix that converts a representation in 𝑣1 , 𝑣2 , and 𝑣3 to one in which
the basis vector are 𝑒1 , 𝑒2 and 𝑒3 is
𝐴 = 𝑀𝑇
1
= 0
0
1
= 0
0
−1
1 1 −1
1 1
0 1
−1 0
1 −1
0
1
In the new system, the representation of w is
−1
𝑏 = π΄π‘Ž = −1
3
That is
𝑀 = −𝑒1 − 𝑒2 + 3𝑒3
Suriyong L.
Homogeneous Coordinate
3 basis vector is not enough to make point and vector different
For any point:
𝑃 = 𝑃0 + π‘₯𝑣1 + 𝑦𝑣2 + 𝑧𝑣3
//General equation for point
In the frame specified by (v1, v2, v3, P0), any point P can be written uniquely as
𝑃 = 𝛼1 𝑣1 + 𝛼2 𝑣2 + 𝛼3 𝑣3 + 𝑃0
The we can express this relation formally, using a matrix product, as
𝑣1
𝛼1
𝑣2 represent
𝛼2
𝛼
𝛼
𝛼
1
𝑃= 1
𝑃=
2
3
𝑣3
𝛼3
𝑃0
1
The yellow matrix called homogeneous-coordinate representation of point P
Suriyong L.
In the same frame, any vector w can be written
𝑀 = 𝛿1 𝑣1 + 𝛿2 𝑣2 + 𝛿3 𝑣3
𝑣1
𝑣2
𝑀 = [𝛿1 𝛿2 𝛿3 0] 𝑣
3
𝑃0
w can be represented by the column matrix
𝛿1
𝛿
𝑀= 2
𝛿3
0
If 𝑣1 , 𝑣2 , 𝑣3 , 𝑃0 and 𝑒1 , 𝑒2 , 𝑒3 , 𝑄0 , can be expressed as
𝑒1
𝛾11 𝑣1 + 𝛾12 𝑣2 + 𝛾13 𝑣3
𝑒2
𝛾21 𝑣1 + 𝛾22 𝑣2 + 𝛾23 𝑣3
=
𝑒3
𝛾31 𝑣1 + 𝛾32 𝑣2 + 𝛾33 𝑣3
𝑄0
𝛾41 𝑣1 + 𝛾42 𝑣2 + 𝛾43 𝑣3 + 𝑃0
Suriyong L.
Can be written in the form
𝑒1
𝑒2
𝑒3 = 𝑀
𝑄0
𝑣1
𝑣2
𝑣3 and 𝑀 =
𝑃0
𝛾11
𝛾21
𝛾31
𝛾41
𝛾11
𝛾22
𝛾32
𝛾41
𝛾13
𝛾23
𝛾33
𝛾41
0
0
0
1
M : the matrix representation of the change of frames.
Suppose a and b are the homogeneous-coordinate representations, then
𝑒1
𝑣1
𝑣1
𝑒2
𝑣2
𝑣2
𝑏 𝑇 𝑒 ⇒ 𝑀𝑏 𝑇 𝑣 = π‘Žπ‘‡ 𝑣
3
3
3
𝑄0
𝑃0
𝑃0
Hence,
π‘Ž = 𝑀𝑇 𝑏
Suriyong L.
When we work with representations, as is usually the case, we are interested
in 𝑀𝑇 , which is of the form
𝛼11 𝛼12
𝛼
𝛼22
𝑀𝑇 = 21
𝛼31 𝛼32
0
0
Only 12 coefficients
𝛼13
𝛼23
𝛼33
0
𝛼14
𝛼24
𝛼34
1
Advantage of using homogeneous representation
o
use 4D matrix instead of 3D matrix
o
o
less calculation
modern hardware support the representation
o
parallelism for high speed calculation
Suriyong L.
Example
If we start with the basic vector v1 , v2 and v3 and convert to a basis determined by
the same u1 , u2 and u3 then the three equations are the same
u1 ο€½ v1 ,
u2 ο€½ v1  v2
u3 ο€½ v1  v2  v3
The reference point does not change, so we add the equation
Q0 =P0
Thus, the matrices in which we are interested are the matrix
1
οƒͺ1
M= οƒͺ
οƒͺ1
οƒͺ
0
0οƒΉ
0 οƒΊοƒΊ
0οƒΊ
οƒΊ
1
its transpose, and their inverses.
0
1
1
0
0
0
1
0
Suriyong L.
Suppose move the point to (1, 2, 3, 1)
with displacement vector
We can move back and forth between
representation
Thus
𝑣 = 𝑣1 + 2𝑣2 + 3𝑣3
1
2
𝑃=
3
1
And move from point P0 to Q0 then
𝑄0 = 𝑃0 + 𝑣1 + 2𝑣2 + 3𝑣3
The matrix
becomes
1 1 1
0 1 1
𝑀𝑇 =
0 0 1
0 0 0
𝑀𝑇
Its inverse is
𝐴 = 𝑀𝑇
−1
1
0
=
0
0
−1
1
0
0
to
1
2
3
1
0
−1
1
0
1
1
−3
1
Suriyong L.
0
0
𝑃′ =
0
1
// old frame view at origin
// new frame view
The origin in the new system is represented as
1 οƒΉ
οƒͺ2οƒΊ
aο€½οƒͺ οƒΊ
οƒͺ3οƒΊ
οƒͺ οƒΊ
0
Original vector
is transformed to
 ο€­1οƒΉ
οƒͺ ο€­1οƒΊ
bο€½οƒͺ οƒΊ
οƒͺ3οƒΊ
οƒͺ οƒΊ
0
<-
b= (MT)-1a
Suriyong L.
Working with Representation
• Represent object from a frame to another such as
world frame to camera frame
• Form of representation
a = Cb
; a, b object in two representation frame
• Object:
– 3 vector, u, v, n, and 1 new frame origin, p -> (u, v, n, p)
– 4 entities -> 4-tuples -> R4
• The solution is inverse matrix form of C, let’s say D
D = C-1
Suriyong L.
DI ο€½ D ο€½ u v n
 u1
οƒͺu
p ο€½ οƒͺ 2
οƒͺ u3
οƒͺ
0
v1
v2
n1
n2
v3
n3
0
0
p1 οƒΉ
p2 οƒΊοƒΊ
p3 οƒΊ
οƒΊ
1
or
C ο€½ u v n
 u1
οƒͺu
ο€­1
p ο€½ οƒͺ 2
οƒͺ u3
οƒͺ
0
v1
n1
v2
n2
v3
0
n3
0
Suriyong L.
p1 οƒΉ
p2 οƒΊοƒΊ
p3 οƒΊ
οƒΊ
1
ο€­1
Frames in OpenGL
• 2 frames:
– Camera: regard as fix
– World:
• The model-view matrix position related to camera
• Convert homogeneous coordinates representation of object to camera
frame
• OpenGL provided matrix stack for store model view
matrices or frames
• By default camera and world have the same origin
• If moving world frame distance d from camera the
model-view matrix would be
Suriyong L.
1
οƒͺ0
A= οƒͺ
οƒͺ0
οƒͺ
0
0
1
0
0
0 0οƒΉ
0 0 οƒΊοƒΊ
1 -d οƒΊ
οƒΊ
0 1
Camera and world frames
Suriyong L.
Suppose camera at point (1, 0, 1, 1)
• World frame center point of
camera
𝑝 = (1, 0, 1, 1)𝑇
• Representation of world frame for
camera
𝑛 = (−1, 0, −1, 0)𝑇
• Camera orientation: up or down
𝑣 = (0, 1, 0, 0)𝑇
• Forming orthogonal product for
determining v let’s say u
Camera at (1,0,1) pointing toward the origin
𝑒 = (−1, 0, 1, 0)𝑇
Suriyong L.
The model view matrix M
1
οƒͺ0
(M T ) ο€­1 ο€½ οƒͺ
οƒͺ ο€­1
οƒͺ
0
0 ο€­1 1 οƒΉ
1 0 0 οƒΊοƒΊ
0 ο€­1 1 οƒΊ
οƒΊ
0 0 1
ο€­1
1
 1
οƒΉ
0
ο€­
0
οƒͺ 2
οƒΊ
2
οƒͺ
οƒΊ
0
1
0
0
οƒΊ
ο€½ οƒͺοƒͺ
οƒΊ
1
1
0 ο€­
1οƒΊ
οƒͺο€­
2
οƒͺ 2
οƒΊ
οƒͺ 0 0 0 1 
Result:
Original origin is 1 unit in the n direction from the origin in the camera frame
which is the point (0, 0, 1, 1)
OpenGL model view matrix
OpenGL set a model-view matrix by send an array of 16 elements to
glLoadMatrix
We use this for transformation like rotation, translation and scales etc.
Suriyong L.
Modeling a Color Cube
• A number of distinct task that we
must perform to generate the
image
–
–
–
–
–
–
Modeling
Converting to the camera frame
Clipping
Projecting
Removing hidden surfaces
Rasterizing
Suriyong L.
One frame of cube animation
Modeling a Cube
• Model as 6 planes intersection or six polygons as cube facets
• Ex. of cube definition
GLfloat vertices[8][3] = {
{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0},
{-1.0,-1.0,1.0}, {1.0,-1.0,1.0},
{1.0,1.0,1.0}, {-1.0,1.0,1.0}
};
// or
typedef point3[3];
// then may define as
point3 vertices[8] = {
{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0},
{-1.0,-1.0,1.0}, {1.0,-1.0,1.0},
{1.0,1.0,1.0}, {-1.0,1.0,1.0}
};
// object may defined as
void polygon(int a, int b, int c , int d) {
/* draw a polygon via list of vertices */
glBegin(GL_POLYGON);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[b]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
} L.
Suriyong
Inward and outward pointing faces
• Be careful about the
order of vertices
• facing outward:
vertices order is 0, 3,
2, 1 etc., obey right
hand rule
Traversal of the edges of a polygon
Suriyong L.
Data Structure for Object Representation
• Topology of Cube description
– Use glBegin(GL_POLYGON); six times
– Use glBegin(GL_QUADS); follow by 24
vertices
• Think as polyhedron
– Vertex shared by 3 surfaces
– Each pair of vertices define edges
– Each edge is shared by two faces
Suriyong L.
Vertex-list representation of a cube
Suriyong L.
The color cube
• Color to vertex list -> color 6 faces
• Define function “quad” for drawing quadrilateral polygon
• Next define 6 faces, be careful about define outwarding
typedef GLfloat point3[3];
point3 vertices[8] = {{-1.0,-1.0, 1.0},{-1.0, 1.0, 1.0}, {1.0,1.0, 1.0}, {1.0,-1.0, 1.0},
{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}};
GLfloat colors[8][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0},
{0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};
void quad(int a, int b, int c , int d) {
glBegin(GL_QUADS);
glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]);
glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]);
glEnd();
}
void colorcube() {
quad(0, 3, 2, 1); quad(2, 3, 7, 6); quad(0, 4, 7, 3); quad(1, 2, 6, 5); quad(4, 5, 6, 7); quad(0, 1, 5, 4);
}
Suriyong L.
Color the cube surface
•
Bilinear interpolation
Projection of polygon
Suriyong L.
Vertex Array
• Use encapsulation, data structure & method together
– Few function call
• 3 step using vertex array
– Enable functionality of vertex array: part of initialization
– Tell OpenGL where and in what format the array are: part of
initialization
– Render the object :part of display call back
• 6 different type of array
– Vertex, color, color index, normal texture coordinate and edge flag
– Enabling the arrays by
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// The arrays are the same as before and can be set up as globals:
GLfloat vertices[] = {{-1,-1,-1},{1,-1,1},{1,1,-1},{-1,1,-1},{-1,1,1}, {1,-1,1}, {1,1,1}, {-1,1,1}};
GLfloat colors[] = {{0,0,0},{1,0,0},{1,1,0},{0,1,0},{0,0,1},{1,0,1},
{1,1,1},{0,1,1}};
// Next identify where the arrays are by
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
// Define the array to hold the 24 order of vertex indices for 6
faces
GLubyte cubeIndices[24] = {0,3,2,1, 2,3,7,6, 0,4,7,3, 1,2,6,5,
4,5,6,7, 0,1,5,4};
glDrawElements(type, n, format, pointer);
for (i=0; i<6; i++)
glDrawElement(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cubeIndex[4*i]};
// Do better by seeing each face as quadrilateral polygon
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
// GL_QUADS starts a new quadrilateral after each four vertices
Suriyong L.
Affine transformation
Transformation:
A function that takes a point (or vector) and maps that points (or
vector) in to another point (or vector)
Point transform
𝑄 = 𝑇 𝑃 → 𝑄 = 𝑓(𝑃)
Vector transform
𝑣 = 𝑅 𝑒 → 𝑣 = 𝑓(𝑒)
Transformation
Suriyong L.
Transformation function must be linearity function
f a p  b q  ο€½ a f  p   b f  q 
Transformation form of 4D matrix
v ο€½ Au
a11 a12 a13 a14 οƒΉ
οƒͺa
οƒΊ
a
a
a
22
23
24 οƒΊ
A ο€½ οƒͺ 21
οƒͺa 31 a 32 a 33 a 34 οƒΊ
οƒͺ
οƒΊ
0
0
0
1


 a1 οƒΉ
οƒͺa οƒΊ
u ο€½ οƒͺ 2οƒΊ
οƒͺa 3 οƒΊ
οƒͺ οƒΊ
0
 b1 οƒΉ
οƒͺb οƒΊ
v ο€½ οƒͺ 2οƒΊ
οƒͺ b3 οƒΊ
οƒͺ οƒΊ
1
For vector has 12 degree of freedom for point and vector
Suriyong L.
Rigid body transformation
• Shape doesn’t change
• Only change in position and orientation
• They are: Translation and rotation.
Suriyong L.
Translation
Is an operation that displaces points by a fixed distance in a given direction
To specify a translation, just specify a displacement vector d thus
Pο‚’ ο€½ P  d
• For all point P of the object
– No reference point to frame or representation
– 3 degree of freedom
Translation. (a) Object in original
Suriyong
L.
position. (b) Object
translated
Rotation
• Need axis and angle as input parameter
x = r.cosf ;
Two-dimensional rotation
2D Point rotation θ radian around origin (Z axis)
x’ = r.cos(q  f
= r.cosf.cosqο€­ r.sinf.sinq
= x.cosq – y.sinq
y’ = r.sin(qf)
= r.cosf.sinq +r.sinf.cosq
= x.sinq+y.cosq
These equations can be written in matrix form as
 xο‚’ οƒΉ cos q
οƒͺ yο‚’οƒΊ ο€½ οƒͺ sin q
  
y = r.sinf
ο€­ sin q οƒΉ  x οƒΉ
cos q  οƒͺ y 
Suriyong L.
3 features for rotation
(3 degrees of freedom)
• Around fixed point (origin)
• Direction ccw is positive
• A line
Rotation
arount a fixed point.
Suriyong L.
3D rotation
• Must define 3 input parameter
– Fixed Point (Pf)
– Rotation angle (θ)
– A line or vector (rotation axis)
• 3 degrees of freedom
• 2 angle necessary specified
orientation of vector
• 1 angle for amount of rotation
Suriyong L.
Three-dimensional
rotation
Scaling
• Non rigid-body transformation
• 2 type of scaling
– Uniform: scale in all direction -> bigger, smaller
– Nonuniform: scale in single direction
• Use in modeling and scaling
Non rigid body transformations
Suriyong L.
Uniform and nonuniform scaling
Scaling: input parameter
• 3 degrees of freedom
– Point (Pf)
– Direction (v)
– Scale factor (a)
Effect of scale factor
• a > 1 : get bigger
• 0 ο‚£ a < 1 : smaller
• a < 0 : reflected
Suriyong L.
Reflection
Transformation in homogeneous coordinate
Most graphics APIs force us to work within some reference system. Although we
can alter this reference system – usually a frame – we cannot work with high-level
representations, such as the expression.
Q = P + αv.
Instead, we work with representations in homogeneous coordinates, and with
expressions such as
q = p + αv.
Within a frame, each affine transformation is represented by a 4x4 matrix of the
form
Suriyong L.
Translation
If we move the point p to pο‚’ by displacing by a distance d then
pο‚’ = p+d
Define
 xο‚’ οƒΉ
 xοƒΉ
a x οƒΉ
οƒͺ yο‚’οƒΊ
οƒͺ yοƒΊ
οƒͺa οƒΊ
οƒͺ
οƒΊ
οƒͺ
οƒΊ
pο‚’=
, p=
, d= οƒͺ y οƒΊ
οƒͺ zο‚’ οƒΊ
οƒͺzοƒΊ
οƒͺa z οƒΊ
οƒͺ οƒΊ
οƒͺ οƒΊ
οƒͺ οƒΊ
1
1 
0
It may be written for each of components
xο‚’ ο€½ x  a x
yο‚’ ο€½ y  a y
zο‚’ ο€½ z  a z
Let T is a translation matrix, we may written in matrix form as:
pο‚’ ο€½ Tp
And
1
οƒͺ0
T= οƒͺ
οƒͺ0
οƒͺ
0
0 0 ax οƒΉ
1 0 a y οƒΊοƒΊ
0 1 az οƒΊ
οƒΊ
0 0 1
Sometimes write it as T a x , a y , a z 
Scaling
Use fixed point parameter as reference
Scaling in one direction means scale in each direction element
The scaling equation are:
π‘₯ ′ = 𝛽π‘₯ π‘₯, 𝑦 ′ = 𝛽𝑦 𝑦, 𝑧 ′ = 𝛽𝑧
For the homogeneous form
𝑝′ = 𝑆𝑝
where
𝑆 = 𝑆 𝛽π‘₯ , 𝛽𝑦 , 𝛽𝑧
𝛽π‘₯
0
=
0
0
0
𝛽𝑦
0
0
0
0
𝛽𝑧
0
0
0
0
1
We may say that the inverse form of scaling is :
𝑆 −1 = 𝑆
1 1 1
, ,
𝛽π‘₯ 𝛽𝑦 𝛽𝑧
Suriyong L.
Rotation
2D rotation is actual 3D rotation around Z axis
For general equations:
xο‚’ ο€½ x.cos q ο€­ y.sin q
yο‚’ ο€½ x.sin q  y.cos q
zο‚’ ο€½ z
For the Homogeneous equation, let denotes R z is a rotation matrix around z axis
Then the homogeneous form for rotation is
pο‚’=R z p
where
cos q
οƒͺ sin q
R z q  ο€½ οƒͺ
οƒͺ 0
οƒͺ
 0
ο€­ sin q
cos q
0
0
0 0οƒΉ
0 0 οƒΊοƒΊ
1 0οƒΊ
οƒΊ
0 1
Suriyong L.
For rotation about x-axis the rotation matrix R x
0
1
οƒͺ0 cos q
R x ο€½ R x q  ο€½ οƒͺ
οƒͺ0 sin q
οƒͺ
0
0
0
ο€­ sin q
cos q
0
0οƒΉ
0 οƒΊοƒΊ
0οƒΊ
οƒΊ
1
and for rotation about y-axis R y
 cos q 0 sin q 0 οƒΉ
οƒͺ 0
οƒΊ
1
0
0
οƒΊ
R y ο€½ R y q  οƒͺ
οƒͺ ο€­ sin q 0 cos q 0 οƒΊ
οƒͺ
οƒΊ
0
0
0
1


For inverse rotation transformation in each axis:
Rotate to the same axis with equivalent angle backward:
R -1 q  = R  ο€­q  ο€½ R T q 
We call a matrix whose inverse is equal to its transpose is call an orthogonal matrix
Suriyong L.
Shear
• Let pull in top right edge and bottom left edge
– Neither y nor z are changed
– Call x shear direction
Shear
Suriyong L.
xο‚’ ο€½ x  y cot q x
yο‚’ ο€½ y
zο‚’ ο€½ z
Leading to the shearing matrix
x
Computation of the shear
matrix
1 cot q x
οƒͺ0
1
οƒͺ
H x q x  ο€½
οƒͺ0
0
οƒͺ
0
0
0
0
1
0
0οƒΉ
0 οƒΊοƒΊ
0οƒΊ
οƒΊ
1
Inverse of the shearing matrix: shear in the opposite direction
Hο€­x1 qx  ο€½ Hx  ο€­qx 
Suriyong L.
Shear in other direction
xο‚’ ο€½ x
yο‚’ ο€½ y
zο‚’ ο€½ z  x cot q z
xο‚’ ο€½ x
yο‚’ ο€½ y  x cot q y
zο‚’ ο€½ z
 1
οƒͺcot q
y
H y q y  ο€½ οƒͺ
οƒͺ 0
οƒͺ
 0
0 0 0οƒΉ
1 0 0 οƒΊοƒΊ
0 1 0οƒΊ
οƒΊ
0 0 1
Shear in y-axis direction when look on xy-plane
 1
οƒͺ 0
H z q z  ο€½ οƒͺ
οƒͺcot q z
οƒͺ
 0
0 0 0οƒΉ
1 0 0 οƒΊοƒΊ
0 1 0οƒΊ
οƒΊ
0 0 1
Shear in z-axis direction when look on zx-plane
What about Hy and Hz when look in yz plane ?
Suriyong L.
Shearing general form
•
1
0
0
0
π‘ β„Žπ‘₯
1
0
0
0
0
1
0
0
0
0
1
y
y
(1,1)
(0,1)
(2,1)
x
(1,0)
(0,0)
(0,0)
(3,1)
x
(1,0)
• shear with reference line
•
1
0
0
0
π‘ β„Žπ‘₯
1
0
0
0 −π‘ β„Žπ‘₯ . π‘¦π‘Ÿπ‘’π‘“
0
0
1
0
0
1
y
(0,1)
(0,0)
y
(1,1)
(1,1)
(1,0)
x
(1/2,0)
yref =-1
Suriyong L.
(2,1)
(3/2,0)
x
Concatenation of transformation
Application of transformations one at a time
The sequence of transformation is
q=CBAp
It can be seen that one do first must be near most to input
We may proceed in two steps
Calculate total transformation matrix:
M=CBA
Matrix operation :
q=Mp
Pipeline transformation
Suriyong L.
Derive example of M: rotation about a fixed point
<- This is what we try to do.
Rotation of a cube about its center
Transformation
processes
Sequence of transformation
Suriyong L.
General rotation
Rotation of a cube about the y-axis
Rotation of a cube about the z-axis. The
cube is show (a) before rotation, and (b)
after rotation
R ο€½ R xR yR z
Rotation of a cube about the x-axis
Suriyong L.
The Instance transformation
• From the example object, 2 options to do with
object
– Define object to its vertices and location
with the desire orientation and size
– Define each object type once at a
convenience size, place and orientation ,
next move to its place
• Object in the scene is instance of the
prototype
• If apply transformation, called instance
transformation
• We can use database and identifier for each
Suriyong L.
Scene of simple objects
Instance transformation
M=TRS
//instant transformation equation of object
Suriyong L.
Rotation around arbitrary axis
• Input parameter
– Object point
– Vector (line segment or 2 points)
– Angle of rotation
• Idea
– Translate to origin first T(-P0)
– Rotate
• q-axis component
• q around 1 component axis, let say z
Suriyong L.
Let rotation axis vector is u and
u = P2 – P1
<- Convert u to unit
length vector
<- Shift to origin (T(-P)),
end shift back (T(P))
Rotation of a cube about
an arbitrary axis
Movement of the fixed
point to the origin
R = Rx(-qx).Ry(-qy).Rz(q).Ry(qy).Rx(qx) <- Individual axis rotation z first
Suriyong L.
Sequence of rotations
Problem: How we fine θx and θy ?
ax2  a y2  az2 ο€½ 1
<- From v, unit vector
cos2 fx  cos2 fy  cos2 fz ο€½ 1 <-
cos fx ο€½ a x
cos f y ο€½ a y
<-
cos fz ο€½ a z
f = angle of vector respect to origin
Suriyong L.
Direction angles
<- For rotation around X, project vector to plane
XZ (X=0) meanwhile projection on YZ plane
with length d
->
d ο€½ a y2  a z2
Computation of the x rotation
1 0
οƒͺ
οƒͺ0 a z
οƒͺ
d
Rx q x  ο€½ οƒͺ
ay
οƒͺ0
οƒͺ
d
οƒͺ0 0

0
ο€­
ay
d
az
d
0
0οƒΉ
οƒΊ
0οƒΊ
οƒΊ
οƒΊ
0οƒΊ
οƒΊ
1 
d
οƒͺ0
Ry q y  ο€½ οƒͺ
οƒͺa x
οƒͺ
0
0 ο€­a x
1 0
0 d
0 0
0οƒΉ
0 οƒΊοƒΊ
0οƒΊ
οƒΊ
1
Computation of the y rotation
Finally we calculate all the matrices to find
M ο€½ T  p 0  Rx  ο€­q x  Ry  ο€­q y  Rz q  Ry q y  Rx q x  T  ο€­ p 0 
Suriyong L.
•
Example: rotate around origin to point (1,2,3) 45 degree
Normalized vector (1, 2, 3) →
1
2
3
,
,
14 14 14
, or
1
2
3
,
,
,0
14 14 14
Z axis rotation (0,0,1,0) 45 degree => θz = 45o
X axis (1,0,0,0) rotation angle is cos −1
Y axis rotation cos −1
𝑅 = 𝑅π‘₯
− cos−1
3
13
𝑅𝑦
3
13
13
14
− cos −1
13
13
3
−1
−1
𝑅 45 𝑅𝑦 cos
𝑅 cos
14 𝑧
14 π‘₯
13
Suriyong L.
2 + 13 2
2− 2−3 7 6−3 2+4 7
0
28
14
28
2− 2+3 7
4+5 2
6−3 2− 7
0
14
14
14
6−3 2−4 7 6−3 2+ 7
18 + 5 2
0
28
14
28
0
0
0
1
Last take back to point
𝑀 = 𝑇 𝑃𝑓 𝑅 𝑇 −𝑃𝑓
Suriyong L.
OpenGL transformation matrices
• Use glMatrixMode function for selected
operation to apply
• Most graphic system use Current
Transformation Matrix (CTM) for any
transformation
Current transformation matrix (CTM)
Suriyong L.
CTM step
• Applied Identity matrix unless operate every round
– C <- I
• Applied Translation
– C <- CT
• Applied Scaling
– C <- CS
• Applied Rotation
– C <- CR
• Or postmultiply with M
– C <- M
or C <- CM
Suriyong L.
CTM: Translation, rotation and scaling
• CTM may be viewed as the part of all
primitive product GL_MODELVIEW and
GL_PROJECTION
• From function of glMatrixMode
Model-view and projection matrices
Suriyong L.
Rotation about a fixed point matrix step
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(4.0, 5.0, 6.0);
glRotatef(45.0, 1.0, 2.0, 3.0);
glTranslatef(-4.0, -5.0, -6.0);
//rotate 45 deg. around a vector line 1,2,3
// with fixed point of (4, 5, 6)
Suriyong L.
Modeling more than 1 independent object
• use paradigm of glPushMatrix() and
glPopMatrix()
Suriyong L.
c ο€½ a  ib ο€½ reiq
The quaternion
• Extension of complex number
• Useful for animation and hardware
implementation of rotation
• Consider the complex number
c ο€½ a  ib ο€½ reiq
Where
r ο€½ a b ;
2
2
(a, b)
b
q ο€½ tan
a
ο€­1
θ
• Rotate point (a, b) around z axis with θ radian
• quaternion is equivalent to rotate in 3D space
Suriyong L.
Quaternion form
a ο€½  q0 , q1 , q2 , q3  ο€½  q0 , q 
where
q ο€½  q1 , q2 , q3  ο€½ q1i  q2 j  q3k
and
i 2 ο€½ j 2 ο€½ k 2 ο€½  i ο‚΄ j  οƒ— k ο€½ ο€­1
• q0 define rotation angle
• q define point
Suriyong L.
Multiplication properties
• Identity
– (1, 0)
• Inverse
π‘Ž
−1
1
= 2 π‘ž0 , −π‘ž
π‘Ž
Suriyong L.
Rotation with quaternion
• Let p=(0,p) // point in space with p= (x,y,z)
• And unit quaternion r and its inverse r-1
πœƒ
2
π‘Ÿ = cos , sin
πœƒ
2
πœƒ
2
// v is a unit vector
πœƒ
2
π‘Ÿ −1 = (cos , − sin 𝑣) // inverse of quaternion
r : rotation of θ around a unit vector, thus rotate of point p around v with θ
and p’ is
𝑝′ = π‘Ÿπ‘π‘Ÿ −1
πœƒ
πœƒ
πœƒ
πœƒ
′
2
2
0, 𝑝 = 0, cos 𝑝 + sin
𝑝. 𝑣 𝑣 + 2 sin cos 𝑣 × π‘
2
2
2
2
if v= (0,0,1) //rotate around z axis, p’ will be
𝑝′ = π‘₯. cos πœƒ − 𝑦. sin πœƒ , π‘₯. sin πœƒ + 𝑦. cos πœƒ , 𝑧
Suriyong L.
Quaternion to Rotation Matrix
Replace:
cos πœƒ = cos2
πœƒ
πœƒ
πœƒ
− sin2 = 1 − 2 sin2 ,
2
2
2
πœƒ
πœƒ
sin πœƒ = 2 cos sin ,
2
2
𝑣π‘₯2 + 𝑣𝑦2 + 𝑣𝑧2 = 1
πœƒ 2
𝑣𝑦 + 𝑣𝑧2
2
πœƒ
πœƒ
πœƒ
2
2
sin
𝑣
𝑣
+
2
cos
sin
𝑣
𝑅=
2 π‘₯ 𝑦
2
2 𝑧
πœƒ
πœƒ
πœƒ
2 sin2 𝑣π‘₯ 𝑣𝑧 − 2 cos sin 𝑣𝑦
2
2
2
0
1 − 2 sin2
πœƒ
πœƒ
πœƒ
2 sin2 𝑣π‘₯ 𝑣𝑦 − 2 cos sin 𝑣𝑧
2
2
2
πœƒ
1 − 2 sin2 𝑣π‘₯2 + 𝑣𝑧2
2
πœƒ
πœƒ
πœƒ
2 sin2 𝑣𝑦 𝑣𝑧 − 2 cos sin 𝑣π‘₯
2
2
2
0
Suriyong L.
πœƒ
πœƒ
πœƒ
2 sin2 𝑣π‘₯ 𝑣𝑧 + 2 cos sin 𝑣𝑦
2
2
2
πœƒ
πœƒ
πœƒ
2 sin2 𝑣𝑦 𝑣𝑧 − 2 cos sin 𝑣π‘₯
2
2
2
πœƒ
1 − 2 sin2 𝑣π‘₯2 + 𝑣𝑦2
2
0
0
0
0
1
Using quaternion in OpenGL
• can use only in the rotation transformation
• the other transformations still use the matrix
manipulation.
• when use in OpenGL
– multiplay the object first and transform the
result to matrix form
– use at the display state
Suriyong L.
Case study Trackball
Suriyong L.
Exercise
4.1Consider the solution of either constant-coefficient linear differential
or difference equations (recurrences). Show that the solutions of the
homogeneous equations form a vector space. Relate the solution for a
particular in homogeneous equation to an affine space.
4.2Show that the following sequences commute:
a. A rotation and a uniform scaling
b. Two rotations about the same axis
c. Two translations
4.3Write a library of functions that will allow you to do geometric
programming. Your library should contain functions for manipulating
the basic geometric types (points, lines, vectors) and operations on
those types, including dot and cross products. It should allow you to
change frames. You can also create functions to interface with
OpenGL, so that you can display the results of geometric calculations.
Suriyong L.
4.4 If we are interested in only two-dimensional graphics, we can use threedimensional homogeneous coordinates by representing a point as p = [x y
1]T and a vector as v =[a b 0]T. Find the 3x3 rotation, translation, scaling,
and shear matrices. How many degrees of freedom are there in an affine
transformation for transforming two-dimensional points?
4.5 We can specify an affine transformation by considering the location of a
small number of points both before and after these points have been
transformed. In three dimensions, how many points must we consider to
specify the transformation uniquely? How does the required number of
points change when we work in two dimensions?
4.6 How must we change the rotation matrices if we are working in a lefthanded system and we retain our definition of a positive rotation?
4.7 Show that any sequence of rotations and translations can be replaced by a
single rotation about the origin, followed by a translation.
4.8 Derive the shear transformation from the rotation, translation, and scaling
transformations.
Suriyong L.
4.9
4.10
4.11
4.12
4.13
4.14
In two dimensions, we can specify a line by the equation y = mx + b.
Find an affine transformation to reflect two- dimensional points about
this line. Extend your result to reflection about a plane in three
dimensions,
In Section 4.8 we showed that an arbitrary rotation matrix could be
composed from successive rotations about the three axes. How many
ways can we compose a given rotation if we can do only three simple
rotations? Are all three of the simple rotation matrices necessary?
Add shear to the instance transformation. Show how to use this
expanded instance transformation to generate parallelepipeds from a
unit cube.
Find a homogeneous-coordinate representation of a plane.
Determine the rotation matrix formed by glRotate. That is, assume that
the fixed point is the origin and that the parameters are those of the
function.
Write a program to generate a Sierpinski gasket as follows. Start with a
white triangle. At each step, use transformations to generate three
similar triangles that are drawn over the original triangle, leaving the
center of the triangle white and the three corners black.
Suriyong L.
4.15
4.16
4.17
4.18
4.19
Start with a cube centered at the origin and aligned with the
coordinate axes. Find a rotation matrix that will orient the cube
symmetrically, as shown in Figure 4.65.
We have used vertices in three dimensions to define objects such as
three-dimensional polygons. Given a set of vertices, find a test to
determine whether the polygon that they determine is planar.
Three vertices determine a triangle if they do not lie in the same line.
Devise a rest for co-linearity of three vertices.
We defined an instance transformation as the product of a translation,
a rotation, and a scaling. Can we accomplish the same effect by
applying these three types of transformations in a different order?
Write a program that allows you to orient the cube with one mouse
button, to translate it with a second, and to zoom in and out with a
third.
Suriyong L.
4.20
4.21
Given two nonparallel three-dimensional vectors u and r, how can we
form an orthogonal coordinate system in which u is one of the basis
vectors?
An incremental rotation about the z-axis is determined by the matrix
1
πœƒ
0
0
4.22
−πœƒ
1
0
0
0
0
1
0
0
0
0
1
What negative aspects are there if we use this matrix for a large
number of steps?
Hint: consider points a distance of 1 from the origin. Can you suggest a
remedy?
Find the quaternion for 90-degree rotations about the x- and y-axes.
Determine their product.
Suriyong L.
4.23
4.24
4.25
Determine the rotation matrix
𝑅 = 𝑅(πœƒπ‘₯ )𝑅(πœƒπ‘¦ )𝑅(πœƒπ‘§ )
find the corresponding quaternion.
Redo the trackball program using quaternion instead of rotation
matrices.
Implement a simple package in C++ that can do the required
mathematical operations for transformations. Such a package
might include matrices, vectors, and frames
Suriyong L.
Download