Title: Quaternion and Virtual Trackball
1Quaternion and Virtual Trackball
- CSE 781 Introduction to 3D Image Generation
- Han-Wei Shen
- Winter 2007
2Euler Rotation Problems
- Gimbal Lock lose one degree of freedom
- Problem happens when the axes of rotation line up
on top of each other. For example
Step one Rotate(0, 0,0,1) Step2 Rotate(90,
0,1,0) Step 3 Rotate(??, 0,0,1)
This is same as
rotation x !! ?
3Euler Rotation Problems
- Rotations with Euler angles to change from one
orientation to another are not unique. Example
(x,y,z) rotation to achieve the following
R
R
x
y
y
OR
z
z
x
z
x
R
z
y
R
z
y
Rotate(180, 1,0,0)
Rotate(180, 0,1,0) then Rotate(180,0,0,1) Eule
r angles (0,0,0) -gt (180,0,0)
Euler angles (0,0,0) -gt (0,180,180)
4Quaternion
- Invented in 1843 as an extension to the complex
numbers - Used by computer graphics since 1985
- Quaternion
- Provide an alternative method to specify rotation
- Can avoid the gimbal lock problem
- Allow unique, smooth and continuous rotation
interpolations
5Mathematical Background
- A quaternion is a 4-tuple of real number, which
can be seen as a vector and a scalar - Q qx, qy, qz, qw qv qw, where
- qw is the real part and
- qv iqx jqy kqz (qx, qy, qz)
is the - imaginary part
- ii jj kk -1
- jk -kj i ki-ikj ij-ji k
- All the regular vector operations (dot product,
cross product, scalar product, addition, etc)
applied to the imaginary part qv
6Basic Operations
- Multiplication QR (qv x rv rwqv qwrv,
- qwrw -
qv.rv) - Addition QR (qvrv, qwrw)
- Conjugate Q (-qv, qw)
- Norm (magnitude) QQ QQ
qxqxqyqyqzqzqwqw - Identity i (0,1)
- Inverse Q (1/ Norm(Q)) Q
- Some more rules can be found in the reference
book (real time rendering) pp46
Imaginary
real
-1
7Polar Representation
- Remember a 2D unit complex number
- cosq i sinq e
- A unit quaternion Q may be written as
- Q (sinf uq , cosf) cosf sinf uq, where
uq is a unit 3-tuple vector - We can also write this unit quaternion as
- Q e
iq
uqf
8Quaternion Rotation
- A rotation can be represented by a unit
quaternion Q (sinfuq, cosf) - Given a point p (x,y,z) -gt we first convert it
to a quaternion p ixjykz 0 (pv, 0) - Then, QpQ is in fact a rotation of p around uq
by an angle 2f !!
-1
9Rotation Concatenation
- Concatenation is easy just multiply all the
quaternions Q1, Q2, Q3, . Together - There is a one-to-one mapping between a
quaternion rotation and 4x4 rotation matrix.
(Q3 (Q2 ( Q1 P Q1 ) Q2 ) Q3 )
(Q3Q2Q1) P (Q1Q2Q3 )
-1 -1 -1
-1 -1 -1
10Quaternion to Rotation Matrix
- Given a quaternion w xi yj kz, it can be
translated to the rotation matrix R -
- 1-2y2-2z2 2xy2wz 2xz-2wy
- R 2xy-2wz 1-2x2-2z2 2yz2wx
- 2xz2wy 2yz-2wx 1-2x2-2y2
- Also you can convert a matrix to quaternion (see
the reference book for detail)
11Interpolation of Rotation
- Should avoid sudden change of orientation and
also should maintain a constant angular speed - Each rotation can be represented as a point on
the surface of a 4D unit sphere - Need to perform smooth interpolation along this
4D sphere
R
How to interpolate A and B to get R?
A
B
12Interpolation Rotation
- Spherical Linear Interpolation (slerp)
- Given two unit quaternion (i.e., two
rotations), we can create a smooth interpolation
using slerp - slerp(Q1, Q2, t)
- sin (f(1-t))
sin(ft) - sinf
sinf - where 0lttlt1
- To compute f, we can use this property
- cosf Q1xQ2xQ1yQ2yQ1zQ2zQ1wQ2w
Q1 Q2
133D Rotations with Euler Angles
- A simple but non-intuitive method specify
separate x, y, z axis rotation angles based on
the mouses horizontal, vertical, and diagonal
movements
cos(q) -sin(q) 0 0 sin(q) cos(q) 0
0 0 0 1 0 0 0
0 1
cos(q) 0 sin(q) 0 0 1 0
0 -sin(q) 0 cos(q) 0 0 0
0 1
1 0 0 0 0 cos(q)
-sin(q) 0 0 sin(q) cos(q) 0 0
0 0 1
14Euler Rotation Problems
- Gimbal Lock lose one degree of freedom
- Problem happens when the axes of rotation line up
on top of each other. For example
z
Step one Rotate(0, 1,0,0) Step2 Rotate(90,
0,1,0) Step 3 Rotate(??, 0,0,1)
This is same as
rotation x !! ?
153D Rotations with Trackball
- Imagine the objects are rotated along with a
imaginary hemi-sphere
16Virtual Trackball
- Allow the user to define 3D rotation using mouse
click in 2D windows - Work similarly like the hardware trackball devices
17Virtual Trackball
- Superimpose a hemi-sphere onto the viewport
- This hemi-sphere is projected to a circle
inscribed to the viewport - The mouse position is projected orthographically
to this hemi-sphere
z
y
(x,y,0)
x
18Virtual Trackball
- Keep track the previous mouse position and the
current position - Calculate their projection positions p1 and p2 to
the virtual hemi-sphere - We then rotate the sphere from p1 to p2 by
finding the proper rotation axis and angle - This rotation ( in eye space!) is then applied to
the object (call the rotation before you define
the camera with gluLookAt()) - You should also remember to accumulate the
current rotation to the previous modelview matrix
19Virtual Trackball
- The axis of rotation is given by the normal to
the plane determined by the origin, p1 , and p2 - The angle between p1
- and p2 is given by
n p1 ? p1
sin q
20Virtual Trackball
- How to calculate p1 and p2?
- Assuming the mouse position is (x,y), then the
sphere point P also has x and y coordinates equal
to x and y - Assume the radius of the hemi-sphere is 1. So the
z coordinate of P is - Note normalize viewport y extend
- to -1 to 1
- If a point is outside the circle, project
- it to the nearest point on the circle
- (set z to 0 and renormalize (x,y))
21Virtual Trackball
Visualization of the algorithm
22Example
- Example from Ed Angels OpenGL Primer
- In this example, the virtual trackball is used to
rotate a color cube - The code for the colorcube function is omitted
- I will not cover the following code, but I am
sure you will find it useful
23Initialization
- define bool int / if system does not support
- bool type /
- define false 0
- define true 1
- define M_PI 3.14159 / if not in math.h /
- int winWidth, winHeight
- float angle 0.0, axis3, trans3
- bool trackingMouse false
- bool redrawContinue false
- bool trackballMove false
- float lastPos3 0.0, 0.0, 0.0
- int curx, cury
- int startX, startY
24The Projection Step
- voidtrackball_ptov(int x, int y, int width, int
height, float v3) -
- float d, a
- / project x,y onto a hemisphere centered
within width, height , note z is up here/ - v0 (2.0x - width) / width
- v1 (height - 2.0Fy) / height
- d sqrt(v0v0 v1v1)
- v2 cos((M_PI/2.0) ((d lt 1.0) ? d
1.0)) - a 1.0 / sqrt(v0v0 v1v1
v2v2) - v0 a v1 a v2 a
25glutMotionFunc (1)
- Void mouseMotion(int x, int y)
-
- float curPos3,
- dx, dy, dz
- / compute position on hemisphere /
- trackball_ptov(x, y, winWidth, winHeight,
curPos) - if(trackingMouse)
-
- / compute the change in position
- on the hemisphere /
- dx curPos0 - lastPos0
- dy curPos1 - lastPos1
- dz curPos2 - lastPos2
-
26glutMotionFunc (2)
- if (dx dy dz)
-
- / compute theta and cross product /
- angle 90.0 sqrt(dxdx dydy dzdz)
- axis0 lastPos1curPos2
- lastPos2curPos1
- axis1 lastPos2curPos0
- lastPos0curPos2
- axis2 lastPos0curPos1
- lastPos1curPos0
- / update position /
- lastPos0 curPos0
- lastPos1 curPos1
- lastPos2 curPos2
-
-
- glutPostRedisplay()
-
27Idle and Display Callbacks
- void spinCube()
-
- if (redrawContinue) glutPostRedisplay()
-
- void display()
- glClear(GL_COLOR_BUFFER_BITGL_DEPTH_BUFFER_B
IT) - if (trackballMove)
-
- glRotatef(angle, axis0, axis1, axis2)
-
- colorcube()
- glutSwapBuffers()
28Mouse Callback
- void mouseButton(int button, int state, int x,
int y) -
- if(buttonGLUT_RIGHT_BUTTON) exit(0)
- / holding down left button
- allows user to rotate cube /
- if(buttonGLUT_LEFT_BUTTON) switch(state)
-
- case GLUT_DOWN
- ywinHeight-y
- startMotion( x,y)
- break
- case GLUT_UP
- stopMotion( x,y)
- break
-
-
29Start Function
- void startMotion(int x, int y)
-
- trackingMouse true
- redrawContinue false
- startX x
- startY y
- curx x
- cury y
- trackball_ptov(x, y, winWidth, winHeight,
lastPos) - trackballMovetrue
30Stop Function
- void stopMotion(int x, int y)
-
- trackingMouse false
- / check if position has changed /
- if (startX ! x startY ! y)
- redrawContinue true
- else
-
- angle 0.0
- redrawContinue false
- trackballMove false
-