Click here to Skip to main content
12,064,291 members (46,557 online)
Click here to Skip to main content
Add your own
alternative version

Stats

149.5K views
3.6K downloads
28 bookmarked
Posted

A virtual trackball rotation controller

, 1 Dec 1999
Rate this:
Please Sign up or sign in to vote.
  • Download source files - 16 Kb
  • <!-- Article Starts -->

    Intro

    This piece of code was born inside an (unfinished) project of mine: a graphical curve editor with a real time 3D preview using OpenGL libraries.

    At first to let the user see the object being constructed from different points of view in the 3D window I simply linked mouse movement with rotation about two axes (usually X and Y when the camera is along Z).

    This method become unsatisfactory after a brief period of use cause it presented an odd effect when the object was "upside-down": moving the mouse to the right caused an apparent left rotation and vice-versa.

    The oddity was caused by the fact that the rotation has to be referred to the local object coordinate system, in the latter example the object is actually turning right while being upside-down.

    In computer graphics literature there are several papers about "intuitive" rotation controllers; I think K. Shoemake's arcball controller represents one of the best solutions: the idea is to project mouse movement on an hypothetical sphere filling the 3D window and to apply the rotation resulting from the sphere being manipulated when the mouse button is pressed to the object.

    Internals

    The global rotation is internally represented and adjusted by using quaternions instead of rotation matrices since this approach is more numerically stable when dealing with small, incremental rotations.

    This trackball is a features merging among the original arcball C code (rewritten in OO style) and a virtual trackball controller by J.Childs (used in his GLcube project) which is in turn derived from some code in M.J.Kilgard's GLUT library.

    The two code presented several similarities (such as the use of quaternions) but different sphere projection algorithms therefore I decided to retain both: J.Childs' controller project mouse movement on a parabolic sheet when it is outside the sphere while K.Shoemake's one project mouse movement on a plane; the first method (the default one) is somewhat more natural because it's effect is continuous while the second one is abruptly interrupted when the mouse leave/enters the virtual sphere.

    In the code I've (re)used a set of math classes which are part of my frequently used graphics programming tools, it's well commented and contains some documentation which can be extracted using Doxygen.

    Use

    The trackball code is demonstrated in the companion article CGLEnabledView - An MDI view class supporting OpenGL

    To use a CBallController in your programs follow these steps:

    1. Include BallController.h where needed.
    2. Construct a CBallController somewhere in your application (I suggest the view class) and specify at construction time the radius of the virtual sphere varying from 0.1 to 1.0 (a radius of 1.0 means that the sphere completely fills the window).
    3. [optional] Call SetAlternateMethod or ToggleMethod member functions to set a project-to-sphere method.
    4. Call the ClientAreaResize method in your window resize handler routine (which should be called before showing the window for the first time too) passing the size of your client area.
    5. [optional but recommended] Call the Key method passing the character code in your key press handler to enable manipulation trough keyboard (see below for the key bindings).
    6. [optional] Set the constraint axes via the UseConstraints method, constraints drawing can be controlled via SetDrawConstraints method.
    7. Call the MouseDown, MouseMove and MouseMove methods in your mouse handlers routines passing the mouse coordinates represented in local window coordinates (origin in upper left corner); it may be a good idea to capture mouse movements outside the windows during mouse button dragging.
    8. Call the IssueGLrotation method just before (re)drawing the scene.
    9. [optional] Call the DrawBall method after drawing the scene to let the BallController show some feedback during rotations using the alternate method.

    Other methods
    With GetDrawConstraints and you can check if constraint drawing is activated. GetAngleKeyIncrement and SetAngleKeyIncrement allow to respectively retrieve and set the angle increment used in keyboard interactions. GetColorV, GetColor, SetColor and SetColorV specify or retrieve the color used in DrawBall method, the color can be passed using a COLORREF structure or a vector of [0-1] ranging floats.
    Keys

    EscInterrupts current rotation operation (a rotation operation starts at every mouse button press)
    Canc or Numpad 5Resets the current rotation (corresponds to an identity rotation matrix)
    Right arrow or Numpad 6rotates +keyIncrement degrees about X axis
    Left arrow or Numpad 4rotates -keyIncrement degrees about X axis
    Up arrow or Numpad 8rotates +keyIncrement degrees about Y axis
    Down arrow or Numpad 2rotates -keyIncrement degrees about Y axis
    Page Up or Numpad 9rotates +keyIncrement degrees about Z axis
    Home or Numpad 7rotates -keyIncrement degrees about Z axis
    TABduring a constrained rotation swicth the constrain axis

    History

    Revision Description
    0.2 Initial public release
    0.1 Personal development release

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    Share

    About the Author

    Alessandro Falappa
    Software Developer (Senior)
    Italy Italy
    Male.
    Living in Rome, Italy
    Computer addict since 12 years old (first computer: Commodore 64).
    Master degree in Computer Science.
    Very interested in computer generated graphics both real time (mainly OpenGL) and photorealistic rendering.

    You may also be interested in...

    Comments and Discussions

     
    Questioni know that this thread is old but ... Pin
    Member 827886013-Nov-11 9:29
    memberMember 827886013-Nov-11 9:29 
    Generalchange center point Pin
    Omar dddd1-Mar-06 18:23
    memberOmar dddd1-Mar-06 18:23 
    QuestionSounds interesting, but where are the screenshots? Pin
    Shawn Poulson17-Feb-06 4:20
    memberShawn Poulson17-Feb-06 4:20 
    GeneralSorry, But... Pin
    raaar28-Jan-05 15:24
    memberraaar28-Jan-05 15:24 
    Generalthanks Pin
    caesar2211-Nov-04 3:44
    membercaesar2211-Nov-04 3:44 
    QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pin
    werter15-Jun-04 22:53
    memberwerter15-Jun-04 22:53 
    AnswerRe: How to run 2 different(or same) OpenGL objects in one DialogBox? Pin
    barto3-Aug-04 6:13
    memberbarto3-Aug-04 6:13 
    GeneralRe: I believe it's missing a file. Pin
    rohangandhi28-Sep-03 9:09
    memberrohangandhi28-Sep-03 9:09 
    GeneralRe: I believe it's missing a file. Pin
    raaar28-Jan-05 15:33
    memberraaar28-Jan-05 15:33 
    GeneralRe: I believe it's missing a file. Pin
    raaar28-Jan-05 15:39
    memberraaar28-Jan-05 15:39 
    GeneralSilky smooth rotation Pin
    AORD11-Aug-03 14:29
    memberAORD11-Aug-03 14:29 
    GeneralGreat piece of work Pin
    Domagoj27-Jul-03 3:06
    memberDomagoj27-Jul-03 3:06 
    GeneralI believe it's missing a file. Pin
    WREY28-Jun-03 4:49
    memberWREY28-Jun-03 4:49 
    GeneralRe: I believe it's missing a file. Pin
    Domagoj27-Jul-03 3:12
    memberDomagoj27-Jul-03 3:12 
    GeneralIt's not worth the effort. Pin
    WREY27-Jul-03 9:07
    memberWREY27-Jul-03 9:07 
    GeneralRe: It's not worth the effort. Pin
    AORD11-Aug-03 14:41
    memberAORD11-Aug-03 14:41 
    GeneralZoom Pin
    Ali Khanlarkhani18-Jan-03 20:59
    memberAli Khanlarkhani18-Jan-03 20:59 
    GeneralEn general Pin
    Jose Gabriel Martinez10-Jul-00 9:30
    sussJose Gabriel Martinez10-Jul-00 9:30 
    GeneralRe: En general Pin
    Anonymous8-Aug-01 11:28
    memberAnonymous8-Aug-01 11:28 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.160204.4 | Last Updated 2 Dec 1999
    Article Copyright 1999 by Alessandro Falappa
    Everything else Copyright © CodeProject, 1999-2016
    Layout: fixed | fluid