Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
FramebufferObject.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef FramebufferObject_INCLUDE_ONCE
33 #define FramebufferObject_INCLUDE_ONCE
34 
35 #include <vlGraphics/Camera.hpp>
36 #include <vlGraphics/Texture.hpp>
37 #include <set>
38 #include <map>
39 
40 namespace vl
41 {
42  class FramebufferObject;
43 //-----------------------------------------------------------------------------
44 // FBOAbstractAttachment
45 //-----------------------------------------------------------------------------
50  {
52 
53  friend class FramebufferObject;
54 
55  private:
56  // no copy constructor and no assignment operator
57  FBOAbstractAttachment( const FBOAbstractAttachment& other ): Object( other ) {}
58  void operator=( const FBOAbstractAttachment& ) {}
59 
60  public:
63 
65  virtual ~FBOAbstractAttachment() { VL_CHECK(fboFramebuffers().size() == 0); }
66 
68  virtual void unbindFromAllFBO();
69 
71  const std::set< ref<FramebufferObject> >& fboFramebuffers() const { return mFramebufferObjects; }
72 
73  protected:
74  virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point ) = 0;
75 
76  protected:
77  std::set< ref<FramebufferObject> > mFramebufferObjects;
78  };
79  //-----------------------------------------------------------------------------
80  // FBORenderbufferAttachment
81  //-----------------------------------------------------------------------------
88  {
90 
91  friend class FramebufferObject;
92 
93  public:
95  FBORenderbufferAttachment(): mHandle( 0 ), mWidth( 0 ), mHeight( 0 ), mSamples( 0 ), mReallocateRenderbuffer( true ) {}
96 
98  ~FBORenderbufferAttachment() { deleteRenderBuffer(); }
99 
104  void createRenderBuffer();
105 
107  void deleteRenderBuffer();
108 
113  void setHandle( GLuint handle ) { if ( mHandle != handle ) { mHandle = handle; mReallocateRenderbuffer = false; } }
114 
116  GLuint handle() const { return mHandle; }
117 
125  void initStorage( int w, int h, int samples );
126 
128  void initStorage() { initStorage( width(), height(), samples() ); }
129 
135  int width() const { return mWidth; }
136 
142  int height() const { return mHeight; }
143 
149  int samples() const { return mSamples; }
150 
155  void setWidth( int w ) { if ( w != mWidth ) /* schedules recreation */ mReallocateRenderbuffer = true; mWidth = w; }
156 
161  void setHeight( int h ) { if ( h != mHeight ) /* schedules recreation */ mReallocateRenderbuffer = true; mHeight = h; }
162 
166  void setSamples( int samples ) { if ( samples != mSamples ) /* schedules recreation */ mReallocateRenderbuffer = true; mSamples = samples; }
167 
169  bool renderbufferStorageReady() const { return !mReallocateRenderbuffer; }
170 
171  protected:
172  void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
173  virtual int internalType() = 0;
174 
175  protected:
176  GLuint mHandle;
177  int mWidth;
178  int mHeight;
179  int mSamples;
181  };
182  //-----------------------------------------------------------------------------
183  // FBOColorBufferAttachment
184  //-----------------------------------------------------------------------------
189  {
191 
192  public:
195  {
196  VL_DEBUG_SET_OBJECT_NAME()
197  mType = type;
198  }
199 
201  void setType( EColorBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
202 
204  EColorBufferFormat type() const { return mType; }
205 
206  protected:
207  virtual int internalType() { return type(); }
208 
209  protected:
211  };
212  //-----------------------------------------------------------------------------
213  // FBODepthBufferAttachment
214  //-----------------------------------------------------------------------------
219  {
221 
222  public:
225  {
226  VL_DEBUG_SET_OBJECT_NAME()
227  mType = type;
228  }
229 
231  void setType( EDepthBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
232 
234  EDepthBufferFormat type() const { return mType; }
235 
236  protected:
237  virtual int internalType() { return type(); }
238 
239  protected:
241  };
242  //-----------------------------------------------------------------------------
243  // FBOStencilBufferAttachment
244  //-----------------------------------------------------------------------------
249  {
251 
252  public:
255  {
256  VL_DEBUG_SET_OBJECT_NAME()
257  mType = type;
258  }
259 
261  void setType( EStencilBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
262 
264  EStencilBufferFormat type() const { return mType; }
265 
266  protected:
267  virtual int internalType() { return type(); }
268 
269  protected:
271  };
272  //-----------------------------------------------------------------------------
273  // FBODepthStencilBufferAttachment
274  //-----------------------------------------------------------------------------
279  {
281 
282  public:
285  {
286  VL_DEBUG_SET_OBJECT_NAME()
287  mType = type;
288  }
289 
291  void setType( EDepthStencilBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
292 
294  EDepthStencilBufferFormat type() const { return mType; }
295 
296  protected:
297  virtual int internalType() { return type(); }
298 
299  protected:
301  };
302  //-----------------------------------------------------------------------------
303  // FBOAbstractTextureAttachment
304  //-----------------------------------------------------------------------------
309  {
311 
312  public:
314  FBOAbstractTextureAttachment( Texture* texture, int mipmap_level ): mTexture(texture), mMipmapLevel(mipmap_level)
315  {
316  VL_DEBUG_SET_OBJECT_NAME()
317  }
318 
320  void setTexture( Texture* texture ) { mTexture = texture; }
321 
323  Texture* texture() { return mTexture.get(); }
324 
326  const Texture* texture() const { return mTexture.get(); }
327 
329  void setMipmapLevel( int mipmap_level ) { mMipmapLevel = mipmap_level; }
330 
332  int mipmapLevel() const { return mMipmapLevel; }
333 
334  protected:
337  };
338  //-----------------------------------------------------------------------------
339  // FBOTexture1DAttachment
340  //-----------------------------------------------------------------------------
346  {
348 
349  public:
352  {
353  VL_DEBUG_SET_OBJECT_NAME()
354  }
355 
357  FBOTexture1DAttachment( Texture* texture, int mipmap_level ): FBOAbstractTextureAttachment( texture, mipmap_level )
358  {
359  VL_DEBUG_SET_OBJECT_NAME()
360  }
361 
362  protected:
363  virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
364  };
365  //-----------------------------------------------------------------------------
366  // FBOTexture2DAttachment
367  //-----------------------------------------------------------------------------
373  {
375 
376  public:
379  {
380  VL_DEBUG_SET_OBJECT_NAME()
381  mTextureTarget = T2DT_TEXTURE_2D;
382  }
383 
385  FBOTexture2DAttachment( Texture* texture, int mipmap_level, ETex2DTarget target ): FBOAbstractTextureAttachment( texture, mipmap_level )
386  {
387  VL_DEBUG_SET_OBJECT_NAME()
388  mTextureTarget = target;
389  }
390 
392  void setTextureTarget( ETex2DTarget target ) { mTextureTarget = target; }
393 
395  ETex2DTarget textureTarget() const { return mTextureTarget; }
396 
397  protected:
398  virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
399 
400  protected:
402  };
403  //-----------------------------------------------------------------------------
404  // FBOTextureAttachment
405  //-----------------------------------------------------------------------------
411  {
413 
414  public:
417  {
418  VL_DEBUG_SET_OBJECT_NAME()
419  }
420 
422  FBOTextureAttachment( Texture* texture, int mipmap_level ): FBOAbstractTextureAttachment( texture, mipmap_level )
423  {
424  VL_DEBUG_SET_OBJECT_NAME()
425  }
426 
427  protected:
428  virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
429 
430  };
431  //-----------------------------------------------------------------------------
432  // FBOTexture3DAttachment
433  //-----------------------------------------------------------------------------
439  {
441 
442  public:
444  {
445  VL_DEBUG_SET_OBJECT_NAME()
446  mLayer = 0;
447  }
448 
449  FBOTexture3DAttachment( Texture* texture, int mipmap_level, int layer ): FBOAbstractTextureAttachment( texture, mipmap_level )
450  {
451  VL_DEBUG_SET_OBJECT_NAME()
452  mLayer = layer;
453  }
454 
456  void setLayer( int layer ) { mLayer = layer; }
457 
459  int layer() const { return mLayer; }
460 
461  protected:
462  virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
463 
464  protected:
465  int mLayer;
466  };
467  //-----------------------------------------------------------------------------
468  // FBOTextureLayerAttachment
469  //-----------------------------------------------------------------------------
475  {
477 
478  public:
481  {
482  VL_DEBUG_SET_OBJECT_NAME()
483  mLayer = 0;
484  }
485 
487  FBOTextureLayerAttachment( Texture* texture, int mipmap_level, int layer ): FBOAbstractTextureAttachment( texture, mipmap_level )
488  {
489  VL_DEBUG_SET_OBJECT_NAME()
490  mLayer = layer;
491  }
492 
494  int layer() const { return mLayer; }
495 
497  void setLayer( int layer ) { mLayer = layer; }
498 
499  protected:
500  virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
501 
502  protected:
505  int mLayer;
506  };
507  //-----------------------------------------------------------------------------
508  // FramebufferObject
509  //-----------------------------------------------------------------------------
539  {
541 
542  friend class OpenGLContext;
543 
544  private:
545  FramebufferObject( const FramebufferObject& other ): Framebuffer( other ), mHandle( 0 ) {}
546 
547  void operator=( const FramebufferObject& ) {}
548 
551  {
552  VL_DEBUG_SET_OBJECT_NAME()
553  }
554 
555  FramebufferObject( OpenGLContext* ctx, int w, int h,
558  Framebuffer( ctx, w, h, draw_buffer, read_buffer ), mHandle( 0 )
559  {
560  VL_DEBUG_SET_OBJECT_NAME()
561  }
562 
563  public:
565  ~FramebufferObject() { if (openglContext()) deleteFBO(); }
566 
571  void createFBO();
572 
577  void deleteFBO();
578 
580  void setHandle( GLuint handle ) { mHandle = handle; }
581 
583  virtual GLuint handle() const { return mHandle; }
584 
590  virtual void bindFramebuffer( EFramebufferBind target = FBB_FRAMEBUFFER );
591 
593  GLenum checkFramebufferStatus();
594 
596  void printFramebufferError( GLenum status ) const;
597 
599  void addColorAttachment( EAttachmentPoint attach_point, FBOColorBufferAttachment* attachment );
600 
602  void addTextureAttachment( EAttachmentPoint attach_point, FBOAbstractTextureAttachment* attachment );
603 
608  void addDepthAttachment( FBOAbstractAttachment* attachment );
609 
614  void addStencilAttachment( FBOAbstractAttachment* attachment );
615 
617  void addDepthStencilAttachment( FBOAbstractAttachment* attachment );
618 
620  void removeAttachment( FBOAbstractAttachment* attachment );
621 
623  void removeAttachment( EAttachmentPoint attach_point );
624 
626  void removeAllAttachments();
627 
629  const std::map< EAttachmentPoint, ref<FBOAbstractAttachment> >& fboAttachments() const { return mFBOAttachments; }
630 
631  public:
632  std::map< EAttachmentPoint, ref<FBOAbstractAttachment> > mFBOAttachments;
633  GLuint mHandle;
634  };
635 }
636 
637 #endif
void setLayer(int layer)
The layer of a 2-dimensional image within a 3-dimensional texture or texture array.
A 2D texture renderbuffer to be attached to a FramebufferObject (wraps glFramebufferTexture2D()).
int height() const
Returns the height of the renderbuffer storage.
void setHeight(int h)
The height of the renderbuffer storage to be allocated.
EAttachmentPoint
Abstract class that represents a framebuffer renderbuffer attachment, that is, a non-texture fbo atta...
int mipmapLevel() const
The mipmap level of the texture to attach.
const Texture * texture() const
The texture bound to this attachment.
FBOTextureLayerAttachment(Texture *texture, int mipmap_level, int layer)
Constructor.
GLuint handle() const
Returns the handle obtained by createRenderBuffer() using glGenRenderbuffers()
~FramebufferObject()
Destructor.
EDepthBufferFormat type() const
The type to specify when allocating the renderbuffer storage.
EColorBufferFormat
Color-renderable formats as defined in section 4.4.4 of opengl api specs 4.1.
void setType(EDepthStencilBufferFormat type)
The type to specify when allocating the renderbuffer storage.
A 3D texture renderbuffer to be attached to a FramebufferObject (wraps glFramebufferTexture3D()).
Abstract class that represents a framebuffer object attachment to be used with FramebufferObject.
EStencilBufferFormat
A depth+stencil renderbuffer to be attached to a FramebufferObject.
Base class for all the framebuffer texture attachments (see also FramebufferObject).
FBOTexture1DAttachment(Texture *texture, int mipmap_level)
Constructor.
EDepthStencilBufferFormat
std::map< EAttachmentPoint, ref< FBOAbstractAttachment > > mFBOAttachments
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
A 1D texture renderbuffer to be attached to a FramebufferObject (wraps glFramebufferTexture1D()).
int layer() const
The layer of a 2-dimensional image within a 3-dimensional texture or texture array.
void setType(EDepthBufferFormat type)
The type to specify when allocating the renderbuffer storage.
Wraps an OpenGL texture object representing and managing all the supported texture types...
Definition: Texture.hpp:143
FBODepthBufferAttachment(EDepthBufferFormat type=DBF_DEPTH_COMPONENT)
Constructor.
EDepthBufferFormat
const std::map< EAttachmentPoint, ref< FBOAbstractAttachment > > & fboAttachments() const
A map associating which fbo-attachment belongs to which attachment point in a framebuffer object...
FBOColorBufferAttachment(EColorBufferFormat type=CBF_RGBA)
Constructor.
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
FBOAbstractAttachment()
Constructor.
FBODepthStencilBufferAttachment(EDepthStencilBufferFormat type=DSBT_DEPTH_STENCIL)
Constructor.
virtual ~FBOAbstractAttachment()
Destructor.
int layer() const
The layer of a 2-dimensional image within a 3-dimensional texture.
ETex2DTarget
ETex2DTarget textureTarget() const
What type of texture is expected, or for cube map textures, which face is to be attached.
EReadDrawBuffer
Visualization Library main namespace.
FBOTexture2DAttachment(Texture *texture, int mipmap_level, ETex2DTarget target)
Constructor.
EStencilBufferFormat type() const
The type to specify when allocating the renderbuffer storage.
void setType(EColorBufferFormat type)
The type to specify when allocating the renderbuffer storage.
FBOTexture3DAttachment(Texture *texture, int mipmap_level, int layer)
FBOTextureAttachment(Texture *texture, int mipmap_level)
Constructor.
void setHandle(GLuint handle)
Sets the handle for this attachment, the handle must have been created by glGenRenderbuffers().
void setSamples(int samples)
Sets the number of samples to be specified when allocating the renderbuffer&#39;s storage.
The base class for all the reference counted objects.
Definition: Object.hpp:158
A color renderbuffer to be attached to a FramebufferObject.
A texture renderbuffer to be attached to a FramebufferObject (wraps glFramebufferTexture()).
A depth renderbuffer to be attached to a FramebufferObject.
void initStorage()
The same as calling initStorage( width(), height() )
#define NULL
Definition: OpenGLDefs.hpp:81
void setMipmapLevel(int mipmap_level)
The mipmap level of the texture to attach.
FBOAbstractTextureAttachment(Texture *texture, int mipmap_level)
Constructor.
EFramebufferBind
int samples() const
Returns the number of samples to be used when allocating the renderbuffer&#39;s storage.
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
A texture layer renderbuffer to be attached to a FramebufferObject (wraps glFramebufferTextureLayer()...
void setType(EStencilBufferFormat type)
The type to specify when allocating the renderbuffer storage.
Implements a framebuffer object to be used as a rendering target as specified by the ARB_framebuffer_...
void setTextureTarget(ETex2DTarget target)
What type of texture is expected, or for cube map textures, which face is to be attached.
std::set< ref< FramebufferObject > > mFramebufferObjects
int width() const
Returns the with of the renderbuffer storage.
void setLayer(int layer)
The layer of a 2-dimensional image within a 3-dimensional texture.
bool renderbufferStorageReady() const
Returns false if the renderbuffer storage needs to be created or reallocated due to a change of the s...
void setWidth(int w)
The width of the renderbuffer storage to be allocated.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
Texture * texture()
The texture bound to this attachment.
EColorBufferFormat type() const
The type to specify when allocating the renderbuffer storage.
#define VL_CHECK(expr)
Definition: checks.hpp:73
EDepthStencilBufferFormat type() const
The type to specify when allocating the renderbuffer storage.
The Framebuffer class defines an abstract &#39;surface&#39; where OpenGL can render into. ...
Definition: Framebuffer.hpp:49
const std::set< ref< FramebufferObject > > & fboFramebuffers() const
Returns an std::set containing the FramebufferObject that use this FBO attachment.
void setHandle(GLuint handle)
The handle of the framebuffer object as returned by glGenFramebuffers.
virtual GLuint handle() const
The handle of the framebuffer object as returned by glGenFramebuffers.
void setTexture(Texture *texture)
The texture bound to this attachment.
A stencil renderbuffer to be attached to a FramebufferObject.
FBOStencilBufferAttachment(EStencilBufferFormat type=SBF_STENCIL_INDEX8)
Constructor.