0% found this document useful (0 votes)
51 views114 pages

2d Geometric Transformation

Uploaded by

Bhanushree K J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views114 pages

2d Geometric Transformation

Uploaded by

Bhanushree K J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 114

MODULE -2

Fill area Primitives, 2D Geometric Transformations and 2D viewing: Fill


area Primitives:
Polygon fill-areas, OpenGL polygon fill area functions, fill area attributes, general
scan line
polygon fill algorithm, OpenGL fill-area attribute functions. 2DGeometric
Transformations:
Basic 2D Geometric Transformations, matrix representations and homogeneous
coordinates.
Inverse transformations, 2DComposite transformations, other 2D transformations,
raster
methods for geometric transformations, OpenGL raster transformations, OpenGL
geometric
transformations function, 2D viewing: 2D viewing pipeline, OpenGL 2D viewing
functions
OVERVIEW

• Two dimensional
transformations
• Matrix representations
• Inverse transformations
• Three dimensional
transformations
Vertices
• We have always represented vertices as
(x,y)
• An alternate method is:

• Exampl
e: x
( x, y)  
 y

2.1
(2.1,4.8)  
4.8 
Matrix * Vector

 x' a b  x   x' a b c  x 
 y'   c  y 
d       f 
 y 
 z'y' gd

x'  ax  x'  ax  by i   z
 
cz y'  dxe 
by y'  cx
ey  fz z' 
 dy
gx  hy  izh
0 1 0
I
1
   00
0 1 0 1

I  0 1
Matrix * Matrix

y Does A*B = B*A?

a b   x
What does the identity
A ,B  do?

c d z w
 ax  bz ay  bw
A* B 
 cx  dz cy  dw
a b 1 0
?   0
c d  1
Geometric transformation functions
• Translatio
n
• Rotation
• Scaling
• Reflection
• Shearing
Transformations
• Rigid Body Transformations - transformations that do not
change the object.
• Translate
– If you translate a rectangle, it is still a rectangle
• Scale
– If you scale a rectangle, it is still a rectangle
• Rotate
– If you rotate a rectangle, it is still a rectangle
Translation
• Translation - repositioning an object along a straight-line
path (the
translation distances) from one coordinate location to
another.

(x’,y’)

(tx,ty)

(x,y)
2D Translations

Point P defined as P(x, y),

translate to Point P(x, y) a distance d x parallel to x axis, d y parallel to y


axis.

x  x  d x y   y  d y
Define  x  vectors
 x the column d
P   , P   ,T  x P’
y y d y

 P


Now

P  P 
T
Translation
• Given P  (x, y) P  (3.7,4.1)
: T T  (7.1,8.2)
(t x ,t y )
x'  x  x'  3.7 
• We
ty'x  y 7.1
want: y
t y'  4.1
8.2
• Matrix  x'   3.7   7.1
x'
    x  t     
 y'      t   y'  4.1  8.2
 
x
form:
   y   y  x' 
P'  P 
3.4
T
y' 
4.1
Translation Examples
• P=(2,4), T=(-1,14), P’=(?,?)
• P=(8.6,-1), T=(0.4,-0.2),
P’=(?,?)
• P=(0,0), T=(1,0), P’=(?,?)
2D Scaling from the origin.

Point P defined as P(x, y),


Performa scale(stretch)to Point P(x, y) by a factor s x along the x
axis, and s y along the y axis.

x  s x .x, y  s y .y
Define the P’
matrix P
 sx 0
S
 0 s y 
Now

 x  s x 0   x
P  S  P or y    0 .
s y  y  
Scale
P  (x, y) P
• Given: (1.4,2.2)
S  (s x ,
sy ) S  (3,3)

x'  s x x' 
• We 3*1.4
x y' 
y'  3*
want: sy y x' 3 031.4
2.2 
   2.2
 y' 0 
 x' s x 0  x
 y'   0    x'  
• Matrix form: sy  y 
 4.2
P'  S 
y' 
P
6.6
2D Rotation about the origin.


P’(x’,y’)

P(x,y)
r

r
x
2D Rotation about the origin.

P’(x’,y’)

P(x,y) x
r
r.cos

r
y y
 r.sin
x
Rotation
• Rotation - repositions an object along
a circular path.
• Rotation requires an  and a pivot
point
Rotation

P  (x, y) x'  r cos  cos  r sin 


