00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #pragma once
00024
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Exception.h"
00027 #include "cinder/gl/gl.h"
00028 #include "cinder/gl/Texture.h"
00029
00030 namespace cinder { namespace gl {
00031
00033 class Fbo {
00034 public:
00035 struct Format;
00036
00038 Fbo() {}
00040 Fbo( int width, int height, Format format = Format() );
00042 Fbo( int width, int height, bool alpha, bool color = true, bool depth = true );
00043
00045 int getWidth() const { return mObj->mWidth; }
00047 int getHeight() const { return mObj->mHeight; }
00049 Vec2i getSize() const { return Vec2i( mObj->mWidth, mObj->mHeight ); }
00051 Area getBounds() const { return Area( 0, 0, mObj->mWidth, mObj->mHeight ); }
00053 float getAspectRatio() const { return mObj->mWidth / (float)mObj->mHeight; }
00055 const Format& getFormat() const { return mObj->mFormat; }
00057 GLenum getTarget() const { return mObj->mFormat.mTarget; }
00058
00060 Texture& getTexture();
00062 Texture& getDepthTexture();
00063
00065 void bindTexture( int textureUnit = 0 );
00067 void unbindTexture();
00069 void bindDepthTexture( int textureUnit = 0 );
00071 void bindFramebuffer();
00073 static void unbindFramebuffer();
00074
00076 static GLint getMaxSamples();
00077
00078 struct Format {
00079 public:
00081 Format();
00082
00084 void setTarget( GLenum target ) { mTarget = target; }
00086 void setColorInternalFormat( GLenum colorInternalFormat ) { mColorInternalFormat = colorInternalFormat; }
00088 void setDepthInternalFormat( GLenum depthInternalFormat ) { mDepthInternalFormat = depthInternalFormat; }
00090 void setSamples( int samples ) { mSamples = samples; }
00092 void setCoverageSamples( int coverageSamples ) { mCoverageSamples = coverageSamples; }
00094 void enableColorBuffer( bool colorBuffer = true ) { mColorBuffer = colorBuffer; }
00096 void enableDepthBuffer( bool depthBuffer = true ) { mDepthBuffer = depthBuffer; }
00097
00099 void enableMipmapping( bool enableMipmapping = true ) { mMipmapping = enableMipmapping; }
00100
00102 void setWrap( GLenum wrapS, GLenum wrapT ) { setWrapS( wrapS ); setWrapT( wrapT ); }
00105 void setWrapS( GLenum wrapS ) { mWrapS = wrapS; }
00108 void setWrapT( GLenum wrapT ) { mWrapT = wrapT; }
00111 void setMinFilter( GLenum minFilter ) { mMinFilter = minFilter; }
00114 void setMagFilter( GLenum magFilter ) { mMagFilter = magFilter; }
00115
00116
00118 GLenum getTarget() const { return mTarget; }
00120 GLenum getColorInternalFormat() const { return mColorInternalFormat; }
00122 GLenum getDepthInternalFormat() const { return mDepthInternalFormat; }
00124 int getSamples() const { return mSamples; }
00126 int getCoverageSamples() const { return mCoverageSamples; }
00128 bool hasColorBuffer() const { return mColorBuffer; }
00130 bool hasDepthBuffer() const { return mDepthBuffer; }
00131
00133 bool hasMipMapping() const { return mMipmapping; }
00134
00135 protected:
00136 GLenum mTarget;
00137 GLenum mColorInternalFormat, mDepthInternalFormat;
00138 int mSamples;
00139 int mCoverageSamples;
00140 bool mMipmapping;
00141 bool mColorBuffer, mDepthBuffer, mStencilBuffer;
00142 GLenum mWrapS, mWrapT;
00143 GLenum mMinFilter, mMagFilter;
00144
00145 friend class Fbo;
00146 };
00147
00148 protected:
00149 void init();
00150 bool initMultisample( bool csaa );
00151 void resolveTexture() const;
00152 void updateMipmaps( bool bindFirst ) const;
00153 bool checkStatus( class FboExceptionInvalidSpecification *resultExc );
00154
00155 struct Obj {
00156 Obj();
00157 Obj( int aWidth, int aHeight );
00158 ~Obj();
00159
00160 int mWidth, mHeight;
00161 Format mFormat;
00162 GLuint mId, mColorTextureId, mDepthTextureId;
00163 GLuint mColorRenderBufferId, mDepthRenderBufferId, mResolveFramebufferId;
00164 Texture mColorTexture, mDepthTexture;
00165 mutable bool mNeedsResolve, mNeedsMipmapUpdate;
00166 };
00167
00168 shared_ptr<Obj> mObj;
00169
00170 static GLint sMaxSamples;
00171
00172 public:
00174
00175 typedef shared_ptr<Obj> Fbo::*unspecified_bool_type;
00176 operator unspecified_bool_type() { return ( mObj.get() == 0 ) ? 0 : &Fbo::mObj; }
00177 void reset() { mObj.reset(); }
00179 };
00180
00181 class FboException : public Exception {
00182 };
00183
00184 class FboExceptionInvalidSpecification : public FboException {
00185 public:
00186 FboExceptionInvalidSpecification() : FboException() { mMessage[0] = 0; }
00187 FboExceptionInvalidSpecification( const std::string &message ) throw();
00188
00189 virtual const char * what() const throw() { return mMessage; }
00190
00191 private:
00192 char mMessage[256];
00193 };
00194
00195 }
00196 }