-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing2.html
executable file
·810 lines (749 loc) · 27.7 KB
/
listing2.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<!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>GLUTBasics - /SurfaceGeometry.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');">GLUTBasics</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">GLUTBasics</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>/SurfaceGeometry.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">/glutBasics.c</option>
<option value="listing2.html">/SurfaceGeometry.c</option>
<option value="listing3.html">/SurfaceGeometry.h</option>
<option value="listing4.html">/trackball.c</option>
<option value="listing5.html">/trackball.h</option></select>
</p>
</form>
<p><strong><a href="GLUTBasics.zip">Download Sample</a></strong> (“GLUTBasics.zip”, 56.7K)<BR>
<strong><a href="GLUTBasics.dmg">Download Sample</a></strong> (“GLUTBasics.dmg”, 118.0K)</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">/*
* SufaceGeometry.c
* Carbon OpenGL
*
* Created by Geoff Stahl.
Copyright: Copyright © 2002-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.
*
*/
/* Uses techniques described by Paul Bourke 1999 - 2002 */
/* Tranguloid Trefoil and other example surfaces by Roger Bagula see <http://astronomy.swin.edu.au/~pbourke/surfaces/> */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "SurfaceGeometry.h"
#define TWOPI 6.283185307179586476925287
#define PI 3.141592653589793238462643
typedef struct {
GLfloat x,y,z;
} recVec;
typedef struct {
GLfloat r,g,b;
} recColor;
typedef struct {
GLfloat s,t;
} recTexCoord;
/* Code based on work by Paul Bourke */
// globals for apps to use
// info
char gSurfName[256] = "";
char gSurfCredit[256] = "";
char gSurfX[256] = "";
char gSurfY[256] = "";
char gSurfZ[256] = "";
char gSurfRange[256] = "";
// simple cube data
GLint cube_num_vertices = 8;
GLfloat cube_vertices [8][3] = {
{1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0},
{1.0, 1.0, -1.0}, {1.0, -1.0, -1.0}, {-1.0, -1.0, -1.0}, {-1.0, 1.0, -1.0} };
GLfloat cube_vertex_colors [8][3] = {
{1.0, 1.0, 1.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 1.0, 1.0},
{1.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 0.0}, {0.0, 0.0, 1.0} };
GLint cube_num_faces = 6;
short cube_faces [6][4] = {
{3, 2, 1, 0}, {2, 3, 7, 6}, {0, 1, 5, 4}, {3, 0, 4, 7}, {1, 2, 6, 5}, {4, 5, 6, 7} };
GLfloat cube_texCoords [2][4] = {
{0.0, 0.0, 1.0, 1.0}, {0.0, 1.0, 1.0, 0.0} };
recColor getColor(GLfloat v, GLfloat vmin, GLfloat vmax, int type)
{
double dv,vmid;
recColor c = {1.0,1.0,1.0};
recColor c1,c2,c3;
double ratio;
if (v < vmin)
v = vmin;
if (v > vmax)
v = vmax;
dv = vmax - vmin;
switch (type) {
case 0:
c.r = 1.0f;
c.b = 1.0f;
c.g = 1.0f;
break;
case 1:
if (v < (vmin + 0.25 * dv)) {
c.r = 0;
c.g = 4 * (v - vmin) / dv;
c.b = 1;
} else if (v < (vmin + 0.5 * dv)) {
c.r = 0;
c.g = 1;
c.b = 1 + 4 * (vmin + 0.25 * dv - v) / dv;
} else if (v < (vmin + 0.75 * dv)) {
c.r = 4 * (v - vmin - 0.5 * dv) / dv;
c.g = 1;
c.b = 0;
} else {
c.r = 1;
c.g = 1 + 4 * (vmin + 0.75 * dv - v) / dv;
c.b = 0;
}
break;
case 2:
c.r = (v - vmin) / dv;
c.g = 0;
c.b = (vmax - v) / dv;
break;
case 3:
c.r = (v - vmin) / dv;
c.b = c.r;
c.g = c.r;
break;
case 4:
if (v < (vmin + dv / 6.0)) {
c.r = 1;
c.g = 6 * (v - vmin) / dv;
c.b = 0;
} else if (v < (vmin + 2.0 * dv / 6.0)) {
c.r = 1 + 6 * (vmin + dv / 6.0 - v) / dv;
c.g = 1;
c.b = 0;
} else if (v < (vmin + 3.0 * dv / 6.0)) {
c.r = 0;
c.g = 1;
c.b = 6 * (v - vmin - 2.0 * dv / 6.0) / dv;
} else if (v < (vmin + 4.0 * dv / 6.0)) {
c.r = 0;
c.g = 1 + 6 * (vmin + 3.0 * dv / 6.0 - v) / dv;
c.b = 1;
} else if (v < (vmin + 5.0 * dv / 6.0)) {
c.r = 6 * (v - vmin - 4.0 * dv / 6.0) / dv;
c.g = 0;
c.b = 1;
} else {
c.r = 1;
c.g = 0;
c.b = 1 + 6 * (vmin + 5.0 * dv / 6.0 - v) / dv;
}
break;
case 5:
c.r = (v - vmin) / (vmax - vmin);
c.g = 1;
c.b = 0;
break;
case 6:
c.r = (v - vmin) / (vmax - vmin);
c.g = (vmax - v) / (vmax - vmin);
c.b = c.r;
break;
case 7:
if (v < (vmin + 0.25 * dv)) {
c.r = 0;
c.g = 4 * (v - vmin) / dv;
c.b = 1 - c.g;
} else if (v < (vmin + 0.5 * dv)) {
c.r = 4 * (v - vmin - 0.25 * dv) / dv;
c.g = 1 - c.r;
c.b = 0;
} else if (v < (vmin + 0.75 * dv)) {
c.g = 4 * (v - vmin - 0.5 * dv) / dv;
c.r = 1 - c.g;
c.b = 0;
} else {
c.r = 0;
c.b = 4 * (v - vmin - 0.75 * dv) / dv;
c.g = 1 - c.b;
}
break;
case 8:
if (v < (vmin + 0.5 * dv)) {
c.r = 2 * (v - vmin) / dv;
c.g = c.r;
c.b = c.r;
} else {
c.r = 1 - 2 * (v - vmin - 0.5 * dv) / dv;
c.g = c.r;
c.b = c.r;
}
break;
case 9:
if (v < (vmin + dv / 3)) {
c.b = 3 * (v - vmin) / dv;
c.g = 0;
c.r = 1 - c.b;
} else if (v < (vmin + 2 * dv / 3)) {
c.r = 0;
c.g = 3 * (v - vmin - dv / 3) / dv;
c.b = 1;
} else {
c.r = 3 * (v - vmin - 2 * dv / 3) / dv;
c.g = 1 - c.r;
c.b = 1;
}
break;
case 10:
if (v < (vmin + 0.2 * dv)) {
c.r = 0;
c.g = 5 * (v - vmin) / dv;
c.b = 1;
} else if (v < (vmin + 0.4 * dv)) {
c.r = 0;
c.g = 1;
c.b = 1 + 5 * (vmin + 0.2 * dv - v) / dv;
} else if (v < (vmin + 0.6 * dv)) {
c.r = 5 * (v - vmin - 0.4 * dv) / dv;
c.g = 1;
c.b = 0;
} else if (v < (vmin + 0.8 * dv)) {
c.r = 1;
c.g = 1 - 5 * (v - vmin - 0.6 * dv) / dv;
c.b = 0;
} else {
c.r = 1;
c.g = 5 * (v - vmin - 0.8 * dv) / dv;
c.b = 5 * (v - vmin - 0.8 * dv) / dv;
}
break;
case 11:
c1.r = 200 / 255.0; c1.g = 60 / 255.0; c1.b = 0 / 255.0;
c2.r = 250 / 255.0; c2.g = 160 / 255.0; c2.b = 110 / 255.0;
c.r = (c2.r - c1.r) * (v - vmin) / dv + c1.r;
c.g = (c2.g - c1.g) * (v - vmin) / dv + c1.g;
c.b = (c2.b - c1.b) * (v - vmin) / dv + c1.b;
break;
case 12:
c1.r = 55 / 255.0; c1.g = 55 / 255.0; c1.b = 45 / 255.0;
c2.r = 200 / 255.0; c2.g = 60 / 255.0; c2.b = 0 / 255.0;
// c2.r = 235 / 255.0; c2.g = 90 / 255.0; c2.b = 30 / 255.0;
c3.r = 250 / 255.0; c3.g = 160 / 255.0; c3.b = 110 / 255.0;
ratio = 0.4;
vmid = vmin + ratio * dv;
if (v < vmid) {
c.r = (c2.r - c1.r) * (v - vmin) / (ratio*dv) + c1.r;
c.g = (c2.g - c1.g) * (v - vmin) / (ratio*dv) + c1.g;
c.b = (c2.b - c1.b) * (v - vmin) / (ratio*dv) + c1.b;
} else {
c.r = (c3.r - c2.r) * (v - vmid) / ((1-ratio)*dv) + c2.r;
c.g = (c3.g - c2.g) * (v - vmid) / ((1-ratio)*dv) + c2.g;
c.b = (c3.b - c2.b) * (v - vmid) / ((1-ratio)*dv) + c2.b;
}
break;
case 13:
c1.r = 0 / 255.0; c1.g = 255 / 255.0; c1.b = 0 / 255.0;
c2.r = 255 / 255.0; c2.g = 150 / 255.0; c2.b = 0 / 255.0;
c3.r = 255 / 255.0; c3.g = 250 / 255.0; c3.b = 240 / 255.0;
ratio = 0.3;
vmid = vmin + ratio * dv;
if (v < vmid) {
c.r = (c2.r - c1.r) * (v - vmin) / (ratio*dv) + c1.r;
c.g = (c2.g - c1.g) * (v - vmin) / (ratio*dv) + c1.g;
c.b = (c2.b - c1.b) * (v - vmin) / (ratio*dv) + c1.b;
} else {
c.r = (c3.r - c2.r) * (v - vmid) / ((1-ratio)*dv) + c2.r;
c.g = (c3.g - c2.g) * (v - vmid) / ((1-ratio)*dv) + c2.g;
c.b = (c3.b - c2.b) * (v - vmid) / ((1-ratio)*dv) + c2.b;
}
break;
case 14:
c.r = 1;
c.g = 1 - (v - vmin) / dv;
c.b = 0;
break;
case 15:
if (v < (vmin + 0.25 * dv)) {
c.r = 0;
c.g = 4 * (v - vmin) / dv;
c.b = 1;
} else if (v < (vmin + 0.5 * dv)) {
c.r = 0;
c.g = 1;
c.b = 1 - 4 * (v - vmin - 0.25 * dv) / dv;
} else if (v < (vmin + 0.75 * dv)) {
c.r = 4 * (v - vmin - 0.5 * dv) / dv;
c.g = 1;
c.b = 0;
} else {
c.r = 1;
c.g = 1;
c.b = 4 * (v - vmin - 0.75 * dv) / dv;
}
break;
case 16:
if (v < (vmin + 0.5 * dv)) {
c.r = 0.0;
c.g = 2 * (v - vmin) / dv;
c.b = 1 - 2 * (v - vmin) / dv;
} else {
c.r = 2 * (v - vmin - 0.5 * dv) / dv;
c.g = 1 - 2 * (v - vmin - 0.5 * dv) / dv;
c.b = 0.0;
}
break;
case 17:
if (v < (vmin + 0.5 * dv)) {
c.r = 1.0;
c.g = 1 - 2 * (v - vmin) / dv;
c.b = 2 * (v - vmin) / dv;
} else {
c.r = 1 - 2 * (v - vmin - 0.5 * dv) / dv;
c.g = 2 * (v - vmin - 0.5 * dv) / dv;
c.b = 1.0;
}
break;
case 18:
c.r = 0;
c.g = (v - vmin) / (vmax - vmin);
c.b = 1;
break;
case 19:
c.r = (v - vmin) / (vmax - vmin);
c.g = c.r;
c.b = 1;
break;
case 20:
c1.r = 0 / 255.0; c1.g = 160 / 255.0; c1.b = 0 / 255.0;
c2.r = 180 / 255.0; c2.g = 220 / 255.0; c2.b = 0 / 255.0;
c3.r = 250 / 255.0; c3.g = 220 / 255.0; c3.b = 170 / 255.0;
ratio = 0.3;
vmid = vmin + ratio * dv;
if (v < vmid) {
c.r = (c2.r - c1.r) * (v - vmin) / (ratio*dv) + c1.r;
c.g = (c2.g - c1.g) * (v - vmin) / (ratio*dv) + c1.g;
c.b = (c2.b - c1.b) * (v - vmin) / (ratio*dv) + c1.b;
} else {
c.r = (c3.r - c2.r) * (v - vmid) / ((1-ratio)*dv) + c2.r;
c.g = (c3.g - c2.g) * (v - vmid) / ((1-ratio)*dv) + c2.g;
c.b = (c3.b - c2.b) * (v - vmid) / ((1-ratio)*dv) + c2.b;
}
break;
}
return(c);
}
void normalise(recVec *p)
{
double length;
length = sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
if (length != 0) {
p->x /= length;
p->y /= length;
p->z /= length;
} else {
p->x = 0;
p->y = 0;
p->z = 0;
}
}
recVec CalcNormal(recVec p,recVec p1,recVec p2)
{
recVec n,pa,pb;
pa.x = p1.x - p.x;
pa.y = p1.y - p.y;
pa.z = p1.z - p.z;
pb.x = p2.x - p.x;
pb.y = p2.y - p.y;
pb.z = p2.z - p.z;
normalise(&pa);
normalise(&pb);
n.x = pa.y * pb.z - pa.z * pb.y;
n.y = pa.z * pb.x - pa.x * pb.z;
n.z = pa.x * pb.y - pa.y * pb.x;
normalise(&n);
return(n);
}
// expects u & v (-PI to PI)
recVec Eval(double u, double v, int type)
{
recVec p;
double temp;
switch (type) {
case kTranguloidTrefoil:
p.x = sin(3*u) * 2 / (2 + cos(v));
p.y = (sin(u) + 2 * sin(2*u)) * 2 / (2 + cos(v + TWOPI / 3));
p.z = (cos(u) - 2 * cos(2*u)) * (2 + cos(v)) * (2 + cos(v + TWOPI/3))/4;
break;
case kTriaxialTritorus:
p.x = 2.0 * sin (u) * (1 + cos (v));
p.y = 2.0 * sin (u + 2 * PI / 3) * (1 + cos (v + 2 * PI / 3));
p.z = 2.0 * sin (u + 4 * PI / 3) * (1 + cos (v + 4 * PI / 3));
break;
case kStilettoSurface:
// reverse u and v for better distribution or points
temp = u;
u = v + PI; v = (temp + PI) / 2.0; // convert to: 0 <= u <= 2 pi, 0 <= v <= 2 pi
p.x = 4.0 * (2.0 + cos(u)) * pow(cos(v), 3.0) * sin(v);
p.y = 4.0 * (2.0 + cos(u+TWOPI/3.0)) * pow (cos(v+TWOPI/3.0), 2.0) * pow (sin(v+TWOPI/3.0), 2.0);
p.z = 4.0 * -(2.0 + cos(u-TWOPI/3.0)) * pow (cos(v+TWOPI/3.0), 2.0) * pow (sin(v+TWOPI/3.0), 2.0);
break;
case kSlippersSurface:
temp = u;
u = v + PI * 2; v = temp + PI; // convert to: 0 <= u <= 4 pi, 0 <= v <= 2 pi
p.x = 4.0 * (2 + cos (u)) * pow (cos (v), 3) * sin(v);
p.y = 4.0 * (2 + cos (u + TWOPI / 3)) * pow (cos (TWOPI / 3 + v), 2) * pow (sin (TWOPI / 3 + v), 2);
p.z = 4.0 * -(2 + cos (u - TWOPI / 3)) * pow (cos (TWOPI / 3 - v), 2) * pow (sin (TWOPI / 3 - v), 3);
break;
case kMaedersOwl:
u = (u + PI) * 2; v = (v + PI) / TWOPI; // convert to: 0 <= u <= 4 pi, 0 <= v <= 1
p.x = 3.0 * v * cos(u) - 0.5 * v * v * cos(2 * u);
p.y = 3.0 * -v * sin(u) - 0.5 * v * v * sin(2 * u);
p.z = 3.0 * 4 * pow(v,1.5) * cos(1.5 * u) / 3;
break;
default:
p.x = 0.0;
p.y = 0.0;
p.z = 0.0;
break;
}
return(p);
}
void GetStrings (unsigned int surface, char ** strName, char ** strAuthor, char ** strX, char ** strY, char ** strZ, char ** strRange)
{
static char strings[6][6][256] = {{"Color Cube",
" ",
" ",
" ",
" ",
" "},
{"Tranguloid Trefoil",
"by Roger Bagula",
"x = 2 sin(3 u) / (2 + cos(v))",
"y = 2 (sin(u) + 2 sin(2 u)) / (2 + cos(v + 2 pi / 3))",
"z = (cos(u) - 2 cos(2 u)) (2 + cos(v)) (2 + cos(v + 2 pi / 3)) / 4",
"-pi <= u <= pi, -pi <= v <= pi"},
{"Triaxial Tritorus",
"by Roger Bagula",
"x = sin(u) (1 + cos(v))",
"y = sin(u + 2pi / 3) (1 + cos(v + 2pi / 3))",
"z = z = sin(u + 4pi / 3) (1 + cos(v + 4pi / 3))",
"0 <= u <= 2 pi, 0 <= v <= 2 pi"},
{"Stiletto Surface",
"by Roger Bagula",
"x = (2 + cos(u)) cos(v)^3 sin(v)",
"y = (2 + cos(u + 2pi /3)) cos(v + 2pi / 3)^2 sin(v + 2pi / 3)^2",
"z = -(2 + cos(u - 2pi / 3)) cos(v + 2pi / 3)^2 sin(v + 2pi / 3)^2",
"0 <= u <= 2 pi, 0 <= v <= 2 pi"},
{"Slippers Surface",
"by Roger Bagula",
"x = (2 + cos(u)) cos(v)^3 sin(v)",
"y = (2 + cos(u + 2pi / 3)) cos(2pi / 3 + v)^2 sin(2pi / 3 + v)^2",
"z = -(2 + cos(u - 2pi / 3)) cos(2pi / 3 - v)^2 sin(2pi / 3 - v)^3",
"0 <= u <= 2 pi, 0 <= v <= 2 pi"},
{"Maeder's Owl",
"by R. Maeder",
"x = v cos(u) - 0.5 v^2 cos(2 u)",
"y = - v sin(u) - 0.5 v^2 sin(2 u)",
"z = 4 v^1.5 cos(3 u / 2) / 3",
"0 <= u <= 4 pi, 0 <= v <= 1"}};
*strName = strings[surface][0];
*strAuthor = strings[surface][1];
*strX = strings[surface][2];
*strY = strings[surface][3];
*strZ = strings[surface][4];
*strRange = strings[surface][5];
}
// special case cube for test purposes
void BuildCube (GLuint * polyList, GLuint * lineList, GLuint * pointList, int colorScheme)
{
float fSize = 2.0f;
long f, i;
*polyList = glGenLists (1);
glNewList(*polyList, GL_COMPILE);
glBegin (GL_QUADS);
for (f = 0; f < cube_num_faces; f++)
for (i = 0; i < 4; i++) {
if (colorScheme)
glColor3f (cube_vertex_colors[cube_faces[f][i]][0], cube_vertex_colors[cube_faces[f][i]][1], cube_vertex_colors[cube_faces[f][i]][2]);
else
glColor3f (1.0f, 1.0f, 1.0f);
glTexCoord2f (cube_texCoords [0][i], cube_texCoords [1][i]);
glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);
}
glEnd ();
glColor3f (0.0, 0.0, 0.0);
for (f = 0; f < cube_num_faces; f++) {
glBegin (GL_LINE_LOOP);
for (i = 0; i < 4; i++)
glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);
glEnd ();
}
glEndList ();
*lineList = glGenLists (1);
glNewList(*lineList, GL_COMPILE);
glColor3f (1.0, 1.0, 1.0);
for (f = 0; f < cube_num_faces; f++) {
glBegin (GL_LINE_LOOP);
for (i = 0; i < 4; i++)
glVertex3f(cube_vertices[cube_faces[f][i]][0] * fSize, cube_vertices[cube_faces[f][i]][1] * fSize, cube_vertices[cube_faces[f][i]][2] * fSize);
glEnd ();
}
glEndList ();
*pointList = glGenLists (1);
glNewList(*pointList, GL_COMPILE);
glColor3f (1.0, 1.0, 1.0);
for (f = 0; f < cube_num_vertices; f++) {
glBegin (GL_POINTS);
glVertex3f(cube_vertices[f][0] * fSize, cube_vertices[f][1] * fSize, cube_vertices[f][2] * fSize);
glEnd ();
}
glEndList ();
}
void BuildGeometry (unsigned int surface, unsigned int colorScheme, unsigned int subdivisions, unsigned int xyRatio,
GLuint * polyList, GLuint * lineList, GLuint * pointList)
{
long i,j, index;
long maxI = subdivisions * xyRatio, maxJ = subdivisions;
double u, v, delta=0.001;
recVec p1,p2;
recVec *vertexPos = NULL,*vertexNormal = NULL;
recColor *vertexColor = NULL;
recTexCoord *vertexTexCoord = NULL;
// set valid surface and color scheme
surface %= kSurfaces;
colorScheme %= kColorSchemes;
// delete existing list
if (*polyList)
glDeleteLists (*polyList, 1);
if (*lineList)
glDeleteLists (*lineList, 1);
if (*pointList)
glDeleteLists (*pointList, 1);
*polyList = *lineList = *pointList = 0;
if (surface == kCube) // build the standard color cube (disregard color, subdivisions, and xyRatio)
BuildCube (polyList, lineList, pointList, colorScheme);
else {
// build buffers
vertexPos = (recVec*) malloc ((maxI) * (maxJ) * sizeof (recVec));
if (vertexNormal)
free (vertexNormal);
vertexNormal = (recVec*) malloc ((maxI) * (maxJ) * sizeof (recVec));
if (vertexColor)
free (vertexColor);
vertexColor = (recColor*) malloc ((maxI) * (maxJ) * sizeof (recColor));
if (vertexTexCoord)
free (vertexTexCoord);
vertexTexCoord = (recTexCoord*) malloc ((maxI) * (maxJ) * sizeof (recTexCoord));
if (!vertexPos || !vertexNormal || !vertexColor || !vertexTexCoord)
return;
// build surface
for (i = 0; i < maxI; i++) {
for (j = 0; j < maxJ; j++) {
index = i * maxJ + j;
u = -PI + (i % maxI) * TWOPI / maxI;
v = -PI + (j % maxJ) * TWOPI / maxJ;
vertexPos[index] = Eval(u,v, surface);
p1 = Eval(u + delta, v, surface);
p2 = Eval(u, v + delta, surface);
vertexNormal[index] = CalcNormal(vertexPos[index],p1,p2);
vertexColor[index] = getColor(u, -PI, PI, colorScheme);
vertexTexCoord[index].s = (float) i * 5.0f / (float) maxI;
vertexTexCoord[index].t = (float) j * 1.0f/ (float) maxJ;
}
}
*polyList = glGenLists (1);
glNewList(*polyList, GL_COMPILE);
for (i=0; i< maxI; i++) {
glBegin(GL_TRIANGLE_STRIP);
for (j = 0; j <= maxJ; j++) {
index = (i % maxI) * maxJ + (j % maxJ);
glColor3fv (&vertexColor[index].r);
glNormal3fv (&vertexNormal[index].x);
glTexCoord2fv (&vertexTexCoord[index].s);
glVertex3fv (&vertexPos[index].x);
index = ((i + 1) % maxI) * maxJ + (j % maxJ);
glColor3fv (&vertexColor[index].r);
glNormal3fv (&vertexNormal[index].x);
glTexCoord2fv (&vertexTexCoord[index].s);
glVertex3fv (&vertexPos[index].x);
// index = ((i - 1) % maxI) * maxJ + (j % maxJ);
}
glEnd ();
}
glEndList ();
*lineList = glGenLists (1);
glNewList(*lineList, GL_COMPILE);
for (i=0; i< maxI; i++) {
glBegin(GL_LINE_STRIP);
for (j = 0; j < maxJ; j++) {
index = i * maxJ + j;
glColor3fv (&vertexColor[index].r);
glVertex3fv (&vertexPos[index].x);
}
index = i * maxJ + 0;
glColor3fv (&vertexColor[index].r);
glVertex3fv (&vertexPos[index].x);
glEnd ();
}
for (j=0; j< maxJ; j++) {
glBegin(GL_LINE_STRIP);
for (i = 0; i < maxI; i++) {
index = i * maxJ + j;
glColor3fv (&vertexColor[index].r);
glVertex3fv (&vertexPos[index].x);
}
index = 0 + j;
glColor3fv (&vertexColor[index].r);
glVertex3fv (&vertexPos[index].x);
glEnd ();
}
glEndList ();
*pointList = glGenLists (1);
glNewList(*pointList, GL_COMPILE);
glBegin(GL_POINTS);
for (i=0; i< maxI; i++) {
for (j = 0; j < maxJ; j++) {
index = i * maxJ + j;
glColor3fv (&vertexColor[index].r);
glVertex3fv (&vertexPos[index].x);
}
}
glEnd ();
glEndList ();
free (vertexPos);
free (vertexNormal);
free (vertexColor);
}
}
</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/GLUTBasics/listing2.html%3Fid%3DDTS10003150-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/GLUTBasics/listing2.html%3Fid%3DDTS10003150-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/GLUTBasics/listing2.html%3Fid%3DDTS10003150-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>