Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 139 lines (115 sloc) 3.808 kb
f329a535 »
2012-01-03 imported
1 //
2 // GLFramebuffer.m
3 // CeedGL
4 //
5 // Created by Raphael Sebbe on 01/11/10.
6 // Copyright (c) 2010 Creaceed. All rights reserved.
7 //
8
9 #import "GLFramebuffer.h"
10 #import "GLRenderbuffer.h"
11 #import "GLTexture.h"
12
13 @implementation GLFramebuffer
14
15 - (id)init {
16 if ((self = [super init])) {
17 // Initialization code here.
18 mAttachments = [NSMutableDictionary new];
19 }
20
21 return self;
22 }
23
24 - (void)dealloc {
25 // Clean-up code here.
26
27 [mAttachments release];
28
29 [super dealloc];
30 }
31
32 + (GLFramebuffer*)framebuffer
33 {
34 return [[[self alloc] init] autorelease];
35 }
36
37 #pragma mark -
38 #pragma mark Handle creation/destruction
39 - (void)createHandle
40 {
41 GL_EXCEPT(mHandle, @"Trying to create a new handle over an existing one");
42
43 glGenFramebuffers(1, &mHandle);
44
45 GL_EXCEPT(!mHandle, @"Could not create handle (no current GL context?)");
46 }
47 - (void)destroyHandle
48 {
49 GL_EXCEPT(!mHandle, @"Trying to destroy a NULL handle");
50
51 glDeleteFramebuffers(1, &mHandle);
52 mHandle = 0;
53 }
54
55 #pragma mark -
56 #pragma mark Attachments
57 - (void)attachRenderBuffer:(GLRenderbuffer*)rbuffer toPoint:(GLenum)attachmentPoint
58 {
59 GL_EXCEPT(rbuffer == nil, NSInvalidArgumentException);
60 GL_EXCEPT(rbuffer.handle == 0, NSInvalidArgumentException);
61 GL_EXCEPT([mAttachments objectForKey:[NSNumber numberWithLong:attachmentPoint]],
62 @"Existing attachment");
63
64 if(mHandle == 0)
65 [self createHandle];
66 [mAttachments setObject:rbuffer forKey:[NSNumber numberWithLong:attachmentPoint]];
67
68 glBindFramebuffer(GL_FRAMEBUFFER, mHandle);
69 //glBindRenderbuffer(GL_RENDERBUFFER, rbuffer.handle);
70 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, rbuffer.handle);
71 glBindFramebuffer(GL_FRAMEBUFFER, 0);
72 }
73 - (void)attachTexture:(GLTexture*)texture toPoint:(GLenum)attachmentPoint target:(GLenum)textureTarget level:(GLint)level
74 {
75 GL_EXCEPT(texture == nil, NSInvalidArgumentException);
76 GL_EXCEPT(texture.handle == 0, NSInvalidArgumentException);
77 GL_EXCEPT([mAttachments objectForKey:[NSNumber numberWithLong:attachmentPoint]],
78 @"Existing attachment");
79
80 if(mHandle == 0)
81 [self createHandle];
82 [mAttachments setObject:texture forKey:[NSNumber numberWithLong:attachmentPoint]];
83
84 glBindFramebuffer(GL_FRAMEBUFFER, mHandle);
85 //glBindRenderbuffer(GL_RENDERBUFFER, rbuffer.handle);
86 glFramebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, textureTarget, texture.handle, level);
87 glBindFramebuffer(GL_FRAMEBUFFER, 0);
88 }
89 - (id)attachmentToPoint:(GLenum)attachmentPoint
90 {
91 return [mAttachments objectForKey:[NSNumber numberWithLong:attachmentPoint]];
92 }
93 - (void)detach:(GLenum)attachmentPoint
94 {
95 GL_EXCEPT(mHandle == 0, @"Receiver in invalid state");
96
97 id attachment = [self attachmentToPoint:attachmentPoint];
98
99 if(attachment == nil)
100 {
101 GLLog(@"warning: no recorded attachment");
102 return;
103 }
104
105 if([attachment isKindOfClass:[GLTexture class]])
106 {
107 glBindFramebuffer(GL_FRAMEBUFFER, mHandle);
108 glFramebufferTexture2D(GL_FRAMEBUFFER, attachmentPoint, 0, 0, 0);
109 glBindFramebuffer(GL_FRAMEBUFFER, 0);
110 [mAttachments removeObjectForKey:[NSNumber numberWithLong:attachmentPoint]];
111 }
112 else if([attachment isKindOfClass:[GLRenderbuffer class]])
113 {
114 glBindFramebuffer(GL_FRAMEBUFFER, mHandle);
115 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachmentPoint, GL_RENDERBUFFER, 0);
116 glBindFramebuffer(GL_FRAMEBUFFER, 0);
117 [mAttachments removeObjectForKey:[NSNumber numberWithLong:attachmentPoint]];
118 }
119 }
120 - (GLenum)checkStatus
121 {
122 GL_EXCEPT(mHandle == 0, NSInvalidArgumentException);
123
124 glBindFramebuffer(GL_FRAMEBUFFER, mHandle);
125 return glCheckFramebufferStatus(GL_FRAMEBUFFER);
126 }
127 - (BOOL)checkCompleteness
128 {
129 return [self checkStatus] == GL_FRAMEBUFFER_COMPLETE;
130 }
131
132 - (void)bind
133 {
134 GL_EXCEPT(mHandle == 0, @"Trying to bind non-existing buffer");
135 glBindFramebuffer(GL_FRAMEBUFFER, mHandle);
136 }
137
138 @end
Something went wrong with that request. Please try again.