R  ( ) sin  y'  r cos  sin   r
x  r cos  sin  cos x'  x cos  y
y  r sin  sin 
x'  r cos(  y'  x sin   y cos
 x' cos
sin cos
sin   yx
)   
 y' 
y'  r cos(  P'  R
) P 
2D Rotation about the origin.

x  r.cos(   )  r.cos .cos 


y r.sin  .sin 
y  r.sin(    )  r.cos.sin  
P’(x’,y’
r.sin  .cos 
P(x,y)
r x
r.cos
 y
y
r
 r.sin
x
x
2D Rotation about the origin.

x  r.cos(   )  r.cos.cos 

r.sin .sin  y  r.sin(   )  r.cos  .sin 


 r.sin  .cos 

Substituting for r :

x  r.cos
y  r.sin
Gives us :

x  x.cos  y.sin
2D Rotation about the origin.

x  x.cos  y.sin

y  x.sin 
y.cos

Rewriting sin
in matrix  . 
form gives us : cos y
 y  

 x  cos
    sin 

Define  the matrix R  ,
cos   x 
sin  sin cos  P  R  P
Example
• P=(4,4)
• =45
degrees
Transformations.

• Translation.
– P=T + P
• Scale
– P=S  P
• Rotation
– P=R  P
• We would like all transformations to be
multiplications so we can concatenate them 
express points in homogenous coordinates.
Homogeneous coordinates
• Add an extra coordinate, W, to a point.
– P(x,y,W).
• Two sets of homogeneous coordinates represent the same point if
they are a multiple of each other.
– (2,5,3) and (4,10,6) represent the same point.
• At least one component must be non-zero  (0,0,0) is not defined.
• If W 0 , divide by it to get Cartesian coordinates of point
(x/W,y/W,1).
• If W=0, point is said to be at infinity.
Homogeneous coordinates
• A standard technique to expand each 2D co-ordinate position
representation (x,y) to a 3 element representation (xh,yh,h) called
homogenous coordinate
• If we represent(x,y,h) in 3-space, all triples representing the
same point describe a line passing through the origin.
• If we homogenize the point, we get a point of form (x/h,y/h,h/h)
=(x,y,1) W P
– homogenised points form a plane at h=1.

W=1 plane

Y
Translations in homogenised
coordinates

• Transformation matrices for 2D


translation are now 3x3.

 x   1 0 d x   x  x  x  d x
 y   0 d y . y 
1 y  y  d y
  1  0 
 0 1  11
 1 
Concatenation.

• We perform 2 translations on the same


point:
P  P T (d x1 , d y1 )

P  P T (d x 2 , d y 2 )

P  P T (dx1 , d y 1 ) T (d x 2 , d y 2 )  P T (dx1  d x 2 ,
dy 1  d y 2 )

So we expect :
T (dx1 , d y 1 ) T (d x 2 , d y 2 )  T (dx1  d x 2 , d y 1  d y 2 )
Concatenation.

The matrix product T (dx1 , d y 1 ) T (d x 2 , d y 2 )


is :
1 0 d x1  1 0 d x 2 
0 d .0
d 
1 y1   1 y2
0
0 1  0 1 ?
Matrix product is variously referred
 to as
 0
compounding, concatenation,or composition
Concatenation.

The matrix product T (d x1 , d y 1 ) T (d x 2 , d y 2 )


is :
1 0 d x1  1 0 d x 2  1 0 d x1  d x 2 
0 d .0 d   0 y1 y2
1 y1   1 y2 1 d
0 0 
0 1  0 1 
d 
 0 
0
Matrix product is variously referred to as
compounding, concatenation, or composition.
This single matrix is called the Coordinate 1Transformation
Matrix or CTM.
Properties of translations.

1. T (0,0)  I
2. T (s x , s y ) T (t x , t y )  T (s x  t x , s y 
ty )
3.T (s x , s y ) T (t x , t y )  T (t x , t y ) T
(s x , s y ) 4. T-1 (s , s )  T (s
,s )
Note : 3. translation matrices are
commutative.
Homogeneous form of Scale.

Recall the (x,y) form of Scale :  sx 0


S (s x , s y )
 0 s 
 y

In homogeneous coordinates :

 sx 0 0
S (sx , sy )   0 sy 0
 0 1
0
Concatenation of Scales.

The matrix product S (s x1 , s y1 )  S (s x 2 , s y


2 ) is :
sx1 0 0  sx2 0 0  sx1 s x2 0 0
 0 s y1 0 . 0 y2 0    0 s y1 y2 0
 s0 1   s 0 0 1
0 0 0 
Only diagonal elementsin the1  matrix - easy tomultiply!

Homogeneous form of rotation.

 x  cos  sin 0   x
 y    sin cos 0 . y 
 1  0
 
 
0 1 
For rotation matrices,  1 
R 1 ( )  R( ).
Rotation matricesare orthogonal, i.e
:
R 1 ( )  R T ( )
i.e. the inverse is the transpose
Orthogonality of rotation matrices.

cos  sin  0   cos sin  0 


R( )  0  , R T ( )    0 
cos cos
 sin   0 1  0 1 
 sin 
 
0 0
sin 
cos  0   cos sin  0 
R( )  sin  cos 0 0     sin  0 
cos
 0 0 1 
  
1  0

Other properties of rotation.

R(0)  I
R( )  R( )  R(   )

and
R( )  R( )  R( )  R( )
only if the centresof rotation are the
same, Otherwise order matters.
2D Composite Transformations
• Combine transformations of different type
– translate, rotate, translate
– translate, scale, translate
– translate, reflect, translate
• Use to rotate or scale an object w.r.t. a point that is not
the origin
• Implement by multiplication of the corresponding
homogeneous matrices
Translation
• Given
P  (3.7,4.1)
: P  (x, y) T  (7.1,8.2)
T  (t x , t y
• We ) x'  3.7 
want: x'  x  7.1
tx y'  4.1
• Matrix y'  y  8.2
form: ty
 x'  x   t x   x'   3.7   7.1
        y'   4.1  8.2
 y'  y  t 
y

P'  P  x' 
T 3.4
Transformation in Homogeneous Coordinates
x’=x+

tx 2D-Translation
y’=y+ty
1 0  x'  1 0 tx   x 
 y '  0 ty    y 
  1
 0 1   1  0 
tx  0 1  1

T  0 1 ty  1 0 2 2 4 4 2 4 6 6 4
(2,2
0 1 3   2 1 1 5 
(4,2
) 0 )
5 4 4 
0 1  1= 1
 0 2 1 1 1 1 1 
1
 (4,5 (6,5
) )
(4,1
(2,1 )
)

tx=2, 3
ty=3 (4,4
(6,4 8
Scaling
Scaling is a non rigid body
transformation, transformed object
can be bigger or smaller Scaling
transformation can be
a) Fixed point ,
b) Direction of the scaling,
c) Scaling factor( )
For  > 1, the object gets longer
in the specified direction. x'   x
For 0<= < 1, the object gets smaller x y' 
in that direction. y y
The negative value of  gives the  x'   x 0  x
reflection about a fixed point in the     4
 y'  0  y  y 0
