Debian Codegraph @

demo/trackball.c

raw
1 /*
2 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
5 * any purpose and without fee is hereby granted, provided that the above
6 * copyright notice appear in all copies and that both the copyright notice
7 * and this permission notice appear in supporting documentation, and that
8 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission.
11 *
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
16 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * US Government Users Restricted Rights
26 * Use, duplication, or disclosure by the Government is subject to
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29 * clause at DFARS 252.227-7013 and/or in similar or successor
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
31 * Unpublished-- rights reserved under the copyright laws of the
32 * United States. Contractor/manufacturer is Silicon Graphics,
33 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
34 *
35 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
36 */
37
38 /*
39 * Trackball code:
40 *
41 * Implementation of a virtual trackball.
42 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
43 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
44 *
45 * Vector manip code:
46 *
47 * Original code from:
48 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
49 *
50 * Much mucking with by:
51 * Gavin Bell
52 */
53 #include "config.h"
54
55 #include <math.h>
56 #include "trackball.h"
57
58 /*
59 * This size should really be based on the distance from the center of
60 * rotation to the point on the object underneath the mouse. That
61 * point would then track the mouse as closely as possible. This is a
62 * simple example, though, so that is left as an Exercise for the
63 * Programmer.
64 */
65 #define TRACKBALLSIZE (0.4f)
66
67 /*
68 * Local function prototypes (not defined in trackball.h)
69 */
70 static float tb_project_to_sphere(float, float, float);
71 static void normalize_quat(float [4]);
72
73 static void
74 vzero(float *v)
75 {
76 v[0] = 0.0;
77 v[1] = 0.0;
78 v[2] = 0.0;
79 }
80
81 static void
82 vset(float *v, float x, float y, float z)
83 {
84 v[0] = x;
85 v[1] = y;
86 v[2] = z;
87 }
88
89 static void
90 vsub(const float *src1, const float *src2, float *dst)
91 {
92 dst[0] = src1[0] - src2[0];
93 dst[1] = src1[1] - src2[1];
94 dst[2] = src1[2] - src2[2];
95 }
96
97 static void
98 vcopy(const float *v1, float *v2)
99 {
100 register int i;
101 for (i = 0 ; i < 3 ; i++)
102 v2[i] = v1[i];
103 }
104
105 static void
106 vcross(const float *v1, const float *v2, float *cross)
107 {
108 float temp[3];
109
110 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
111 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
112 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
113 vcopy(temp, cross);
114 }
115
116 static float
117 vlength(const float *v)
118 {
119 return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
120 }
121
122 static void
123 vscale(float *v, float div)
124 {
125 v[0] *= div;
126 v[1] *= div;
127 v[2] *= div;
128 }
129
130 static void
131 vnormal(float *v)
132 {
133 vscale(v,1.0f/vlength(v));
134 }
135
136 static float
137 vdot(const float *v1, const float *v2)
138 {
139 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
140 }
141
142 static void
143 vadd(const float *src1, const float *src2, float *dst)
144 {
145 dst[0] = src1[0] + src2[0];
146 dst[1] = src1[1] + src2[1];
147 dst[2] = src1[2] + src2[2];
148 }
149
150 /*
151 * Ok, simulate a track-ball. Project the points onto the virtual
152 * trackball, then figure out the axis of rotation, which is the cross
153 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
154 * Note: This is a deformed trackball-- is a trackball in the center,
155 * but is deformed into a hyperbolic sheet of rotation away from the
156 * center. This particular function was chosen after trying out
157 * several variations.
158 *
159 * It is assumed that the arguments to this routine are in the range
160 * (-1.0 ... 1.0)
161 */
162 void
163 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
164 {
165 float a[3]; /* Axis of rotation */
166 float phi; /* how much to rotate about axis */
167 float p1[3], p2[3], d[3];
168 float t;
169
170 #pragma GCC diagnostic push
171 #pragma GCC diagnostic ignored "-Wfloat-equal"
172 if (p1x == p2x && p1y == p2y) {
173 /* Zero rotation */
174 vzero(q);
175 q[3] = 1.0;
176 return;
177 }
178 #pragma GCC diagnostic pop
179
180 /*
181 * First, figure out z-coordinates for projection of P1 and P2 to
182 * deformed sphere
183 */
184 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
185 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
186
187 /*
188 * Now, we want the cross product of P1 and P2
189 */
190 vcross(p2,p1,a);
191
192 /*
193 * Figure out how much to rotate around that axis.
194 */
195 vsub(p1,p2,d);
196 t = vlength(d) / (2.0f*TRACKBALLSIZE);
197
198 /*
199 * Avoid problems with out-of-control values...
200 */
201 if (t > 1.0f) t = 1.0f;
202 if (t < -1.0f) t = -1.0f;
203 phi = 2.0f * asin(t);
204
205 axis_to_quat(a,phi,q);
206 }
207
208 /*
209 * Given an axis and angle, compute quaternion.
210 */
211 void
212 axis_to_quat(float a[3], float phi, float q[4])
213 {
214 vnormal(a);
215 vcopy(a,q);
216 vscale(q,sin(phi/2.0f));
217 q[3] = cos(phi/2.0f);
218 }
219
220 /*
221 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
222 * if we are away from the center of the sphere.
223 */
224 static float
225 tb_project_to_sphere(float r, float x, float y)
226 {
227 float d, t, z;
228
229 d = sqrt(x*x + y*y);
230 if (d < r * 0.70710678118654752440f) { /* Inside sphere */
231 z = sqrt(r*r - d*d);
232 } else { /* On hyperbola */
233 t = r / 1.41421356237309504880f;
234 z = t*t / d;
235 }
236 return z;
237 }
238
239 /*
240 * Given two rotations, e1 and e2, expressed as quaternion rotations,
241 * figure out the equivalent single rotation and stuff it into dest.
242 *
243 * This routine also normalizes the result every RENORMCOUNT times it is
244 * called, to keep error from creeping in.
245 *
246 * NOTE: This routine is written so that q1 or q2 may be the same
247 * as dest (or each other).
248 */
249
250 #define RENORMCOUNT 97
251
252 void
253 add_quats(float q1[4], float q2[4], float dest[4])
254 {
255 static int count=0;
256 float t1[4], t2[4], t3[4];
257 float tf[4];
258
259 vcopy(q1,t1);
260 vscale(t1,q2[3]);
261
262 vcopy(q2,t2);
263 vscale(t2,q1[3]);
264
265 vcross(q2,q1,t3);
266 vadd(t1,t2,tf);
267 vadd(t3,tf,tf);
268 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
269
270 dest[0] = tf[0];
271 dest[1] = tf[1];
272 dest[2] = tf[2];
273 dest[3] = tf[3];
274
275 if (++count > RENORMCOUNT) {
276 count = 0;
277 normalize_quat(dest);
278 }
279 }
280
281 /*
282 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
283 * If they don't add up to 1.0, dividing by their magnitued will
284 * renormalize them.
285 *
286 * Note: See the following for more information on quaternions:
287 *
288 * - Shoemake, K., Animating rotation with quaternion curves, Computer
289 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
290 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
291 * graphics, The Visual Computer 5, 2-13, 1989.
292 */
293 static void
294 normalize_quat(float q[4])
295 {
296 int i;
297 float mag;
298
299 mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
300 for (i = 0; i < 4; i++) q[i] /= mag;
301 }
302
303 /*
304 * Build a rotation matrix, given a quaternion rotation.
305 *
306 */
307 void
308 build_rotmatrix(float m[4][4], float q[4])
309 {
310 m[0][0] = 1.0f - 2.0f * (q[1] * q[1] + q[2] * q[2]);
311 m[0][1] = 2.0f * (q[0] * q[1] - q[2] * q[3]);
312 m[0][2] = 2.0f * (q[2] * q[0] + q[1] * q[3]);
313 m[0][3] = 0.0f;
314
315 m[1][0] = 2.0f * (q[0] * q[1] + q[2] * q[3]);
316 m[1][1]= 1.0f - 2.0f * (q[2] * q[2] + q[0] * q[0]);
317 m[1][2] = 2.0f * (q[1] * q[2] - q[0] * q[3]);
318 m[1][3] = 0.0f;
319
320 m[2][0] = 2.0f * (q[2] * q[0] - q[1] * q[3]);
321 m[2][1] = 2.0f * (q[1] * q[2] + q[0] * q[3]);
322 m[2][2] = 1.0f - 2.0f * (q[1] * q[1] + q[0] * q[0]);
323 m[2][3] = 0.0f;
324
325 m[3][0] = 0.0f;
326 m[3][1] = 0.0f;
327 m[3][2] = 0.0f;
328 m[3][3] = 1.0f;
329 }
330