aboutsummaryrefslogtreecommitdiff
path: root/KaplaDemo/samples/sampleViewer3/FrameBufferObject.cpp
blob: 103c9c5a8d2edc8da81498f0b85633010302410d (plain) (blame)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//glew uses iostreams which uses exceptions, so we need to turn off the following warning
//warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
#pragma warning( disable : 4530 )

/*
  Copyright (c) 2005, 
      Aaron Lefohn    ([email protected])
      Robert Strzodka ([email protected])
      Adam Moerschell ([email protected])
  All rights reserved.

  This software is licensed under the BSD open-source license. See
  http://www.opensource.org/licenses/bsd-license.php for more detail.

  *************************************************************
  Redistribution and use in source and binary forms, with or 
  without modification, are permitted provided that the following 
  conditions are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer. 

  Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution. 

  Neither the name of the University of Californa, Davis nor the names of 
  the contributors may be used to endorse or promote products derived 
  from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
  OF SUCH DAMAGE.
*/

#include "FrameBufferObject.h"
#include <iostream>
using namespace std;

#define LOG_FBO_CALLS 0

FrameBufferObject::FrameBufferObject()
  : m_fboId(_GenerateFboId()),
    m_savedFboId(0)
{
  // Bind this FBO so that it actually gets created now
  _GuardedBind();
  _GuardedUnbind();
}

FrameBufferObject::~FrameBufferObject() 
{
#if LOG_FBO_CALLS
  printf("deleting FBO %d\n",m_fboId);
#endif

  glDeleteFramebuffersEXT(1, &m_fboId);
}

void FrameBufferObject::Bind() 
{
#if LOG_FBO_CALLS
  printf("binding framebuffer %d\n",m_fboId);
#endif
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fboId);
}

void FrameBufferObject::Disable() 
{
#if LOG_FBO_CALLS
  printf("binding window framebuffer \n");
#endif
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

void
FrameBufferObject::AttachTexture( GLenum texTarget, GLuint texId, 
                                  GLenum attachment, int mipLevel, int zSlice )
{
  _GuardedBind();

  /*
#ifndef NDEBUG
  if( GetAttachedId(attachment) != texId ) {
#endif
  */

    _FramebufferTextureND( attachment, texTarget,
                           texId, mipLevel, zSlice );

/*
#ifndef NDEBUG
  }
  else {
    cerr << "FrameBufferObject::AttachTexture PERFORMANCE WARNING:\n"
      << "\tRedundant bind of texture (id = " << texId << ").\n"
      << "\tHINT : Compile with -DNDEBUG to remove this warning.\n";
  }
#endif
*/

  _GuardedUnbind();
}

void
FrameBufferObject::AttachTextures( int numTextures, GLenum texTarget[], GLuint texId[],
                                  GLenum attachment[], int mipLevel[], int zSlice[] )
{
  for(int i = 0; i < numTextures; ++i) {
    AttachTexture( texTarget[i], texId[i], 
                   attachment ? attachment[i] : (GL_COLOR_ATTACHMENT0_EXT + i), 
                   mipLevel ? mipLevel[i] : 0, 
                   zSlice ? zSlice[i] : 0 );
  }
}

void
FrameBufferObject::AttachRenderBuffer( GLuint buffId, GLenum attachment )
{
  _GuardedBind();

#ifndef NDEBUG
  if( GetAttachedId(attachment) != buffId ) {
#endif

#if LOG_FBO_CALLS
	printf("fbo %d: attaching renderbuffer to attachment point %d\n",m_fboId,attachment);
#endif

    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attachment, 
                                 GL_RENDERBUFFER_EXT, buffId);

#ifndef NDEBUG
  }
  else {
    cerr << "FrameBufferObject::AttachRenderBuffer PERFORMANCE WARNING:\n"
      << "\tRedundant bind of Renderbuffer (id = " << buffId << ")\n"
      << "\tHINT : Compile with -DNDEBUG to remove this warning.\n";
  }
#endif

  _GuardedUnbind();
}

void
FrameBufferObject::AttachRenderBuffers( int numBuffers, GLuint buffId[], GLenum attachment[] )
{
  for(int i = 0; i < numBuffers; ++i) {
    AttachRenderBuffer( buffId[i], 
                        attachment ? attachment[i] : (GL_COLOR_ATTACHMENT0_EXT + i) );
  }
}

void
FrameBufferObject::Unattach( GLenum attachment )
{
  _GuardedBind();
  GLenum type = GetAttachedType(attachment);

#if LOG_FBO_CALLS
  printf("fbo %d: detatching from attachment point %d\n",m_fboId,attachment);
#endif

  switch(type) {
  case GL_NONE:
    break;
  case GL_RENDERBUFFER_EXT:
    AttachRenderBuffer( 0, attachment );
    break;
  case GL_TEXTURE:
    AttachTexture( GL_TEXTURE_2D, 0, attachment );
    break;
  default:
    cerr << "FrameBufferObject::unbind_attachment ERROR: Unknown attached resource type\n";
  }
  _GuardedUnbind();
}

void
FrameBufferObject::UnattachAll()
{
  int numAttachments = GetMaxColorAttachments();
  for(int i = 0; i < numAttachments; ++i) {
    Unattach( GL_COLOR_ATTACHMENT0_EXT + i );
  }
}

GLint FrameBufferObject::GetMaxColorAttachments()
{
  GLint maxAttach = 0;
  glGetIntegerv( GL_MAX_COLOR_ATTACHMENTS_EXT, &maxAttach );
  return maxAttach;
}

