Download presentation
Presentation is loading. Please wait.
Published byBlake Burke Modified over 9 years ago
1
3D Transformation
2
In 3D, we have x, y, and z. We will continue use column vectors:. Homogenous systems:. 3D Transformation glVertex3f(x, y,z);
3
A Right-Handle Coordinate System
4
Transformation: 3D Translation Given a position ( x, y, z ) and an offset vector ( tx, ty, tz ):
5
Transformation: 3D Scaling Change the object size:
6
Transformation: 3D Rotation Rotation needs an angle and an axis. Rotation is defined according to the right-hand rule (our convention).
7
Transformation: 3D Rotation AxisMatrix Rotate around X: glRotatef(θ, 1, 0, 0); Rotate around Y: glRotatef(θ, 0, 1, 0); Rotate around Z: glRotatef(θ, 0, 0, 1);
8
How to quickly remember them… The axis coordinate is unchanged. For the other two coordinates: Diagonals are filled with cos. Off-diagonals are filled with sin. There is a sign…
9
How to quickly remember them… Here is an easier way to think about it: The vector before rotation The vector after rotation
10
How to quickly remember them… X axis What X becomes After rotation Y axis What Y becomes After rotation Z axis What Z becomes After rotation
11
For example X Y Z θ 1 θ X Y Z
12
Generic Rotation Use the rotation function: (x, y, z) θ glRotatef(theta, x, y, z); In degree No need to be normalized Don’t need to remember it, just for your reference.
13
What if I don’t like a transformation, how do I get back? How to reverse a transformation
14
Inverse Translation glTranslatef(tx, ty, tz); glTranslatef(-tx, -ty, -tz);
15
Inverse Scaling glScalef(sx, sy, sz); glScalef(1/sx, 1/sy, 1/sz);
16
Inverse Rotation glRotatef(theta, 1, 0, 0); Rotate_X by θ glRotatef(-theta, 1, 0, 0); Rotate_X by - θ
17
Inverse Transformation The transformation has multiple matrices: Its inverse: Scaling Translation Rotation Translation M1M1 M2M2 M3M3 M4M4 v v’= Translation Rotation Translation Scaling v v’=
18
OpenGL handles multiple matrices glLoadIdentity(); glRotatef(…); glTranslatef(…); glScalef(…); glTranslatef(…); glBegin(GL_POINTS); glVertex3fv(v); glEnd();
19
OpenGL handles multiple matrices glLoadIdentity(); glRotatef(…); glTranslatef(…); glScalef(…); glTranslatef(…); glBegin(GL_POINTS); glVertex3fv(v); glEnd(); Define v Translation Scaling Translation Rotation Reverse Order
20
OpenGL has a reason. glRotatef(…); //A glTranslatef(…);//B glScalef(…); //C glVertex3fv(v); Define v v=Cv v=Bv v=Av We think: v is transformed in a world coordinate system. Given a world local1=A(world) local2=B(local1) local3=C(local2) Define v in local3 OpenGL think: world is moved into local. Each transformation is defined respect to the local.
21
For example glTranslatef(3,2,0); glVertex3f(2,2,0); We think (2, 2) moves by an offset (3, 2). OpenGL thinks the coordinate System moves by an offset (3, 2). The vertex defines at (2, 2) locally.
22
For example glRotatef(45,0,0,1); glVertex3f(4,0,0); We think (4, 0) rotates 45 degree. OpenGL thinks the coordinate System rotates 45 degree. The vertex defines at (4, 0) locally.
23
For example glRotatef(45,0,0,1); glTranslatef(4,0,0); glVertex3f(0,0,0); We think (0, 0) first Translates, then rotates. OpenGL thinks the coordinate System first rotates, then translates.
24
For example glRotatef(45,0,0,1); glTranslatef(4,0,0); glBegin(…); glVertex3f(0,0,0); glEnd(); Important Note: The second translation is defined respect to L1, not W! OpenGL thinks the coordinate System first rotates, then translates. L1 L2 W
25
A quiz glRotatef(45,0,0,1); glTranslatef(4,0,0); glVertex3f(0,0,0); L2 W (2.8, 2.8) L1 glTranslatef(2.8,2.8,0); glRotatef(45,0,0,1); glVertex3f(0,0,0); L1 L2 W
26
What if we change the order? glRotatef(45,0,0,1); glTranslatef(2.8,2.8,0); glVertex3f(0,0,0); L1 L2 W Important Note: The second transformation is defined respect to L1, not W!
27
Order is important. L2 W (2.8, 2.8) L1 glTranslatef(2.8,2.8,0); glRotatef(45,0,0,1); glVertex3f(0,0,0); glRotatef(45,0,0,1); glTranslatef(2.8,2.8,0); glVertex3f(0,0,0); L1 L2 W
28
A quiz W (3.0, 4.0) L1 glTranslatef(3,4,0);
29
A quiz W (3.0, 4.0) L1 glTranslatef(3,4,0); glRotatef(45,0,0,1); L2
30
A quiz W (3.0, 4.0) L1 glTranslatef(3,4,0); glRotatef(45,0,0,1); L3 glScalef(1,2,1);
31
A quiz W (3.0, 4.0) L1 glTranslatef(3,4,0); glRotatef(45,0,0,1); L3 glScalef(1,2,1); L4 glTranslatef(3,1,0);
32
A quiz W (3.0, 4.0) L1 glTranslatef(3,4,0); glRotatef(45,0,0,1); L3 glScalef(1,2,1); L4 glTranslatef(3,1,0); glVertex3f(3,0,0);
33
How do I know the coordinate system? W L3 OpenGL only use a ModelView matrix M. M is transformed in various ways. M ’s effect is to update each vertex as:
34
How do I know the coordinate system? W L3 Case 1: If we draw a dot at (0, 0, 0) in L3, we actually get (d, h, l) in W.
35
How do I know the coordinate system? W L3 Case 2: If we draw a dot at (1, 0, 0) in L3, we actually get (a+d, e+h, i+l) in W.
36
How do I know the coordinate system? W L3 Case 2: In other words, a unit x vector in L3 is (a, e, i) in W.
37
How do I know the coordinate system? W L3 Case 2: In other words, a unit y vector in L3 is (b, f, j) in W.
38
How do I know the coordinate system? W L3 L3’s Origin in W L3’s Y in W L3’s X in WL3’s Z in W ModelView matrix is the coordinate system L3:
39
Summary Alternatively, OpenGL thinks: A transformation updates the coordinate system. For each change, the transformation is defined in the current (local) coordinate system. Transformation matrix and coordinate system are the same. The code is in the forward order! If we think: Each transformation updates the vertex in an absolute world. Then, the code is arranged in a reverse order.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.