|
| 1 | +/* |
| 2 | + File: EAGLView.m |
| 3 | + Abstract: n/a |
| 4 | + Version: 1.2 |
| 5 | + |
| 6 | + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
| 7 | + Inc. ("Apple") in consideration of your agreement to the following |
| 8 | + terms, and your use, installation, modification or redistribution of |
| 9 | + this Apple software constitutes acceptance of these terms. If you do |
| 10 | + not agree with these terms, please do not use, install, modify or |
| 11 | + redistribute this Apple software. |
| 12 | + |
| 13 | + In consideration of your agreement to abide by the following terms, and |
| 14 | + subject to these terms, Apple grants you a personal, non-exclusive |
| 15 | + license, under Apple's copyrights in this original Apple software (the |
| 16 | + "Apple Software"), to use, reproduce, modify and redistribute the Apple |
| 17 | + Software, with or without modifications, in source and/or binary forms; |
| 18 | + provided that if you redistribute the Apple Software in its entirety and |
| 19 | + without modifications, you must retain this notice and the following |
| 20 | + text and disclaimers in all such redistributions of the Apple Software. |
| 21 | + Neither the name, trademarks, service marks or logos of Apple Inc. may |
| 22 | + be used to endorse or promote products derived from the Apple Software |
| 23 | + without specific prior written permission from Apple. Except as |
| 24 | + expressly stated in this notice, no other rights or licenses, express or |
| 25 | + implied, are granted by Apple herein, including but not limited to any |
| 26 | + patent rights that may be infringed by your derivative works or by other |
| 27 | + works in which the Apple Software may be incorporated. |
| 28 | + |
| 29 | + The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
| 30 | + MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 31 | + THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
| 32 | + FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
| 33 | + OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
| 34 | + |
| 35 | + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
| 36 | + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 37 | + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 38 | + INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
| 39 | + MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
| 40 | + AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
| 41 | + STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
| 42 | + POSSIBILITY OF SUCH DAMAGE. |
| 43 | + |
| 44 | + Copyright (C) 2010 Apple Inc. All Rights Reserved. |
| 45 | + |
| 46 | + */ |
| 47 | + |
| 48 | +#import <QuartzCore/QuartzCore.h> |
| 49 | +#import <OpenGLES/EAGLDrawable.h> |
| 50 | +#import "EAGLView.h" |
| 51 | +#import "Imaging.h" |
| 52 | + |
| 53 | + |
| 54 | +// A class extension to declare private methods |
| 55 | +@interface EAGLView () |
| 56 | + |
| 57 | +@property (nonatomic, retain) EAGLContext *context; |
| 58 | + |
| 59 | +@end |
| 60 | + |
| 61 | + |
| 62 | +@implementation EAGLView |
| 63 | + |
| 64 | +@synthesize context; |
| 65 | +@synthesize slider; |
| 66 | +@synthesize tabBar; |
| 67 | + |
| 68 | + |
| 69 | +// You must implement this method |
| 70 | ++ (Class)layerClass |
| 71 | +{ |
| 72 | + return [CAEAGLLayer class]; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +// The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: |
| 77 | +- (id)initWithCoder:(NSCoder*)coder |
| 78 | +{ |
| 79 | + if ((self = [super initWithCoder:coder])) |
| 80 | + { |
| 81 | + // Get the layer |
| 82 | + CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; |
| 83 | + eaglLayer.opaque = YES; |
| 84 | + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: |
| 85 | + [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, |
| 86 | + kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, |
| 87 | + nil]; |
| 88 | + |
| 89 | + context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; |
| 90 | + if (!context || ![EAGLContext setCurrentContext:context]) |
| 91 | + { |
| 92 | + [self release]; |
| 93 | + return nil; |
| 94 | + } |
| 95 | + |
| 96 | + // Create system framebuffer object. The backing will be allocated in -reshapeFramebuffer |
| 97 | + glGenFramebuffersOES(1, &viewFramebuffer); |
| 98 | + glGenRenderbuffersOES(1, &viewRenderbuffer); |
| 99 | + glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); |
| 100 | + glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); |
| 101 | + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); |
| 102 | + |
| 103 | + // Perform additional one-time GL initialization |
| 104 | + initGL(); |
| 105 | + } |
| 106 | + return self; |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +- (void)drawView |
| 111 | +{ |
| 112 | + int mode = self.tabBar.selectedItem.tag; |
| 113 | + float value = self.slider.value; |
| 114 | + |
| 115 | + // This application only creates a single GL context, so it is already current here. |
| 116 | + // If there are multiple contexts, ensure the correct one is current before drawing. |
| 117 | + drawGL(backingWidth, backingHeight, value, mode); |
| 118 | + |
| 119 | + // This application only creates a single (color) renderbuffer, so it is already bound here. |
| 120 | + // If there are multiple renderbuffers (for example color and depth), ensure the correct one is bound here. |
| 121 | + [context presentRenderbuffer:GL_RENDERBUFFER_OES]; |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +- (void)reshapeFramebuffer |
| 126 | +{ |
| 127 | + // Allocate GL color buffer backing, matching the current layer size |
| 128 | + [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; |
| 129 | + |
| 130 | + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); |
| 131 | + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); |
| 132 | + |
| 133 | + // This application only needs color. If depth and/or stencil are needed, ensure they are also resized here. |
| 134 | + rt_assert(GL_FRAMEBUFFER_COMPLETE_OES == glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); |
| 135 | + glCheckError(); |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +- (void)layoutSubviews |
| 140 | +{ |
| 141 | + [self reshapeFramebuffer]; |
| 142 | + [self drawView]; |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +- (void)dealloc |
| 147 | +{ |
| 148 | + if ([EAGLContext currentContext] == context) |
| 149 | + [EAGLContext setCurrentContext:nil]; |
| 150 | + |
| 151 | + self.context = nil; |
| 152 | + self.slider = nil; |
| 153 | + self.tabBar = nil; |
| 154 | + [super dealloc]; |
| 155 | +} |
| 156 | + |
| 157 | +@end |
0 commit comments