forked from eway2012/SampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEAGLView.m
More file actions
528 lines (409 loc) · 15 KB
/
EAGLView.m
File metadata and controls
528 lines (409 loc) · 15 KB
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
/*
File: EAGLView.m
Abstract: The EAGLView class is responsible for rendering the GL view.
Version: 1.5
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) 2010 Apple Inc. All Rights Reserved.
*/
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import "EAGLView.h"
#import "PVRTexture.h"
#define USE_DEPTH_BUFFER 0
// A class extension to declare private methods
@interface EAGLView ()
@property (nonatomic, retain) EAGLContext *context;
- (BOOL)createFramebuffer;
- (void)destroyFramebuffer;
- (void)updateSettings;
@end
@implementation EAGLView
@synthesize compressionSupported = _compressionSupported;
@synthesize anisotropySupported = _anisotropySupported;
@dynamic compressionSetting;
@dynamic mipmapFilterSetting;
@dynamic filterSetting;
@synthesize context = _context;
- (void)loadImageFile:(NSString *)name ofType:(NSString *)extension mipmap:(BOOL)mipmap texture:(uint32_t)texture
{
UIImage *image;
int width, height;
CGImageRef cgImage;
GLubyte *data;
CGContextRef cgContext;
CGColorSpaceRef colorSpace;
GLenum err;
image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:extension]];
if (image == nil)
{
NSLog(@"Failed to load %@.%@", name, extension);
return;
}
cgImage = [image CGImage];
width = CGImageGetWidth(cgImage);
height = CGImageGetHeight(cgImage);
colorSpace = CGColorSpaceCreateDeviceRGB();
// Malloc may be used instead of calloc if your cg image has dimensions equal to the dimensions of the cg bitmap context
data = (GLubyte *)calloc(width * height * 4, sizeof(GLubyte));
cgContext = CGBitmapContextCreate(data, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast);
if (cgContext != NULL)
{
// Set the blend mode to copy. We don't care about the previous contents.
CGContextSetBlendMode(cgContext, kCGBlendModeCopy);
CGContextDrawImage(cgContext, CGRectMake(0.0f, 0.0f, width, height), cgImage);
glGenTextures(1, &(_textures[texture]));
glBindTexture(GL_TEXTURE_2D, _textures[texture]);
if (mipmap)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
if (mipmap)
glGenerateMipmapOES(GL_TEXTURE_2D);
err = glGetError();
if (err != GL_NO_ERROR)
NSLog(@"Error uploading texture. glError: 0x%04X", err);
CGContextRelease(cgContext);
}
free(data);
CGColorSpaceRelease(colorSpace);
}
- (void)loadTextures
{
if (_compressionSupported)
{
PVRTexture *pvrTexture;
NSArray *names = [NSArray arrayWithObjects:@"Brick_mipmap_4", @"Brick_mipmap_2", @"Brick_4", @"Brick_2", nil];
[EAGLContext setCurrentContext:_context];
[_pvrTextures removeAllObjects];
for (int i=0; i < [names count]; i++)
{
pvrTexture = [PVRTexture pvrTextureWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[names objectAtIndex:i] ofType:@"pvr"]];
if (pvrTexture == nil)
NSLog(@"Failed to load %@.pvr", [names objectAtIndex:i]);
else
_textures[i] = [pvrTexture name];
[_pvrTextures addObject:pvrTexture];
}
}
[self loadImageFile:@"Brick" ofType:@"png" mipmap:TRUE texture:kTextureMipmap];
[self loadImageFile:@"Brick" ofType:@"png" mipmap:FALSE texture:kTexture];
}
// You must implement this method
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
- (Compression)compressionSetting
{
return _compressionSetting;
}
- (MipmipFilter)mipmapFilterSetting
{
return _mipmapFilterSetting;
}
- (Filter)filterSetting
{
return _filterSetting;
}
- (void)setCompressionSetting:(Compression)setting
{
_compressionSetting = setting;
[self updateSettings];
[self drawView];
}
- (void)setMipmapFilterSetting:(MipmipFilter)setting
{
_mipmapFilterSetting = setting;
[self updateSettings];
[self drawView];
}
- (void)setFilterSetting:(Filter)setting
{
_filterSetting = setting;
[self updateSettings];
[self drawView];
}
- (void)updateSettings
{
// choose texture name
if (_mipmapFilterSetting == kMipmapFilterOff)
{
if (_compressionSetting == kCompressionFourBPP)
_texSelection = kTexturePvrtc4;
else if (_compressionSetting == kCompressionTwoBPP)
_texSelection = kTexturePvrtc2;
else if (_compressionSetting == kCompressionOff)
_texSelection = kTexture;
}
else
{
if (_compressionSetting == kCompressionFourBPP)
_texSelection = kTexturePvrtcMipmap4;
else if (_compressionSetting == kCompressionTwoBPP)
_texSelection = kTexturePvrtcMipmap2;
else if (_compressionSetting == kCompressionOff)
_texSelection = kTextureMipmap;
}
// choose filter settings
if (_mipmapFilterSetting == kMipmapFilterOff)
{
if (_filterSetting == kFilterNearest)
{
_minTexParam = GL_NEAREST;
_magTexParam = GL_NEAREST;
}
else if (_filterSetting == kFilterLinear || _filterSetting == kFilterSuper)
{
_minTexParam = GL_LINEAR;
_magTexParam = GL_LINEAR;
}
}
else if (_mipmapFilterSetting == kMipmapFilterNearest)
{
if (_filterSetting == kFilterNearest)
{
_minTexParam = GL_NEAREST_MIPMAP_NEAREST;
_magTexParam = GL_NEAREST;
}
else if (_filterSetting == kFilterLinear || _filterSetting == kFilterSuper)
{
_minTexParam = GL_LINEAR_MIPMAP_NEAREST;
_magTexParam = GL_LINEAR;
}
}
else if (_mipmapFilterSetting == kMipmapFilterLinear)
{
if (_filterSetting == kFilterNearest)
{
_minTexParam = GL_NEAREST_MIPMAP_LINEAR;
_magTexParam = GL_NEAREST;
}
else if (_filterSetting == kFilterLinear || _filterSetting == kFilterSuper)
{
_minTexParam = GL_LINEAR_MIPMAP_LINEAR;
_magTexParam = GL_LINEAR;
}
}
if (_filterSetting == kFilterSuper)
_anisotropyTexParam = 2.0f;
else
_anisotropyTexParam = 1.0f;
}
- (BOOL)extensionSupported:(NSString *)name
{
NSString *extensionsString = [NSString stringWithCString:(char *)glGetString(GL_EXTENSIONS) encoding:NSUTF8StringEncoding];
NSArray *extensionsNames = [extensionsString componentsSeparatedByString:@" "];
return [extensionsNames containsObject:name];
}
// The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!_context || ![EAGLContext setCurrentContext:_context])
{
[self release];
return nil;
}
_scale = 1.5f;
_rotate = 0.0f;
_compressionSetting = kCompressionOff;
_mipmapFilterSetting = kMipmapFilterOff;
_filterSetting = kFilterNearest;
_texSelection = kTexture;
_minTexParam = GL_NEAREST;
_magTexParam = GL_NEAREST;
_anisotropyTexParam = 1.0f;
// PVRTC textures will not be loaded if the PVRTC extension is not supported.
// UI selection for compression levels will also be disabled.
_compressionSupported = [self extensionSupported:@"GL_IMG_texture_compression_pvrtc"];
// The use of GL_TEXTURE_MAX_ANISOTROPY_EXT will be avoided if anisotropic filtering is not supported.
// UI selection for anisotropic filtering will also be disabled.
_anisotropySupported = [self extensionSupported:@"GL_EXT_texture_filter_anisotropic"];
_pvrTextures = [[NSMutableArray alloc] initWithCapacity:10];
[self loadTextures];
[self setMultipleTouchEnabled:YES];
}
return self;
}
- (void)drawView
{
const GLfloat squareVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
const GLfloat squareTexCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
GLenum err;
[EAGLContext setCurrentContext:_context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
glViewport(0, 0, _backingWidth, _backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-1.0f, 1.0f, -1.5, 1.5, 1.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(_scale, _scale, 1.0f);
glTranslatef(0.0f, 0.0f, -2.0f);
glRotatef(_rotate, 1.0f, 0.0f, 0.0f);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, squareTexCoords);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textures[_texSelection]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _minTexParam);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _magTexParam);
if (_anisotropySupported)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, _anisotropyTexParam);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_TEXTURE_2D);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer);
[_context presentRenderbuffer:GL_RENDERBUFFER_OES];
err = glGetError();
if (err != GL_NO_ERROR)
NSLog(@"Error in frame. glError: 0x%04X", err);
}
- (void)layoutSubviews
{
[EAGLContext setCurrentContext:_context];
[self destroyFramebuffer];
[self createFramebuffer];
[self drawView];
}
- (BOOL)createFramebuffer
{
glGenFramebuffersOES(1, &_viewFramebuffer);
glGenRenderbuffersOES(1, &_viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer);
[_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &_backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &_backingHeight);
if (USE_DEPTH_BUFFER)
{
glGenRenderbuffersOES(1, &_depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, _backingWidth, _backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, _depthRenderbuffer);
}
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}
return YES;
}
- (void)destroyFramebuffer
{
glDeleteFramebuffersOES(1, &_viewFramebuffer);
_viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &_viewRenderbuffer);
_viewRenderbuffer = 0;
if(_depthRenderbuffer)
{
glDeleteRenderbuffersOES(1, &_depthRenderbuffer);
_depthRenderbuffer = 0;
}
}
- (void)dealloc
{
if ([EAGLContext currentContext] == _context)
[EAGLContext setCurrentContext:nil];
if (_textures[kTexture] != 0)
glDeleteTextures(1, &_textures[kTexture]);
if (_textures[kTextureMipmap] != 0)
glDeleteTextures(1, &_textures[kTextureMipmap]);
[_pvrTextures release];
[_context release];
[super dealloc];
}
- (float)distanceFromPoint:(CGPoint)pointA toPoint:(CGPoint)pointB
{
float xD = fabs(pointA.x - pointB.x);
float yD = fabs(pointA.y - pointB.y);
return sqrt(xD*xD + yD*yD);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touchA, *touchB;
CGPoint pointA, pointB;
if ([touches count] == 1)
{
touchA = [[touches allObjects] objectAtIndex:0];
pointA = [touchA locationInView:self];
pointB = [touchA previousLocationInView:self];
float yDistance = pointA.y - pointB.y;
_rotate += 0.5 * yDistance;
[self drawView];
}
else if ([touches count] == 2)
{
touchA = [[touches allObjects] objectAtIndex:0];
touchB = [[touches allObjects] objectAtIndex:1];
pointA = [touchA locationInView:self];
pointB = [touchB locationInView:self];
float currDistance = [self distanceFromPoint:pointA toPoint:pointB];
pointA = [touchA previousLocationInView:self];
pointB = [touchB previousLocationInView:self];
float prevDistance = [self distanceFromPoint:pointA toPoint:pointB];
_scale += 0.005 * (currDistance - prevDistance);
if (_scale > 10.0f)
_scale = 10.0f;
else if (_scale < 0.025f)
_scale = 0.025f;
[self drawView];
}
}
@end