transformation in open GL. why use open GL modes

jawadsafee 10 views 33 slides May 05, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

transformation in open GL.


Slide Content

INSTRUCTOR: Sabina Irum 1

#include<GL/gl.h>
#include<GL/glut.h>
GLfloatangle=0.0;
GLfloattangle=0.0;
voidcube( ){
glPushMatrices();
glTranslatef(1,0,0 ,10);
glRotatef(Angle,1.0,0.0,0.0);
glRotatef(Angles,0.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);
glutWireCube(2);
glPop();}
voidcube2( ){
glPushMatrices();
transformations
glTranslatef(–1,0,0);
glRotatef(tangle,0.0,0.0);
glColor3f(0.0,1.0,0.0);
glutWireCube(2);
glPopMatrices();}
voiddisplay( )
{glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
cube();
cube2(); }
intmain(intargc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(“AbasicOpenGLWin
dow“);
glutDisplayFunc(mydisplaYY);
glutMainLoop();
return0;
}

Transformations
Why use transformations?
Create object in convenient coordinates
Reuse basic shape multiple times
Learn how to carry out transformations in
•OpenGL
-Rotation
-Translation
-Scaling
•• Introduce OpenGL matrix modes
–Viewing transformation(Projection)
•Used for positioning and aiming a camera.
–Modeling transformation.(Model-view)
•Used for positioning and orienting the model.
3INSTRUCTOR: Sabina Irum

CURRENT TRANSFORMATION MATRIX (CTM)
•Conceptuallythereisa4x4homogeneous
coordinate matrix, the current
transformationmatrix(CTM)thatispartof
thestateandisappliedtoallverticesthat
passdownthepipeline
•TheCTMisdefinedintheuserprogramand
loadedintoatransformationunit
CTMismanipulatedbydoingthefollowingto
theobject.
Translatingtheobject.
Scalingtheobject.
Rotatingtheobject.
INSTRUCTOR: Sabina Irum 5

Transformations
•Translation
INSTRUCTOR: Sabina Irum 6cba,,

Translation:
Translate(a,b,c)
x’ 1 0 0 tx x
y’ = 0 1 0 ty * y
Z’ 0 0 1 tz z
1 0 0 0 1 1









z
y
x 









z
y
x
t
t
t
= +









'
'
'
z
y
x
x’ 1 0 0 a x
y’ = 0 1 0 b * y
Z’ 0 0 1 c z
1 0 0 0 1 1
glTranslatef(a,b,c);
glTranslated(a,b,c);
INSTRUCTOR: Sabina Irum 7

Transformations
•Scaling
INSTRUCTOR: Sabina Irum 8




































111
'
'
'
z
y
x
c
b
a
z
y
x
glScalef(a,b,c);
glScaled(a,b,c);
scale(a,b,c)
x’ Sx 0 0 0 x
y’ = 0 Sy 0 0 * y
Z’ 0 0 Sz 0 z
1 0 0 0 1 1

Rotations (2D)
INSTRUCTOR: Sabina Irum 9

sin
cos
ry
rx

 

