1 /* gltrackball, Copyright (c) 2002, 2005 Jamie Zawinski <jwz@jwz.org>
2 * GL-flavored wrapper for trackball.c
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
21 # include <OpenGL/gl.h>
26 #include "trackball.h"
27 #include "gltrackball.h"
29 struct trackball_state {
34 /* Returns a trackball_state object, which encapsulates the stuff necessary
35 to make dragging the mouse on the window of a GL program do the right thing.
38 gltrackball_init (void)
40 trackball_state *ts = (trackball_state *) calloc (1, sizeof (*ts));
42 trackball (ts->q, 0, 0, 0, 0);
46 /* Begin tracking the mouse: Call this when the mouse button goes down.
47 x and y are the mouse position relative to the window.
48 w and h are the size of the window.
51 gltrackball_start (trackball_state *ts, int x, int y, int w, int h)
57 /* Track the mouse: Call this each time the mouse moves with the button down.
58 x and y are the new mouse position relative to the window.
59 w and h are the size of the window.
62 gltrackball_track (trackball_state *ts, int x, int y, int w, int h)
66 (2.0 * ts->x - w) / w,
67 (h - 2.0 * ts->y) / h,
72 add_quats (q2, ts->q, ts->q);
75 /* Execute the rotations current encapsulated in the trackball_state:
76 this does something analagous to glRotatef().
79 gltrackball_rotate (trackball_state *ts)
82 build_rotmatrix (m, ts->q);
83 glMultMatrixf (&m[0][0]);
87 # define Button4 4 /* X11/Xlib.h */
90 /* Call this when a mouse-wheel click is detected.
91 Clicks act like horizontal or vertical drags.
92 Percent is the length of the drag as a percentage of the screen size.
93 Button is 'Button4' or 'Button5'.
96 gltrackball_mousewheel (trackball_state *ts,
97 int button, int percent, int horizontal_p)
102 case Button4: up_p = 1; break;
103 case Button5: up_p = 0; break;
104 default: abort(); break;
107 if (horizontal_p) up_p = !up_p;
110 ? 1.0 - (percent / 100.0)
111 : 1.0 + (percent / 100.0));
113 gltrackball_start (ts, 50, 50, 100, 100);
115 gltrackball_track (ts, 50*move, 50, 100, 100);
117 gltrackball_track (ts, 50, 50*move, 100, 100);
121 gltrackball_get_quaternion (trackball_state *ts, float q[4])