Scaling transformation in
Homogeneous
Coordinates
(2,2 (4,2
)
x 0
)

S 0 0 y 0
 0 1
 
0  (4,1
(2,1 )
)

With respect to origin Scaling in x direction by 2


units , y direction no change

2 0 0 2 4 4 2 4 8 8
0 0  1  
 1 

0 4 
1 1 1

1 1 2 
2 1 21 2 1 
0 1 1 1 1
40
Transformations Concatenation
Concatenation – q=Mp
well known
strategy q = C B
Ap
interpretatio
n
q = C (B (A p) )

Transformati
on

41
Scaling with a arbitrary fixed point
Move to Scal Move
origin e back

x f , yf

4
4

1 0 xf   S x 0 0  1 0 x f   S x 0 x f 1  S x  
    
0

1
yf   
S 0  1 y 
f  0 Sy
 yf
y 
y

1   0 1 S  
0 0
0 1 
0 0 0
0
1 0 1

 0
Scaling with a arbitrary fixed point
4,8


1 0 x f   S x 0 0 1 0 x f S
 x 0 x f 1 Sx
0 0
 1 y  Sy 
0  0 1  y 
 y f 1 Sy 
3,
  
6 f
0 0 1  
f
0 1 

5
4
0 Step1:0Move 1toorigin
0 0 translate (inverse
2, 6, translate)
  0 Sy
