Mercurial > xcodescriptingbridge
comparison Classes/EAGLView.m @ 0:5ec52341f221
Initial commit
author | Eric Wing <ewing@anscamobile.com> |
---|---|
date | Fri, 29 Jul 2011 18:18:15 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:5ec52341f221 |
---|---|
1 // | |
2 // EAGLView.m | |
3 // MySampleProject | |
4 // | |
5 // Created by Eric Wing on 7/29/11. | |
6 // Copyright 2011 __MyCompanyName__. All rights reserved. | |
7 // | |
8 | |
9 #import <QuartzCore/QuartzCore.h> | |
10 | |
11 #import "EAGLView.h" | |
12 | |
13 @interface EAGLView (PrivateMethods) | |
14 - (void)createFramebuffer; | |
15 - (void)deleteFramebuffer; | |
16 @end | |
17 | |
18 @implementation EAGLView | |
19 | |
20 @dynamic context; | |
21 | |
22 // You must implement this method | |
23 + (Class)layerClass | |
24 { | |
25 return [CAEAGLLayer class]; | |
26 } | |
27 | |
28 //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:. | |
29 - (id)initWithCoder:(NSCoder*)coder | |
30 { | |
31 self = [super initWithCoder:coder]; | |
32 if (self) | |
33 { | |
34 CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; | |
35 | |
36 eaglLayer.opaque = TRUE; | |
37 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: | |
38 [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, | |
39 kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, | |
40 nil]; | |
41 } | |
42 | |
43 return self; | |
44 } | |
45 | |
46 - (void)dealloc | |
47 { | |
48 [self deleteFramebuffer]; | |
49 [context release]; | |
50 | |
51 [super dealloc]; | |
52 } | |
53 | |
54 - (EAGLContext *)context | |
55 { | |
56 return context; | |
57 } | |
58 | |
59 - (void)setContext:(EAGLContext *)newContext | |
60 { | |
61 if (context != newContext) | |
62 { | |
63 [self deleteFramebuffer]; | |
64 | |
65 [context release]; | |
66 context = [newContext retain]; | |
67 | |
68 [EAGLContext setCurrentContext:nil]; | |
69 } | |
70 } | |
71 | |
72 - (void)createFramebuffer | |
73 { | |
74 if (context && !defaultFramebuffer) | |
75 { | |
76 [EAGLContext setCurrentContext:context]; | |
77 | |
78 // Create default framebuffer object. | |
79 glGenFramebuffers(1, &defaultFramebuffer); | |
80 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); | |
81 | |
82 // Create color render buffer and allocate backing store. | |
83 glGenRenderbuffers(1, &colorRenderbuffer); | |
84 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); | |
85 [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer]; | |
86 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth); | |
87 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight); | |
88 | |
89 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); | |
90 | |
91 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) | |
92 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); | |
93 } | |
94 } | |
95 | |
96 - (void)deleteFramebuffer | |
97 { | |
98 if (context) | |
99 { | |
100 [EAGLContext setCurrentContext:context]; | |
101 | |
102 if (defaultFramebuffer) | |
103 { | |
104 glDeleteFramebuffers(1, &defaultFramebuffer); | |
105 defaultFramebuffer = 0; | |
106 } | |
107 | |
108 if (colorRenderbuffer) | |
109 { | |
110 glDeleteRenderbuffers(1, &colorRenderbuffer); | |
111 colorRenderbuffer = 0; | |
112 } | |
113 } | |
114 } | |
115 | |
116 - (void)setFramebuffer | |
117 { | |
118 if (context) | |
119 { | |
120 [EAGLContext setCurrentContext:context]; | |
121 | |
122 if (!defaultFramebuffer) | |
123 [self createFramebuffer]; | |
124 | |
125 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); | |
126 | |
127 glViewport(0, 0, framebufferWidth, framebufferHeight); | |
128 } | |
129 } | |
130 | |
131 - (BOOL)presentFramebuffer | |
132 { | |
133 BOOL success = FALSE; | |
134 | |
135 if (context) | |
136 { | |
137 [EAGLContext setCurrentContext:context]; | |
138 | |
139 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); | |
140 | |
141 success = [context presentRenderbuffer:GL_RENDERBUFFER]; | |
142 } | |
143 | |
144 return success; | |
145 } | |
146 | |
147 - (void)layoutSubviews | |
148 { | |
149 // The framebuffer will be re-created at the beginning of the next setFramebuffer method call. | |
150 [self deleteFramebuffer]; | |
151 } | |
152 | |
153 @end |