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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/**********************************************************************\
* AUTHOR : HILLAIRE S�bastien
*
* MAIL : [email protected]
* SITE : sebastien.hillaire.free.fr
*
* You are free to totally or partially use this file/code.
* If you do, please credit me in your software or demo and leave this
* note.
* Share your work and your ideas as much as possible!
\*********************************************************************/
#include "FrameBufferObjectMRT.h"
//#include "ImageBMP.h"
bool CheckFramebufferStatus(const GLuint fbo)
{
GLenum status;
bool ret = false;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
status = (GLenum) glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch(status) {
case GL_FRAMEBUFFER_COMPLETE_EXT:
//printf("Frame Buffer Created\n");
ret = true;
break;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
printf("Unsupported framebuffer format\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
printf("Framebuffer incomplete, missing attachment\n");
break;
//case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
// printf("Framebuffer incomplete, duplicate attachment\n");
// break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
printf("Framebuffer incomplete, attached images must have same dimensions\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
printf("Framebuffer incomplete, attached images must have same format\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
printf("Framebuffer incomplete, missing draw buffer\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
printf("Framebuffer incomplete, missing read buffer\n");
break;
default:
printf("Framebuffer incomplete, UNKNOW ERROR\n");
assert(0);
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
return ret;
}
//////////////////////////////////////////////////////////////////////////////////
FrameBufferObjectMRT::FrameBufferObjectMRT(unsigned int width, unsigned int height,
unsigned int nbColorBuffer,
const unsigned int* colorBufferInternalFormat,
const unsigned int* colorBufferSWRAP,
const unsigned int* colorBufferTWRAP,
const unsigned int* colorBufferMinFiltering,
const unsigned int* colorBufferMagFiltering,
FBO_DepthBufferType depthBufferType,
const unsigned int depthBufferMinFiltering,
const unsigned int depthBufferMagFiltering,
const unsigned int depthBufferSWRAP,
const unsigned int depthBufferTWRAP)
{
this->width = width;
this->height= height;
this->nbColorAttachement = nbColorBuffer;
if(this->nbColorAttachement>this->getMaxColorAttachments())
this->nbColorAttachement = this->getMaxColorAttachments();
/////////////////INITIALIZATION/////////////////
//color render buffer
if(this->nbColorAttachement>0)
{
this->colorTextures = new GLuint[this->nbColorAttachement];
this->colorMinificationFiltering = new GLuint[this->nbColorAttachement];
for(int i=0; i<this->nbColorAttachement; i++)
{
glGenTextures(1, &this->colorTextures[i]);
glBindTexture(GL_TEXTURE_2D, this->colorTextures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, colorBufferMinFiltering[i]);
this->colorMinificationFiltering[i] = colorBufferMinFiltering[i];
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, colorBufferMagFiltering[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, colorBufferSWRAP[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, colorBufferTWRAP[i]);
glTexImage2D(GL_TEXTURE_2D, 0, colorBufferInternalFormat[i], width, height, 0, GL_RGB, GL_FLOAT, NULL);
if(this->colorMinificationFiltering[i]==GL_NEAREST_MIPMAP_NEAREST
|| this->colorMinificationFiltering[i]==GL_LINEAR_MIPMAP_NEAREST
||this->colorMinificationFiltering[i]==GL_NEAREST_MIPMAP_LINEAR
|| this->colorMinificationFiltering[i]==GL_LINEAR_MIPMAP_LINEAR)
{
glGenerateMipmapEXT(GL_TEXTURE_2D);
}
}
}
//depth render buffer
this->depthType = depthBufferType;
if(this->depthType!=FBO_DepthBufferType_NONE)
{
switch(this->depthType)
{
case FBO_DepthBufferType_TEXTURE:
glGenTextures(1, &this->depthID);
glBindTexture(GL_TEXTURE_2D, this->depthID);
this->depthMinificationFiltering = depthBufferMinFiltering;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, depthBufferMinFiltering);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, depthBufferMagFiltering);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, depthBufferSWRAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, depthBufferTWRAP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24_ARB, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
if(this->depthMinificationFiltering==GL_NEAREST_MIPMAP_NEAREST
|| this->depthMinificationFiltering==GL_LINEAR_MIPMAP_NEAREST
||this->depthMinificationFiltering==GL_NEAREST_MIPMAP_LINEAR
|| this->depthMinificationFiltering==GL_LINEAR_MIPMAP_LINEAR)
{
glGenerateMipmapEXT(GL_TEXTURE_2D);
}
break;
case FBO_DepthBufferType_RENDERTARGET:
default:
glGenRenderbuffersEXT(1, &this->depthID);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, this->depthID);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24_ARB, width, height);
break;
}
}
/////////////////ATTACHEMENT/////////////////
glGenFramebuffersEXT(1, &this->fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,this->fbo);
//color render buffer attachement
if(nbColorBuffer>0)
{
for(int i=0; i<this->nbColorAttachement; i++)
{
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_TEXTURE_2D, this->colorTextures[i], 0 );
}
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
}
else
{
glReadBuffer(GL_NONE);
glDrawBuffer(GL_NONE);
}
//depth render buffer attachement
if(this->depthType!=FBO_DepthBufferType_NONE)
{
switch(this->depthType)
{
case FBO_DepthBufferType_TEXTURE:
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, this->depthID, 0);
break;
case FBO_DepthBufferType_RENDERTARGET:
default:
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, this->depthID);
break;
}
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
CheckFramebufferStatus(this->fbo);
}
FrameBufferObjectMRT::~FrameBufferObjectMRT()
{
if(this->nbColorAttachement>0)
{
delete [] this->colorTextures;
delete [] this->colorMinificationFiltering;
}
}
//////////////////////////////////////////////////////////////////////////////////
const GLint FrameBufferObjectMRT::getMaxColorAttachments()
{
GLint maxAttach = 0;
glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS_EXT, &maxAttach );
return maxAttach;
}
const GLint FrameBufferObjectMRT::getMaxBufferSize()
{
GLint maxSize = 0;
glGetIntegerv( GL_MAX_RENDERBUFFER_SIZE_EXT, &maxSize );
return maxSize;
}
void FrameBufferObjectMRT::enableRenderToColorAndDepth(const unsigned int colorBufferNum) const
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->fbo);
if(this->nbColorAttachement>0)
{
unsigned int colorBuffer = GL_COLOR_ATTACHMENT0_EXT + colorBufferNum;
if((int)colorBufferNum>this->nbColorAttachement)
colorBuffer = GL_COLOR_ATTACHMENT0_EXT;
glDrawBuffer(colorBuffer);
}
else
{
glDrawBuffer(GL_NONE); //for example, in the case of rendering in a depth buffer
}
}
void FrameBufferObjectMRT::enableRenderToColorAndDepth_MRT(const GLuint numBuffers, const GLenum* drawbuffers) const
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->fbo);
//HERE, THERE SHOULD HAVE A TEST OF THE DRAW BUFFERS ID AND NUMBER
/*for(GLuint i=0; i<numBuffers; i++)
{
}*/
glDrawBuffers(numBuffers, drawbuffers);
}
void FrameBufferObjectMRT::disableRenderToColorDepth()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void FrameBufferObjectMRT::saveAndSetViewPort() const
{
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, this->width, this->height);
}
void FrameBufferObjectMRT::restoreViewPort() const
{
glPopAttrib();
}
void FrameBufferObjectMRT::bindColorTexture(const unsigned int colorBufferNum) const
{
if(this->nbColorAttachement>0 && (int)colorBufferNum<this->nbColorAttachement)
{
glBindTexture(GL_TEXTURE_2D, this->colorTextures[colorBufferNum]);
}
else
glBindTexture(GL_TEXTURE_2D, 0);
}
void FrameBufferObjectMRT::bindDepthTexture() const
{
if(this->depthType==FBO_DepthBufferType_TEXTURE)
{
glBindTexture(GL_TEXTURE_2D, this->depthID);
}
else
glBindTexture(GL_TEXTURE_2D, 0);
}
void FrameBufferObjectMRT::generateColorBufferMipMap(const unsigned int colorBufferNum) const
{
if(this->nbColorAttachement>0 && (int)colorBufferNum<this->nbColorAttachement)
{
if(this->colorMinificationFiltering[colorBufferNum]==GL_NEAREST
|| this->colorMinificationFiltering[colorBufferNum]==GL_LINEAR)
return; //don't allow to generate mipmap chain for texture that don't support it at the creation
glBindTexture(GL_TEXTURE_2D, this->colorTextures[colorBufferNum]);
glGenerateMipmapEXT(GL_TEXTURE_2D);
}
}
void FrameBufferObjectMRT::generateDepthBufferMipMap() const
{
if(this->depthType==FBO_DepthBufferType_TEXTURE)
{
if(this->depthMinificationFiltering==GL_NEAREST
|| this->depthMinificationFiltering==GL_LINEAR)
return; //don't allow to generate mipmap chain for texture that don't support it at the creation
glBindTexture(GL_TEXTURE_2D, this->depthID);
glGenerateMipmapEXT(GL_TEXTURE_2D);
}
}
unsigned int FrameBufferObjectMRT::getNumberOfColorAttachement() const
{
return this->nbColorAttachement;
}
FBO_DepthBufferType FrameBufferObjectMRT::getDepthBufferType() const
{
return this->depthType;
}
unsigned int FrameBufferObjectMRT::getWidth() const
{
return this->width;
}
unsigned int FrameBufferObjectMRT::getHeight() const
{
return this->height;
}
/*
void FrameBufferObjectMRT::saveToDisk(const unsigned int colorBufferNum, const char* filepath) const
{
if(this->nbColorAttachement>0 && (int)colorBufferNum<this->nbColorAttachement)
{
ImageBMP::TextureToDisk(filepath,GL_TEXTURE_2D,this->colorTextures[colorBufferNum]);
}
}*/
|