GLuint FrameBufferObject::_GenerateFboId()
{
  GLuint id = 0;
  glGenFramebuffersEXT(1, &id);

#if LOG_FBO_CALLS
  printf("created FBO %d\n",id);
#endif

  return id;
}

void FrameBufferObject::_GuardedBind() 
{
  // Only binds if m_fboId is different than the currently bound FBO
  glGetIntegerv( GL_FRAMEBUFFER_BINDING_EXT, &m_savedFboId );
  if (m_fboId != (GLuint)m_savedFboId) {

#if LOG_FBO_CALLS
    printf("binding framebuffer %d\n",m_fboId);
#endif
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fboId);
  }
}

void FrameBufferObject::_GuardedUnbind() 
{
  // Returns FBO binding to the previously enabled FBO
  if (m_fboId != (GLuint)m_savedFboId) {
#if LOG_FBO_CALLS
    printf("binding framebuffer %d\n",m_savedFboId);
#endif
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint)m_savedFboId);
  }
}

void
FrameBufferObject::_FramebufferTextureND( GLenum attachment, GLenum texTarget,
                                         GLuint texId, int mipLevel,
                                         int zSlice )
{
#if LOG_FBO_CALLS
	printf("fbo %d: attaching texture %d to attachment point %d\n",m_fboId,texId,attachment);
#endif

  if (texTarget == GL_TEXTURE_1D) {
    glFramebufferTexture1DEXT( GL_FRAMEBUFFER_EXT, attachment,
                               GL_TEXTURE_1D, texId, mipLevel );
  }
  else if (texTarget == GL_TEXTURE_3D) {
    glFramebufferTexture3DEXT( GL_FRAMEBUFFER_EXT, attachment,
                               GL_TEXTURE_3D, texId, mipLevel, zSlice );
  }
  else {
    // Default is GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB, or cube faces
    glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, attachment,
                               texTarget, texId, mipLevel );
  }
}

#ifndef NDEBUG
bool FrameBufferObject::IsValid( ostream& ostr )
{
  _GuardedBind();

#if LOG_FBO_CALLS
  printf("fbo %d: checking for FBO errors\n",m_fboId);
#endif

  bool isOK = false;

  GLenum status;                                            
  status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  switch(status) {                                          
  case GL_FRAMEBUFFER_COMPLETE_EXT: // Everything's OK
    isOK = true;
    break;
  case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT\n";
    isOK = false;
    break;
  case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT\n";
    isOK = false;
    break;
  case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT\n";
    isOK = false;
    break;
  case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT\n";
    isOK = false;
    break;
  case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT\n";
    isOK = false;
    break;
  case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT\n";
    isOK = false;
    break;
  case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "GL_FRAMEBUFFER_UNSUPPORTED_EXT\n";
    isOK = false;
    break;
  default:
    ostr << "glift::CheckFramebufferStatus() ERROR:\n\t"
      << "Unknown ERROR\n";
    isOK = false;
  }

  _GuardedUnbind();
  return isOK;
}
#endif // NDEBUG

/// Accessors
GLenum FrameBufferObject::GetAttachedType( GLenum attachment )
{
#if LOG_FBO_CALLS
  printf("querying type of attachment at %d\n",attachment);
#endif

  // Returns GL_RENDERBUFFER_EXT or GL_TEXTURE
  _GuardedBind();
  GLint type = 0;
  glGetFramebufferAttachmentParameterivEXT(GL_FRAMEBUFFER_EXT, attachment,
                                           GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT, 
                                           &type);
  _GuardedUnbind();
  return GLenum(type);
}

GLuint FrameBufferObject::GetAttachedId( GLenum attachment )
{
#if LOG_FBO_CALLS
  printf("getting id of attachment at %d\n",attachment);
#endif

  _GuardedBind();
  GLint id = 0;
  glGetFramebufferAttachmentParameterivEXT(GL_FRAMEBUFFER_EXT, attachment,
                                           GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT,
                                           &id);
  _GuardedUnbind();
  return GLuint(id);
}

GLint FrameBufferObject::GetAttachedMipLevel( GLenum attachment )
{
#if LOG_FBO_CALLS
  printf("getting mip level of attachment at %d\n",attachment);
#endif

  _GuardedBind();
  GLint level = 0;
  glGetFramebufferAttachmentParameterivEXT(GL_FRAMEBUFFER_EXT, attachment,
                                           GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT, 
                                           &level);
  _GuardedUnbind();
  return level;
}

GLint FrameBufferObject::GetAttachedCubeFace( GLenum attachment )
{
#if LOG_FBO_CALLS
  printf("getting cube face id of attachment at %d\n",attachment);
#endif

  _GuardedBind();
  GLint level = 0;
  glGetFramebufferAttachmentParameterivEXT(GL_FRAMEBUFFER_EXT, attachment,
                                           GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT,
                                           &level);
  _GuardedUnbind();
  return level;
}

GLint FrameBufferObject::GetAttachedZSlice( GLenum attachment )
{
#if LOG_FBO_CALLS
  printf("querying z slice of attachment at %d\n",attachment);
#endif
  _GuardedBind();
  GLint slice = 0;
  glGetFramebufferAttachmentParameterivEXT(GL_FRAMEBUFFER_EXT, attachment,
                                           GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT,
                                           &slice);
  _GuardedUnbind();
  return slice;
}