Fawkes API  Fawkes Development Version
trackball.h
00001 
00002 /***************************************************************************
00003  *  trackball.h - Smooth mouse movements for OpenGL window
00004  *
00005  *  Created: Fri Apr 01 19:56:31 2011
00006  *  Copyright  2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  *  The code has is based on the OpenGL example "smooth" by Nate Robins
00009  *  It states:
00010  *  "Simple trackball-like motion adapted (ripped off) from projtex.c
00011  *   (written by David Yu and David Blythe).  See the SIGGRAPH '96
00012  *   Advanced OpenGL course notes."
00013  *
00014  ****************************************************************************/
00015 
00016 /*  This program is free software; you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License as published by
00018  *  the Free Software Foundation; either version 2 of the License, or
00019  *  (at your option) any later version.
00020  *
00021  *  This program is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU Library General Public License for more details.
00025  *
00026  *  Read the full text in the LICENSE.GPL file in the doc directory.
00027  */
00028 
00029 /* 
00030  *  Usage:
00031  *  
00032  *  o  call tbInit() in before any other tb call
00033  *  o  call tbReshape() from the reshape callback
00034  *  o  call tbMatrix() to get the trackball matrix rotation
00035  *  o  call tbStartMotion() to begin trackball movememt
00036  *  o  call tbStopMotion() to stop trackball movememt
00037  *  o  call tbMotion() from the motion callback
00038  *  o  call tbAnimate(GL_TRUE) if you want the trackball to continue 
00039  *     spinning after the mouse button has been released
00040  *  o  call tbAnimate(GL_FALSE) if you want the trackball to stop 
00041  *     spinning after the mouse button has been released
00042  *
00043  *  Typical setup:
00044  *
00045  *
00046     void
00047     init(void)
00048     {
00049       tbInit(GLUT_MIDDLE_BUTTON);
00050       tbAnimate(GL_TRUE);
00051       . . .
00052     }
00053 
00054     void
00055     reshape(int width, int height)
00056     {
00057       tbReshape(width, height);
00058       . . .
00059     }
00060 
00061     void
00062     display(void)
00063     {
00064       glPushMatrix();
00065 
00066       tbMatrix();
00067       . . . draw the scene . . .
00068 
00069       glPopMatrix();
00070     }
00071 
00072     void
00073     mouse(int button, int state, int x, int y)
00074     {
00075       tbMouse(button, state, x, y);
00076       . . .
00077     }
00078 
00079     void
00080     motion(int x, int y)
00081     {
00082       tbMotion(x, y);
00083       . . .
00084     }
00085 
00086     int
00087     main(int argc, char** argv)
00088     {
00089       . . .
00090       init();
00091       glutReshapeFunc(reshape);
00092       glutDisplayFunc(display);
00093       glutMouseFunc(mouse);
00094       glutMotionFunc(motion);
00095       . . .
00096     }
00097  *
00098  * */
00099 
00100 #include <GL/gl.h>
00101 
00102 /* functions */
00103 void tbInit(GLuint button);
00104 void tbMatrix();
00105 void tbReshape(int width, int height);
00106 void tbMouse(int button, int state, int x, int y);
00107 void tbMotion(int x, int y);
00108 void tbAnimate(GLboolean animate, void (* idle_func)() = 0);