visageSDK
ShowcaseDemo/ShowcaseDemo/CustomGLView.m
1 #import <QuartzCore/QuartzCore.h>
2 #import <OpenGLES/EAGLDrawable.h>
3 
4 #import "CustomGLView.h"
5 
6 // A class extension to declare private methods
7 @interface CustomGLView (private)
8 
9 - (BOOL)createFramebuffer;
10 - (void)destroyFramebuffer;
11 - (void)setupView;
12 
13 @end
14 
15 
16 @implementation CustomGLView
17 
18 @synthesize backingWidth;
19 @synthesize backingHeight;
20 
21 // Implement this to override the default layer class (which is [CALayer class]).
22 // We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
23 + (Class) layerClass
24 {
25  return [CAEAGLLayer class];
26 }
27 
28 - (id)initWithFrame:(CGRect)frame {
29 
30  if ((self = [super initWithFrame:frame])) {
31  CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
32 
33  eaglLayer.opaque = NO;
34  eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
35  [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
36  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37 
38  context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
39 
40  if (!context || ![EAGLContext setCurrentContext:context]) {
41 
42  //[self release];
43  return nil;
44  }
45  }
46 
47  return self;
48 }
49 
50 // The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
51 - (id)initWithCoder:(NSCoder*)coder {
52 
53  if ((self = [super initWithCoder:coder])) {
54  CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
55 
56  eaglLayer.opaque = NO;
57  eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
58  [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
59  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
60 
61  context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
62 
63  if (!context || ![EAGLContext setCurrentContext:context]) {
64  return nil;
65  }
66  }
67 
68  return self;
69 }
70 
71 // If our view is resized, we'll be asked to layout subviews.
72 // This is the perfect opportunity to also update the framebuffer so that it is
73 // the same size as our display area.
74 -(void)layoutSubviews
75 {
76  [EAGLContext setCurrentContext:context];
77  [self destroyFramebuffer];
78  [self createFramebuffer];
79 }
80 
81 - (BOOL)createFramebuffer
82 {
83  // Generate IDs for a framebuffer object and a color renderbuffer
84  glGenFramebuffersOES(1, &viewFramebuffer);
85  glGenRenderbuffersOES(1, &viewRenderbuffer);
86 
87  glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
88  glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
89  // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
90  // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
91  [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
92  glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
93 
94  glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
95  glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
96 
97  // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
98  glGenRenderbuffersOES(1, &depthRenderbuffer);
99  glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
100  glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
101  glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
102 
103  if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
104  {
105  NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
106  return NO;
107  }
108 
109  return YES;
110 }
111 
112 // Clean up any buffers we have allocated.
113 - (void)destroyFramebuffer
114 {
115  glDeleteFramebuffersOES(1, &viewFramebuffer);
116  viewFramebuffer = 0;
117  glDeleteRenderbuffersOES(1, &viewRenderbuffer);
118  viewRenderbuffer = 0;
119 
120  if(depthRenderbuffer)
121  {
122  glDeleteRenderbuffersOES(1, &depthRenderbuffer);
123  depthRenderbuffer = 0;
124  }
125 }
126 
127 - (void)setOpenGLContext
128 {
129  // Make sure that you are drawing to the current context
130  [EAGLContext setCurrentContext:context];
131 
132  glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
133  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
134  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
135 }
136 
137 - (void)swapOpenGLBuffers
138 {
139  glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
140  [context presentRenderbuffer:GL_RENDERBUFFER_OES];
141 }
142 
143 - (void)dealloc
144 {
145  if([EAGLContext currentContext] == context)
146  {
147  [EAGLContext setCurrentContext:nil];
148  }
149 }
150 
151 @end
void setOpenGLContext()
Method for setting the OpenGL context.
void swapOpenGLBuffers()
Method for swapping OpenGL buffers.
Controls OpenGL view used for tracking output.