1 /* gltrackball, Copyright (c) 2002-2008 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
22 # include <OpenGL/gl.h>
27 #include "trackball.h"
28 #include "gltrackball.h"
30 struct trackball_state {
35 /* Returns a trackball_state object, which encapsulates the stuff necessary
36 to make dragging the mouse on the window of a GL program do the right thing.
39 gltrackball_init (void)
41 trackball_state *ts = (trackball_state *) calloc (1, sizeof (*ts));
43 trackball (ts->q, 0, 0, 0, 0);
47 /* Reset the trackball to the default unrotated state.
50 gltrackball_reset (trackball_state *ts)
52 memset (ts, 0, sizeof(*ts));
53 trackball (ts->q, 0, 0, 0, 0);
56 /* Begin tracking the mouse: Call this when the mouse button goes down.
57 x and y are the mouse position relative to the window.
58 w and h are the size of the window.
61 gltrackball_start (trackball_state *ts, int x, int y, int w, int h)
67 /* Track the mouse: Call this each time the mouse moves with the button down.
68 x and y are the new mouse position relative to the window.
69 w and h are the size of the window.
72 gltrackball_track (trackball_state *ts, int x, int y, int w, int h)
76 (2.0 * ts->x - w) / w,
77 (h - 2.0 * ts->y) / h,
82 add_quats (q2, ts->q, ts->q);
85 /* Execute the rotations current encapsulated in the trackball_state:
86 this does something analagous to glRotatef().
89 gltrackball_rotate (trackball_state *ts)
92 build_rotmatrix (m, ts->q);
93 glMultMatrixf (&m[0][0]);
97 # define Button4 4 /* X11/Xlib.h */
102 /* Call this when a mouse-wheel click is detected.
103 Clicks act like horizontal or vertical drags.
104 Percent is the length of the drag as a percentage of the screen size.
105 Button is 'Button4' or 'Button5' (for the vertical wheel)
106 or 'Button5' or 'Button6' (for the horizontal wheel).
107 If `flip_p' is true, swap the horizontal and vertical axes.
110 gltrackball_mousewheel (trackball_state *ts,
111 int button, int percent, int flip_p)
118 flip_p = 0; /* MacOS has already handled this. */
122 case Button4: up_p = 1; horizontal_p = 0; break;
123 case Button5: up_p = 0; horizontal_p = 0; break;
124 case Button6: up_p = 1; horizontal_p = 1; break;
125 case Button7: up_p = 0; horizontal_p = 1; break;
126 default: abort(); break;
131 horizontal_p = !horizontal_p;
136 ? 1.0 - (percent / 100.0)
137 : 1.0 + (percent / 100.0));
139 gltrackball_start (ts, 50, 50, 100, 100);
141 gltrackball_track (ts, 50*move, 50, 100, 100);
143 gltrackball_track (ts, 50, 50*move, 100, 100);
147 gltrackball_get_quaternion (trackball_state *ts, float q[4])