annotate Classes/EAGLView.m @ 8:78830332a3be tip

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