Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/FramebufferObject.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef FramebufferObject_INCLUDE_ONCE
00033 #define FramebufferObject_INCLUDE_ONCE
00034 
00035 #include <vlGraphics/Camera.hpp>
00036 #include <vlGraphics/Texture.hpp>
00037 #include <set>
00038 #include <map>
00039 
00040 namespace vl
00041 {
00042   class FramebufferObject;
00043 //-----------------------------------------------------------------------------
00044 // FBOAbstractAttachment
00045 //-----------------------------------------------------------------------------
00049   class VLGRAPHICS_EXPORT FBOAbstractAttachment: public Object
00050   {
00051     VL_INSTRUMENT_ABSTRACT_CLASS(vl::FBOAbstractAttachment, Object)
00052 
00053     friend class FramebufferObject;
00054 
00055   private:
00056     // no copy constructor and no assignment operator
00057     FBOAbstractAttachment( const FBOAbstractAttachment& other ): Object( other ) {}
00058     void operator=( const FBOAbstractAttachment& ) {}
00059 
00060   public:
00062     FBOAbstractAttachment() {}
00063 
00065     virtual ~FBOAbstractAttachment() { VL_CHECK(fboFramebuffers().size() == 0); }
00066 
00068     virtual void unbindFromAllFBO();
00069 
00071     const std::set< ref<FramebufferObject> >& fboFramebuffers() const { return mFramebufferObjects; }
00072 
00073   protected:
00074     virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point ) = 0;
00075 
00076   protected:
00077     std::set< ref<FramebufferObject> > mFramebufferObjects;
00078   };
00079   //-----------------------------------------------------------------------------
00080   // FBORenderbufferAttachment
00081   //-----------------------------------------------------------------------------
00087   class VLGRAPHICS_EXPORT FBORenderbufferAttachment: public FBOAbstractAttachment
00088   {
00089     VL_INSTRUMENT_ABSTRACT_CLASS(vl::FBORenderbufferAttachment, FBOAbstractAttachment)
00090 
00091     friend class FramebufferObject;
00092 
00093   public:    
00095     FBORenderbufferAttachment(): mHandle( 0 ), mWidth( 0 ), mHeight( 0 ), mSamples( 0 ), mReallocateRenderbuffer( true ) {}
00096 
00098     ~FBORenderbufferAttachment() { deleteRenderBuffer(); }
00099     
00104     void createRenderBuffer();
00105     
00107     void deleteRenderBuffer();
00108     
00113     void setHandle( GLuint  handle ) { if ( mHandle != handle ) { mHandle = handle; mReallocateRenderbuffer = false; } }
00114     
00116     GLuint handle() const { return mHandle; }
00117     
00125     void initStorage( int w, int h, int samples );
00126     
00128     void initStorage() { initStorage( width(), height(), samples() ); }
00129     
00135     int width() const { return mWidth; }
00136     
00142     int height() const { return mHeight; }
00143     
00149     int samples() const { return mSamples; }
00150     
00155     void setWidth( int w ) { if ( w != mWidth ) /* schedules recreation */ mReallocateRenderbuffer = true; mWidth = w; }
00156     
00161     void setHeight( int h ) { if ( h != mHeight ) /* schedules recreation */ mReallocateRenderbuffer = true; mHeight = h; }
00162     
00166     void setSamples( int samples ) { if ( samples != mSamples ) /* schedules recreation */ mReallocateRenderbuffer = true; mSamples = samples; }
00167     
00169     bool renderbufferStorageReady() const { return !mReallocateRenderbuffer; }
00170 
00171   protected:
00172     void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
00173     virtual int internalType() = 0;
00174 
00175   protected:
00176     GLuint mHandle;
00177     int mWidth;
00178     int mHeight;
00179     int mSamples;
00180     bool mReallocateRenderbuffer;
00181   };
00182   //-----------------------------------------------------------------------------
00183   // FBOColorBufferAttachment
00184   //-----------------------------------------------------------------------------
00188   class VLGRAPHICS_EXPORT FBOColorBufferAttachment: public FBORenderbufferAttachment
00189   {
00190     VL_INSTRUMENT_CLASS(vl::FBOColorBufferAttachment, FBOColorBufferAttachment)
00191 
00192   public:
00194     FBOColorBufferAttachment( EColorBufferFormat type = CBF_RGBA )
00195     {
00196       VL_DEBUG_SET_OBJECT_NAME()
00197       mType = type;
00198     }
00199     
00201     void setType( EColorBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
00202     
00204     EColorBufferFormat type() const { return mType; }
00205 
00206   protected:
00207     virtual int internalType() { return type(); }
00208 
00209   protected:
00210     EColorBufferFormat mType;
00211   };
00212   //-----------------------------------------------------------------------------
00213   // FBODepthBufferAttachment
00214   //-----------------------------------------------------------------------------
00218   class VLGRAPHICS_EXPORT FBODepthBufferAttachment: public FBORenderbufferAttachment
00219   {
00220     VL_INSTRUMENT_CLASS(vl::FBODepthBufferAttachment, FBORenderbufferAttachment)
00221 
00222   public:
00224     FBODepthBufferAttachment( EDepthBufferFormat type = DBF_DEPTH_COMPONENT )
00225     {
00226       VL_DEBUG_SET_OBJECT_NAME()
00227       mType = type;
00228     }
00229 
00231     void setType( EDepthBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
00232 
00234     EDepthBufferFormat type() const { return mType; }
00235 
00236   protected:
00237     virtual int internalType() { return type(); }
00238 
00239   protected:
00240     EDepthBufferFormat mType;
00241   };
00242   //-----------------------------------------------------------------------------
00243   // FBOStencilBufferAttachment
00244   //-----------------------------------------------------------------------------
00248   class VLGRAPHICS_EXPORT FBOStencilBufferAttachment: public FBORenderbufferAttachment
00249   {
00250     VL_INSTRUMENT_CLASS(vl::FBOStencilBufferAttachment, FBORenderbufferAttachment)
00251 
00252   public:
00254     FBOStencilBufferAttachment( EStencilBufferFormat type = SBF_STENCIL_INDEX8 )
00255     {
00256       VL_DEBUG_SET_OBJECT_NAME()
00257       mType = type;
00258     }
00259 
00261     void setType( EStencilBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
00262 
00264     EStencilBufferFormat type() const { return mType; }
00265 
00266   protected:
00267     virtual int internalType() { return type(); }
00268 
00269   protected:
00270     EStencilBufferFormat mType;
00271   };
00272   //-----------------------------------------------------------------------------
00273   // FBODepthStencilBufferAttachment
00274   //-----------------------------------------------------------------------------
00278   class VLGRAPHICS_EXPORT FBODepthStencilBufferAttachment: public FBORenderbufferAttachment
00279   {
00280     VL_INSTRUMENT_CLASS(vl::FBODepthStencilBufferAttachment, FBORenderbufferAttachment)
00281 
00282   public:
00284     FBODepthStencilBufferAttachment( EDepthStencilBufferFormat type = DSBT_DEPTH_STENCIL )
00285     {
00286       VL_DEBUG_SET_OBJECT_NAME()
00287       mType = type;
00288     }
00289 
00291     void setType( EDepthStencilBufferFormat type ) { if ( type != mType ) /* schedules recreation */ mReallocateRenderbuffer = true; mType = type; }
00292 
00294     EDepthStencilBufferFormat type() const { return mType; }
00295 
00296   protected:
00297     virtual int internalType() { return type(); }
00298 
00299   protected:
00300     EDepthStencilBufferFormat mType;
00301   };
00302   //-----------------------------------------------------------------------------
00303   // FBOAbstractTextureAttachment
00304   //-----------------------------------------------------------------------------
00308   class VLGRAPHICS_EXPORT FBOAbstractTextureAttachment: public FBOAbstractAttachment
00309   {
00310     VL_INSTRUMENT_ABSTRACT_CLASS(vl::FBOAbstractTextureAttachment, FBOAbstractAttachment)
00311 
00312   public:
00314     FBOAbstractTextureAttachment( Texture* texture, int mipmap_level ): mTexture(texture), mMipmapLevel(mipmap_level)
00315     {
00316       VL_DEBUG_SET_OBJECT_NAME()
00317     }
00318 
00320     void setTexture( Texture* texture ) { mTexture = texture; }
00321 
00323     Texture* texture() { return mTexture.get(); }
00324 
00326     const Texture* texture() const { return mTexture.get(); }
00327 
00329     void setMipmapLevel( int mipmap_level ) { mMipmapLevel = mipmap_level; }
00330 
00332     int mipmapLevel() const { return mMipmapLevel; }
00333 
00334   protected:
00335     ref<Texture> mTexture;
00336     int mMipmapLevel;
00337   };
00338   //-----------------------------------------------------------------------------
00339   // FBOTexture1DAttachment
00340   //-----------------------------------------------------------------------------
00345   class VLGRAPHICS_EXPORT FBOTexture1DAttachment: public FBOAbstractTextureAttachment
00346   {
00347     VL_INSTRUMENT_CLASS(vl::FBOTexture1DAttachment, FBOAbstractTextureAttachment)
00348 
00349   public:
00351     FBOTexture1DAttachment(): FBOAbstractTextureAttachment( NULL, 0 )
00352     {
00353       VL_DEBUG_SET_OBJECT_NAME()
00354     }
00355 
00357     FBOTexture1DAttachment( Texture* texture, int mipmap_level ): FBOAbstractTextureAttachment( texture, mipmap_level )
00358     {
00359       VL_DEBUG_SET_OBJECT_NAME()
00360     }
00361 
00362   protected:
00363     virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
00364   };
00365   //-----------------------------------------------------------------------------
00366   // FBOTexture2DAttachment
00367   //-----------------------------------------------------------------------------
00372   class VLGRAPHICS_EXPORT FBOTexture2DAttachment: public FBOAbstractTextureAttachment
00373   {
00374     VL_INSTRUMENT_CLASS(vl::FBOTexture2DAttachment, FBOAbstractTextureAttachment)
00375 
00376   public:
00378     FBOTexture2DAttachment( ): FBOAbstractTextureAttachment( NULL, 0 )
00379     {
00380       VL_DEBUG_SET_OBJECT_NAME()
00381       mTextureTarget = T2DT_TEXTURE_2D;
00382     }
00383 
00385     FBOTexture2DAttachment( Texture* texture, int mipmap_level, ETex2DTarget target ): FBOAbstractTextureAttachment( texture, mipmap_level )
00386     {
00387       VL_DEBUG_SET_OBJECT_NAME()
00388       mTextureTarget = target;
00389     }
00390 
00392     void setTextureTarget( ETex2DTarget target ) { mTextureTarget = target; }
00393 
00395     ETex2DTarget textureTarget() const { return mTextureTarget; }
00396 
00397   protected:
00398     virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
00399 
00400   protected:
00401     ETex2DTarget mTextureTarget;
00402   };
00403   //-----------------------------------------------------------------------------
00404   // FBOTextureAttachment
00405   //-----------------------------------------------------------------------------
00410   class VLGRAPHICS_EXPORT FBOTextureAttachment: public FBOAbstractTextureAttachment
00411   {
00412     VL_INSTRUMENT_CLASS(vl::FBOTextureAttachment, FBOAbstractTextureAttachment)
00413 
00414   public:
00416     FBOTextureAttachment(): FBOAbstractTextureAttachment( NULL, 0 )
00417     {
00418       VL_DEBUG_SET_OBJECT_NAME()
00419     }
00420 
00422     FBOTextureAttachment( Texture* texture, int mipmap_level ): FBOAbstractTextureAttachment( texture, mipmap_level )
00423     {
00424       VL_DEBUG_SET_OBJECT_NAME()
00425     }
00426 
00427   protected:
00428     virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
00429 
00430   };
00431   //-----------------------------------------------------------------------------
00432   // FBOTexture3DAttachment
00433   //-----------------------------------------------------------------------------
00438   class VLGRAPHICS_EXPORT FBOTexture3DAttachment: public FBOAbstractTextureAttachment
00439   {
00440     VL_INSTRUMENT_CLASS(vl::FBOTexture3DAttachment, FBOAbstractTextureAttachment)
00441 
00442   public:
00443     FBOTexture3DAttachment(): FBOAbstractTextureAttachment( NULL, 0 )
00444     {
00445       VL_DEBUG_SET_OBJECT_NAME()
00446       mLayer = 0;
00447     }
00448 
00449     FBOTexture3DAttachment( Texture* texture, int mipmap_level, int layer ): FBOAbstractTextureAttachment( texture, mipmap_level )
00450     {
00451       VL_DEBUG_SET_OBJECT_NAME()
00452       mLayer   = layer;
00453     }
00454 
00456     void setLayer( int layer ) { mLayer = layer; }
00457 
00459     int layer() const { return mLayer; }
00460 
00461   protected:
00462     virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
00463 
00464   protected:
00465     int mLayer;
00466   };
00467   //-----------------------------------------------------------------------------
00468   // FBOTextureLayerAttachment
00469   //-----------------------------------------------------------------------------
00474   class VLGRAPHICS_EXPORT FBOTextureLayerAttachment: public FBOAbstractTextureAttachment
00475   {
00476     VL_INSTRUMENT_CLASS(vl::FBOTextureLayerAttachment, FBOAbstractTextureAttachment)
00477 
00478   public:
00480     FBOTextureLayerAttachment(): FBOAbstractTextureAttachment( NULL, 0 )
00481     {
00482       VL_DEBUG_SET_OBJECT_NAME()
00483       mLayer = 0;
00484     }
00485 
00487     FBOTextureLayerAttachment( Texture* texture, int mipmap_level, int layer ): FBOAbstractTextureAttachment( texture, mipmap_level )
00488     {
00489       VL_DEBUG_SET_OBJECT_NAME()
00490       mLayer   = layer;
00491     }
00492 
00494     int layer() const { return mLayer; }
00495 
00497     void setLayer( int layer ) { mLayer = layer; }
00498 
00499   protected:
00500     virtual void bindAttachment( FramebufferObject* fbo, EAttachmentPoint attach_point );
00501 
00502   protected:
00503     ref<Texture> mTexture;
00504     int mMipmapLevel;
00505     int mLayer;
00506   };
00507   //-----------------------------------------------------------------------------
00508   // FramebufferObject
00509   //-----------------------------------------------------------------------------
00538   class VLGRAPHICS_EXPORT FramebufferObject: public Framebuffer
00539   {
00540     VL_INSTRUMENT_CLASS(vl::FramebufferObject, Framebuffer)
00541 
00542     friend class OpenGLContext;
00543 
00544   private:
00545     FramebufferObject( const FramebufferObject& other ): Framebuffer( other ), mHandle( 0 ) {}
00546     
00547     void operator=( const FramebufferObject& ) {}
00548     
00549     FramebufferObject(): 
00550     Framebuffer( NULL, 0, 0, RDB_COLOR_ATTACHMENT0, RDB_COLOR_ATTACHMENT0 ), mHandle( 0 )
00551     {
00552       VL_DEBUG_SET_OBJECT_NAME()
00553     }
00554 
00555     FramebufferObject( OpenGLContext* ctx, int w, int h, 
00556         EReadDrawBuffer draw_buffer=RDB_COLOR_ATTACHMENT0, 
00557         EReadDrawBuffer read_buffer=RDB_COLOR_ATTACHMENT0 ):
00558     Framebuffer( ctx, w, h, draw_buffer, read_buffer ), mHandle( 0 )
00559     {
00560       VL_DEBUG_SET_OBJECT_NAME()
00561     }
00562 
00563   public:
00565     ~FramebufferObject() { if (openglContext()) deleteFBO(); }
00566 
00571     void createFBO();
00572 
00577     void deleteFBO();
00578 
00580     void setHandle( GLuint handle ) { mHandle = handle; }
00581 
00583     virtual GLuint handle() const { return mHandle; }
00584 
00590     virtual void bindFramebuffer( EFramebufferBind target = FBB_FRAMEBUFFER );
00591 
00593     GLenum checkFramebufferStatus();
00594 
00596     void printFramebufferError( GLenum status ) const;
00597 
00599     void addColorAttachment( EAttachmentPoint attach_point, FBOColorBufferAttachment* attachment );
00600     
00602     void addTextureAttachment( EAttachmentPoint attach_point, FBOAbstractTextureAttachment* attachment );
00603     
00608     void addDepthAttachment( FBOAbstractAttachment* attachment );
00609     
00614     void addStencilAttachment( FBOAbstractAttachment* attachment );
00615     
00617     void addDepthStencilAttachment( FBOAbstractAttachment* attachment );
00618     
00620     void removeAttachment( FBOAbstractAttachment* attachment );
00621     
00623     void removeAttachment( EAttachmentPoint attach_point );
00624     
00626     void removeAllAttachments();
00627 
00629     const std::map< EAttachmentPoint, ref<FBOAbstractAttachment> >& fboAttachments() const { return mFBOAttachments; }
00630     
00631   public:
00632     std::map< EAttachmentPoint, ref<FBOAbstractAttachment> > mFBOAttachments;
00633     GLuint mHandle;
00634   };
00635 }
00636 
00637 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:37.
Permission is granted to use this page to write and publish articles regarding Visualization Library.