From http://www.jwz.org/xscreensaver/xscreensaver-5.16.tar.gz
[xscreensaver] / hacks / glx / gltrackball.c
1 /* gltrackball, Copyright (c) 2002-2012 Jamie Zawinski <jwz@jwz.org>
2  * GL-flavored wrapper for trackball.c
3  *
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 
10  * implied warranty.
11  */
12
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #ifndef HAVE_COCOA
22 # include <GL/gl.h>
23 #endif
24
25 #ifdef HAVE_JWZGLES
26 # include "jwzgles.h"
27 #endif /* HAVE_JWZGLES */
28
29 #include "trackball.h"
30 #include "gltrackball.h"
31
32 struct trackball_state {
33   int x, y;
34   GLfloat q[4];
35 };
36
37 /* Returns a trackball_state object, which encapsulates the stuff necessary
38    to make dragging the mouse on the window of a GL program do the right thing.
39  */
40 trackball_state *
41 gltrackball_init (void)
42 {
43   trackball_state *ts = (trackball_state *) calloc (1, sizeof (*ts));
44   if (!ts) return 0;
45   trackball (ts->q, 0, 0, 0, 0);
46   return ts;
47 }
48
49 /* Reset the trackball to the default unrotated state.
50  */
51 void
52 gltrackball_reset (trackball_state *ts)
53 {
54   memset (ts, 0, sizeof(*ts));
55   trackball (ts->q, 0, 0, 0, 0);
56 }
57
58 /* Begin tracking the mouse: Call this when the mouse button goes down.
59    x and y are the mouse position relative to the window.
60    w and h are the size of the window.
61  */
62 void
63 gltrackball_start (trackball_state *ts, int x, int y, int w, int h)
64 {
65   ts->x = x;
66   ts->y = y;
67 }
68
69 /* Track the mouse: Call this each time the mouse moves with the button down.
70    x and y are the new mouse position relative to the window.
71    w and h are the size of the window.
72  */
73 void
74 gltrackball_track (trackball_state *ts, int x, int y, int w, int h)
75 {
76   float q2[4];
77   trackball (q2,
78              (2.0 * ts->x - w) / w,
79              (h - 2.0 * ts->y) / h,
80              (2.0 * x - w) / w,
81              (h - 2.0 * y) / h);
82   ts->x = x;
83   ts->y = y;
84   add_quats (q2, ts->q, ts->q);
85 }
86
87 /* Execute the rotations current encapsulated in the trackball_state:
88    this does something analagous to glRotatef().
89  */
90 void
91 gltrackball_rotate (trackball_state *ts)
92 {
93   GLfloat m[4][4];
94   build_rotmatrix (m, ts->q);
95   glMultMatrixf (&m[0][0]);
96 }
97
98
99 # define Button4 4  /* X11/Xlib.h */
100 # define Button5 5
101 # define Button6 6
102 # define Button7 7
103
104 /* Call this when a mouse-wheel click is detected.
105    Clicks act like horizontal or vertical drags.
106    Percent is the length of the drag as a percentage of the screen size.
107    Button is 'Button4' or 'Button5' (for the vertical wheel)
108    or 'Button5' or 'Button6' (for the horizontal wheel).
109    If `flip_p' is true, swap the horizontal and vertical axes.
110  */
111 void
112 gltrackball_mousewheel (trackball_state *ts,
113                         int button, int percent, int flip_p)
114 {
115   int up_p;
116   double move;
117   int horizontal_p;
118
119 #ifdef HAVE_COCOA
120   flip_p = 0;      /* MacOS has already handled this. */
121 #endif
122
123   switch (button) {
124   case Button4: up_p = 1; horizontal_p = 0; break;
125   case Button5: up_p = 0; horizontal_p = 0; break;
126   case Button6: up_p = 1; horizontal_p = 1; break;
127   case Button7: up_p = 0; horizontal_p = 1; break;
128   default: abort(); break;
129   }
130
131   if (flip_p)
132     {
133       horizontal_p = !horizontal_p;
134       up_p = !up_p;
135     }
136
137   move = (up_p
138           ? 1.0 - (percent / 100.0)
139           : 1.0 + (percent / 100.0));
140
141   gltrackball_start (ts, 50, 50, 100, 100);
142   if (horizontal_p)
143     gltrackball_track (ts, 50*move, 50, 100, 100);
144   else
145     gltrackball_track (ts, 50, 50*move, 100, 100);
146 }
147
148 void
149 gltrackball_get_quaternion (trackball_state *ts, float q[4])
150 {
151   int i;
152   for (i=0; i<4; i++)
153     q[i] = ts->q[i];
154 }