Debian Codegraph @

debian/external_deps/fetk/sg/src/ogl/ogl.c

raw
1 /*
2 * ***************************************************************************
3 * SG = < Socket Graphics >
4 * Copyright (C) 1994-- Michael Holst
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * rcsid="$Id: ogl.c,v 1.15 2010/08/12 04:56:22 fetk Exp $"
21 * ***************************************************************************
22 */
23
24 /*
25 * ***************************************************************************
26 * File: ogl.c
27 *
28 * Purpose: Class Ogl: methods.
29 *
30 * Notes: Ogl is an OpenGL object manipulator for 2D plane polygon-based
31 * objects. It is designed to look and act very much like Geomview.
32 * The manipulator itself is designed to feel like a combination
33 * of the manipulator in Geomview, and the Arcball rotator of
34 * Ken Shoemake.
35 *
36 * Ogl is a simple, machine and window system-independent layer
37 * built on the OpenGL libraris (GL and GLU). It is written to be
38 * independent of the particular windowing system. The windowing
39 * system-specific code (for example, X, Win32, or other window
40 * system) is to be wrapped around this small set of graphics
41 * routines (which is itself wrapped around the OpenGL library...)
42 *
43 * On the ordering of the vertices in a polygon:
44 *
45 * All closed polygons must be specified by a set of consecutive
46 * vertices in counter-clockwise-ordering by vertices, as seen from
47 * the "up" side of the polygon. This produces the correct
48 * surface-normals from the right-hand-rule for surfaces, which is
49 * required to get correct shading effects from OpenGL. Since
50 * the polygons are allowed to be oriented in any manner in R^3,
51 * it is important that you provide these orderings correctly.
52 * (There is no way to determine the ordering for you; for example,
53 * there is no such consistent ordering for a mobius strip, which
54 * is an example of a non-orientable surface that can be displayed
55 * using Ogl.
56 *
57 * General notes and warnings to myself about programming with OpenGL:
58 *
59 * (1) The "GL_MODELVIEW" matrix is the default matrix which stack
60 * operations will pertain to. When switching to the
61 * "GL_PROJECTION" matrix with "glMatrixMode" to define the view
62 * volume projector, you MUST switch back to the "GL_MODELVIEW"
63 * matrix with another "glMatrixMode" command.
64 *
65 * (2) The node ordering/shading relationship is subtle; you may not
66 * see mistakes in the normal vectors if both front and back
67 * sides of polygons are set to be drawn with fill. To try to
68 * catch mistakes, we always set the back-side of polygons to be
69 * drawn wire. For a closed 3D object, no back-sides should be
70 * seen. If some polygons appear to be drawn with wire rather
71 * than solid, this means a node ordering for some tetrahedra
72 * was given incorrectly. (The fill type is set as
73 * "glPolygonMode(GL_BACK, GL_LINE)", with GL_LINE or GL_FILL
74 * specified).
75 *
76 * Author: Michael Holst
77 * ***************************************************************************
78 */
79
80 #include "ogl_p.h"
81
82 VEMBED(rcsid="$Id: ogl.c,v 1.15 2010/08/12 04:56:22 fetk Exp $")
83
84 #define CUBESIZE 1.0
85 #define BALLSIZE 1.0
86 #define NUMARC 20
87 #define NUMPDB 10
88 #define RENORMAL 100
89 #define TOL 1.0e-5
90
91 /*
92 * VPRIVATE float C_BLACK[4] = { 0.0, 0.0, 0.0, 1.0 };
93 * VPRIVATE float C_RED[4] = { 1.0, 0.0, 0.0, 1.0 };
94 * VPRIVATE float C_GREEN[4] = { 0.0, 1.0, 0.0, 1.0 };
95 * VPRIVATE float C_BLUE[4] = { 0.0, 0.0, 1.0, 1.0 };
96 * VPRIVATE float C_YELLOW[4] = { 1.0, 1.0, 0.0, 1.0 };
97 * VPRIVATE float C_CYAN[4] = { 0.0, 1.0, 1.0, 1.0 };
98 * VPRIVATE float C_MAGENTA[4] = { 1.0, 0.0, 1.0, 1.0 };
99 * VPRIVATE float C_WHITE[4] = { 1.0, 1.0, 1.0, 1.0 };
100 * VPRIVATE float C_DGREY[4] = { 0.333, 0.333, 0.333, 1.0 };
101 * VPRIVATE float C_LGREY[4] = { 0.666, 0.666, 0.666, 1.0 };
102 * VPRIVATE float C_CLEAR_GV[4] = { 0.310, 0.320, 0.310, 1.0 };
103 * VPRIVATE float C_INTERIOR[4] = { 0.0, 1.0, 0.0, 1.0 };
104 * VPRIVATE float C_CLEAR[4] = { 1.0, 1.0, 1.0, 1.0 };
105 */
106
107 #if 1
108 VPRIVATE float C_CLEAR_GV[4]={(float)1.0, (float)1.0, (float)1.0, (float)1.0};
109 #else
110 VPRIVATE float C_CLEAR_GV[4]={(float)0.31,(float)0.32,(float)0.31,(float)1.0};
111 #endif
112 VPRIVATE float C_BLACK[4] ={(float)0.0, (float)0.0, (float)0.0, (float)1.0};
113 VPRIVATE float C_CYAN[4] ={(float)0.0, (float)1.0, (float)1.0, (float)1.0};
114 VPRIVATE float C_BALL[4] ={(float)0.8, (float)1.0, (float)1.0, (float)1.0};
115
116 VPRIVATE void makeBallList(Ogl *thee);
117 VPRIVATE void makeBBoxList(Ogl *thee);
118 VPRIVATE void dragBall(Ogl *thee);
119 VPRIVATE void initView(Ogl *thee);
120 VPRIVATE void setLights(Ogl *thee);
121 VPRIVATE void setEffects(Ogl *thee);
122 VPRIVATE void clearBBox(Ogl *thee);
123 VPRIVATE void makeBBox(Ogl *thee);
124 VPRIVATE void rotate(Ogl *thee);
125 VPRIVATE void scale(Ogl *thee);
126 VPRIVATE void translate(Ogl *thee);
127
128 VPRIVATE void vzero(float u[]);
129 VPRIVATE void vset(float u[], float x, float y, float z);
130 VPRIVATE void vsub(const float u[], const float v[], float w[]);
131 VPRIVATE void vcopy(const float u[], float v[]);
132 VPRIVATE void vcross(const float u[], const float v[], float w[]);
133 VPRIVATE float vlength(const float u[]);
134 VPRIVATE void vscale(float u[], float scale);
135 VPRIVATE void vnormal(float u[]);
136 VPRIVATE float vdot(const float u[], const float v[]);
137 VPRIVATE void vadd(const float u[], const float v[], float w[]);
138
139 VPRIVATE void buildQuat(float q[4], float r0[3], float r2[3]);
140 VPRIVATE void axisQuat(float a[3], float phi, float q[4]);
141 VPRIVATE void addQuat(float q0[4], float q1[4], float des[4]);
142 VPRIVATE void normalQuat(float q[4]);
143 VPRIVATE void buildRotation(float m[4][4], float q[4]);
144 VPRIVATE float projectSphere(float r, float x, float y);
145
146 #define sph8_numV 6
147 VPRIVATE float sph8_V[sph8_numV][3] = {
148 { 0.0000000000e+00, 0.0000000000e+00, 1.0000000000e+00 },
149 { 0.0000000000e+00, 0.0000000000e+00, -1.0000000000e+00 },
150 { 7.0710680000e-01, 7.0710680000e-01, 0.0000000000e+00 },
151 { -7.0710680000e-01, 7.0710680000e-01, 0.0000000000e+00 },
152 { -7.0710680000e-01, -7.0710680000e-01, 0.0000000000e+00 },
153 { 7.0710680000e-01, -7.0710680000e-01, 0.0000000000e+00 }
154 };
155 #if 0
156 #define sph8_numS 8
157 VPRIVATE int sph8_S[sph8_numS][3] = {
158 { 0, 2, 3 },
159 { 0, 3, 4 },
160 { 0, 4, 5 },
161 { 0, 5, 2 },
162 { 1, 3, 2 },
163 { 1, 4, 3 },
164 { 1, 5, 4 },
165 { 1, 2, 5 }
166 };
167 #endif
168
169 #if 0
170 #define sph20_numV 12
171 VPRIVATE float sph20_V[sph20_numV][3] = {
172 { -5.257e-01, 0.000e+00, 8.507e-01 },
173 { 5.257e-01, 0.000e+00, 8.507e-01 },
174 { -5.257e-01, 0.000e+00, -8.507e-01 },
175 { 5.257e-01, 0.000e+00, -8.507e-01 },
176 { 0.000e+00, 8.507e-01, 5.257e-01 },
177 { 0.000e+00, 8.507e-01, -5.257e-01 },
178 { 0.000e+00, -8.507e-01, 5.257e-01 },
179 { 0.000e+00, -8.507e-01, -5.257e-01 },
180 { 8.507e-01, 5.257e-01, 0.000e+00 },
181 { -8.507e-01, 5.257e-01, 0.000e+00 },
182 { 8.507e-01, -5.257e-01, 0.000e+00 },
183 { -8.507e-01, -5.257e-01, 0.000e+00 }
184 };
185 #define sph20_numS 20
186 VPRIVATE int sph20_S[sph20_numS][3] = {
187 { 0, 1, 4 },
188 { 0, 4, 9 },
189 { 9, 4, 5 },
190 { 4, 8, 5 },
191 { 4, 1, 8 },
192 { 8, 1, 10 },
193 { 8, 10, 3 },
194 { 5, 8, 3 },
195 { 5, 3, 2 },
196 { 2, 3, 7 },
197 { 7, 3, 10 },
198 { 7, 10, 6 },
199 { 7, 6, 11 },
200 { 11, 6, 0 },
201 { 0, 6, 1 },
202 { 6, 10, 1 },
203 { 9, 11, 0 },
204 { 9, 2, 11 },
205 { 9, 5, 2 },
206 { 7, 11, 2 }
207 };
208 #endif
209
210 #define sph80_numV 42
211 VPRIVATE float sph80_V[sph80_numV][3] = {
212 { -5.257e-01, 0.000e+00, 8.507e-01 },
213 { 5.257e-01, 0.000e+00, 8.507e-01 },
214 { -5.257e-01, 0.000e+00, -8.507e-01 },
215 { 5.257e-01, 0.000e+00, -8.507e-01 },
216 { 0.000e+00, 8.507e-01, 5.257e-01 },
217 { 0.000e+00, 8.507e-01, -5.257e-01 },
218 { 0.000e+00, -8.507e-01, 5.257e-01 },
219 { 0.000e+00, -8.507e-01, -5.257e-01 },
220 { 8.507e-01, 5.257e-01, 0.000e+00 },
221 { -8.507e-01, 5.257e-01, 0.000e+00 },
222 { 8.507e-01, -5.257e-01, 0.000e+00 },
223 { -8.507e-01, -5.257e-01, 0.000e+00 },
224 { -3.090e-01, 5.000e-01, 8.090e-01 },
225 { 3.090e-01, 5.000e-01, 8.090e-01 },
226 { 0.000e+00, 0.000e+00, 1.000e+00 },
227 { -8.090e-01, 3.090e-01, 5.000e-01 },
228 { -5.000e-01, 8.090e-01, 3.090e-01 },
229 { -5.000e-01, 8.090e-01, -3.090e-01 },
230 { 0.000e+00, 1.000e+00, 0.000e+00 },
231 { 5.000e-01, 8.090e-01, 3.090e-01 },
232 { 5.000e-01, 8.090e-01, -3.090e-01 },
233 { 8.090e-01, 3.090e-01, 5.000e-01 },
234 { 1.000e+00, 0.000e+00, 0.000e+00 },
235 { 8.090e-01, -3.090e-01, 5.000e-01 },
236 { 8.090e-01, 3.090e-01, -5.000e-01 },
237 { 8.090e-01, -3.090e-01, -5.000e-01 },
238 { 3.090e-01, 5.000e-01, -8.090e-01 },
239 { -3.090e-01, 5.000e-01, -8.090e-01 },
240 { 0.000e+00, 0.000e+00, -1.000e+00 },
241 { -3.090e-01, -5.000e-01, -8.090e-01 },
242 { 3.090e-01, -5.000e-01, -8.090e-01 },
243 { 5.000e-01, -8.090e-01, -3.090e-01 },
244 { 5.000e-01, -8.090e-01, 3.090e-01 },
245 { 0.000e+00, -1.000e+00, 0.000e+00 },
246 { -5.000e-01, -8.090e-01, 3.090e-01 },
247 { -5.000e-01, -8.090e-01, -3.090e-01 },
248 { -3.090e-01, -5.000e-01, 8.090e-01 },
249 { -8.090e-01, -3.090e-01, 5.000e-01 },
250 { 3.090e-01, -5.000e-01, 8.090e-01 },
251 { -1.000e+00, 0.000e+00, 0.000e+00 },
252 { -8.090e-01, 3.090e-01, -5.000e-01 },
253 { -8.090e-01, -3.090e-01, -5.000e-01 }
254 };
255 #if 0
256 #define sph80_numS 80
257 VPRIVATE int sph80_S[sph80_numS][3] = {
258 { 4, 12, 13 },
259 { 0, 12, 15 },
260 { 9, 16, 17 },
261 { 4, 19, 18 },
262 { 4, 13, 19 },
263 { 8, 21, 22 },
264 { 3, 24, 25 },
265 { 3, 26, 24 },
266 { 2, 27, 28 },
267 { 7, 29, 30 },
268 { 7, 30, 31 },
269 { 10, 32, 31 },
270 { 6, 34, 33 },
271 { 6, 36, 34 },
272 { 0, 36, 14 },
273 { 1, 38, 23 },
274 { 0, 15, 37 },
275 { 9, 40, 39 },
276 { 5, 27, 17 },
277 { 2, 29, 41 },
278 { 0, 14, 12 },
279 { 1, 13, 14 },
280 { 14, 13, 12 },
281 { 4, 16, 12 },
282 { 9, 15, 16 },
283 { 16, 15, 12 },
284 { 4, 18, 16 },
285 { 5, 17, 18 },
286 { 18, 17, 16 },
287 { 8, 20, 19 },
288 { 5, 18, 20 },
289 { 20, 18, 19 },
290 { 1, 21, 13 },
291 { 8, 19, 21 },
292 { 21, 19, 13 },
293 { 1, 23, 21 },
294 { 10, 22, 23 },
295 { 23, 22, 21 },
296 { 8, 22, 24 },
297 { 10, 25, 22 },
298 { 22, 25, 24 },
299 { 5, 20, 26 },
300 { 8, 24, 20 },
301 { 20, 24, 26 },
302 { 5, 26, 27 },
303 { 3, 28, 26 },
304 { 26, 28, 27 },
305 { 2, 28, 29 },
306 { 3, 30, 28 },
307 { 28, 30, 29 },
308 { 3, 25, 30 },
309 { 10, 31, 25 },
310 { 25, 31, 30 },
311 { 6, 33, 32 },
312 { 7, 31, 33 },
313 { 33, 31, 32 },
314 { 11, 35, 34 },
315 { 7, 33, 35 },
316 { 35, 33, 34 },
317 { 0, 37, 36 },
318 { 11, 34, 37 },
319 { 37, 34, 36 },
320 { 6, 38, 36 },
321 { 1, 14, 38 },
322 { 38, 14, 36 },
323 { 6, 32, 38 },
324 { 10, 23, 32 },
325 { 32, 23, 38 },
326 { 9, 39, 15 },
327 { 11, 37, 39 },
328 { 39, 37, 15 },
329 { 2, 41, 40 },
330 { 11, 39, 41 },
331 { 41, 39, 40 },
332 { 2, 40, 27 },
333 { 9, 17, 40 },
334 { 40, 17, 27 },
335 { 7, 35, 29 },
336 { 11, 41, 35 },
337 { 35, 41, 29 }
338 };
339 #endif
340
341 #define sph320_numV 162
342 VPRIVATE float sph320_V[sph320_numV][3] = {
343 { -5.2573110000e-01, 0.0000000000e+00, 8.5065080000e-01 },
344 { 5.2573110000e-01, 0.0000000000e+00, 8.5065080000e-01 },
345 { -5.2573110000e-01, 0.0000000000e+00, -8.5065080000e-01 },
346 { 5.2573110000e-01, 0.0000000000e+00, -8.5065080000e-01 },
347 { 0.0000000000e+00, 8.5065080000e-01, 5.2573110000e-01 },
348 { 0.0000000000e+00, 8.5065080000e-01, -5.2573110000e-01 },
349 { 0.0000000000e+00, -8.5065080000e-01, 5.2573110000e-01 },
350 { 0.0000000000e+00, -8.5065080000e-01, -5.2573110000e-01 },
351 { 8.5065080000e-01, 5.2573110000e-01, 0.0000000000e+00 },
352 { -8.5065080000e-01, 5.2573110000e-01, 0.0000000000e+00 },
353 { 8.5065080000e-01, -5.2573110000e-01, 0.0000000000e+00 },
354 { -8.5065080000e-01, -5.2573110000e-01, 0.0000000000e+00 },
355 { -3.0901699170e-01, 5.0000000229e-01, 8.0901699398e-01 },
356 { 3.0901699170e-01, 5.0000000229e-01, 8.0901699398e-01 },
357 { 0.0000000000e+00, 0.0000000000e+00, 1.0000000000e+00 },
358 { -8.0901699398e-01, 3.0901699170e-01, 5.0000000229e-01 },
359 { -5.0000000229e-01, 8.0901699398e-01, 3.0901699170e-01 },
360 { -5.0000000229e-01, 8.0901699398e-01, -3.0901699170e-01 },
361 { 0.0000000000e+00, 1.0000000000e+00, 0.0000000000e+00 },
362 { 5.0000000229e-01, 8.0901699398e-01, 3.0901699170e-01 },
363 { 5.0000000229e-01, 8.0901699398e-01, -3.0901699170e-01 },
364 { 8.0901699398e-01, 3.0901699170e-01, 5.0000000229e-01 },
365 { 1.0000000000e+00, 0.0000000000e+00, 0.0000000000e+00 },
366 { 8.0901699398e-01, -3.0901699170e-01, 5.0000000229e-01 },
367 { 8.0901699398e-01, 3.0901699170e-01, -5.0000000229e-01 },
368 { 8.0901699398e-01, -3.0901699170e-01, -5.0000000229e-01 },
369 { 3.0901699170e-01, 5.0000000229e-01, -8.0901699398e-01 },
370 { -3.0901699170e-01, 5.0000000229e-01, -8.0901699398e-01 },
371 { 0.0000000000e+00, 0.0000000000e+00, -1.0000000000e+00 },
372 { -3.0901699170e-01, -5.0000000229e-01, -8.0901699398e-01 },
373 { 3.0901699170e-01, -5.0000000229e-01, -8.0901699398e-01 },
374 { 5.0000000229e-01, -8.0901699398e-01, -3.0901699170e-01 },
375 { 5.0000000229e-01, -8.0901699398e-01, 3.0901699170e-01 },
376 { 0.0000000000e+00, -1.0000000000e+00, 0.0000000000e+00 },
377 { -5.0000000229e-01, -8.0901699398e-01, 3.0901699170e-01 },
378 { -5.0000000229e-01, -8.0901699398e-01, -3.0901699170e-01 },
379 { -3.0901699170e-01, -5.0000000229e-01, 8.0901699398e-01 },
380 { -8.0901699398e-01, -3.0901699170e-01, 5.0000000229e-01 },
381 { 3.0901699170e-01, -5.0000000229e-01, 8.0901699398e-01 },
382 { -1.0000000000e+00, 0.0000000000e+00, 0.0000000000e+00 },
383 { -8.0901699398e-01, 3.0901699170e-01, -5.0000000229e-01 },
384 { -8.0901699398e-01, -3.0901699170e-01, -5.0000000229e-01 },
385 { 0.0000000000e+00, 5.2573111404e-01, 8.5065080716e-01 },
386 { -1.6062203536e-01, 7.0204644650e-01, 6.9378047588e-01 },
387 { 1.6062203536e-01, 7.0204644650e-01, 6.9378047588e-01 },
388 { -5.8778525089e-01, 4.2532540412e-01, 6.8819096147e-01 },
389 { -4.3388855988e-01, 2.5989191600e-01, 8.6266848187e-01 },
390 { -6.9378047588e-01, 1.6062203536e-01, 7.0204644650e-01 },
391 { -5.2573111404e-01, 8.5065080716e-01, 0.0000000000e+00 },
392 { -7.0204644650e-01, 6.9378047588e-01, 1.6062203536e-01 },
393 { -7.0204644650e-01, 6.9378047588e-01, -1.6062203536e-01 },
394 { 2.6286555729e-01, 9.5105651619e-01, 1.6245984673e-01 },
395 { 2.5989191600e-01, 8.6266848187e-01, 4.3388855988e-01 },
396 { 0.0000000000e+00, 9.6193835912e-01, 2.7326652422e-01 },
397 { 4.2532540412e-01, 6.8819096147e-01, 5.8778525089e-01 },
398 { 9.5105651619e-01, 1.6245984673e-01, 2.6286555729e-01 },
399 { 8.6266848187e-01, 4.3388855988e-01, 2.5989191600e-01 },
400 { 9.6193835912e-01, 2.7326652422e-01, 0.0000000000e+00 },
401 { 8.5065080716e-01, 0.0000000000e+00, -5.2573111404e-01 },
402 { 6.9378047588e-01, 1.6062203536e-01, -7.0204644650e-01 },
403 { 6.9378047588e-01, -1.6062203536e-01, -7.0204644650e-01 },
404 { 5.8778525089e-01, 4.2532540412e-01, -6.8819096147e-01 },
405 { 4.3388855988e-01, 2.5989191600e-01, -8.6266848187e-01 },
406 { -1.6245984673e-01, 2.6286555729e-01, -9.5105651619e-01 },
407 { -4.3388855988e-01, 2.5989191600e-01, -8.6266848187e-01 },
408 { -2.7326652422e-01, 0.0000000000e+00, -9.6193835912e-01 },
409 { 0.0000000000e+00, -5.2573111404e-01, -8.5065080716e-01 },
410 { -1.6062203536e-01, -7.0204644650e-01, -6.9378047588e-01 },
411 { 1.6062203536e-01, -7.0204644650e-01, -6.9378047588e-01 },
412 { 4.2532540412e-01, -6.8819096147e-01, -5.8778525089e-01 },
413 { 2.5989191600e-01, -8.6266848187e-01, -4.3388855988e-01 },
414 { 5.2573111404e-01, -8.5065080716e-01, 0.0000000000e+00 },
415 { 7.0204644650e-01, -6.9378047588e-01, 1.6062203536e-01 },
416 { 7.0204644650e-01, -6.9378047588e-01, -1.6062203536e-01 },
417 { -2.6286555729e-01, -9.5105651619e-01, 1.6245984673e-01 },
418 { -2.5989191600e-01, -8.6266848187e-01, 4.3388855988e-01 },
419 { 0.0000000000e+00, -9.6193835912e-01, 2.7326652422e-01 },
420 { -4.2532540412e-01, -6.8819096147e-01, 5.8778525089e-01 },
421 { -1.6062203536e-01, -7.0204644650e-01, 6.9378047588e-01 },
422 { -1.6245984673e-01, -2.6286555729e-01, 9.5105651619e-01 },
423 { -4.3388855988e-01, -2.5989191600e-01, 8.6266848187e-01 },
424 { -2.7326652422e-01, 0.0000000000e+00, 9.6193835912e-01 },
425 { 5.8778525089e-01, -4.2532540412e-01, 6.8819096147e-01 },
426 { 4.3388855988e-01, -2.5989191600e-01, 8.6266848187e-01 },
427 { 6.9378047588e-01, -1.6062203536e-01, 7.0204644650e-01 },
428 { -8.5065080716e-01, 0.0000000000e+00, 5.2573111404e-01 },
429 { -6.9378047588e-01, -1.6062203536e-01, 7.0204644650e-01 },
430 { -9.5105651619e-01, 1.6245984673e-01, -2.6286555729e-01 },
431 { -8.6266848187e-01, 4.3388855988e-01, -2.5989191600e-01 },
432 { -9.6193835912e-01, 2.7326652422e-01, 0.0000000000e+00 },
433 { -4.2532540412e-01, 6.8819096147e-01, -5.8778525089e-01 },
434 { -1.6062203536e-01, 7.0204644650e-01, -6.9378047588e-01 },
435 { -2.5989191600e-01, 8.6266848187e-01, -4.3388855988e-01 },
436 { -5.8778525089e-01, -4.2532540412e-01, -6.8819096147e-01 },
437 { -4.3388855988e-01, -2.5989191600e-01, -8.6266848187e-01 },
438 { -6.9378047588e-01, -1.6062203536e-01, -7.0204644650e-01 },
439 { -1.6245984673e-01, 2.6286555729e-01, 9.5105651619e-01 },
440 { 1.6245984673e-01, 2.6286555729e-01, 9.5105651619e-01 },
441 { 4.3388855988e-01, 2.5989191600e-01, 8.6266848187e-01 },
442 { 2.7326652422e-01, 0.0000000000e+00, 9.6193835912e-01 },
443 { -4.2532540412e-01, 6.8819096147e-01, 5.8778525089e-01 },
444 { -2.5989191600e-01, 8.6266848187e-01, 4.3388855988e-01 },
445 { -6.8819096147e-01, 5.8778525089e-01, 4.2532540412e-01 },
446 { -8.6266848187e-01, 4.3388855988e-01, 2.5989191600e-01 },
447 { -2.6286555729e-01, 9.5105651619e-01, 1.6245984673e-01 },
448 { -2.6286555729e-01, 9.5105651619e-01, -1.6245984673e-01 },
449 { 0.0000000000e+00, 9.6193835912e-01, -2.7326652422e-01 },
450 { 5.2573111404e-01, 8.5065080716e-01, 0.0000000000e+00 },
451 { 7.0204644650e-01, 6.9378047588e-01, -1.6062203536e-01 },
452 { 7.0204644650e-01, 6.9378047588e-01, 1.6062203536e-01 },
453 { 2.6286555729e-01, 9.5105651619e-01, -1.6245984673e-01 },
454 { 2.5989191600e-01, 8.6266848187e-01, -4.3388855988e-01 },
455 { 5.8778525089e-01, 4.2532540412e-01, 6.8819096147e-01 },
456 { 6.9378047588e-01, 1.6062203536e-01, 7.0204644650e-01 },
457 { 6.8819096147e-01, 5.8778525089e-01, 4.2532540412e-01 },
458 { 8.5065080716e-01, 0.0000000000e+00, 5.2573111404e-01 },
459 { 9.5105651619e-01, -1.6245984673e-01, 2.6286555729e-01 },
460 { 9.6193835912e-01, -2.7326652422e-01, 0.0000000000e+00 },
461 { 8.6266848187e-01, -4.3388855988e-01, 2.5989191600e-01 },
462 { 9.5105651619e-01, 1.6245984673e-01, -2.6286555729e-01 },
463 { 8.6266848187e-01, 4.3388855988e-01, -2.5989191600e-01 },
464 { 9.5105651619e-01, -1.6245984673e-01, -2.6286555729e-01 },
465 { 8.6266848187e-01, -4.3388855988e-01, -2.5989191600e-01 },
466 { 4.2532540412e-01, 6.8819096147e-01, -5.8778525089e-01 },
467 { 1.6062203536e-01, 7.0204644650e-01, -6.9378047588e-01 },
468 { 6.8819096147e-01, 5.8778525089e-01, -4.2532540412e-01 },
469 { 0.0000000000e+00, 5.2573111404e-01, -8.5065080716e-01 },
470 { 1.6245984673e-01, 2.6286555729e-01, -9.5105651619e-01 },
471 { 2.7326652422e-01, 0.0000000000e+00, -9.6193835912e-01 },
472 { -1.6245984673e-01, -2.6286555729e-01, -9.5105651619e-01 },
473 { 1.6245984673e-01, -2.6286555729e-01, -9.5105651619e-01 },
474 { 4.3388855988e-01, -2.5989191600e-01, -8.6266848187e-01 },
475 { 5.8778525089e-01, -4.2532540412e-01, -6.8819096147e-01 },
476 { 6.8819096147e-01, -5.8778525089e-01, -4.2532540412e-01 },
477 { 2.6286555729e-01, -9.5105651619e-01, 1.6245984673e-01 },
478 { 2.5989191600e-01, -8.6266848187e-01, 4.3388855988e-01 },
479 { 2.6286555729e-01, -9.5105651619e-01, -1.6245984673e-01 },
480 { 0.0000000000e+00, -9.6193835912e-01, -2.7326652422e-01 },
481 { -5.2573111404e-01, -8.5065080716e-01, 0.0000000000e+00 },
482 { -7.0204644650e-01, -6.9378047588e-01, -1.6062203536e-01 },
483 { -7.0204644650e-01, -6.9378047588e-01, 1.6062203536e-01 },
484 { -2.6286555729e-01, -9.5105651619e-01, -1.6245984673e-01 },
485 { -2.5989191600e-01, -8.6266848187e-01, -4.3388855988e-01 },
486 { -5.8778525089e-01, -4.2532540412e-01, 6.8819096147e-01 },
487 { -6.8819096147e-01, -5.8778525089e-01, 4.2532540412e-01 },
488 { -8.6266848187e-01, -4.3388855988e-01, 2.5989191600e-01 },
489 { 0.0000000000e+00, -5.2573111404e-01, 8.5065080716e-01 },
490 { 1.6062203536e-01, -7.0204644650e-01, 6.9378047588e-01 },
491 { 1.6245984673e-01, -2.6286555729e-01, 9.5105651619e-01 },
492 { 4.2532540412e-01, -6.8819096147e-01, 5.8778525089e-01 },
493 { 6.8819096147e-01, -5.8778525089e-01, 4.2532540412e-01 },
494 { -9.5105651619e-01, 1.6245984673e-01, 2.6286555729e-01 },
495 { -9.5105651619e-01, -1.6245984673e-01, 2.6286555729e-01 },
496 { -9.6193835912e-01, -2.7326652422e-01, 0.0000000000e+00 },
497 { -8.5065080716e-01, 0.0000000000e+00, -5.2573111404e-01 },
498 { -6.9378047588e-01, 1.6062203536e-01, -7.0204644650e-01 },
499 { -9.5105651619e-01, -1.6245984673e-01, -2.6286555729e-01 },
500 { -8.6266848187e-01, -4.3388855988e-01, -2.5989191600e-01 },
501 { -5.8778525089e-01, 4.2532540412e-01, -6.8819096147e-01 },
502 { -6.8819096147e-01, 5.8778525089e-01, -4.2532540412e-01 },
503 { -4.2532540412e-01, -6.8819096147e-01, -5.8778525089e-01 },
504 { -6.8819096147e-01, -5.8778525089e-01, -4.2532540412e-01 }
505 };
506 #if 0
507 #define sph320_numS 320
508 VPRIVATE int sph320_S[sph320_numS][3] = {
509 { 12, 42, 43 },
510 { 12, 45, 46 },
511 { 16, 48, 49 },
512 { 19, 51, 52 },
513 { 13, 54, 44 },
514 { 21, 55, 56 },
515 { 24, 58, 59 },
516 { 26, 61, 62 },
517 { 27, 63, 64 },
518 { 29, 66, 67 },
519 { 30, 69, 68 },
520 { 32, 71, 72 },
521 { 34, 74, 75 },
522 { 36, 77, 78 },
523 { 36, 79, 80 },
524 { 38, 82, 83 },
525 { 15, 85, 47 },
526 { 40, 87, 88 },
527 { 27, 90, 91 },
528 { 29, 93, 94 },
529 { 14, 96, 81 },
530 { 13, 97, 98 },
531 { 12, 96, 42 },
532 { 16, 100, 101 },
533 { 15, 102, 103 },
534 { 15, 45, 102 },
535 { 18, 104, 53 },
536 { 17, 105, 92 },
537 { 16, 104, 48 },
538 { 20, 107, 108 },
539 { 18, 110, 106 },
540 { 18, 51, 110 },
541 { 21, 112, 113 },
542 { 19, 114, 109 },
543 { 19, 54, 114 },
544 { 23, 115, 84 },
545 { 22, 116, 117 },
546 { 22, 55, 116 },
547 { 22, 119, 57 },
548 { 25, 121, 122 },
549 { 24, 119, 58 },
550 { 20, 123, 111 },
551 { 24, 125, 120 },
552 { 20, 125, 123 },
553 { 26, 126, 124 },
554 { 28, 127, 128 },
555 { 26, 127, 126 },
556 { 28, 129, 65 },
557 { 30, 130, 131 },
558 { 29, 129, 66 },
559 { 25, 132, 60 },
560 { 31, 133, 73 },
561 { 30, 132, 69 },
562 { 33, 134, 76 },
563 { 31, 136, 70 },
564 { 33, 136, 134 },
565 { 35, 138, 139 },
566 { 33, 141, 137 },
567 { 33, 74, 141 },
568 { 37, 143, 86 },
569 { 34, 144, 140 },
570 { 34, 77, 144 },
571 { 38, 146, 147 },
572 { 14, 148, 99 },
573 { 14, 79, 148 },
574 { 32, 149, 135 },
575 { 23, 150, 118 },
576 { 32, 150, 149 },
577 { 39, 151, 89 },
578 { 37, 152, 145 },
579 { 15, 151, 85 },
580 { 41, 154, 95 },
581 { 39, 156, 153 },
582 { 39, 87, 156 },
583 { 40, 158, 155 },
584 { 17, 159, 50 },
585 { 17, 90, 159 },
586 { 35, 160, 142 },
587 { 41, 161, 157 },
588 { 29, 160, 93 },
589 { 13, 44, 42 },
590 { 4, 43, 44 },
591 { 44, 43, 42 },
592 { 15, 47, 45 },
593 { 0, 46, 47 },
594 { 47, 46, 45 },
595 { 17, 50, 48 },
596 { 9, 49, 50 },
597 { 50, 49, 48 },
598 { 18, 53, 51 },
599 { 4, 52, 53 },
600 { 53, 52, 51 },
601 { 19, 52, 54 },
602 { 4, 44, 52 },
603 { 52, 44, 54 },
604 { 22, 57, 55 },
605 { 8, 56, 57 },
606 { 57, 56, 55 },
607 { 25, 60, 58 },
608 { 3, 59, 60 },
609 { 60, 59, 58 },
610 { 24, 59, 61 },
611 { 3, 62, 59 },
612 { 59, 62, 61 },
613 { 28, 65, 63 },
614 { 2, 64, 65 },
615 { 65, 64, 63 },
616 { 30, 68, 66 },
617 { 7, 67, 68 },
618 { 68, 67, 66 },
619 { 31, 70, 69 },
620 { 7, 68, 70 },
621 { 70, 68, 69 },
622 { 31, 73, 71 },
623 { 10, 72, 73 },
624 { 73, 72, 71 },
625 { 33, 76, 74 },
626 { 6, 75, 76 },
627 { 76, 75, 74 },
628 { 34, 75, 77 },
629 { 6, 78, 75 },
630 { 75, 78, 77 },
631 { 14, 81, 79 },
632 { 0, 80, 81 },
633 { 81, 80, 79 },
634 { 23, 84, 82 },
635 { 1, 83, 84 },
636 { 84, 83, 82 },
637 { 37, 86, 85 },
638 { 0, 47, 86 },
639 { 86, 47, 85 },
640 { 39, 89, 87 },
641 { 9, 88, 89 },
642 { 89, 88, 87 },
643 { 17, 92, 90 },
644 { 5, 91, 92 },
645 { 92, 91, 90 },
646 { 41, 95, 93 },
647 { 2, 94, 95 },
648 { 95, 94, 93 },
649 { 12, 46, 96 },
650 { 0, 81, 46 },
651 { 46, 81, 96 },
652 { 14, 99, 97 },
653 { 1, 98, 99 },
654 { 99, 98, 97 },
655 { 14, 97, 96 },
656 { 13, 42, 97 },
657 { 97, 42, 96 },
658 { 12, 43, 100 },
659 { 4, 101, 43 },
660 { 43, 101, 100 },
661 { 16, 49, 102 },
662 { 9, 103, 49 },
663 { 49, 103, 102 },
664 { 12, 100, 45 },
665 { 16, 102, 100 },
666 { 100, 102, 45 },
667 { 16, 101, 104 },
668 { 4, 53, 101 },
669 { 101, 53, 104 },
670 { 18, 106, 105 },
671 { 5, 92, 106 },
672 { 106, 92, 105 },
673 { 18, 105, 104 },
674 { 17, 48, 105 },
675 { 105, 48, 104 },
676 { 19, 109, 107 },
677 { 8, 108, 109 },
678 { 109, 108, 107 },
679 { 20, 111, 110 },
680 { 5, 106, 111 },
681 { 111, 106, 110 },
682 { 19, 107, 51 },
683 { 20, 110, 107 },
684 { 107, 110, 51 },
685 { 13, 98, 112 },
686 { 1, 113, 98 },
687 { 98, 113, 112 },
688 { 21, 56, 114 },
689 { 8, 109, 56 },
690 { 56, 109, 114 },
691 { 13, 112, 54 },
692 { 21, 114, 112 },
693 { 112, 114, 54 },
694 { 21, 113, 115 },
695 { 1, 84, 113 },
696 { 113, 84, 115 },
697 { 23, 118, 116 },
698 { 10, 117, 118 },
699 { 118, 117, 116 },
700 { 21, 115, 55 },
701 { 23, 116, 115 },
702 { 115, 116, 55 },
703 { 24, 120, 119 },
704 { 8, 57, 120 },
705 { 120, 57, 119 },
706 { 22, 117, 121 },
707 { 10, 122, 117 },
708 { 117, 122, 121 },
709 { 22, 121, 119 },
710 { 25, 58, 121 },
711 { 121, 58, 119 },
712 { 26, 124, 123 },
713 { 5, 111, 124 },
714 { 124, 111, 123 },
715 { 20, 108, 125 },
716 { 8, 120, 108 },
717 { 108, 120, 125 },
718 { 24, 61, 125 },
719 { 26, 123, 61 },
720 { 61, 123, 125 },
721 { 27, 91, 126 },
722 { 5, 124, 91 },
723 { 91, 124, 126 },
724 { 26, 62, 127 },
725 { 3, 128, 62 },
726 { 62, 128, 127 },
727 { 28, 63, 127 },
728 { 27, 126, 63 },
729 { 63, 126, 127 },
730 { 29, 94, 129 },
731 { 2, 65, 94 },
732 { 94, 65, 129 },
733 { 28, 128, 130 },
734 { 3, 131, 128 },
735 { 128, 131, 130 },
736 { 28, 130, 129 },
737 { 30, 66, 130 },
738 { 130, 66, 129 },
739 { 30, 131, 132 },
740 { 3, 60, 131 },
741 { 131, 60, 132 },
742 { 25, 122, 133 },
743 { 10, 73, 122 },
744 { 122, 73, 133 },
745 { 25, 133, 132 },
746 { 31, 69, 133 },
747 { 133, 69, 132 },
748 { 32, 135, 134 },
749 { 6, 76, 135 },
750 { 135, 76, 134 },
751 { 33, 137, 136 },
752 { 7, 70, 137 },
753 { 137, 70, 136 },
754 { 31, 71, 136 },
755 { 32, 134, 71 },
756 { 71, 134, 136 },
757 { 34, 140, 138 },
758 { 11, 139, 140 },
759 { 140, 139, 138 },
760 { 35, 142, 141 },
761 { 7, 137, 142 },
762 { 142, 137, 141 },
763 { 34, 138, 74 },
764 { 35, 141, 138 },
765 { 138, 141, 74 },
766 { 36, 80, 143 },
767 { 0, 86, 80 },
768 { 80, 86, 143 },
769 { 37, 145, 144 },
770 { 11, 140, 145 },
771 { 145, 140, 144 },
772 { 36, 143, 77 },
773 { 37, 144, 143 },
774 { 143, 144, 77 },
775 { 36, 78, 146 },
776 { 6, 147, 78 },
777 { 78, 147, 146 },
778 { 38, 83, 148 },
779 { 1, 99, 83 },
780 { 83, 99, 148 },
781 { 36, 146, 79 },
782 { 38, 148, 146 },
783 { 146, 148, 79 },
784 { 38, 147, 149 },
785 { 6, 135, 147 },
786 { 147, 135, 149 },
787 { 32, 72, 150 },
788 { 10, 118, 72 },
789 { 72, 118, 150 },
790 { 23, 82, 150 },
791 { 38, 149, 82 },
792 { 82, 149, 150 },
793 { 15, 103, 151 },
794 { 9, 89, 103 },
795 { 103, 89, 151 },
796 { 39, 153, 152 },
797 { 11, 145, 153 },
798 { 153, 145, 152 },
799 { 39, 152, 151 },
800 { 37, 85, 152 },
801 { 152, 85, 151 },
802 { 40, 155, 154 },
803 { 2, 95, 155 },
804 { 155, 95, 154 },
805 { 41, 157, 156 },
806 { 11, 153, 157 },
807 { 157, 153, 156 },
808 { 40, 154, 87 },
809 { 41, 156, 154 },
810 { 154, 156, 87 },
811 { 27, 64, 158 },
812 { 2, 155, 64 },
813 { 64, 155, 158 },
814 { 40, 88, 159 },
815 { 9, 50, 88 },
816 { 88, 50, 159 },
817 { 27, 158, 90 },
818 { 40, 159, 158 },
819 { 158, 159, 90 },
820 { 29, 67, 160 },
821 { 7, 142, 67 },
822 { 67, 142, 160 },
823 { 35, 139, 161 },
824 { 11, 157, 139 },
825 { 139, 157, 161 },
826 { 35, 161, 160 },
827 { 41, 93, 161 },
828 { 161, 93, 160 }
829 };
830 #endif
831
832 /*
833 * ***************************************************************************
834 * Class Ogl: Inlineable methods
835 * ***************************************************************************
836 */
837 #if !defined(VINLINE_OGL)
838 #endif /* if !defined(VINLINE_OGL) */
839
840 /*
841 * ***************************************************************************
842 * Class Ogl: Non-inlineable methods
843 * ***************************************************************************
844 */
845
846 VPUBLIC Ogl* Ogl_ctor(int frameMode, int orthoMode)
847 {
848 int i;
849 Ogl *thee = VNULL;
850
851 thee = (Ogl*)calloc( 1, sizeof(Ogl) );
852 VJMPERR1( thee != VNULL );
853
854 /* one-time initialization of main view parameters */
855 thee->frameMode = frameMode; /* frame (0=centered, 1=offset) */
856 thee->orthoMode = orthoMode; /* projection (0=perspect, 1=ortho) */
857
858 /* initialization of main view parameters */
859 thee->lightMode = 1; /* lighting (0=no, 1=yes) */
860 thee->ballMode = 0; /* draw (0=none, 1=draw arcball) */
861 thee->bboxMode = 0; /* draw (0=none, 1=draw bounding box) */
862 thee->backMode = 1; /* polybackside dark paint (0=no, 1=yes) */
863 thee->mouseMode = 0; /* mouse (0=rotate, 1=scale, 2=translate) */
864 thee->mouseDown = 0; /* is mouse button pressed (0=no, 1=yes) */
865 thee->scale = 1.0; /* current global scaling */
866 thee->eyezInit = 3.0; /* current eye position up the z-axis */
867 thee->viewWidth = 0; /* width of the plotbox */
868 thee->viewHeight = 0; /* height of the plotbox */
869
870 /* display list initialization */
871 for (i=0; i<10; i++) {
872 thee->list[i] = 0;
873 thee->listMode[i] = 0;
874 }
875 thee->ball = 0;
876 thee->bbox = 0;
877 thee->bboxUpdate = 0;
878
879 /* additional view parameter initialization; can be reset the user */
880 initView(thee);
881
882 /* initialize lighting and effects */
883 setEffects(thee);
884 setLights(thee);
885
886 /* default bbox */
887 clearBBox(thee);
888
889 /* normal return */
890 return thee;
891
892 VERROR1:
893 /* something went wrong */
894 fprintf(stderr,"Ogl_ctor: problem initalizing.\n");
895 return VNULL;
896 }
897
898 VPUBLIC void Ogl_dtor(Ogl **thee)
899 {
900 if ((*thee) != VNULL) {
901 free( *thee );
902 (*thee) = VNULL;
903 }
904 }
905
906 /*
907 * ***************************************************************************
908 * Purpose: Initialize the OpenGL context.
909 *
910 * Author: Michael Holst
911 * ***************************************************************************
912 */
913 VPUBLIC void Ogl_initBegin(Ogl *thee)
914 {
915 int i;
916 clearBBox(thee);
917 if ( glIsList(thee->ball) ) {
918 glDeleteLists(thee->ball,1);
919 thee->ball = 0;
920 }
921 if ( glIsList(thee->bbox) ) {
922 glDeleteLists(thee->bbox,1);
923 thee->bbox = 0;
924 }
925 for (i=0; i<10; i++) {
926 if ( glIsList(thee->list[i]) ) {
927 glDeleteLists(thee->list[i],1);
928 thee->list[i] = 0;
929 thee->listMode[i] = 0;
930 }
931 }
932 }
933 VPUBLIC void Ogl_initEnd(Ogl *thee)
934 {
935 makeBBox(thee);
936 makeBBoxList(thee);
937 makeBallList(thee);
938 }
939
940 /*
941 * ***************************************************************************
942 * Purpose: The main 3D==>2D Projection function.
943 *
944 * Author: Michael Holst
945 * ***************************************************************************
946 */
947 VPUBLIC void Ogl_reshape(Ogl *thee, int w, int h)
948 {
949 thee->viewWidth = w;
950 thee->viewHeight = h;
951
952 glViewport(0, 0, w, h);
953 glMatrixMode(GL_PROJECTION);
954 glLoadIdentity();
955 if (thee->orthoMode) {
956 if (thee->frameMode) {
957 glOrtho( -1.0001, 2.0001, -1.0001, 1.0001, -1.0, 20.0 );
958 } else {
959 glOrtho( -1.0001, 1.0001, -1.0001, 1.0001, -1.0, 20.0 );
960 }
961 } else {
962 gluPerspective(45.0, (double)w / (double)h, 0.01, 20.0);
963 }
964 glMatrixMode(GL_MODELVIEW);
965 glLoadIdentity();
966 setLights(thee);
967 }
968
969 /*
970 * ***************************************************************************
971 * Purpose: Main OpenGL display driver.
972 *
973 * Notes: We use the Z-buffer here for hidden line and hidden surface
974 * removal. We just have to make sure that we get a visual
975 * device with some Z-buffer bits, and that we don't mess up the
976 * transformations so that the Z-axis points somewhere other than
977 * positively straight at the eye.
978 *
979 * Frame codes:
980 *
981 * If "frameMode==0", we draw only the frame 4 lists (4-9),
982 * centered in frame 1 show below, regardless of window shape.
983 *
984 * If "frameMode==1", we draw frame 1,2,3,4 lists (0-9), in the
985 * appropriate frames.
986 *
987 * ._____._____._____. ._____._____._____. ._____._____.......
988 * | | | | | | | .
989 * | | | | 2 | | | .
990 * | 1 | | 4 |_____| | ? |......
991 * | | | | | | | .
992 * | | | | 3 | | | .
993 * |_____._____._____| |_____._____|_____| |_____._____.......
994 *
995 * Display list codes:
996 *
997 * List Frame Type
998 *
999 * 0 1 non-rotating, non-lighted
1000 * 1 1 non-rotating, non-lighted
1001 * 2 2 non-rotating, non-lighted
1002 * 3 3 non-rotating, non-lighted
1003 *
1004 * 4 4 non-rotating, non-lighted
1005 * 5 4 rotating, non-lighted
1006 * 6 4 rotating, non-lighted
1007 * 7 4 rotating, lighted
1008 * 8 4 rotating, lighted
1009 * 9 4 non-rotating, lighted
1010 *
1011 * Author: Michael Holst
1012 * ***************************************************************************
1013 */
1014 VPUBLIC void Ogl_display(Ogl *thee)
1015 {
1016 int i, lightSave;
1017 float quat[4], m[4][4];
1018
1019 /* clear the OpenGL buffer and push matrix transformations on the stack */
1020 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1021
1022 /*
1023 * HACK: This empty glBegin/glEnd is required to get the buggy
1024 * nVidia OpenGL libraries to do two-sided lighting effects
1025 * correctly.
1026 */
1027 #if 1
1028 glBegin(GL_LINES);
1029 glEnd();
1030 #endif
1031
1032 glPushMatrix();
1033
1034 /* setup the view (handles possible translation) */
1035 if (thee->orthoMode) {
1036 gluLookAt( thee->eye[0], thee->eye[1], thee->eye[2],
1037 thee->origin[0], thee->origin[1], thee->origin[2],
1038 0.0, 1.0, 0.0 );
1039 } else {
1040 gluLookAt( thee->eye[0], thee->eye[1], thee->eye[2],
1041 thee->origin[0], thee->origin[1], thee->origin[2],
1042 0.0, 1.0, 0.0 );
1043 if (thee->frameMode) {
1044 glTranslatef( -0.5, 0.0, 0.0 );
1045 }
1046 }
1047
1048 /* translation fix for left-shifted rotation origin in a wide window */
1049
1050 /* call list=9 (lighted, but not rotated) */
1051 if (thee->listMode[9] == 1)
1052 if ( glIsList(thee->list[9]) )
1053 glCallList(thee->list[9]);
1054
1055 glPushMatrix();
1056
1057 /* global scaling and translation */
1058 glScalef( 1./thee->scale, 1./thee->scale, 1./thee->scale );
1059 glTranslatef( thee->trans[0], thee->trans[1], thee->trans[2] );
1060
1061 /* call dragarc display list (lighted, but not rotated) */
1062 if ( (thee->ballMode == 1)
1063 && (thee->mouseMode == 0)
1064 && (thee->mouseDown > 0) ) dragBall(thee);
1065
1066 /* compute the rotation */
1067 addQuat(thee->quatDrag, thee->quat, quat);
1068 buildRotation(m, quat);
1069 glMultMatrixf(&m[0][0]);
1070
1071 /* call trackball list (lighted, and rotated) */
1072 if (thee->ballMode == 1)
1073 if ( glIsList(thee->ball) )
1074 glCallList(thee->ball);
1075
1076 /* call list=8,7 (lighted, and rotated) */
1077 for (i=7; i<=8; i++) {
1078 if (thee->listMode[i] == 1)
1079 if ( glIsList(thee->list[i]) )
1080 glCallList(thee->list[i]);
1081 }
1082
1083 /* turn the lights out */
1084 lightSave = thee->lightMode;
1085 thee->lightMode = 0;
1086 setLights(thee);
1087
1088 /* call list=6,5 (not lighted, but rotated) */
1089 for (i=5; i<=6; i++) {
1090 if (thee->listMode[i] == 1)
1091 if ( glIsList(thee->list[i]) )
1092 glCallList(thee->list[i]);
1093 }
1094
1095 /* call the bounding box list (not lighted, but rotated) */
1096 if (thee->bboxMode == 1)
1097 if ( glIsList(thee->bbox) )
1098 glCallList(thee->bbox);
1099
1100 glPopMatrix();
1101
1102 /* call list=4 (not lighted, not rotated) */
1103 if (thee->listMode[4] == 1)
1104 if ( glIsList(thee->list[4]) )
1105 glCallList(thee->list[4]);
1106
1107 /* call list=3,2,1,0 (not lighted, not rotated) */
1108 if (thee->frameMode) {
1109 for (i=0; i<=3; i++) {
1110 if (thee->listMode[i] == 1)
1111 if ( glIsList(thee->list[i]) )
1112 glCallList(thee->list[i]);
1113 }
1114 }
1115
1116 glPopMatrix();
1117
1118 /* turn the lights back on */
1119 thee->lightMode = lightSave;
1120 setLights(thee);
1121
1122 /* flush the graphics pipeline */
1123 glFlush();
1124
1125 /* int error;
1126 * while ((error=glGetError()) != GL_NO_ERROR)
1127 * fprintf(stderr, "GL error: %s\n", gluErrorString(error));
1128 */
1129 }
1130
1131 /*
1132 * ***************************************************************************
1133 * Purpose: Some simple callback routines to manipulate the object.
1134 *
1135 * Author: Michael Holst
1136 * ***************************************************************************
1137 */
1138 VPUBLIC int Ogl_lightModeOnOff(Ogl *thee)
1139 {
1140 thee->lightMode = (thee->lightMode + 1) % 2;
1141 return thee->lightMode;
1142 }
1143 VPUBLIC int Ogl_orthoModeOnOff(Ogl *thee)
1144 {
1145 thee->orthoMode = (thee->orthoMode + 1) % 2;
1146 return thee->orthoMode;
1147 }
1148 VPUBLIC int Ogl_frameModeOnOff(Ogl *thee)
1149 {
1150 thee->frameMode = (thee->frameMode + 1) % 2;
1151 return thee->frameMode;
1152 }
1153 VPUBLIC int Ogl_ballModeOnOff(Ogl *thee)
1154 {
1155 thee->ballMode = (thee->ballMode + 1) % 2;
1156 return thee->ballMode;
1157 }
1158 VPUBLIC int Ogl_bboxModeOnOff(Ogl *thee)
1159 {
1160 thee->bboxMode = (thee->bboxMode + 1) % 2;
1161 return thee->bboxMode;
1162 }
1163 VPUBLIC int Ogl_listModeOnOff(Ogl *thee, int list)
1164 {
1165 thee->listMode[list] = (thee->listMode[list] + 1) % 2;
1166 return thee->listMode[list];
1167 }
1168 VPUBLIC int Ogl_isList(Ogl *thee, int list)
1169 {
1170 return glIsList(thee->list[list]);
1171 }
1172 VPUBLIC int Ogl_isListOn(Ogl *thee, int list)
1173 {
1174 return thee->listMode[list];
1175 }
1176
1177 VPUBLIC void Ogl_resetPos(Ogl *thee)
1178 {
1179 initView(thee);
1180 }
1181
1182 /*
1183 * ***************************************************************************
1184 * Purpose: Draw (immediate mode) a circular partial arc on the trackball.
1185 *
1186 * Author: Michael Holst
1187 * ***************************************************************************
1188 */
1189 VPRIVATE void dragBall(Ogl *thee)
1190 {
1191 int i;
1192 float alpha, pq[3];
1193
1194 glLineWidth( (float)3.0 );
1195 glColor4fv( C_CYAN );
1196 glBegin(GL_LINE_STRIP);
1197 for (i=0; i<NUMARC; i++) {
1198 alpha = (float)i / (float)(NUMARC-1);
1199 pq[0] = (1.0-alpha)*thee->r0[0] + alpha*thee->r2[0];
1200 pq[1] = (1.0-alpha)*thee->r0[1] + alpha*thee->r2[1];
1201 pq[2] = (1.0-alpha)*thee->r0[2] + alpha*thee->r2[2];
1202 vnormal(pq);
1203 vscale(pq, BALLSIZE);
1204 glVertex3fv(pq);
1205 }
1206 glEnd();
1207 glLineWidth( (float)1.0 );
1208 }
1209
1210 /*
1211 * ***************************************************************************
1212 * Purpose: Make a trackball display list.
1213 *
1214 * Author: Michael Holst
1215 * ***************************************************************************
1216 */
1217 VPRIVATE void makeBallList(Ogl *thee)
1218 {
1219 static int init=0;
1220 static GLUquadricObj *quadObj;
1221
1222 if (!init) {
1223 quadObj = gluNewQuadric();
1224 gluQuadricDrawStyle(quadObj, GLU_FILL);
1225 init = 1;
1226 }
1227
1228 VJMPERR1( glIsList(thee->ball=glGenLists(1)) );
1229 glNewList(thee->ball,GL_COMPILE);
1230
1231 glColor4fv( C_BALL );
1232 glPolygonMode( GL_BACK, GL_FILL );
1233 glPolygonMode( GL_FRONT, GL_POINT );
1234 gluSphere(quadObj, BALLSIZE, NUMARC, NUMARC);
1235 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1236
1237 glEndList();
1238
1239 /* normal return */
1240 return;
1241
1242 VERROR1:
1243 /* something went wrong */
1244 fprintf(stderr,"makeBallList: problem creating display list.\n");
1245 return;
1246 }
1247
1248 /*
1249 * ***************************************************************************
1250 * Purpose: Make a bounding box display list.
1251 *
1252 * Author: Michael Holst
1253 * ***************************************************************************
1254 */
1255 VPRIVATE void makeBBoxList(Ogl *thee)
1256 {
1257 VJMPERR1( glIsList(thee->bbox=glGenLists(1)) );
1258 glNewList(thee->bbox,GL_COMPILE);
1259
1260 glColor4fv( C_BLACK );
1261
1262 glBegin(GL_LINES);
1263
1264 glVertex3fv(thee->bboxCoord[0]);
1265 glVertex3fv(thee->bboxCoord[1]);
1266 glVertex3fv(thee->bboxCoord[0]);
1267 glVertex3fv(thee->bboxCoord[2]);
1268 glVertex3fv(thee->bboxCoord[0]);
1269 glVertex3fv(thee->bboxCoord[4]);
1270
1271 glVertex3fv(thee->bboxCoord[5]);
1272 glVertex3fv(thee->bboxCoord[1]);
1273 glVertex3fv(thee->bboxCoord[5]);
1274 glVertex3fv(thee->bboxCoord[4]);
1275 glVertex3fv(thee->bboxCoord[5]);
1276 glVertex3fv(thee->bboxCoord[7]);
1277
1278 glVertex3fv(thee->bboxCoord[6]);
1279 glVertex3fv(thee->bboxCoord[4]);
1280 glVertex3fv(thee->bboxCoord[6]);
1281 glVertex3fv(thee->bboxCoord[7]);
1282 glVertex3fv(thee->bboxCoord[6]);
1283 glVertex3fv(thee->bboxCoord[2]);
1284
1285 glVertex3fv(thee->bboxCoord[3]);
1286 glVertex3fv(thee->bboxCoord[1]);
1287 glVertex3fv(thee->bboxCoord[3]);
1288 glVertex3fv(thee->bboxCoord[2]);
1289 glVertex3fv(thee->bboxCoord[3]);
1290 glVertex3fv(thee->bboxCoord[7]);
1291
1292 glEnd();
1293
1294 glEndList();
1295
1296 /* normal return */
1297 return;
1298
1299 VERROR1:
1300 /* something went wrong */
1301 fprintf(stderr,"makeBBoxList: problem creating display list.\n");
1302 return;
1303 }
1304
1305 /*
1306 * ***************************************************************************
1307 * Purpose: Make a generic display list.
1308 *
1309 * Author: Michael Holst
1310 * ***************************************************************************
1311 */
1312 VPUBLIC void Ogl_beginList(Ogl *thee, int list, int show)
1313 {
1314 VJMPERR1( (0 <= list) && (list <= 9) );
1315 VJMPERR1( glIsList(thee->list[list]=glGenLists(1)) );
1316 glNewList(thee->list[list],GL_COMPILE);
1317
1318 #if defined(HAVE_POLYGON_OFFSET)
1319 glEnable( GL_POLYGON_OFFSET_FILL );
1320 glPolygonOffset(1.0, 1.0);
1321 #endif
1322
1323 /* are we supposed to display the list by default? */
1324 if (show) {
1325 thee->listMode[list] = 1;
1326 } else {
1327 thee->listMode[list] = 0;
1328 }
1329
1330 /* do we update the bounding box? */
1331 if ((4 <= list) && (list <= 9)) {
1332 thee->bboxUpdate = 1;
1333 } else {
1334 thee->bboxUpdate = 0;
1335 }
1336
1337 /* normal return */
1338 return;
1339
1340 VERROR1:
1341 /* something went wrong */
1342 fprintf(stderr,"Ogl_beginList: problem creating display list.\n");
1343 return;
1344 }
1345 VPUBLIC void Ogl_endList(Ogl *thee)
1346 {
1347 #if defined(HAVE_POLYGON_OFFSET)
1348 glDisable( GL_POLYGON_OFFSET_FILL );
1349 #endif
1350
1351 glEndList();
1352 }
1353 VPUBLIC void Ogl_addListObject(Ogl *thee,
1354 int type, int numPV, float vx[][3], float c[4], float r, char text[])
1355 {
1356 int j, p;
1357 float d1[3], d2[3], d3[3], vv[sph320_numV][3];
1358 static int init=0;
1359 static GLUquadricObj *quadObj;
1360 #if 0
1361 float b[4];
1362 #endif
1363
1364 if (!init) {
1365 quadObj = gluNewQuadric();
1366 gluQuadricDrawStyle(quadObj, GLU_FILL);
1367 init = 1;
1368 }
1369
1370 /*
1371 * type==0 ==> edges (a line strip)
1372 * type==1 ==> filled polygon
1373 * type==3 ==> text
1374 * type==4 ==> 2-sphere (coarse resolution)
1375 * type==5 ==> 2-sphere (medium resolution)
1376 * type==6 ==> 2-sphere (fine resolution)
1377 */
1378
1379 /* deal with text immediately and return */
1380 if (type == 3) {
1381 VASSERT( numPV == 1 );
1382 glRasterPos3f( vx[0][0], vx[0][1], vx[0][2] );
1383 glColor4fv( c );
1384 glListBase(2000);
1385 glCallLists( strlen(text), GL_UNSIGNED_BYTE, text );
1386 return;
1387 }
1388
1389 /* collect bbox info (ignore vertices outside unit cube) */
1390 if (thee->bboxUpdate) {
1391 for (j=0; j<numPV; j++) {
1392 for (p=0; p<3; p++) {
1393 /*
1394 * if ((vx[j][p]>(CUBESIZE+TOL))||(vx[j][p]<-(CUBESIZE+TOL))){
1395 * fprintf(stderr,
1396 * "Ogl_addListObject: object outside cube; clipping.\n");
1397 * return;
1398 * }
1399 */
1400 if (vx[j][p] < thee->min[p]) thee->min[p] = vx[j][p];
1401 if (vx[j][p] > thee->max[p]) thee->max[p] = vx[j][p];
1402 }
1403 }
1404 }
1405
1406 /* deal with point/line/polygon differences */
1407 if (numPV == 1) {
1408
1409 glColor4fv( c );
1410
1411 /* POINT: if zero radius, just register point and that's it */
1412 if ((type == 1) || (r == 0.0)) {
1413 glBegin(GL_POINTS);
1414 glVertex3fv(vx[0]);
1415 glEnd();
1416
1417 /* NON-zero radius: type tells how nice to make a sphere at point */
1418 } else if (type == 4) {
1419 for (j=0; j<sph8_numV; j++) {
1420 vcopy(sph8_V[j],vv[j]);
1421 vnormal(vv[j]);
1422 vscale(vv[j],r);
1423 }
1424 glPushMatrix();
1425 glTranslatef( vx[0][0], vx[0][1], vx[0][2] );
1426 #if 1
1427 gluSphere(quadObj, r, NUMPDB, NUMPDB);
1428 #else
1429 glBegin(GL_TRIANGLES);
1430 for (i=0; i<sph8_numS; i++) {
1431 glNormal3fv(sph8_V[ sph8_S[i][0] ]);
1432 glVertex3fv(vv[ sph8_S[i][0] ]);
1433 glNormal3fv(sph8_V[ sph8_S[i][1] ]);
1434 glVertex3fv(vv[ sph8_S[i][1] ]);
1435 glNormal3fv(sph8_V[ sph8_S[i][2] ]);
1436 glVertex3fv(vv[ sph8_S[i][2] ]);
1437 }
1438 glEnd();
1439 #endif
1440 glPopMatrix();
1441 } else if (type == 5) {
1442 for (j=0; j<sph80_numV; j++) {
1443 vcopy(sph80_V[j],vv[j]);
1444 vnormal(vv[j]);
1445 vscale(vv[j],r);
1446 }
1447 glPushMatrix();
1448 glTranslatef( vx[0][0], vx[0][1], vx[0][2] );
1449 #if 1
1450 gluSphere(quadObj, r, NUMPDB, NUMPDB);
1451 #else
1452 glBegin(GL_TRIANGLES);
1453 for (i=0; i<sph80_numS; i++) {
1454 glNormal3fv(sph80_V[ sph80_S[i][0] ]);
1455 glVertex3fv(vv[ sph80_S[i][0] ]);
1456 glNormal3fv(sph80_V[ sph80_S[i][1] ]);
1457 glVertex3fv(vv[ sph80_S[i][1] ]);
1458 glNormal3fv(sph80_V[ sph80_S[i][2] ]);
1459 glVertex3fv(vv[ sph80_S[i][2] ]);
1460 }
1461 glEnd();
1462 #endif
1463 glPopMatrix();
1464 } else if (type == 6) {
1465 for (j=0; j<sph320_numV; j++) {
1466 vcopy(sph320_V[j],vv[j]);
1467 vnormal(vv[j]);
1468 vscale(vv[j],r);
1469 }
1470 glPushMatrix();
1471 glTranslatef( vx[0][0], vx[0][1], vx[0][2] );
1472 #if 1
1473 gluSphere(quadObj, r, NUMPDB, NUMPDB);
1474 #else
1475 glBegin(GL_TRIANGLES);
1476 for (i=0; i<sph320_numS; i++) {
1477 glNormal3fv(sph320_V[ sph320_S[i][0] ]);
1478 glVertex3fv(vv[ sph320_S[i][0] ]);
1479 glNormal3fv(sph320_V[ sph320_S[i][1] ]);
1480 glVertex3fv(vv[ sph320_S[i][1] ]);
1481 glNormal3fv(sph320_V[ sph320_S[i][2] ]);
1482 glVertex3fv(vv[ sph320_S[i][2] ]);
1483 }
1484 glEnd();
1485 #endif
1486 glPopMatrix();
1487 }
1488
1489 } else if (numPV >= 2) {
1490
1491 if ((numPV == 2) || (type == 0)) {
1492
1493 /* LINE: set the frontface and backface colors */
1494 glColor4fv( c );
1495
1496 /* register the line */
1497 if (numPV == 2) {
1498 glBegin(GL_LINES);
1499 glVertex3fv(vx[0]);
1500 glVertex3fv(vx[1]);
1501 glEnd();
1502 } else if (type == 0) {
1503 glBegin(GL_LINE_STRIP);
1504 for (j=0; j<numPV; j++)
1505 glVertex3fv(vx[j]);
1506 glEnd();
1507 }
1508
1509 } else if ((numPV >= 3) && (type == 1)) {
1510
1511 /* POLYGON: set the frontface and backface colors */
1512 glColor4fv( c );
1513 #if 0
1514 if (thee->backMode == 1) {
1515 for (j=0; j<4; j++) b[j] = 0.5 * c[j];
1516 }
1517 #endif
1518
1519 /* get a normal vector using first three vertices to define plane */
1520 for (j=0; j<3; j++) {
1521 d1[j] = vx[0][j] - vx[1][j];
1522 d2[j] = vx[0][j] - vx[2][j];
1523 }
1524 vcross(d1, d2, d3);
1525 vnormal(d3);
1526 glNormal3fv(d3);
1527
1528 /* register the polygon */
1529 if (numPV == 3) {
1530 glBegin(GL_TRIANGLES);
1531 glVertex3fv(vx[0]);
1532 glVertex3fv(vx[1]);
1533 glVertex3fv(vx[2]);
1534 glEnd();
1535 } else if (numPV == 4) {
1536 glBegin(GL_QUADS);
1537 glVertex3fv(vx[0]);
1538 glVertex3fv(vx[1]);
1539 glVertex3fv(vx[2]);
1540 glVertex3fv(vx[3]);
1541 glEnd();
1542 } else { /* (numPV >= 5) */
1543 glBegin(GL_POLYGON);
1544 for (j=0; j<numPV; j++)
1545 glVertex3fv(vx[j]);
1546 glEnd();
1547 }
1548 } else {
1549 fprintf(stderr,
1550 "Ogl_addListObject: object is defective; clipping.\n");
1551 return;
1552 }
1553 } else {
1554 fprintf(stderr,
1555 "Ogl_addListObject: object is defective; clipping.\n");
1556 return;
1557 }
1558 }
1559
1560 /*
1561 * ***************************************************************************
1562 * Purpose: Initialize the OpenGL context view parameters.
1563 *
1564 * Author: Michael Holst
1565 * ***************************************************************************
1566 */
1567 VPRIVATE void initView(Ogl *thee)
1568 {
1569 thee->origin[0] = 0.0;
1570 thee->origin[1] = 0.0;
1571 thee->origin[2] = 0.0;
1572 thee->eye[0] = 0.0;
1573 #if 1
1574 thee->eye[1] = 0.0;
1575 #else
1576 thee->eye[1] = 0.4 * thee->eyezInit;
1577 #endif
1578 thee->eye[2] = thee->eyezInit;
1579 thee->trans[0] = 0.0;
1580 thee->trans[1] = 0.0;
1581 thee->trans[2] = 0.0;
1582 thee->scale = 1.0;
1583 vzero(thee->quat);
1584 thee->quat[3] = 1.0;
1585 vzero(thee->quatDrag);
1586 thee->quatDrag[3] = 1.0;
1587 vset(thee->r0, 0.0, 0.0, projectSphere(BALLSIZE, 0.0, 0.0));
1588 vset(thee->r1, 0.0, 0.0, projectSphere(BALLSIZE, 0.0, 0.0));
1589 vset(thee->r2, 0.0, 0.0, projectSphere(BALLSIZE, 0.0, 0.0));
1590 vzero(thee->s1);
1591 vzero(thee->s2);
1592 vzero(thee->t1);
1593 vzero(thee->t2);
1594 }
1595
1596 /*
1597 * ***************************************************************************
1598 * Purpose: Set the OpenGL lighting and other effects.
1599 *
1600 * Author: Michael Holst
1601 * ***************************************************************************
1602 */
1603 VPRIVATE float amb[4] = {(float)0.12, (float)0.12, (float)0.12, (float)0.0 };
1604 VPRIVATE float dif[4] = {(float)0.5, (float)0.5, (float)0.5, (float)0.0 };
1605 VPRIVATE float spe[4] = {(float)0.6, (float)0.6, (float)0.6, (float)0.0 };
1606 VPRIVATE void setLights(Ogl *thee)
1607 {
1608 VPRIVATE float pos0[4] ={(float) 0.0, (float) 0.0, (float) 1.0, (float)0.0};
1609 VPRIVATE float spt0[3] ={(float) 0.0, (float) 0.0, (float)-1.0};
1610
1611 if (thee->lightMode) {
1612 glEnable( GL_LIGHTING );
1613 glEnable( GL_LIGHT0 );
1614 glLightfv( GL_LIGHT0, GL_AMBIENT, amb );
1615 glLightfv( GL_LIGHT0, GL_DIFFUSE, dif );
1616 glLightfv( GL_LIGHT0, GL_SPECULAR, spe );
1617 glLightfv( GL_LIGHT0, GL_POSITION, pos0 );
1618 glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spt0 );
1619 } else glDisable( GL_LIGHTING );
1620 }
1621 VPRIVATE void setEffects(Ogl *thee)
1622 {
1623 VPRIVATE float emi[4] = {(float)0.0, (float)0.0, (float)0.0, (float)0.0 };
1624 VPRIVATE float shi[1] = {(float)12.0 };
1625
1626 glClearColor( C_CLEAR_GV[0], C_CLEAR_GV[1], C_CLEAR_GV[2], C_CLEAR_GV[3] );
1627 glLineWidth( (float)1.0 );
1628 glFrontFace( GL_CCW );
1629 glDepthFunc( GL_LEQUAL );
1630 glShadeModel( GL_SMOOTH );
1631 glEnable( GL_DEPTH_TEST );
1632 glEnable( GL_NORMALIZE );
1633 glEnable( GL_COLOR_MATERIAL );
1634 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
1635 glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE );
1636 glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
1637 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb );
1638 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, dif );
1639 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, spe );
1640 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emi );
1641 glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, shi );
1642 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
1643 #if 0
1644 glEnable( GL_CULL_FACE );
1645 glCullFace( GL_BACK );
1646 #endif
1647 }
1648 VPRIVATE void clearBBox(Ogl *thee)
1649 {
1650 thee->min[0] = VLARGE;
1651 thee->max[0] = -VLARGE;
1652 thee->min[1] = VLARGE;
1653 thee->max[1] = -VLARGE;
1654 thee->min[2] = VLARGE;
1655 thee->max[2] = -VLARGE;
1656
1657 thee->bboxCoord[0][0] = - CUBESIZE;
1658 thee->bboxCoord[0][1] = - CUBESIZE;
1659 thee->bboxCoord[0][2] = - CUBESIZE;
1660
1661 thee->bboxCoord[1][0] = CUBESIZE;
1662 thee->bboxCoord[1][1] = - CUBESIZE;
1663 thee->bboxCoord[1][2] = - CUBESIZE;
1664
1665 thee->bboxCoord[2][0] = - CUBESIZE;
1666 thee->bboxCoord[2][1] = CUBESIZE;
1667 thee->bboxCoord[2][2] = - CUBESIZE;
1668
1669 thee->bboxCoord[3][0] = CUBESIZE;
1670 thee->bboxCoord[3][1] = CUBESIZE;
1671 thee->bboxCoord[3][2] = - CUBESIZE;
1672
1673 thee->bboxCoord[4][0] = - CUBESIZE;
1674 thee->bboxCoord[4][1] = - CUBESIZE;
1675 thee->bboxCoord[4][2] = CUBESIZE;
1676
1677 thee->bboxCoord[5][0] = CUBESIZE;
1678 thee->bboxCoord[5][1] = - CUBESIZE;
1679 thee->bboxCoord[5][2] = CUBESIZE;
1680
1681 thee->bboxCoord[6][0] = - CUBESIZE;
1682 thee->bboxCoord[6][1] = CUBESIZE;
1683 thee->bboxCoord[6][2] = CUBESIZE;
1684
1685 thee->bboxCoord[7][0] = CUBESIZE;
1686 thee->bboxCoord[7][1] = CUBESIZE;
1687 thee->bboxCoord[7][2] = CUBESIZE;
1688 }
1689 VPRIVATE void makeBBox(Ogl *thee)
1690 {
1691 thee->bboxCoord[0][0] = thee->min[0];
1692 thee->bboxCoord[0][1] = thee->min[1];
1693 thee->bboxCoord[0][2] = thee->min[2];
1694
1695 thee->bboxCoord[1][0] = thee->max[0];
1696 thee->bboxCoord[1][1] = thee->min[1];
1697 thee->bboxCoord[1][2] = thee->min[2];
1698
1699 thee->bboxCoord[2][0] = thee->min[0];
1700 thee->bboxCoord[2][1] = thee->max[1];
1701 thee->bboxCoord[2][2] = thee->min[2];
1702
1703 thee->bboxCoord[3][0] = thee->max[0];
1704 thee->bboxCoord[3][1] = thee->max[1];
1705 thee->bboxCoord[3][2] = thee->min[2];
1706
1707 thee->bboxCoord[4][0] = thee->min[0];
1708 thee->bboxCoord[4][1] = thee->min[1];
1709 thee->bboxCoord[4][2] = thee->max[2];
1710
1711 thee->bboxCoord[5][0] = thee->max[0];
1712 thee->bboxCoord[5][1] = thee->min[1];
1713 thee->bboxCoord[5][2] = thee->max[2];
1714
1715 thee->bboxCoord[6][0] = thee->min[0];
1716 thee->bboxCoord[6][1] = thee->max[1];
1717 thee->bboxCoord[6][2] = thee->max[2];
1718
1719 thee->bboxCoord[7][0] = thee->max[0];
1720 thee->bboxCoord[7][1] = thee->max[1];
1721 thee->bboxCoord[7][2] = thee->max[2];
1722 }
1723
1724 /*
1725 * ***************************************************************************
1726 * Purpose: Primary rotation/scaling/translation routines.
1727 *
1728 * Author: Michael Holst
1729 * ***************************************************************************
1730 */
1731 VPRIVATE void rotate(Ogl *thee)
1732 {
1733 if ( (thee->r0[0]==thee->r2[0]) && (thee->r0[1]==thee->r2[1]) ) {
1734 vzero(thee->quatDrag);
1735 thee->quatDrag[3] = 1.0;
1736 } else { buildQuat(thee->quatDrag, thee->r0, thee->r2); }
1737 }
1738 VPRIVATE void scale(Ogl *thee)
1739 {
1740 float px, py, qx, qy, dx, dy, d, scale;
1741 if ( (thee->s1[0]==thee->s2[0]) && (thee->s1[1]==thee->s2[1]) ) {
1742 } else {
1743 px = thee->s1[0];
1744 py = thee->s1[1];
1745 qx = thee->s2[0];
1746 qy = thee->s2[1];
1747 dx = px-qx;
1748 dy = py-qy;
1749 d = VSQRT( dx*dx + dy*dy );
1750 if (VABS(dx) > VABS(dy)) {
1751 scale = 1.0 + VSIGN(d,dx);
1752 } else {
1753 scale = 1.0 + VSIGN(d,dy);
1754 }
1755 thee->scale *= scale;
1756 }
1757 }
1758 VPRIVATE void translate(Ogl *thee)
1759 {
1760 float px, py, qx, qy, dx, dy;
1761 if ( (thee->t1[0]==thee->t2[0]) && (thee->t1[1]==thee->t2[1]) ) {
1762 } else {
1763 px = thee->t1[0];
1764 py = thee->t1[1];
1765 qx = thee->t2[0];
1766 qy = thee->t2[1];
1767 dx = px-qx;
1768 dy = py-qy;
1769 thee->trans[0] -= 1.0*dx;
1770 thee->trans[1] -= 1.0*dy;
1771 }
1772 }
1773 VPUBLIC void Ogl_setMouseMode(Ogl *thee, int value)
1774 {
1775 int xval = 0;
1776 if ((0 <= value) && (value <= 2)) xval = value;
1777 thee->mouseMode = xval;
1778 }
1779 VPUBLIC void Ogl_mouseDown(Ogl *thee, int x, int y)
1780 {
1781 float px, py;
1782 VASSERT( thee->mouseDown < 3 );
1783 thee->mouseDown++;
1784 if ( thee->mouseDown == 1 ) {
1785 vzero(thee->quatDrag);
1786 thee->quatDrag[3] = 1.0;
1787 px = (2.0*x - thee->viewWidth) / thee->viewWidth;
1788 py = (thee->viewHeight - 2.0*y) / thee->viewHeight;
1789 if (thee->mouseMode == 0) {
1790 vset(thee->r0, px, py, projectSphere(BALLSIZE, px, py));
1791 vset(thee->r1, px, py, projectSphere(BALLSIZE, px, py));
1792 vset(thee->r2, px, py, projectSphere(BALLSIZE, px, py));
1793 } else if (thee->mouseMode == 1) {
1794 vset(thee->s1, px, py, 0.0);
1795 vset(thee->s2, px, py, 0.0);
1796 } else if (thee->mouseMode == 2) {
1797 vset(thee->t1, px, py, 0.0);
1798 vset(thee->t2, px, py, 0.0);
1799 } else { VASSERT(0); }
1800 }
1801 }
1802 VPUBLIC void Ogl_mouseUp(Ogl *thee, int x, int y)
1803 {
1804 VASSERT( thee->mouseDown > 0 );
1805 thee->mouseDown--;
1806 if ( thee->mouseDown == 0 ) {
1807 addQuat(thee->quatDrag, thee->quat, thee->quat);
1808 vzero(thee->quatDrag);
1809 thee->quatDrag[3] = 1.0;
1810 }
1811 }
1812 VPUBLIC void Ogl_mouseMotion(Ogl *thee, int x, int y)
1813 {
1814 float px, py;
1815 VASSERT( thee->mouseDown > 0 );
1816 px = (2.0*x - thee->viewWidth) / thee->viewWidth;
1817 py = (thee->viewHeight - 2.0*y) / thee->viewHeight;
1818 if (thee->mouseMode == 0) {
1819 vcopy(thee->r2, thee->r1);
1820 vset(thee->r2, px, py, projectSphere(BALLSIZE, px, py));
1821 rotate( thee );
1822 } else if (thee->mouseMode == 1) {
1823 vcopy(thee->s2, thee->s1);
1824 vset(thee->s2, px, py, 0.0);
1825 scale( thee );
1826 } else if (thee->mouseMode == 2) {
1827 vcopy(thee->t2, thee->t1);
1828 vset(thee->t2, px, py, 0.0);
1829 translate( thee );
1830 } else { VASSERT(0); }
1831 }
1832 VPUBLIC int Ogl_spin(Ogl *thee)
1833 {
1834 int i;
1835 float r0[3], r1[3], r2[3], bc[3], r02[3], r12[3];
1836 float alpha, d02, d12;
1837
1838 vcopy(thee->r0, r0);
1839 vcopy(thee->r1, r1);
1840 vcopy(thee->r2, r2);
1841
1842 vsub(r0,r2,r02);
1843 vsub(r1,r2,r12);
1844 d02 = vlength(r02);
1845 d12 = vlength(r12);
1846 alpha = d12 / d02;
1847
1848 vadd(r0,r2,bc);
1849 vscale(bc,0.5);
1850 for (i=0; i<3; i++) {
1851 r0[i] = (1.0-alpha)*bc[i] + alpha*r0[i];
1852 r2[i] = (1.0-alpha)*bc[i] + alpha*r2[i];
1853 }
1854
1855 if (d12 > TOL) {
1856 buildQuat(thee->quatDrag, r0, r2);
1857 addQuat(thee->quatDrag, thee->quat, thee->quat);
1858 vzero(thee->quatDrag);
1859 thee->quatDrag[3] = 1.0;
1860 return 1;
1861 } else {
1862 return 0;
1863 }
1864 }
1865
1866 /*
1867 * ***************************************************************************
1868 * Purpose: Vector utilities to support the quaternion-based rotator.
1869 *
1870 * Author: Michael Holst
1871 * ***************************************************************************
1872 */
1873 VPRIVATE void vzero(float u[]) {
1874 u[0] = 0.0;
1875 u[1] = 0.0;
1876 u[2] = 0.0;
1877 }
1878 VPRIVATE void vset(float u[], float x, float y, float z)
1879 {
1880 u[0] = x;
1881 u[1] = y;
1882 u[2] = z;
1883 }
1884 VPRIVATE void vsub(const float u[], const float v[], float w[])
1885 {
1886 w[0] = u[0] - v[0];
1887 w[1] = u[1] - v[1];
1888 w[2] = u[2] - v[2];
1889 }
1890 VPRIVATE void vcopy(const float u[], float v[])
1891 {
1892 v[0] = u[0];
1893 v[1] = u[1];
1894 v[2] = u[2];
1895 }
1896 VPRIVATE void vcross(const float u[], const float v[], float w[])
1897 {
1898 float tmp[3];
1899 tmp[0] = (u[1] * v[2]) - (u[2] * v[1]);
1900 tmp[1] = (u[2] * v[0]) - (u[0] * v[2]);
1901 tmp[2] = (u[0] * v[1]) - (u[1] * v[0]);
1902 vcopy(tmp, w);
1903 }
1904 VPRIVATE float vlength(const float u[])
1905 {
1906 return VSQRT( u[0]*u[0] + u[1]*u[1] + u[2]*u[2] );
1907 }
1908 VPRIVATE void vscale(float u[], float scale)
1909 {
1910 u[0] *= scale;
1911 u[1] *= scale;
1912 u[2] *= scale;
1913 }
1914 VPRIVATE void vnormal(float u[])
1915 {
1916 vscale(u, 1.0 / vlength(u));
1917 }
1918 VPRIVATE float vdot(const float u[], const float v[])
1919 {
1920 return ( u[0]*v[0] + u[1]*v[1] + u[2]*v[2] );
1921 }
1922 VPRIVATE void vadd(const float u[], const float v[], float w[])
1923 {
1924 w[0] = u[0] + v[0];
1925 w[1] = u[1] + v[1];
1926 w[2] = u[2] + v[2];
1927 }
1928
1929 /*
1930 * ***************************************************************************
1931 * Purpose: Quaternion utilities for the quaternion-based rotator.
1932 *
1933 * Notes: The six routines:
1934 *
1935 * buildQuat, axisQuat, addQuat, normalQuat,
1936 * buildRotation, projectSphere
1937 *
1938 * are my quaternion rotator hacks, based heavily on the trackball
1939 * rotator example in Mark Kilgard's OpenGL programming book.
1940 *
1941 * Unfortunately, Kilgard's example exhibits hysteresis.
1942 * However, I used Ken Shoemake's trick from his famous arcball
1943 * implementation in Graphics Gems to remove this effect.
1944 * The trick is to avoid accumulating the quaternion rotations
1945 * during mouse dragging, and to keep the current drag and the
1946 * initial rotation as separate quaternions.
1947 * You then accumulate the two quaternions only on mouse release.
1948 *
1949 * Author: Michael Holst
1950 * ***************************************************************************
1951 */
1952 VPRIVATE void buildQuat(float q[4], float r0[3], float r2[3])
1953 {
1954 float a[3], d[3], phi, t;
1955 vcross(r2, r0, a);
1956 vsub(r0, r2, d);
1957 t = vlength(d) / (2.0 * BALLSIZE);
1958 if (t > 1.0) t = 1.0;
1959 if (t < -1.0) t = -1.0;
1960 phi = 2.0 * VASIN(t);
1961 axisQuat(a, phi, q);
1962 }
1963 VPRIVATE void axisQuat(float a[3], float phi, float q[4])
1964 {
1965 vnormal(a);
1966 vcopy(a, q);
1967 vscale(q, VSIN(phi / 2.0));
1968 q[3] = VCOS(phi / 2.0);
1969 }
1970 VPRIVATE void addQuat(float q0[4], float q1[4], float des[4])
1971 {
1972 static int count = 0;
1973 float r0[4], r1[4], r2[4], r3[4];
1974
1975 vcopy(q0, r0);
1976 vscale(r0, q1[3]);
1977 vcopy(q1, r1);
1978 vscale(r1, q0[3]);
1979
1980 vcross(q1, q0, r2);
1981 vadd(r0, r1, r3);
1982 vadd(r2, r3, r3);
1983 r3[3] = q0[3] * q1[3] - vdot(q0, q1);
1984
1985 des[0] = r3[0];
1986 des[1] = r3[1];
1987 des[2] = r3[2];
1988 des[3] = r3[3];
1989
1990 count++;
1991 if (count > RENORMAL) {
1992 count = 0;
1993 normalQuat(des);
1994 }
1995 }
1996 VPRIVATE void normalQuat(float q[4])
1997 {
1998 int i;
1999 float mag;
2000 mag = ( q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3] );
2001 for (i=0; i<4; i++) q[i] /= mag;
2002 }
2003 VPRIVATE void buildRotation(float m[4][4], float q[4])
2004 {
2005 m[0][0] = 1.0 - 2.0*(q[1]*q[1] + q[2]*q[2]);
2006 m[0][1] = 2.0*(q[0]*q[1] - q[2]*q[3]);
2007 m[0][2] = 2.0*(q[2]*q[0] + q[1]*q[3]);
2008 m[0][3] = 0.0;
2009
2010 m[1][0] = 2.0*(q[0]*q[1] + q[2]*q[3]);
2011 m[1][1] = 1.0 - 2.0*(q[2]*q[2] + q[0]*q[0]);
2012 m[1][2] = 2.0*(q[1]*q[2] - q[0]*q[3]);
2013 m[1][3] = 0.0;
2014
2015 m[2][0] = 2.0*(q[2]*q[0] - q[1]*q[3]);
2016 m[2][1] = 2.0*(q[1]*q[2] + q[0]*q[3]);
2017 m[2][2] = 1.0 - 2.0*(q[1]*q[1] + q[0]*q[0]);
2018 m[2][3] = 0.0;
2019
2020 m[3][0] = 0.0;
2021 m[3][1] = 0.0;
2022 m[3][2] = 0.0;
2023 m[3][3] = 1.0;
2024 }
2025 VPRIVATE float projectSphere(float r, float x, float y)
2026 {
2027 static int init = 0;
2028 static float SQRTHALF, SQRTTWO;
2029 float d, t, z;
2030
2031 if (!init) {
2032 SQRTTWO = VSQRT( 2.0 );
2033 SQRTHALF = 1.0 / SQRTTWO;
2034 init = 1;
2035 }
2036
2037 d = VSQRT( x*x + y*y );
2038 if (d < r*SQRTHALF) {
2039 z = VSQRT( r*r - d*d );
2040 } else {
2041 t = r / SQRTTWO;
2042 z = t*t / d;
2043 }
2044 return z;
2045 }
2046