cos)sin(sin)cos('
sin)sin(cos)cos('
rry
rrx

 )sin('
)cos('




ry
rx 

cossinsincos)sin(
sinsincoscos)cos(

   

cossin'
sincos'
yxy
yxx

 yx, ','yx x y

Rotations 2D
So in matrix notation
INSTRUCTOR: Sabina Irum 10














 









y
x
y
x


cossin
sincos
'
'

Rotations (3D)
INSTRUCTOR: Sabina Irum 11








 
























100
0cossin
0sincos
)(
cos0sin
010
sin0cos
)(
cossin0
sincos0
001
)(








z
y
x
R
R
R

OPENGL ROTATION MATRIX
INSTRUCTOR: Sabina Irum 12
x’ x
y’ = M * y
Z’ z
1 1

2D transformations
INSTRUCTOR: Sabina Irum 13
©Marc Levoy

Combining Translation & Rotation
INSTRUCTOR: Sabina Irum 14)1,1(T )45(R )45(R )1,1(T

Example Revisit
We want rotation and then translation
Generate wrong results if you do:
INSTRUCTOR: Sabina Irum 15
glRotated(60,0,0,1);
glTranslated(5,0,0);
glBegin()
glTranslated(5,0,0);
glRotate(60,0,0,1);
glBegin()

You need to specify the transformation
in the opposite order!!

HOW STRANGE …
OpenGLhasitsreason…
Itwantsyoutothinkoftransformationina
differentway.
Insteadofthinkingoftransformtheobjectina
fixedglobalcoordinatesystem,
youshouldthinkoftransforminganobjectasmoving
(transforming)itslocalcoordinatesystem.
INSTRUCTOR: Sabina Irum 16

WhenusingOpenGL,weneedtothinkofobject
transformationsasmoving(transforming)an
object’slocalcoordinateframe.
Allthetransformationsareperformedrelativeto
thecurrentcoordinateframeoriginandaxes.
INSTRUCTOR: Sabina Irum 17
OpenGL Transformation

Translate Coordinate Frame
INSTRUCTOR: Sabina Irum 18
Translate (3,3)?

Translate Coordinate Frame (2)
INSTRUCTOR: Sabina Irum 19
Translate (3,3)?

Rotate Coordinate Frame
INSTRUCTOR: Sabina Irum 20
Rotate 30 degree?
30 degree

Scale Coordinate Frame
INSTRUCTOR: Sabina Irum 21
Scale (0.5,0.5)?

Compose Transformations
INSTRUCTOR: Sabina Irum 22
(7,9)
45
o
Answer:
1.Translate(7,9)
2.Rotate 45
3.Scale (2,2)
Transformations?

Another example
INSTRUCTOR: Sabina Irum 23
(5,5)
60
o
How do you transform from C1 to C2?
Translate (5,5) and then Rotate (60)
OR
Rotate (60) and then Translate (5,5) ???
Answer: Translate(5,5) and then
Rotate (60)
C1
C2

Another example (cont’d)
60
o
If you Rotate(60) and then Translate(5,5) …
60
o
55
C1 C2
You will be translated (5,5)
relative to C2!!
INSTRUCTOR: Sabina Irum 24

Transform Objects
INSTRUCTOR: Sabina Irum 25
Does coordinate frame transformation have
anything to do with object transformation?
Yes,
you can view transformation as paste the
object to a local coordinate frame
and move that coordinate frame.

Example
Old way: Transformation as moving
the object relative to the origin of a
global world coordinate frame.
(5,0)
60
o
1)Rotate ( )
2)Translate (5,0)
60
o
INSTRUCTOR: Sabina Irum 26

Example (cont’d)
If you think of transformations as
moving the local coordinate frame.
1)Translate (5,0)
2) Rotate ( ) 60
o
(5,0)
60
o
Exact the opposite order compared to the
previous slide!!
INSTRUCTOR: Sabina Irum 27

Put it all together
When you use OpenGL …
•Think of transformation as moving coordinate
frames.
•Call OpenGL transformation functions in that order.
•OpenGL will actually perform the transformations in
the reverse order.
•Everything will be just right!!!
INSTRUCTOR: Sabina Irum 28

INSTRUCTOR: Sabina Irum 29

INSTRUCTOR: Sabina Irum 30

INSTRUCTOR: Sabina Irum 31

SUMMARIZING TRANSFORMATIONS
Translation
glTranslatef(dx, dy, dz)
can be used to shift an object in space
Rotation
glRotatef(theta, vx, vy, vz);
Note that thetais in degrees, and (vx, vy, vz) define the axis of rotation
•scaling
glScalef( sx, sy, sz)
Each has a float (f) and double (d) format (e.g., glScaled)
INSTRUCTOR: Sabina Irum 32

OPENGL STACK MATRIX
PUSH AND POP MATRIX STACK
•glTranslate(5,0,0)
•Draw_base();

glPushMatrix();
•glRotate(75, 0,1,0);
•Draw_left_hammer();
•glPopMatrix();

glRotate(-75, 0,1,0);
•Draw_right_hammer();
INSTRUCTOR: Sabina Irum 33

Example (cont’d)
(5,0)
INSTRUCTOR: Sabina Irum 34
(0,0)
Tags