-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLFramebuffer.m
More file actions
163 lines (128 loc) · 4.39 KB
/
GLFramebuffer.m
File metadata and controls
163 lines (128 loc) · 4.39 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
//
// GLFramebuffer.m
// openGLES Wrappers
//
// Created by denis on 11/15/13.
// Copyright (c) 2013 denis. All rights reserved.
//
#import "GLFramebuffer.h"
#import "GLContext+Private.h"
#import "EAGLContext+GLContext.h"
@interface GLFramebuffer ()
@property (nonatomic, retain) id<GLFramebufferRenderTarget> colorTexturePtr;
@property (nonatomic, retain) id<GLFramebufferRenderTarget> depthTexturePtr;
@property (nonatomic, retain) id<GLFramebufferRenderTarget> stencilTexturePtr;
@end
@implementation GLFramebuffer
@dynamic colorTarget;
@dynamic depthTarget;
@dynamic stencilTarget;
+ (id)objectWithColorAttachment:(id<GLFramebufferRenderTarget>)colorFace {
GLFramebuffer *me = [self object];
me.colorTarget = colorFace;
return me;
}
#pragma mark -
#pragma mark Inialization and Deallocation
- (void)dealloc {
[self bind];
self.colorTarget = nil;
self.depthTarget = nil;
self.stencilTarget = nil;
[self unbind];
GLCall(glDeleteFramebuffers(1, &_uId));
[super dealloc];
}
- (id)init {
self = [super init];
if (self) {
GLCall(glGenFramebuffers(1, &_uId));
}
return self;
}
#pragma mark -
#pragma mark GLObject
- (void)internalBind:(BOOL)bind {
GLCall(glBindFramebuffer(GL_FRAMEBUFFER, bind ? self.uId : 0));
}
+ (GLObjectType)glType {
return GLObjectTypeFramebuffer;
}
#pragma mark -
#pragma mark Public
- (void)readRGBAUBytePixels:(GLvoid *)pixels fromRect:(GLRect)rect {
[self bind];
/*
Only two combinations of format and type are accepted. The first is format RGBA and type UNSIGNED_BYTE.
The second is an implementation-chosen format from among those defined in table 3.4, excluding formats LUMINANCE and LUMINANCE_ALPHA.
The values of format and type for this format may be determined by calling GetIntegerv with the symbolic constants IMPLEMENTATION_COLOR_READ_FORMAT and IMPLEMENTATION_COLOR_READ_TYPE, respectively.
*/
GLCall(glReadPixels(rect.x, rect.y, rect.width, rect.height, GLInternalFormatRGBA, GLDataUByte, pixels));
[self unbind];
}
static void _GLFramebufferAttachTexture(GLFramebuffer *me, id<GLFramebufferRenderTarget> *field,
id<GLFramebufferRenderTarget> face, GLFramebufferAttachment point)
{
if (*field != face) {
[me bind];
if (face) {
[face internalAttach:YES
toFramebuffer:me
point:point];
} else {
[*field internalAttach:NO
toFramebuffer:me
point:point];
}
GLenum completeness = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (completeness != GL_FRAMEBUFFER_COMPLETE) {
[NSException raise:NSInternalInconsistencyException
format:@"framebuffer incomplete status 0x%x", completeness];
}
[me unbind];
[*field release];
*field = [face retain];
}
}
- (id<GLFramebufferRenderTarget>)colorTarget {
return _colorTexturePtr;
}
- (id<GLFramebufferRenderTarget>)depthTarget {
return _depthTexturePtr;
}
- (id<GLFramebufferRenderTarget>)stencilTarget {
return _stencilTexturePtr;
}
- (void)setColorTarget:(id<GLFramebufferRenderTarget>)colorTexture {
_GLFramebufferAttachTexture(self, &_colorTexturePtr, colorTexture, GLFramebufferAttachmentColor);
}
- (void)setDepthTarget:(id<GLFramebufferRenderTarget>)depthTexture {
_GLFramebufferAttachTexture(self, &_depthTexturePtr, depthTexture, GLFramebufferAttachmentDepth);
}
- (void)setStencilTarget:(id<GLFramebufferRenderTarget>)stencilTexture {
_GLFramebufferAttachTexture(self, &_stencilTexturePtr, stencilTexture, GLFramebufferAttachmentStencil);
}
@end
@implementation GLExternalFramebuffer
+ (id)object {
GLint fboCurrent;
GLCall(glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fboCurrent));
if (fboCurrent == 0) {
return nil;
}
GLContext *context = [[EAGLContext currentContext] gl_context];
assert(!context.framebuffer || context.framebuffer.uId != fboCurrent);
GLExternalFramebuffer *me = [[self new] autorelease];
if (me) {
me.uId = fboCurrent;
[me bind];
}
return me;
}
- (void)internalBind:(BOOL)bind {
GLCall(glBindFramebuffer(GL_FRAMEBUFFER, bind ? self.uId : 0));
}
+ (GLObjectType)glType {
return GLObjectTypeFramebuffer;
}
@end