forked from ScanNet/ScanNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEAGLView.mm
More file actions
190 lines (144 loc) · 5.22 KB
/
EAGLView.mm
File metadata and controls
190 lines (144 loc) · 5.22 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
/*
This file is part of the Structure SDK.
Copyright © 2015 Occipital, Inc. All rights reserved.
http://structure.io
*/
#import <QuartzCore/QuartzCore.h>
#import "EAGLView.h"
#import <mach/mach.h>
@interface EAGLView () {
// The pixel dimensions of the CAEAGLLayer.
GLint framebufferWidth;
GLint framebufferHeight;
// The OpenGL ES names for the framebuffer and renderbuffer used to render to this view.
GLuint defaultFramebuffer, colorRenderbuffer, depthRenderbuffer;
}
- (void)createFramebuffer;
- (void)deleteFramebuffer;
@end
@implementation EAGLView
@dynamic context;
// You must implement this method
+ (Class)layerClass
{
return [CAEAGLLayer class];
}
//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:.
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self)
{
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
self.contentScaleFactor = 1.0;
}
return self;
}
- (void)dealloc
{
[self deleteFramebuffer];
}
- (EAGLContext *)context
{
return context;
}
- (void)setContext:(EAGLContext *)newContext
{
if (context != newContext)
{
[self deleteFramebuffer];
context = newContext;
[EAGLContext setCurrentContext:nil];
}
}
- (void)createFramebuffer
{
if (context && !defaultFramebuffer)
{
[EAGLContext setCurrentContext:context];
// Create default framebuffer object.
glGenFramebuffers(1, &defaultFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
// Create color render buffer and allocate backing store.
glGenRenderbuffers(1, &colorRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
glGenRenderbuffers(1, &depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, framebufferWidth, framebufferHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
}
}
- (void)deleteFramebuffer
{
if (context)
{
[EAGLContext setCurrentContext:context];
if (defaultFramebuffer)
{
glDeleteFramebuffers(1, &defaultFramebuffer);
defaultFramebuffer = 0;
}
if (depthRenderbuffer)
{
glDeleteRenderbuffers(1, &depthRenderbuffer);
depthRenderbuffer = 0;
}
if (colorRenderbuffer)
{
glDeleteRenderbuffers(1, &colorRenderbuffer);
colorRenderbuffer = 0;
}
}
}
- (void)setFramebuffer
{
if (context)
{
[EAGLContext setCurrentContext:context];
if (!defaultFramebuffer) {
[self createFramebuffer];
}
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glViewport(0, 0, framebufferWidth, framebufferHeight);
}
}
- (BOOL)presentFramebuffer
{
BOOL success = FALSE;
// iOS may crash if presentRenderbuffer is called when the application is in background.
if (context && [UIApplication sharedApplication].applicationState != UIApplicationStateBackground)
{
[EAGLContext setCurrentContext:context];
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
success = [context presentRenderbuffer:GL_RENDERBUFFER];
}
return success;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// CAREFUL!!!! If you have autolayout enabled, you will re-create your framebuffer all the time if
// your EAGLView has any subviews that are updated. For example, having a UILabel that is updated
// to display FPS will result in layoutSubviews being called every frame. Two ways around this:
// 1) don't use autolayout
// 2) don't add any subviews to the EAGLView. Have the EAGLView be a subview of another "master" view.
// The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
[self deleteFramebuffer];
}
- (CGSize)getFramebufferSize
{
return CGSizeMake(framebufferWidth, framebufferHeight);
}
@end