-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing25.html
executable file
·369 lines (319 loc) · 16 KB
/
listing25.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>GLCarbonSharedPbuffer - /trackball.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/GraphicsImaging/index.html">Graphics & Imaging</a> > <a href="../../samplecode/GraphicsImaging/idxOpenGL-date.html">OpenGL</a> > <A HREF="javascript:location.replace('index.html');">GLCarbonSharedPbuffer</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">GLCarbonSharedPbuffer</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/trackball.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/camera.h</option>
<option value="listing2.html">/drawInfo.h</option>
<option value="listing3.html">/glCheck.c</option>
<option value="listing4.html">/glCheck.h</option>
<option value="listing5.html">/HID Support/HID_Config_Utilities.c</option>
<option value="listing6.html">/HID Support/HID_Config_Utilities.h</option>
<option value="listing7.html">/HID Support/HID_Error_Handler.c</option>
<option value="listing8.html">/HID Support/HID_Error_Handler.h</option>
<option value="listing9.html">/HID Support/HID_Name_Lookup.c</option>
<option value="listing10.html">/HID Support/HID_Name_Lookup.h</option>
<option value="listing11.html">/HID Support/HID_Queue_Utilities.c</option>
<option value="listing12.html">/HID Support/HID_Queue_Utilities.h</option>
<option value="listing13.html">/HID Support/HID_Utilities.c</option>
<option value="listing14.html">/HID Support/HID_Utilities.h</option>
<option value="listing15.html">/HID Support/HID_Utilities_External.h</option>
<option value="listing16.html">/HID Support/HID_Utilities_Internal.h</option>
<option value="listing17.html">/HID Support/HIDSupport.c</option>
<option value="listing18.html">/HID Support/HIDSupport.h</option>
<option value="listing19.html">/main.c</option>
<option value="listing20.html">/main.h</option>
<option value="listing21.html">/pbuffer.c</option>
<option value="listing22.html">/pbuffer.h</option>
<option value="listing23.html">/SurfaceGeometry.c</option>
<option value="listing24.html">/SurfaceGeometry.h</option>
<option value="listing25.html">/trackball.c</option>
<option value="listing26.html">/trackball.h</option></select>
</p>
</form>
<p><strong><a href="GLCarbonSharedPbuffer.zip">Download Sample</a></strong> (“GLCarbonSharedPbuffer.zip”, 221.9K)<BR>
<strong><a href="GLCarbonSharedPbuffer.dmg">Download Sample</a></strong> (“GLCarbonSharedPbuffer.dmg”, 271.8K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
trackball.c
Created by daves on Wed May 03 2000.
Modified by ggs Dec 6 2002
Copyright: Copyright © 2003 Apple Computer, Inc., All Rights Reserved
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "trackball.h"
#include <math.h>
static const float kTol = 0.001;
static const float kRad2Deg = 180. / 3.1415927;
static const float kDeg2Rad = 3.1415927 / 180.;
float gRadiusTrackball;
float gStartPtTrackball[3];
float gEndPtTrackball[3];
long gXCenterTrackball = 0, gYCenterTrackball = 0;
// mouse positon and view size as inputs
void startTrackball (long x, long y, long originX, long originY, long width, long height)
{
float xxyy;
float nx, ny;
/* Start up the trackball. The trackball works by pretending that a ball
encloses the 3D view. You roll this pretend ball with the mouse. For
example, if you click on the center of the ball and move the mouse straight
to the right, you roll the ball around its Y-axis. This produces a Y-axis
rotation. You can click on the "edge" of the ball and roll it around
in a circle to get a Z-axis rotation.
The math behind the trackball is simple: start with a vector from the first
mouse-click on the ball to the center of the 3D view. At the same time, set the radius
of the ball to be the smaller dimension of the 3D view. As you drag the mouse
around in the 3D view, a second vector is computed from the surface of the ball
to the center. The axis of rotation is the cross product of these two vectors,
and the angle of rotation is the angle between the two vectors.
*/
nx = width;
ny = height;
if (nx > ny)
gRadiusTrackball = ny * 0.5;
else
gRadiusTrackball = nx * 0.5;
// Figure the center of the view.
gXCenterTrackball = originX + width * 0.5;
gYCenterTrackball = originY + height * 0.5;
// Compute the starting vector from the surface of the ball to its center.
gStartPtTrackball [0] = x - gXCenterTrackball;
gStartPtTrackball [1] = y - gYCenterTrackball;
xxyy = gStartPtTrackball [0] * gStartPtTrackball[0] + gStartPtTrackball [1] * gStartPtTrackball [1];
if (xxyy > gRadiusTrackball * gRadiusTrackball) {
// Outside the sphere.
gStartPtTrackball[2] = 0.;
} else
gStartPtTrackball[2] = sqrt (gRadiusTrackball * gRadiusTrackball - xxyy);
}
// update to new mouse position, output rotation angle
void rollToTrackball (long x, long y, float rot [4]) // rot is output rotation angle
{
float xxyy;
float cosAng, sinAng;
float ls, le, lr;
gEndPtTrackball[0] = x - gXCenterTrackball;
gEndPtTrackball[1] = y - gYCenterTrackball;
if (fabs (gEndPtTrackball [0] - gStartPtTrackball [0]) < kTol && fabs (gEndPtTrackball [1] - gStartPtTrackball [1]) < kTol)
return; // Not enough change in the vectors to have an action.
// Compute the ending vector from the surface of the ball to its center.
xxyy = gEndPtTrackball [0] * gEndPtTrackball [0] + gEndPtTrackball [1] * gEndPtTrackball [1];
if (xxyy > gRadiusTrackball * gRadiusTrackball) {
// Outside the sphere.
gEndPtTrackball [2] = 0.;
} else
gEndPtTrackball[ 2] = sqrt (gRadiusTrackball * gRadiusTrackball - xxyy);
// Take the cross product of the two vectors. r = s X e
rot[1] = gStartPtTrackball[1] * gEndPtTrackball[2] - gStartPtTrackball[2] * gEndPtTrackball[1];
rot[2] = -gStartPtTrackball[0] * gEndPtTrackball[2] + gStartPtTrackball[2] * gEndPtTrackball[0];
rot[3] = gStartPtTrackball[0] * gEndPtTrackball[1] - gStartPtTrackball[1] * gEndPtTrackball[0];
// Use atan for a better angle. If you use only cos or sin, you only get
// half the possible angles, and you can end up with rotations that flip around near
// the poles.
// cos(a) = (s . e) / (||s|| ||e||)
cosAng = gStartPtTrackball[0] * gEndPtTrackball[0] + gStartPtTrackball[1] * gEndPtTrackball[1] + gStartPtTrackball[2] * gEndPtTrackball[2]; // (s . e)
ls = sqrt(gStartPtTrackball[0] * gStartPtTrackball[0] + gStartPtTrackball[1] * gStartPtTrackball[1] + gStartPtTrackball[2] * gStartPtTrackball[2]);
ls = 1. / ls; // 1 / ||s||
le = sqrt(gEndPtTrackball[0] * gEndPtTrackball[0] + gEndPtTrackball[1] * gEndPtTrackball[1] + gEndPtTrackball[2] * gEndPtTrackball[2]);
le = 1. / le; // 1 / ||e||
cosAng = cosAng * ls * le;
// sin(a) = ||(s X e)|| / (||s|| ||e||)
sinAng = lr = sqrt(rot[1] * rot[1] + rot[2] * rot[2] + rot[3] * rot[3]); // ||(s X e)||;
// keep this length in lr for normalizing the rotation vector later.
sinAng = sinAng * ls * le;
rot[0] = (float) atan2 (sinAng, cosAng) * kRad2Deg; // GL rotations are in degrees.
// Normalize the rotation axis.
lr = 1. / lr;
rot[1] *= lr; rot[2] *= lr; rot[3] *= lr;
// returns rotate
}
static void rotation2Quat (float *A, float *q)
{
float ang2; // The half-angle
float sinAng2; // sin(half-angle)
// Convert a GL-style rotation to a quaternion. The GL rotation looks like this:
// {angle, x, y, z}, the corresponding quaternion looks like this:
// {{v}, cos(angle/2)}, where {v} is {x, y, z} / sin(angle/2).
ang2 = A[0] * kDeg2Rad * 0.5; // Convert from degrees ot radians, get the half-angle.
sinAng2 = sin(ang2);
q[0] = A[1] * sinAng2; q[1] = A[2] * sinAng2; q[2] = A[3] * sinAng2;
q[3] = cos(ang2);
}
void addToRotationTrackball (float * dA, float * A)
{
float q0[4], q1[4], q2[4];
float theta2, sinTheta2;
// Figure out A' = A . dA
// In quaternions: let q0 <- A, and q1 <- dA.
// Figure out q2 = q1 + q0 (note the order reversal!).
// A' <- q3.
rotation2Quat(A, q0);
rotation2Quat(dA, q1);
// q2 = q1 + q0;
q2[0] = q1[1]*q0[2] - q1[2]*q0[1] + q1[3]*q0[0] + q1[0]*q0[3];
q2[1] = q1[2]*q0[0] - q1[0]*q0[2] + q1[3]*q0[1] + q1[1]*q0[3];
q2[2] = q1[0]*q0[1] - q1[1]*q0[0] + q1[3]*q0[2] + q1[2]*q0[3];
q2[3] = q1[3]*q0[3] - q1[0]*q0[0] - q1[1]*q0[1] - q1[2]*q0[2];
// Here's an excersize for the reader: it's a good idea to re-normalize your quaternions
// every so often. Experiment with different frequencies.
// An identity rotation is expressed as rotation by 0 about any axis.
// The "angle" term in a quaternion is really the cosine of the half-angle.
// So, if the cosine of the half-angle is one (or, 1.0 within our tolerance),
// then you have an identity rotation.
if (fabs(fabs(q2[3] - 1.)) < 1.0e-7) {
// Identity rotation.
A[0] = 0.0f;
A[1] = 1.0f;
A[2] = A[3] = 0.0f;
return;
}
// If you get here, then you have a non-identity rotation. In non-identity rotations,
// the cosine of the half-angle is non-0, which means the sine of the angle is also
// non-0. So we can safely divide by sin(theta2).
// Turn the quaternion back into an {angle, {axis}} rotation.
theta2 = (float) acos (q2[3]);
sinTheta2 = (float) (1.0 / sin ((double) theta2));
A[0] = theta2 * 2.0f * kRad2Deg;
A[1] = q2[0] * sinTheta2;
A[2] = q2[1] * sinTheta2;
A[3] = q2[2] * sinTheta2;
}
</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/GLCarbonSharedPbuffer/listing25.html%3Fid%3DDTS10003149-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/GLCarbonSharedPbuffer/listing25.html%3Fid%3DDTS10003149-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/GLCarbonSharedPbuffer/listing25.html%3Fid%3DDTS10003149-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>