Computer Graphics Transformations (Rotations with Quaternions) October 4, 5 Virtual Trackball (/3) Using the mouse position to control rotation about two axes Supporting continuous rotations of objects trackball frame
Virtual Trackball (/3) Rotation with a virtual trackball projection of the trackball position to the plane x + y + z = r y = r x z Virtual Trackball (3/3) Rotation with a virtual trackball (cont ) determination of the orientation of a plane n = p p rotation angle θ = cos Quaternions ( p p ) n θ
Quaternions (/) One real part + three imaginary part q = s + ia+ jb+ kc Properties: i = j = k ij = ji = k jk = kj = i ki = ik = j = Addition and scalar multiplication ( s + s ) + i( a + a ) + j( b + b ) + k( c ) q + + q = c dq = sd + iad + jbd + kcd Quaternions (/) Ordered-pair notation scalar s + vector v = (a, b, c) Addition: q Multiplication q q = s s v v, s v + s v + v v Magnitude q q = + q = s ( s, v) ( s +, v + v ) ( ) = s + v v Inverse ( s, v) qq = q q = (, ) q = q
Quaternions and 3D Rotation For a 3D point (α, β, γ) a unit quaternion q = ( s, a, b, c) q = ( s, a, b, c) q, α, β, γ q =, α, β, γ For q = its conjugate ( ) ( ) Rotating (α, β, γ) by angle θ about the axis parallel to (u x, u y, u z ) ( cosθ, sinθ ( u, u, u ) R q is a 3D rotation about (u x, u y, u z ) by θ R q x y ( p) = q p q z Rotations with Quaternions (/) Rotation about any axis setting up a unit quaternion (u: unit vector) θ θ s = cos, v = usin = ( a, b, c) representing any point position P in quaternion notation (p = (x, y, z)) P =, p ( ) carrying out with the quaternion operation (q - =(s, v)) P = qpq producing the new quaternion P =, p ( ) ( p v) + ( v p) + v ( v p) p = s p + v s
Rotations with Quaternions (/) Obtaining the rotation matrix by quaternion multiplication M R ( θ ) b c = ab+ sc ac sb = R Including the translations x ( θ ) R ( θ ) R ( θ ) R ( θ ) R ( θ ) R x y a y ab sc bc + sa ( θ ) = T M ( θ )T R z c y y ac + sb bc sa a x x b Header File for a Trackball typedef double vec3d[3]; #ifndef M_PI #define M_PI 3.45965 #endif class TrackBall private: int width, height; vec3d lastpos; //quaternions double s; vec3d v; public: double rmat[6]; public: TrackBall(); virtual ~TrackBall(); ; void initialize( void ); void resize( const int &cx, const int &cy ); void project( int &xi, int &yi, vec3d &v ); void start( int xi, int yi ); void end( int xi, int yi );
Vector Operations void normalize( vec3d &v ) double d =. / sqrt( v[]*v[] + v[]*v[] + v[]*v[] ); v[] *= d; v[] *= d; v[] *= d; return; double dot( vec3d &a, vec3d &b ) return (a[]*b[] + a[]*b[] + a[]*b[]); void cross( vec3d &a, vec3d &b, vec3d &c ) c[] = a[]*b[] - a[]*b[]; c[] = a[]*b[] - a[]*b[]; c[] = a[]*b[] - a[]*b[]; return; Projection to a Hemisphere TrackBall::TrackBall() initialize(); TrackBall::~TrackBall() void TrackBall::initialize( void ) s =.; v[] = v[] = v[] =.; for(int i=; i<6; i++) rmat[i] =.; rmat[] = rmat[5] = rmat[] = rmat[5]=.; return; void TrackBall::resize( const int &cx, const int &cy ) width = cx; height = cy; return; void TrackBall::project( int &xi, int &yi, vec3d &v ) v[] = (.*xi - width) / (double)width; v[] = (height -.*yi) / (double)height; double d = sqrt( v[]*v[] + v[]*v[] ); v[] = cos(m_pi *.5 * ((d<.)? d :.) ); normalize( v ); return;
Creation of Quaternions void TrackBall::start( int xi, int yi ) project( xi, yi, lastpos ); return; void TrackBall::end( int xi, int yi ) vec3d currpos, t; project( xi, yi, currpos ); t[] = currpos[] - lastpos[]; t[] = currpos[] - lastpos[]; t[] = currpos[] - lastpos[]; if( t[] t[] t[] ) double angle = M_PI*.5*sqrt( t[]*t[] + t[]*t[] + t[]*t[] ); vec3d axis; cross( currpos, lastpos, axis ); normalize( axis ); double s = sin(angle*.5); vec3d v = s*axis[], s*axis[], s*axis[] ; s = cos(angle*.5); double s = s; vec3d v = v[], v[], v[], v3; cross( v, v, v3 ); s = ( s * s ) - dot( v, v ); v[] = s*v[] + s*v[] + v3[]; v[] = s*v[] + s*v[] + v3[]; v[] = s*v[] + s*v[] + v3[]; Creation of a Rotation Matrix double d =. / sqrt( s*s + v[]*v[] + v[]*v[] + v[]*v[] ); s *= d; v[] *= d; v[] *= d; v[] *= d; // rotation with quaternions // P' = quat * P * quat^- // M = -b^-c^, ab-sc, ac+sb, // ab+sc, -a^-c^, bc-sa, // ac-sb, bc+sa, -a^-b^ ; rmat[] =. -. * (v[]*v[] + v[]*v[]); rmat[] =. * (v[]*v[] - s*v[]); rmat[] =. * (v[]*v[] + s*v[]); //rmat[3] =.; rmat[4] =. * (v[]*v[] + s*v[]); rmat[5] =. -. * (v[]*v[] + v[]*v[]); rmat[6] =. * (v[]*v[] - s*v[]); //rmat[7] =.; rmat[8] =. * (v[]*v[] - s*v[]); rmat[9] =. * (v[]*v[] + s*v[]); rmat[] =. -. * (v[]*v[] + v[]*v[]); //rmat[] =.; //rmat[] = rmat[3] = rmat[4] =.; //rmat[5] =.; lastpos[] = currpos[]; lastpos[] = currpos[]; lastpos[] = currpos[]; return;
Global Variables Initialization & Resize
Mouse Controls DrawScene ( )
Result Virtual Trackball Computer Graphics Viewing October 4, 5
Contents Projections in OpenGL Parallel viewing Orthographic projection Perspective viewing Perspective projection Walk through a scene Look-at function Shadow projection Projections in OpenGL Angle of view only objects that fit within the angle of view of the camera appear in the image View volume being clipped out of scene frustum truncated pyramid
Parallel Viewing Orthographic viewing function glmatrixmode(gl_projection); glloadidentity( ); ); glortho(xmin, xmax, ymin, ymax, near, far); OpenGL provides only this parallel-viewing function near < far!! no restriction on the sign z max = far z min = near Orthographic Projection (/3) Direction of Projection simple camera view frustum a right parallelepiped y x void glortho(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) z
Orthographic Projection (/3) Orthographic Projection (3/3)
Perspective Viewing (/) Specification of a frustum glmatrixmode(gl_projection); glloadidentity( ); ); glfrustum(xmin, xmax, ymin, ymax, near, far); near, far: positive number!! z max = far z min = near Perspective Viewing (/) Specification using the field of view glmatrixmode(gl_projection); glloadidentity( ); ); gluperspective(fovy, aspect, near, far); fovy: the angle of view in the up (y) direction aspect ratio: width divided by height fov: angle between top and bottom planes
Perspective Projection (/5) General viewing projection plane anywhere in front of the camera void glfrustum(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) Perspective Projection (/5)
Perspective Projection (3/5) Perspective Projection (4/5)
Perspective Projection (5/5) Walking Though a Scene (/) void keys(unsigned char key, int x, int y) if(key == x ) viewer[] -=.; if(key == X ) viewer[] +=.; if(key == y ) viewer[] -=.; if(key == Y ) viewer[] +=.; if(key == z ) viewer[] -=.; if(key == Z ) viewer[] +=.; void display(void) glenable( GL_DEPTH_TEST ); glclearcolor(.f,.f,.f,.f); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_modelview); glloadidentity(); glulookat(viewer[], viewer[], viewer[],,,,,,); glrotatef(theta[],.,.,.); glrotatef(theta[],.,.,.); glrotatef(theta[],.,.,.); colorcube( ); glutswapbuffers( );
Walking Though a Scene (/) void myreshape(int w, int h) glmatrixmode(gl_projection); glloadidentity( ); glviewport(,, w, h); if( w <= h ) glfrustum(-.,., -.*(GLfloat)h/(GLfloat)w,.*(GLfloat)h/(GLfloat)w,.,.); else glfrustum(-. *(GLfloat)w/(GLfloat)h,. *(GLfloat)w/(GLfloat)h, -.,.,.,.); return; Look-At Function OpenGL utility function glulookat(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); eye-position, target-position, and up-vector look-at positioning
Definition of Eye Position glulookat ( )
Key Controls Walking Though a Scene
Exercise () Create a menu view and its sub menus orthographic, and perspective. If you choose one, your program must change its projection. Projections & Shadows (/) Shadow polygon Steps light source at (x l, y l, z l ) translation (-x l, -y l, -z l ) perspective projection through the origin translation (x l, y l, z l ) = = / l l l l l l l z y x y z y x PT T M
Projections & Shadows (/) glcolor3fv(polygon_color); glbegin(gl_polygon);.. /* draw the polygon normally */. glend( ); GLfloat m[6]; /* shadow projection matrix */ for(i=; i<6; i++) m[i] =.; m[] = m[5] = m[] =.; m[7] = -./yl; glpushmatrix( ); /* save state */ gltranslatef(xl, yl, zl); /* translate back */ glmultmatrixf(m); /* project */ gltranslatef(-xl, -yl, -zl); /* move light to origin */ glcolorfv(shadow_color); glbegin(gl_polygon);.. /* draw the polygon again */. glend( ); glpopmatrix( ); /* restore state */ Global Variables
Shadow Projections Rectangle ( )
What s Wrong? () Exercise () Fix your program
What s Wrong? () Exercise (3) Fix your program more