4 4 1 
0
Step2 :Scale at the origin sx=2,
1 0  3 2
sy =3
0  6  8   2
0 1  6 14   1 1
2 0 0  1 3 1  2 6 4 2
1 1 
0 0  2 2   6 3 1 1
0 1  1  1 Step3: translate back to original
2 6 position
4
1 3 2 1 6 
0 1 1 1 0 3  2 6 2 1  92 5
0 6  6 
 0 
0   1 1
 0 1  112 1
6  
Scaling with a arbitrary fixed point
4,8

3,
6

2, 6, Scaling factors sx=2, sy xf=3,yf


4 4 =3, =6

1 0 x f  S x 0 0 1 0 x f 
S
 x 0 x f 1 Sx
0 0
 1 y  Sy 0   0 1  y  y f 1 Sy 
 
  
f
0 0 1  
0 f 1
0  1
0 1 
0   0 Sy
2 0  3  20 6

4 1 9 05
0 12 8   0 12

 1 1 1
4 0  4
0 3  41 6
2D Rotation about a fixed point
Move to Rotat Move
origin e back

 xr , y r

1 0 xr   c o s  sin  0 1 0 xr 
 0 1 y r     s i n c o s 0    0  1y r  
0 1   0 1 0 10
0  0

cos 
 sin  x r 1  c o s    y r s i n  
 s i n  y r  1  c o s    x r s i n  
 0 cos 
1  5
0 1
Transformation in Homogeneous Coordinates

3D - Translation x’=x+tx, y’=y+ty, z’=z+tz


Translation
1 0 0 tx  x' 1 0 0 tx   x 
0 1 0 ty   y' 0
1 ty   y y
T  0     
0 0 1  z'  0 tz   z 
 
1  1  0 0  1 
1
tz    
Line segment with end points 0
(0,0,0)0 & (-5,-4,
0 3),
1
tx=2,ty=5,tz=1
1 0 0 2  0  5  0 2  3
0 
1 0 5  * 0  4 = 5 1 
x , y ,
0 1 0  3 01  4
0
0 1
  
1 
x, y, z 
0 0 1  1 1  1 
Inverse translation is T-1=[-
x
tx,-ty,-tz]
z 3
9
3D Scaling
y y
x  x 
Sx

y  y 
Sy
z x z x
Enlarging object also moves it from
z  x 
origin Sz
 x   Sx 0 0 0  x
 y 0 0  
Sy 0  y
P    z     0  S
1 0 0 Sz P
     
0 z
0 0
1  
1
Scaling transformation in
Homogeneous
Coordinates
Scaling matrix with fixed point of the origin
allows for independent scaling along the
coordinate
The equation axes.
(in 3
D x
2D) x’= x x  x’= x
y’=y

y’=  y y y
z’=  z
x 0 x 0 0
 z
0

S 0 0 y 0 y
 S 0 
  0 1 0 0
0    0
0  0 
1
0
Inversion S-1 = S ( 1/x , 1/ y , z1/ 4
1
operations:
Scaling with respect to a fixed
point (not necessarily of
object)y
y y

x , yf , z x , yf , z x , yf , z
z f f
z f f
z f f

y
  
x x S x 0 0 1  S x xx f 
 
T  S  T 1 
Sy 1 0S y yf

 0 0 0 Sz 1  S z
 f

x , yf , z  0 1
z f f
 z 
 0 0
 
4
x 8
Rotation
Rotation of object by an angle ϴ
Consider a point (x,y) to rotated about the origin,
to a new location (x’,y’) with an angle theta.
The polar form of (x,y) and (x’,y’)
x = r cos  y = r sin 
x’ = r cos (+) y’ = r sin (+)
x’ = r cos  cos - r sin  sin
= x cos - y sin
y’ = r cos  sin - r sin 
cos
= x sin + y cos 
 x'  cos
sin  cos
sin    xy 
 
 y ' 
4
9
Rotation
Three features of Rotation
1)Rotation about fixed
point figure shows
rotation about a
fixed point is in the
center of the object.

2)Rotation in 3D plane is all


about the axis (x axis or y
axis or z axis). Positive
direction rotation is
counterclockwise.
5
0
Rotation about an axis

Z- axis X-axis Y-axis


3D rotation about an axis in Homogeneous Coordinates

Rotation about z Rotation about y-


