-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisting66.html
executable file
·1319 lines (1016 loc) · 45.4 KB
/
listing66.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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>GLSL Showpiece Lite - /GLSLShowpiece Lite/Viewer/Sources/OpenGLView.m</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');">GLSL Showpiece Lite</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">GLSL Showpiece Lite</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>/GLSLShowpiece Lite/Viewer/Sources/OpenGLView.m</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">/GLSLShowpiece Lite/Exhibits/3Dlabs-license.txt</option>
<option value="listing2.html">/GLSLShowpiece Lite/Exhibits/ATI-License.txt</option>
<option value="listing3.html">/GLSLShowpiece Lite/Exhibits/Cloud1/Cloud1.h</option>
<option value="listing4.html">/GLSLShowpiece Lite/Exhibits/Cloud1/Cloud1.m</option>
<option value="listing5.html">/GLSLShowpiece Lite/Exhibits/Cloud2/Cloud2.h</option>
<option value="listing6.html">/GLSLShowpiece Lite/Exhibits/Cloud2/Cloud2.m</option>
<option value="listing7.html">/GLSLShowpiece Lite/Exhibits/Earth/Earth.h</option>
<option value="listing8.html">/GLSLShowpiece Lite/Exhibits/Earth/Earth.m</option>
<option value="listing9.html">/GLSLShowpiece Lite/Exhibits/EnvMap/EnvMap.h</option>
<option value="listing10.html">/GLSLShowpiece Lite/Exhibits/EnvMap/EnvMap.m</option>
<option value="listing11.html">/GLSLShowpiece Lite/Exhibits/Eroded/Eroded.h</option>
<option value="listing12.html">/GLSLShowpiece Lite/Exhibits/Eroded/Eroded.m</option>
<option value="listing13.html">/GLSLShowpiece Lite/Exhibits/Fire/Fire.h</option>
<option value="listing14.html">/GLSLShowpiece Lite/Exhibits/Fire/Fire.m</option>
<option value="listing15.html">/GLSLShowpiece Lite/Exhibits/Fur/Fur.h</option>
<option value="listing16.html">/GLSLShowpiece Lite/Exhibits/Fur/Fur.m</option>
<option value="listing17.html">/GLSLShowpiece Lite/Exhibits/Glass/Glass.h</option>
<option value="listing18.html">/GLSLShowpiece Lite/Exhibits/Glass/Glass.m</option>
<option value="listing19.html">/GLSLShowpiece Lite/Exhibits/Gooch/Gooch.h</option>
<option value="listing20.html">/GLSLShowpiece Lite/Exhibits/Gooch/Gooch.m</option>
<option value="listing21.html">/GLSLShowpiece Lite/Exhibits/Granite/Granite.h</option>
<option value="listing22.html">/GLSLShowpiece Lite/Exhibits/Granite/Granite.m</option>
<option value="listing23.html">/GLSLShowpiece Lite/Exhibits/Inferno/Inferno.h</option>
<option value="listing24.html">/GLSLShowpiece Lite/Exhibits/Inferno/Inferno.m</option>
<option value="listing25.html">/GLSLShowpiece Lite/Exhibits/LightworkDesign-license.txt</option>
<option value="listing26.html">/GLSLShowpiece Lite/Exhibits/Marble/Marble.h</option>
<option value="listing27.html">/GLSLShowpiece Lite/Exhibits/Marble/Marble.m</option>
<option value="listing28.html">/GLSLShowpiece Lite/Exhibits/Plasma/Plasma.h</option>
<option value="listing29.html">/GLSLShowpiece Lite/Exhibits/Plasma/Plasma.m</option>
<option value="listing30.html">/GLSLShowpiece Lite/Exhibits/Toon/Toon.h</option>
<option value="listing31.html">/GLSLShowpiece Lite/Exhibits/Toon/Toon.m</option>
<option value="listing32.html">/GLSLShowpiece Lite/Exhibits/Toyball/Toyball.h</option>
<option value="listing33.html">/GLSLShowpiece Lite/Exhibits/Toyball/Toyball.m</option>
<option value="listing34.html">/GLSLShowpiece Lite/Exhibits/VertexNoise/VertexNoise.h</option>
<option value="listing35.html">/GLSLShowpiece Lite/Exhibits/VertexNoise/VertexNoise.m</option>
<option value="listing36.html">/GLSLShowpiece Lite/Exhibits/Wobble/Wobble.h</option>
<option value="listing37.html">/GLSLShowpiece Lite/Exhibits/Wobble/Wobble.m</option>
<option value="listing38.html">/GLSLShowpiece Lite/Exhibits/Wood1/Wood1.h</option>
<option value="listing39.html">/GLSLShowpiece Lite/Exhibits/Wood1/Wood1.m</option>
<option value="listing40.html">/GLSLShowpiece Lite/Exhibits/Wood2/Wood2.h</option>
<option value="listing41.html">/GLSLShowpiece Lite/Exhibits/Wood2/Wood2.m</option>
<option value="listing42.html">/GLSLShowpiece Lite/Exhibits/WoodShader/WoodShader.h</option>
<option value="listing43.html">/GLSLShowpiece Lite/Exhibits/WoodShader/WoodShader.m</option>
<option value="listing44.html">/GLSLShowpiece Lite/Extras/Noise3DTexture/Noise3DTexture.cpp</option>
<option value="listing45.html">/GLSLShowpiece Lite/Extras/Noise3DTexture/Noise3DTexture.h</option>
<option value="listing46.html">/GLSLShowpiece Lite/Models/Bunny/Bunny.c</option>
<option value="listing47.html">/GLSLShowpiece Lite/Models/Bunny/Bunny.h</option>
<option value="listing48.html">/GLSLShowpiece Lite/Models/Models/Models.h</option>
<option value="listing49.html">/GLSLShowpiece Lite/Models/Models/Models.m</option>
<option value="listing50.html">/GLSLShowpiece Lite/Models/Teapot/Teapot.c</option>
<option value="listing51.html">/GLSLShowpiece Lite/Models/Teapot/Teapot.h</option>
<option value="listing52.html">/GLSLShowpiece Lite/Surfaces/Constants/GeometryConstants.h</option>
<option value="listing53.html">/GLSLShowpiece Lite/Surfaces/Klein Surface/Headers/KleinSurface.h</option>
<option value="listing54.html">/GLSLShowpiece Lite/Surfaces/Klein Surface/Sources/KleinSurface.mm</option>
<option value="listing55.html">/GLSLShowpiece Lite/Surfaces/Surfaces/Headers/Surfaces.h</option>
<option value="listing56.html">/GLSLShowpiece Lite/Surfaces/Surfaces/Sources/Surfaces.mm</option>
<option value="listing57.html">/GLSLShowpiece Lite/Utilities/Numerics/Numerics.h</option>
<option value="listing58.html">/GLSLShowpiece Lite/Utilities/Numerics/Numerics.m</option>
<option value="listing59.html">/GLSLShowpiece Lite/Utilities/UniformData/UniformData.h</option>
<option value="listing60.html">/GLSLShowpiece Lite/Utilities/UniformData/UniformData.m</option>
<option value="listing61.html">/GLSLShowpiece Lite/Viewer/Headers/Exhibit.h</option>
<option value="listing62.html">/GLSLShowpiece Lite/Viewer/Headers/OpenGLView.h</option>
<option value="listing63.html">/GLSLShowpiece Lite/Viewer/Headers/UIController.h</option>
<option value="listing64.html">/GLSLShowpiece Lite/Viewer/Sources/Exhibit.m</option>
<option value="listing65.html">/GLSLShowpiece Lite/Viewer/Sources/main.m</option>
<option value="listing66.html">/GLSLShowpiece Lite/Viewer/Sources/OpenGLView.m</option>
<option value="listing67.html">/GLSLShowpiece Lite/Viewer/Sources/UIController.m</option>
<option value="listing68.html">/LessonGuide/GLSLPrereqs HTML/GLSLPrereqs.html</option>
<option value="listing69.html">/LessonGuide/GLSLPrereqs HTML/GLSLPrereqs_files/GLSLPrereqs.css</option>
<option value="listing70.html">/Textures/EarthImages.txt</option>
<option value="listing71.html">/Textures/envmaps.txt</option></select>
</p>
</form>
<p><strong><a href="GLSLShowpieceLite.zip">Download Sample</a></strong> (“GLSLShowpieceLite.zip”, 2.30M)<BR>
<strong><a href="GLSLShowpieceLite.dmg">Download Sample</a></strong> (“GLSLShowpieceLite.dmg”, 2.57M)</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">//-------------------------------------------------------------------------
//
// File: OpenGLView.m
//
// Abstract: Main rendering class
//
// Disclaimer: IMPORTANT: This Apple software is supplied to you by
// Apple 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 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.
//
// Copyright (c) 2004-2007 Apple Inc., All rights reserved.
//
//-------------------------------------------------------------------------
//------------------------------------------------------------------------
#import <Accelerate/Accelerate.h>
//------------------------------------------------------------------------
#import "OpenGLView.h"
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static const double kViewRotationDegreesPerSecond = 20.0;
static const double kExhibitHeightMovementUnitsPerSecond = 0.5;
static const NSTimeInterval kScheduledTimerInSeconds = 1.0f/150.0f;
//------------------------------------------------------------------------
//------------------------------------------------------------------------
typedef struct
{
GLuint imageBitsPerPixel;
GLuint imageBitsPerComponent;
GLuint imageSamplesPerPixel;
GLuint imageStorageSize;
CGBitmapInfo imageBitmapInfo;
vImage_Buffer imageBuffer;
} CGImageBitmap;
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static inline GLboolean CheckForExtension( const char *extensionName, const GLubyte *extensions )
{
GLboolean bExtensionAvailable = gluCheckExtension( (GLubyte *)extensionName, extensions );
return bExtensionAvailable;
} // CheckForExtension
//------------------------------------------------------------------------
static inline void CheckForAndLogExtensionAvailable( const GLboolean extensionAvailable, const char *extensionName )
{
if ( extensionAvailable == GL_FALSE )
{
NSLog( @">> WARNING: \"%s\" extension is not available!\n", extensionName );
} // if
} // CheckForExtensions
//------------------------------------------------------------------------
static void CheckForShaders( NSOpenGLPixelFormatAttribute *thePixelAttributes )
{
const GLubyte *extensions = glGetString( GL_EXTENSIONS );
GLboolean bShaderObjectAvailable = CheckForExtension( "GL_ARB_shader_objects", extensions );
GLboolean bShaderLanguage100Available = CheckForExtension( "GL_ARB_shading_language_100", extensions );
GLboolean bVertexShaderAvailable = CheckForExtension( "GL_ARB_vertex_shader", extensions );
GLboolean bFragmentShaderAvailable = CheckForExtension( "GL_ARB_fragment_shader", extensions );
GLboolean bForceSoftwareRendering = ( bShaderObjectAvailable == GL_FALSE )
|| ( bShaderLanguage100Available == GL_FALSE )
|| ( bVertexShaderAvailable == GL_FALSE )
|| ( bFragmentShaderAvailable == GL_FALSE );
if ( bForceSoftwareRendering )
{
// Force software rendering, so fragment shaders will execute
CheckForAndLogExtensionAvailable( bShaderObjectAvailable, "GL_ARB_shader_objects" );
CheckForAndLogExtensionAvailable( bShaderLanguage100Available, "GL_ARB_shading_language_100" );
CheckForAndLogExtensionAvailable( bVertexShaderAvailable, "GL_ARB_vertex_shader" );
CheckForAndLogExtensionAvailable( bFragmentShaderAvailable, "GL_ARB_fragment_shader" );
thePixelAttributes [3] = NSOpenGLPFARendererID;
thePixelAttributes [4] = kCGLRendererGenericFloatID;
} // if
} // CheckForShaders
//------------------------------------------------------------------------
static void CheckForClipVolumeHint( NSOpenGLPixelFormatAttribute *thePixelAttributes )
{
const GLubyte *extensions = glGetString(GL_EXTENSIONS);
// Inform OpenGL that the geometry is entirely within the view volume and that view-volume
// clipping is unnecessary. Normal clipping can be resumed by setting this hint to GL_DONT_CARE.
// When clipping is disabled with this hint, results are undefined if geometry actually falls
// outside the view volume.
GLboolean bClipVolumeHintExtAvailable = CheckForExtension( "GL_EXT_clip_volume_hint", extensions );
if ( bClipVolumeHintExtAvailable == GL_TRUE )
{
glHint( GL_CLIP_VOLUME_CLIPPING_HINT_EXT,GL_FASTEST );
} // if
} // CheckForClipVolumeHint
//------------------------------------------------------------------------
static void CheckForGLSLHardwareSupport( NSOpenGLPixelFormatAttribute *thePixelAttributes )
{
// Create a pre-flight context to check for GLSL hardware support
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes: thePixelAttributes];
if ( pixelFormat != nil )
{
NSOpenGLContext *preflight = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
if ( preflight != nil )
{
[preflight makeCurrentContext];
CheckForShaders( thePixelAttributes );
CheckForClipVolumeHint( thePixelAttributes );
[preflight release];
} // if
[pixelFormat release];
} // if
} // CheckForGLSLHardwareSupport
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static void OpenGLSetupProjections()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.3, 0.3, 0.0, 0.6, 1.0, 8.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -2.0);
} // OpenGLSetupProjections
//------------------------------------------------------------------------
static void OpenGLSetupBlending()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} // OpenGLSetupBlending
//------------------------------------------------------------------------
static void OpenGLSetup( const GLboolean setupBlending )
{
// For some OpenGL implementations, texture coordinates generated during rasterization
// aren't perspective correct. However, you can usually make them perspective correct
// by calling glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST). Colors generated at the
// rasterization stage aren't perspective correct in almost every OpenGL implementation,
// and can't be made so. For this reason, you're more likely to encounter this problem
// with colors than texture coordinates.
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
// Set up the projection
OpenGLSetupProjections();
// Use blending
if ( setupBlending )
{
OpenGLSetupBlending();
} // if
// Turn on depth test
glEnable( GL_DEPTH_TEST );
// front- or back-facing facets can be culled
glEnable( GL_CULL_FACE );
} // OpenGLSetup
//------------------------------------------------------------------------
static void OpenGLNoError( )
{
while( glGetError() != GL_NO_ERROR )
{
;
} // while
} // OpenGLNoError
//------------------------------------------------------------------------
static void OpenGLDisableCapabilitiesForCopy( )
{
glDisable(GL_COLOR_TABLE);
glDisable(GL_CONVOLUTION_1D);
glDisable(GL_CONVOLUTION_2D);
glDisable(GL_HISTOGRAM);
glDisable(GL_MINMAX);
glDisable(GL_POST_COLOR_MATRIX_COLOR_TABLE);
glDisable(GL_POST_CONVOLUTION_COLOR_TABLE);
glDisable(GL_SEPARABLE_2D);
} // OpenGLDisableCapabilitiesForCopy
//------------------------------------------------------------------------
static void OpenGLPixelMap( )
{
GLfloat values = 0.0f;
glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 1, &values);
glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 1, &values);
glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 1, &values);
glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 1, &values);
} // OpenGLPixelMap
//------------------------------------------------------------------------
static void OpenGLPixelStore( NSPoint *origin, NSRect *rect1, NSRect *rect2 )
{
glPixelStorei(GL_PACK_SWAP_BYTES, 0);
glPixelStorei(GL_PACK_LSB_FIRST, 0);
glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
glPixelStoref(GL_PACK_ROW_LENGTH, NSWidth(*rect2));
glPixelStoref(GL_PACK_SKIP_PIXELS, origin->x);
glPixelStoref(GL_PACK_SKIP_ROWS, NSHeight(*rect2) - (origin->y + NSHeight(*rect1)));
glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
} // OpenGLPixelStore
//------------------------------------------------------------------------
static void OpenGLPixelTransfer( )
{
glPixelTransferi(GL_MAP_COLOR, 0);
glPixelTransferf(GL_RED_SCALE, 1.0f);
glPixelTransferf(GL_RED_BIAS, 0.0f);
glPixelTransferf(GL_GREEN_SCALE, 1.0f);
glPixelTransferf(GL_GREEN_BIAS, 0.0f);
glPixelTransferf(GL_BLUE_SCALE, 1.0f);
glPixelTransferf(GL_BLUE_BIAS, 0.0f);
glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
glPixelTransferf(GL_ALPHA_BIAS, 0.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_RED_SCALE, 1.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_RED_BIAS, 0.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_GREEN_SCALE, 1.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_GREEN_BIAS, 0.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_BLUE_SCALE, 1.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_BLUE_BIAS, 0.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_ALPHA_SCALE, 1.0f);
glPixelTransferf(GL_POST_COLOR_MATRIX_ALPHA_BIAS, 0.0f);
} // OpenGLPixelTransfer
//------------------------------------------------------------------------
static inline void OpenGLReadRGBAPixels( NSRect *rect, GLvoid *pixels )
{
GLint x = (GLint) NSMinX(*rect);
GLint y = (GLint) NSMinY(*rect);
GLsizei width = (GLsizei) NSWidth(*rect);
GLsizei height = (GLsizei) NSHeight(*rect);
GLenum format = GL_RGBA;
GLenum type = GL_UNSIGNED_BYTE;
glReadPixels( x, y, width, height, format, type, pixels );
} // OpenGLReadRGBAPixels
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static GLvoid CGImageBitmapMemset( CGImageBitmap *imageBitmap )
{
imageBitmap->imageBitsPerPixel = 0;
imageBitmap->imageBitsPerComponent = 0;
imageBitmap->imageSamplesPerPixel = 0;
imageBitmap->imageBitmapInfo = 0;
imageBitmap->imageStorageSize = 0;
imageBitmap->imageBuffer.width = 0;
imageBitmap->imageBuffer.height = 0;
imageBitmap->imageBuffer.rowBytes = 0;
imageBitmap->imageBuffer.data = NULL;
} // CGImageBitmapMemset
//------------------------------------------------------------------------
static BOOL CGImageBitmapMalloc( CGImageRef imageRef, CGImageBitmap *imageBitmap )
{
BOOL imageBitmapAllocated = NO;
imageBitmap->imageBitsPerPixel = 32;
imageBitmap->imageBitsPerComponent = 8;
imageBitmap->imageSamplesPerPixel = 4;
imageBitmap->imageBitmapInfo = kCGImageAlphaPremultipliedLast; // RGBA
imageBitmap->imageBuffer.width = CGImageGetWidth( imageRef );
imageBitmap->imageBuffer.height = CGImageGetHeight( imageRef );
imageBitmap->imageBuffer.rowBytes = imageBitmap->imageBuffer.width * imageBitmap->imageSamplesPerPixel;
imageBitmap->imageStorageSize = imageBitmap->imageBuffer.rowBytes * imageBitmap->imageBuffer.height;
imageBitmap->imageBuffer.data = (GLvoid *)malloc( imageBitmap->imageStorageSize );
if ( imageBitmap->imageBuffer.data != NULL )
{
imageBitmapAllocated = YES;
} // if
else
{
CGImageBitmapMemset( imageBitmap );
} // else
return imageBitmapAllocated;
} // CGImageBitmapMalloc
//------------------------------------------------------------------------
static BOOL CGImageBitmapFree( CGImageBitmap *imageBitmap )
{
BOOL imageBitmapFreed = NO;
if ( imageBitmap->imageBuffer.data != NULL )
{
free( imageBitmap->imageBuffer.data );
imageBitmapFreed = YES;
} // if
CGImageBitmapMemset( imageBitmap );
return imageBitmapFreed;
} // CGImageBitmapFree
//------------------------------------------------------------------------
static CGContextRef CGImageBitmapContexMalloc( CGImageBitmap *imageBitmap )
{
CGContextRef imageContextRef = NULL;
CGColorSpaceRef imageColorSpaceRef = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
if ( imageColorSpaceRef != NULL )
{
imageContextRef = CGBitmapContextCreate( imageBitmap->imageBuffer.data,
imageBitmap->imageBuffer.width,
imageBitmap->imageBuffer.height,
imageBitmap->imageBitsPerComponent,
imageBitmap->imageBuffer.rowBytes,
imageColorSpaceRef,
imageBitmap->imageBitmapInfo
);
CGColorSpaceRelease( imageColorSpaceRef );
} // if
return imageContextRef;
} // CGImageBitmapContexMalloc
//------------------------------------------------------------------------
static BOOL CGImageBitmapVerticalReflect( CGImageRef imageRef, CGImageBitmap *imageBitmap )
{
BOOL imageBitmapReflected = NO;
CGContextRef imageContextRef = CGImageBitmapContexMalloc( imageBitmap );
if ( imageContextRef != NULL )
{
CGRect imageRect = { { 0, 0 }, { imageBitmap->imageBuffer.width, imageBitmap->imageBuffer.height } };
// The alpha will be added here
CGContextDrawImage( imageContextRef, imageRect, imageRef );
vImageVerticalReflect_ARGB8888( &(imageBitmap->imageBuffer), &(imageBitmap->imageBuffer), kvImageNoFlags );
CGContextRelease( imageContextRef );
imageBitmapReflected = YES;
} // if bitmap context
return imageBitmapReflected;
} // CGImageBitmapVerticalReflect
//------------------------------------------------------------------------
static CGImageRef CGImageGetFromCGImageBitmap( CGImageBitmap *imageBitmap )
{
CGImageRef imageRef = NULL;
// Create a data provider
CGDataProviderRef imageDataProvider = CGDataProviderCreateWithData( NULL,
imageBitmap->imageBuffer.data,
imageBitmap->imageStorageSize,
NULL );
if ( imageDataProvider != NULL )
{
// Create a color space for the image
CGColorSpaceRef imageColorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
if ( imageColorSpace != NULL )
{
// Create the actual image
GLfloat *imageDecode = NULL;
bool imageShouldInterpolate = true;
CGColorRenderingIntent imageRenderingIntent = kCGRenderingIntentDefault;
imageRef = CGImageCreate( imageBitmap->imageBuffer.width,
imageBitmap->imageBuffer.height,
imageBitmap->imageBitsPerComponent,
imageBitmap->imageBitsPerPixel,
imageBitmap->imageBuffer.rowBytes,
imageColorSpace,
imageBitmap->imageBitmapInfo,
imageDataProvider,
imageDecode,
imageShouldInterpolate,
imageRenderingIntent );
// the image will retain the data provider & colorspace as needed, so we can release them now
CGColorSpaceRelease( imageColorSpace );
} //if
CGDataProviderRelease( imageDataProvider );
} // if
return imageRef;
} // CGImageGetFromCGImageBitmap
//------------------------------------------------------------------------
//------------------------------------------------------------------------
static NSImage *NSImageGetFromCGImage( CGImageRef imageRef )
{
NSImage *image = nil;
if ( imageRef != NULL )
{
NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
// Get the image dimensions
imageRect.size.height = CGImageGetHeight( imageRef );
imageRect.size.width = CGImageGetWidth( imageRef );
// Create a new image to receive the Quartz image data
image = [[[NSImage alloc] initWithSize:imageRect.size] autorelease];
if ( image != nil )
{
[image lockFocus];
// Get the Quartz context and draw
CGContextRef imageContextRef = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
if ( imageContextRef != NULL )
{
CGContextDrawImage( imageContextRef, *(CGRect*)&imageRect, imageRef );
} // if
[image unlockFocus];
} // if
} // if
return image;
} // NSImageGetFromCGImage
//------------------------------------------------------------------------
static NSImage *NSImageGetFromCGImageAddAlphaAndVerticalReflect( CGImageRef imageRefSrc )
{
NSImage *imageDst = nil;
if ( imageRefSrc != NULL )
{
CGImageBitmap imageBitmap;
if ( CGImageBitmapMalloc( imageRefSrc, &imageBitmap ) )
{
if ( CGImageBitmapVerticalReflect( imageRefSrc, &imageBitmap ) )
{
CGImageRef imageRefDst = CGImageGetFromCGImageBitmap( &imageBitmap );
if ( imageRefDst != NULL )
{
imageDst = NSImageGetFromCGImage( imageRefDst );
CGImageRelease( imageRefDst );
} // if
} // if image vertical reflect
} // if bitmap data
} // if image ref
return imageDst;
} // NSImageGetFromCGImageAddAlphaAndVerticalReflect
//------------------------------------------------------------------------
//------------------------------------------------------------------------
@implementation OpenGLView
//------------------------------------------------------------------------
- (GLdouble) getExhibitChangeInTime:(GLdouble *)theLastTime
{
GLdouble deltaTime = 0.0;
GLdouble now = (GLdouble)[NSDate timeIntervalSinceReferenceDate];
if ( *theLastTime < 0 )
{
deltaTime = 0;
} // if
else
{
deltaTime = now - *theLastTime;
} // else
*theLastTime = now;
return deltaTime;
} // getExhibitChangeInTime
//------------------------------------------------------------------------
- (void) renderExhibit
{
glPushMatrix( );
glTranslatef(0.0f, 0.45f, 0.0f);
glScalef(0.2f, 0.2f, 0.2f);
if ( targetExhibit )
{
[targetExhibit renderFrame];
} // if
glPopMatrix( );
} // renderExhibit
//------------------------------------------------------------------------
- (void) setExhibitPitch
{
if (pitch < -45.0f)
{
pitch = -45.0f;
} // if
else if (pitch > 90.0f)
{
pitch = 90.0f;
} // else if
glRotatef(pitch, 1.0f, 0.0f, 0.0f);
} // setExhibitPitch
//------------------------------------------------------------------------
- (void) setExhibitAngle
{
if ( !leftMouseIsDown && !rightMouseIsDown )
{
GLdouble deltaTime = [self getExhibitChangeInTime:&lastFrameReferenceTime];
angle += kViewRotationDegreesPerSecond * deltaTime;
if ( angle >= 360.0f )
{
angle -= 360.0f;
} // if
} // if
glRotatef( angle, 0.0f, 1.0f, 0.0f );
} // setExhibitAngle
//------------------------------------------------------------------------
-(void) rotateExhibit
{
glPushMatrix( );
// Constant rotation of the subject
[self setExhibitPitch];
glTranslatef(0.0f, -0.5f, -0.15f);
[self setExhibitAngle];
// Draw the exhibit
[self renderExhibit];
glPopMatrix( );
} // rotateExhibit
//------------------------------------------------------------------------
- (void) setExhibitPrespectiveMatrix:(GLfloat)theWidth boundsHeight:(GLfloat)theHeight
{
GLfloat aspect = theWidth / theHeight;
GLdouble right = 0.15 * aspect * zoom;
GLdouble left = -right;
GLdouble top = 0.15 * zoom;
GLdouble bottom = -top;
GLdouble zNear = 1.0;
GLdouble zFar = 8.0;
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glFrustum( left, right, bottom, top, zNear, zFar );
glMatrixMode( GL_MODELVIEW );
} // setExhibitPrespectiveMatrix
//------------------------------------------------------------------------
- (void) setExhibitViewport
{
NSRect theBounds = [self bounds];
GLfloat theWidth = NSWidth(theBounds);
GLfloat theHeight = NSHeight(theBounds);
[self setExhibitPrespectiveMatrix:theWidth boundsHeight:theHeight];
glViewport( 0, 0, theWidth, theHeight );
} // setExhibitViewport
//------------------------------------------------------------------------
- (void) drawExhibit
{
[[self openGLContext] makeCurrentContext];
[self setExhibitViewport];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
[self rotateExhibit];
} // drawExhibit
//------------------------------------------------------------------------
- (void) updateExhibit
{
[self drawExhibit];
[self setNeedsDisplay:YES];
} // updateExhibit
//------------------------------------------------------------------------
- (void) setExhibit: (OpenGLExhibit *) newExhibit
{
if ( newExhibit )
{
[targetExhibit release];
targetExhibit = [newExhibit retain];
} // if
} // setExhibit
//------------------------------------------------------------------------
-(void) setupOpenGL
{
OpenGLSetup( GL_FALSE );
} // setupOpenGL
//------------------------------------------------------------------------
- (void) newUpdateTimer
{
GLint swapInterval = 1;
timer = [NSTimer timerWithTimeInterval: kScheduledTimerInSeconds
target: self
selector: @selector(updateExhibit)
userInfo: nil
repeats: YES];
[timer retain];
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSEventTrackingRunLoopMode];
lastFrameReferenceTime = -1;
leftMouseIsDown = NO;
rightMouseIsDown = NO;
angle = 0;
pitch = 25;
zoom = 1;
// Sync to VBL to avoid tearing.
[[self openGLContext] setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
} // newUpdateTimer
//------------------------------------------------------------------------
- (id) initWithFrame:(NSRect)theFrame pixelFormat:(NSOpenGLPixelFormat*)thePixelFormat
{
// Create a GL Context to use - i.e. init the superclass
if( thePixelFormat == nil )
{
NSOpenGLPixelFormatAttribute pixelAttributes []
= {
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAStencilSize, 8,
0
};
CheckForGLSLHardwareSupport( pixelAttributes );
thePixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: pixelAttributes] autorelease];
} // if
if ( ( self = [super initWithFrame:theFrame pixelFormat:thePixelFormat] ) )
{
// Basic GL initializations
[[self openGLContext] makeCurrentContext];
[self setupOpenGL];
// Basic initializations
[self setFrameSize:theFrame.size];
// Create an update timer
[self newUpdateTimer];
// Did the frame change?
[self setPostsFrameChangedNotifications:YES];
} // if
return self;
} // initWithFrame
//------------------------------------------------------------------------
- (id)init
{
if ( ( self = [super init] ) )
{
// superclass may return nil
targetExhibit = nil;
timer = nil;
} // if
return self;
} // init
//------------------------------------------------------------------------
- (id) initWithFrame:(NSRect)theFrame
{
return [self initWithFrame:theFrame pixelFormat:nil];
} // initWithFrame
- (void)update // moved or resized
{
[super update];
[self drawExhibit];
} // update
//------------------------------------------------------------------------
- (void)reshape // scrolled, moved or resized
{
[super reshape];
[self drawExhibit];
} // reshape
//------------------------------------------------------------------------
- (void) dealloc
{
// Release the update timer
if (timer)