axis (XY plane), z axis
values are (XZ plane) (y values
unchanged unchanged)
x’=x cos ϴ - y sinϴ x’ =z sin ϴ+ x cosϴ
y’ =x sin ϴ+ y cosϴ z’=z cos ϴ - x
z’=z sinϴ y’=y
 sin 0 0
 cos
cos 
sin 00 
Rz  sin 00 Ry 
0 1 0 0
 0   sin

  cos 0 
 0  0 1
cos 54
3D rotation about an axis in Homogeneous Coordinates
Rotation about x- Inverse Rotation
axis
(YZ plane) R-1 () = R ( - )
x’=x
Cos(- ϴ) =cos(ϴ)
y’=y cos ϴ - z sinϴ
Sin(- ϴ)= -sin(ϴ)
z’ =y sin ϴ+ z cosϴ
So we can say that
1 0 0 0
 inverse of rotation is
Rx  0
0 cos  sin 0 transpose of rotation
 0 matrix.
  sin  
1 R-1 () = RT ()

cos
0
55
3D Rotation About a Fixed Point

Rotation about the fixed


point
M = T(pf ) Rz(  ) T(- pf )
 sin  0 xf  xf cos  yf
 cos
 sin  sin   yf  xf sin   yf cos 
M   0 0 
cos 0
 
 0 0 1 1  6
3
Rotations of an object

Rz Rx

Cube can be rotated about all


x, y, z axis
In our case the transformation
matrix is defined
M = Rzx Ryz Rxy = Ry Rx Rz
Ry
Fundamentals of Computer 6
Graphics 4
Rotating cube
#include
<stdlib.h>
#include
<GL/glut.h>
GLfloat vertices[][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}};
GLfloat normals[][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},
void polygon(int a, int b, int c ,
int d)
{/* draw a polygon via list of
vertices */
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[
a]);
glVertex3fv(vertices[a
]);
glColor3fv(colors[b]);
glNormal3fv(normals[
b]);
glVertex3fv(vertices[
b]);
glColor3fv(colors[c]);
glNormal3fv(normals[
c]);
void colorcube(void)
{/* map vertices to
faces */
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
static GLfloat theta[] =
{0.0,0.0,0.0}; static GLint axis
= 2;
void display(void)
{/* display callback, clear frame buffer and z
buffer, rotate cube and draw, swap buffers
*/
glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0,
0.0);
glRotatef(theta[1], 0.0, 1.0,
0.0);
glRotatef(theta[2], 0.0, 0.0,
1.0); colorcube();
void spinCube()

{/* Idle callback, spin cube 2 degrees about


selected axis*/
theta[axis] += 0.1;
if( theta[axis] > 360.0 ) theta[axis] -=
360.0;
/* display(); */
glutPostRedisplay
();
}
void mouse(int btn, int state, int x, int y)
{/* mouse callback, selects an axis about which to
rotate */

if(btn==GLUT_LEFT_BUTTON && state


== GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON &&
state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state
== GLUT_DOWN) axis = 2;
}
void myReshape(int w, int h)
{ glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION
); glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 *
(GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat)
w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w /
(GLfloat) h,
2.0 * (GLfloat) w / (GLfloat)
h, -2.0, 2.0, -10.0,
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Rotating a Color
Cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST); /* Enable
hidden
surface--
3D Rotation about arbitrary axis

1. Translate the object such that rotation axis passes


through the origin.
2. Rotate the object such that rotation axis coincides

4
7
with one of Cartesian axes.
3. Perform specified rotation about the Cartesian axis.
4. Apply inverse rotation to return rotation axis to
original direction.
5. Apply inverse translation to return rotation axis to
original position.
Rotations About
an Arbitrary
Given:
Axis
• points p1 , p2 and rotation angle 
• objects to be rotated, p0 is
fixed point, center of the
cube.

Step1. Translate the object to the


origin
T(-P0).
Step2. Aligning the arbitrary axis to the co-ordinate axis . This can
be done in various method, one of the method is rotate the
arbitrary axis with respect x axis, then rotate along the y axis
and the get the aligned with z axis, rotate about the z axis.
R = Rx(-x) Ry(-y) Rz() Ry(y) Rx(x)
Rotations About an Arbitrary Axis
Given:
• points p1 , p2 and
rotation angle 
• objects to be rotated

Define
vectors
u = p1 - p2 -
andv= ux/ ,| y normalize
v =[  , z ]T
u|  + y +d z = 1 – directional
cosines
x
2 2 2

cos( x ) = x , cos( y ) = y , cos( z )


= z
cos2( x ) + cos2( y ) + cos2 ( z ) = 1 7
 only two directions angles 6
Rotations About an Arbitrary Axis

Transformation
R = Rx(-x) Ry(-y) Rz() Ry(y)
Rx(x)

70
Rotations About an Arbitrary Axis
• Object is moved to
the origin
• Rotation about x
axis

1 0 0 0
Rx  x  0 0
z / d   y / d 

0  y / d z / d 10
d  2
2 0 0 0
y
71
z
Rotations About an Arbitrary Axis
• Object is moved to
the origin
• Rotation about y
axis note the “-”
position
d 0  x 0
R y y  0 0
1 0 
 
  x

0 d 0

0 0 0 1
Complete transformation
M = T(p0) Rx(-x) Ry(-y) Rz() Ry(y) Rx(x) T(-
p0 ) 72
2D Reflections

y y y

x x

1 0 0  1 0 0  1 0 0
0 0
0 
1 0  0 0
1  1
   0 0 1   

0 0 1   0 0 1 

8
0
The instance Transformation
Transformation is defined
by the instance
transformation M

M=TRS
(order is substantial!)

Each occurrence of an
object in the scene is an
instance of the object’s
prototype
To obtain proper size,
location, orientation –
instance transformation
74
to the prototype is to be
OpenGL Transformation Matrices

Three matrices as a part of the state in


OpenGL Only Model-View will be used
CMT – current transformation matrix – can be
changed by OpenGL functions – 4 x 4 size
Supported operations: translation, scaling &
rotation – last two with the fixed point in
C the origin
I initialization C  CT translati
on
C  CS scaling C  CR rotation 75
OpenGL Transformation Matrices

Most systems allow to set directly or load or


post- multiply the CMT with an arbitrary
matrix M , scaling & rotation – last two
with the fixed point in the origin

CM loading
C  CM post-multiplication

76
OpenGL Transformation Matrices

OpenGL model-view (GL_MODELVIEW) and


projection (GL_PROJECTION) matrices
(actually their product) are applied to ALL
primitives – we should consider them as
one CMT matrix – can be manipulated
individually using glMatrixMode function

glLoadMatrixf(pointer_to_matrix);
/* vector of 16 position – column first order */
77
OpenGL Transformation Matrices

glLoadIdentity ( ); /* loads identity matrix */

glRotatef(angle, vx, vy, vz); /* f – float used */


/* specifies general rotation angle in degrees,
v – specifies the vector – fixed point P0 is the
origin
*/
glTranslatef(dx, dy, /* translation */
dz); glScalef ( sx, /* scaling 78
sy, sz); */
Rotation about a Fixed Point
glMatrixMode(GL_MODELVIE
W); glLoadIdentity ( );
glTranslatef(4.0, 5.0, 6.0);
glRotatef(45.0, 1.0, 2.0, 3.0);
glTranslatef(-4.0, -5.0, -6.0);
/* rotates objects about the vector (1.0, 2.0,
3.0) with the angle 45° */

79
Transformation Order
The sequence specified
recently:
C  I, initializati
C  CT (4.0, 5.0, on
6.0),
C  CR (45.0, 1.0, 2.0, 3.0),translatio
rotation
C  CT n translation back
(-4.0, -5.0, -6.0),
in each step the CTM matrix is post-multiplied
forming new CTM matrix
C = T (4.0, 5.0, 6.0) R(45.0, 1.0, 2.0, 3.0) T (-4.0, -
5.0, -
6.0)
Each vertex specified after the model-view
matrix has been specified will be
80
multiplied
Loading, Pushing & Popping
glLoadMatrixf(myarray);
/* 4 x 4 matrix of floats -column first order from
a vector
*/
glMultMatrixf(myarray);
/* multiplies the current matrix by user
specified matrix
*/
Sequence example
GLfloat myarray
[16];
for ( i=0; i<3; i+
81
+) for ( j=0;
Loading, Pushing & Popping
Sometimes it is reasonable to return the
transformations back after they have been
applied to some objects.
Instead of re-computation the stack mechanism
can be
utilized
glPushMatrix ( );
/* local transformation specifications
*/ glTranslatef ( .....); ................
/* DRAW OBJECTS HERE */
................
/* recover recent state */
82
glPopMatrix ( );
The LookAt Function
• GLU library contains the function (gluLookAt) to
form the modelview matrix through a simple
interface.

• Need to set up a direction and initialize:


– Concatenate with modeling transformations.

• Example: isometric view of cube aligned with


axes.
glMatrixMode(GL_MODELVIEW):
glLoadIdentity();
gluLookAt(1.0, 1.0, 1.0,
0.0, 0.0, 0.0, 0., 1.0.
CS4395: Computer
0.0);
Graphics 83
Function: gluLookAt()

glLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)

CS4395: Computer
Graphics 84
OpenGL Perspective

glFrustum(left,right,bottom,top,near,far)

CS4395: Computer
Graphics 85
Using Field of View
• With glFrustum it is often difficult to
get the desired view.
• gluPerpective(fovy, aspect, near,
far) often provides a better
interface.

front plane

aspect = w/h

CS4395: Computer raphic


G s 86
1 0 0  x1 
The vector from P1 to P2 is:
T  0 1 0  y1 
z 1 
V  P2  P1   x 2  x1 , y 2  y1 , z 2  z1   0 0 1 1 

0 0 0

Unit rotation vector: u  V | V |  a, b,


a   x 2  x1  | V y
c u   a, b,

| b   y 2  y1  | c
x
2  b  c 
2 2

V a| c   z 2  z1 
1 z
|V|

87
Rotating u to coincide with z axis

First rotate u around x axis to lay in x  z plane.

Equivqlent to rotation u 's projection on y  z plane around x axis.

cos   c b  c
2
2
 c d , sin   b d
.
b2 
W e obtained a unit vector w  a, c2  d  in x  z plane.
0,
y
1 0 0 0
u
0 

R x    0 c d 0  x
0 
  b  u   a, b, c 
1
d b d z
c d
88
0
Rotate w counterclockwise around y axis.

w is a unit vector whose x  component is a, y  component is 0,

hence z  component is b 2c 2



y u   a, b,
d.
cos   d , sin   a
c
 d 0  x
 0 0a
R y 
   a 0 z
 0 0
1 w   a, 0, d
1 0
01 
R   T

1
x   R1y   Rz   y
    Rx   
R R 
d T
0 89
 cos 
 sin  0

 sin  0  0 
R z    0 0
 cos  0
  0 
0 11 
0 0
R    1
 1
  1
  
x y z
 Ry     Rx   
T R R R
T
M R   

ab 1  cos    c ac 1  cos    b
 a 1  cos   
2

 ca1  cos    b
ba 1  cos   c bc  1  cos    a sin 
cos
 sin  b 2 1  cos    c2 
sin 1 cos    cos 
sin  

sin  
cos  cb 1  cos   
a sin 
April 9
2010 7
Reflection

corresponds to negative scale


factors

sx = -1 sy = 1 original

sx = -1 sy = -1 sx = 1 sy = -1
Reflection
•A transformation that produces a mirror

image of an object is called a reflection.


•For a two-dimensional reflection, this image

is generated relative to an axis of reflection


by rotating the object 180◦ about the
reflection axis.
•Reflection about the line y = 0 (the x

axis) is accomplished with the


Reflection
•This transformation retains x
values, but “flips” the y values of
coordinate positions. The resulting
orientation of an object after it
has been reflected about the x axis is
shown below.
Reflection
•Reflection about the line y = 0 (the x
axis) is accomplished with the
transformation Matrix
•A reflection about the line x = 0
(the y axis) flips x coordinates
while keeping y coordinates the
same. The matrix for this
transformation is
Flipping both the x and y
coordinates of a point by reflecting
relative to an axis that is
perpendicular to the xy plane and
that passes through the
coordinate origin the matrix
representation for this reflection is
•If we choose the reflection axis as the diagonal
line y = x
(Figure below), the reflection matrix is
reflection in x and y axes reflection in origin
1 a  x  xa
y
0   y   
 1    y 

1 0   x    xSh gives  the slope


b 
1 y    
bx of ayvertical line
x

     
after shear, as dx/dy
Shear
A transformation that distorts the shape of an object such that
the transformed shape appears as if the object were
composed of internal layers that had been caused to slide
over each other is called a shear.

•Two common shearing transformations are


-- shift coordinate x values
An x-directionshear relative to the x axis is produced with
-- shift coordinate y values.
transformation the
Matrix.
2D Rotation about a fixed point
define BLACK 0
#include
<stdio.h>
#include
<math.h>
#include
<GL/glut.h>

GLfloat house[3]
[9]=
GLfloat rotatemat[3][3]={{0},
{{100.0,100.0,17
{0},{0}};
5.0,250.0,250.0,1
GLfloat result[3][9]={{0},
50.0,150.0,200.0,
{0},{0}}; GLfloat
200.0},
5
arbitrary_x=100.0;
{100.0,300.0,40 GLfloat 2
2D Rotation about a fixed point

void multiply()
{
int i,j,k;
for(i=0;i<3;i
++)
for(j=0;j
<9;j++)
{
result[i][j]=0;
for(k=0;k<3;k
++)

result[i][j]=result[i][j]
+rotatemat[i] 10
7
2D Rotation about a fixed point

void multiply()
{
int i,j,k;
for(i=0;i<3;i
++)
for(j=0;j
<9;j++)
{
result[i][j]=0;
for(k=0;k<3;k
++)
result[i]
[j]=result[i][j]
10
+rotatemat[i] 8
2D Rotation about a fixed point
void rotate()
{
GLfloat m,n;
m=-arbitrary_x*(cos(rotation_angle) -1) +
arbitrary_y * (sin(rotation_angle));
n=-arbitrary_y * (cos(rotation_angle) - 1) -
arbitrary_x * (sin(rotation_angle));
rotatemat[0]
[0]=cos(rotation_angle);
rotatemat[0][1]=-
sin(rotation_angle);
rotatemat[0][2]=m;
rotatemat[1]
[0]=sin(rotation_angle);
rotatemat[1]
[1]=cos(rotation_angle);
rotatemat[1][2]=n;
rotatemat[2][0]=0; 5
5
2D Rotation about a fixed point
void drawhouse()
{
glColor3f(0.0, 0.0, 1.0);

glBegin(GL_LINE_LOOP
);
glVertex2f(house[0]
[0],house[1][0]);
glVertex2f(house[0]
[1],house[1][1]);
glVertex2f(house[0]
[3],house[1][3]);
glVertex2f(house[0]
glBegin(GL_LINE_LOOP);
[4],house[1][4]); glEnd();
glVertex2f(house[0]
[5],house[1][5]);
glVertex2f(house[0][6],house[1]
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
[6]);
glVertex2f(house[0]
glVertex2f(house[0][7],house[1]
[1],house[1][1]);
[7]);
glVertex2f(house[0][2],house[1]
glVertex2f(house[0]
glColor3f(0.0, 0.0,
[2]);
[8],house[1][8]); glEnd();
1.0);
glVertex2f(house[0]
[3],house[1][3]); glEnd();
}
11
0
2D Rotation about a fixed point

void drawrotatedhouse() glBegin(GL_LINE_LOOP);


{ glVertex2f(result[0]
glColor3f(0.0, 0.0, 1.0); [5],result[1][5]);
glVertex2f(result[0][6],result[1]
glBegin(GL_LINE_LOOP); [6]);
glVertex2f(result[0] glVertex2f(result[0][7],result[1]
[0],result[1][0]); [7]);
glVertex2f(result[0][1],result[1] glVertex2f(result[0]
[1]); [8],result[1][8]); glEnd();
glVertex2f(result[0][3],result[1] glColor3f(0.0, 0.0,
[3]); 1.0);
glVertex2f(result[0] glBegin(GL_LINE_LOOP
[4],result[1][4]); glEnd(); );
} glVertex2f(result[0
glColor3f(1.0,0.0,0.0); ][1],result[1][1]); 11
glVertex2f(result[0 1
2D Rotation about a fixed point
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawhouse();
rotate();
drawrotatedhous
e(); glFlush();
}
void myinit()
{
glClearColor(1.0,
1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION
11
} ); glLoadIdentity(); 2
2D Rotation about a fixed point
void main(int argc, char** argv)
{

printf("Enter the rotation angle in


degree :"); scanf("%f",
&rotation_angle); rotation_angle=
(3.14 * rotation_angle) / 180;
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|
GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("house rotation");
glutDisplayFunc(display);
11
}
myinit(); 3

You might also like