Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Fbo.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Exception.h"
27 #include "cinder/gl/gl.h"
28 #include "cinder/gl/Texture.h"
29 
30 namespace cinder { namespace gl {
31 
33 class Renderbuffer {
34  public:
38 #if defined( CINDER_GLES )
39  Renderbuffer( int width, int height, GLenum internalFormat = GL_RGBA8_OES );
40 #else
42 #endif
43  Renderbuffer( int width, int height, GLenum internalFormat, int msaaSamples, int coverageSamples = 0 );
45 
47  int getWidth() const { return mObj->mWidth; }
49  int getHeight() const { return mObj->mHeight; }
51  Vec2i getSize() const { return Vec2i( mObj->mWidth, mObj->mHeight ); }
53  Area getBounds() const { return Area( 0, 0, mObj->mWidth, mObj->mHeight ); }
55  float getAspectRatio() const { return mObj->mWidth / (float)mObj->mHeight; }
56 
58  GLuint getId() const { return mObj->mId; }
60  GLenum getInternalFormat() const { return mObj->mInternalFormat; }
62  int getSamples() const { return mObj->mSamples; }
64  int getCoverageSamples() const { return mObj->mCoverageSamples; }
65 
66  private:
67  struct Obj {
68  Obj();
69  Obj( int aWidth, int aHeight, GLenum internalFormat, int msaaSamples, int coverageSamples );
70  ~Obj();
71 
72  int mWidth, mHeight;
73  GLuint mId;
74  GLenum mInternalFormat;
75  int mSamples, mCoverageSamples;
76  };
77 
78  std::shared_ptr<Obj> mObj;
79 
80  public:
82  typedef std::shared_ptr<Obj> Renderbuffer::*unspecified_bool_type;
84  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Renderbuffer::mObj; }
85  void reset() { mObj.reset(); }
87 };
88 
90 class Fbo {
91  public:
92  struct Format;
93 
95  Fbo() {}
97  Fbo( int width, int height, Format format = Format() );
99  Fbo( int width, int height, bool alpha, bool color = true, bool depth = true );
100 
102  int getWidth() const { return mObj->mWidth; }
104  int getHeight() const { return mObj->mHeight; }
106  Vec2i getSize() const { return Vec2i( mObj->mWidth, mObj->mHeight ); }
108  Area getBounds() const { return Area( 0, 0, mObj->mWidth, mObj->mHeight ); }
110  float getAspectRatio() const { return mObj->mWidth / (float)mObj->mHeight; }
112  const Format& getFormat() const { return mObj->mFormat; }
114  GLenum getTarget() const { return mObj->mFormat.mTarget; }
115 
117  Texture& getTexture( int attachment = 0 );
120 
122  void bindTexture( int textureUnit = 0, int attachment = 0 );
124  void unbindTexture();
126  void bindDepthTexture( int textureUnit = 0 );
128  void bindFramebuffer();
130  static void unbindFramebuffer();
131 
133  GLuint getId() const { return mObj->mId; }
134 
135 #if ! defined( CINDER_GLES )
136  GLuint getResolveId() const { if( mObj->mResolveFramebufferId ) return mObj->mResolveFramebufferId; else return mObj->mId; }
138 
140  void blitTo( Fbo dst, const Area &srcArea, const Area &dstArea, GLenum filter = GL_NEAREST, GLbitfield mask = GL_COLOR_BUFFER_BIT ) const;
142  void blitToScreen( const Area &srcArea, const Area &dstArea, GLenum filter = GL_NEAREST, GLbitfield mask = GL_COLOR_BUFFER_BIT ) const;
144  void blitFromScreen( const Area &srcArea, const Area &dstArea, GLenum filter = GL_NEAREST, GLbitfield mask = GL_COLOR_BUFFER_BIT );
145 #endif
146 
148  static GLint getMaxSamples();
150  static GLint getMaxAttachments();
151 
152  struct Format {
153  public:
155  Format();
156 
160  void setColorInternalFormat( GLenum colorInternalFormat ) { mColorInternalFormat = colorInternalFormat; }
162  void setDepthInternalFormat( GLenum depthInternalFormat ) { mDepthInternalFormat = depthInternalFormat; }
164  void setSamples( int samples ) { mSamples = samples; }
168  void enableColorBuffer( bool colorBuffer = true, int numColorBuffers = 1 );
170  void enableDepthBuffer( bool depthBuffer = true, bool asTexture = true );
171 // void enableStencilBuffer( bool stencilBuffer = true ) { mStencilBuffer = stencilBuffer; }
174 
176  void setWrap( GLenum wrapS, GLenum wrapT ) { setWrapS( wrapS ); setWrapT( wrapT ); }
179  void setWrapS( GLenum wrapS ) { mWrapS = wrapS; }
182  void setWrapT( GLenum wrapT ) { mWrapT = wrapT; }
185  void setMinFilter( GLenum minFilter ) { mMinFilter = minFilter; }
188  void setMagFilter( GLenum magFilter ) { mMagFilter = magFilter; }
189 
191  GLenum getTarget() const { return mTarget; }
197  int getSamples() const { return mSamples; }
199  int getCoverageSamples() const { return mCoverageSamples; }
201  bool hasColorBuffer() const { return mNumColorBuffers > 0; }
203  int getNumColorBuffers() const { return mNumColorBuffers; }
205  bool hasDepthBuffer() const { return mDepthBuffer; }
208 // bool hasStencilBuffer() const { return mStencilBuffer; }
210  bool hasMipMapping() const { return mMipmapping; }
211 
212  protected:
215  int mSamples;
222 
223  friend class Fbo;
224  };
225 
226  protected:
227  void init();
228  bool initMultisample( bool csaa );
229  void resolveTextures() const;
230  void updateMipmaps( bool bindFirst, int attachment ) const;
231  bool checkStatus( class FboExceptionInvalidSpecification *resultExc );
232 
233  struct Obj {
234  Obj();
235  Obj( int aWidth, int aHeight );
236  ~Obj();
237 
242  std::vector<Renderbuffer> mMultisampleColorRenderbuffers;
244  std::vector<Texture> mColorTextures;
248  };
249 
250  std::shared_ptr<Obj> mObj;
251 
253 
254  public:
256  typedef std::shared_ptr<Obj> Fbo::*unspecified_bool_type;
258  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Fbo::mObj; }
259  void reset() { mObj.reset(); }
261 };
262 
263 class FboException : public Exception {
264 };
265 
267  public:
269  FboExceptionInvalidSpecification( const std::string &message ) throw();
270 
271  virtual const char * what() const throw() { return mMessage; }
272 
273  private:
274  char mMessage[256];
275 };
276 
277 } } // namespace cinder::gl
Obj()
Definition: Fbo.cpp:103
std::vector< Texture > mColorTextures
Definition: Fbo.h:244
int getHeight() const
Returns the height of the Renderbuffer in pixels.
Definition: Fbo.h:49
Definition: Fbo.h:152
GLuint getId() const
Returns the ID of the Renderbuffer.
Definition: Fbo.h:58
bool hasDepthBuffer() const
Returns whether the FBO contains a depth buffer.
Definition: Fbo.h:205
void setWrapT(GLenum wrapT)
Sets the vertical wrapping behavior for the FBO's textures. Default is GL_CLAMP_TO_EDGE. Possible values are GL_CLAMP, GL_REPEAT and GL_CLAMP_TO_EDGE.
Definition: Fbo.h:182
void reset()
Emulates shared_ptr-like behavior.
Definition: Fbo.h:259
int mWidth
Definition: Fbo.h:238
Definition: Area.h:37
void bindFramebuffer()
Binds the Fbo as the currently active framebuffer, meaning it will receive the results of all subsequ...
Definition: Fbo.cpp:415
bool hasColorBuffer() const
Returns whether the FBO contains a color buffer.
Definition: Fbo.h:201
GLsizei const GLchar ** string
Definition: GLee.h:2427
GLenum mMagFilter
Definition: Fbo.h:221
void unbindTexture()
Unbinds the texture associated with an Fbo's target.
Definition: Fbo.cpp:354
Renderbuffer()
Creates a NULL Renderbuffer.
Definition: Fbo.h:36
GLuint mResolveFramebufferId
Definition: Fbo.h:241
Definition: Fbo.h:233
GLenum internalFormat
Definition: GLee.h:7149
void updateMipmaps(bool bindFirst, int attachment) const
Definition: Fbo.cpp:398
Vec2i getSize() const
Returns the size of the Renderbuffer in pixels.
Definition: Fbo.h:51
int getNumColorBuffers() const
Returns the number of color buffers.
Definition: Fbo.h:203
GLenum GLsizei width
Definition: GLee.h:969
Definition: Fbo.h:263
int mNumColorBuffers
Definition: Fbo.h:219
int mSamples
Definition: Fbo.h:215
bool hasDepthBufferTexture() const
Returns whether the FBO contains a depth buffer implemened as a texture. Always false on OpenGL ES...
Definition: Fbo.h:207
static GLint sMaxAttachments
Definition: Fbo.h:252
static GLint sMaxSamples
Definition: Fbo.h:252
void bindTexture(int textureUnit=0, int attachment=0)
Binds the color texture associated with an Fbo to its target. Optionally binds to a multitexturing un...
Definition: Fbo.cpp:347
GLenum mWrapT
Definition: Fbo.h:220
GLenum target
Definition: GLee.h:13607
#define GL_NEAREST
Definition: gldx.h:933
GLuint getId() const
Returns the ID of the framebuffer itself. For antialiased FBOs this is the ID of the output multisamp...
Definition: Fbo.h:133
Renderbuffer mDepthRenderbuffer
Definition: Fbo.h:246
bool mNeedsMipmapUpdate
Definition: Fbo.h:247
std::shared_ptr< Obj > Renderbuffer::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: Fbo.h:83
void reset()
Emulates shared_ptr-like behavior.
Definition: Fbo.h:85
void setMinFilter(GLenum minFilter)
Sets the minification filtering behavior for the FBO's textures. Default is GL_LINEAR: Possible value...
Definition: Fbo.h:185
GLsizei coverageSamples
Definition: GLee.h:13032
#define GL_COLOR_BUFFER_BIT
Definition: gldx.h:99
void blitToScreen(const Area &srcArea, const Area &dstArea, GLenum filter=GL_NEAREST, GLbitfield mask=GL_COLOR_BUFFER_BIT) const
Copies to the screen from Area srcArea to dstArea using filter filter. mask allows specification of c...
Definition: Fbo.cpp:509
void enableMipmapping(bool enableMipmapping=true)
Enables or disables mip-mapping for the FBO's textures.
Definition: Fbo.h:173
Texture mDepthTexture
Definition: Fbo.h:245
Area getBounds() const
Returns the bounding area of the Renderbuffer in pixels.
Definition: Fbo.h:53
unsigned int GLuint
Definition: gldx.h:55
std::vector< Renderbuffer > mMultisampleColorRenderbuffers
Definition: Fbo.h:242
int getCoverageSamples() const
Returns the number of coverage samples used in CSAA-style antialiasing. Defaults to none...
Definition: Fbo.h:64
int mCoverageSamples
Definition: Fbo.h:216
void setTarget(GLenum target)
Set the texture target associated with the FBO. Defaults to GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB i...
Definition: Fbo.h:158
void setWrap(GLenum wrapS, GLenum wrapT)
Sets the wrapping behavior for the FBO's textures. Possible values are GL_CLAMP, GL_REPEAT and GL_CLA...
Definition: Fbo.h:176
Represents an OpenGL Texture. Implicitly shared object.
Definition: Texture.h:41
Area getBounds() const
Returns the bounding area of the FBO in pixels.
Definition: Fbo.h:108
GLenum getTarget() const
Returns the texture target associated with the FBO.
Definition: Fbo.h:191
Format()
Default constructor, sets the target to GL_TEXTURE_2D with an 8-bit color+alpha, a 24-bit depth textu...
Definition: Fbo.cpp:126
bool initMultisample(bool csaa)
Definition: Fbo.cpp:261
int getWidth() const
Returns the width of the Renderbuffer in pixels.
Definition: Fbo.h:47
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
GLclampf GLclampf GLclampf alpha
Definition: GLee.h:951
void setWrapS(GLenum wrapS)
Sets the horizontal wrapping behavior for the FBO's textures. Default is GL_CLAMP_TO_EDGE. Possible values are GL_CLAMP, GL_REPEAT and GL_CLAMP_TO_EDGE.
Definition: Fbo.h:179
bool mMipmapping
Definition: Fbo.h:217
Definition: gl.h:380
GLenum mTarget
Definition: Fbo.h:213
void bindDepthTexture(int textureUnit=0)
Binds the depth texture associated with an Fbo to its target.
Definition: Fbo.cpp:359
GLenum getColorInternalFormat() const
Returns the GL internal format for the color buffer. Defaults to GL_RGBA8.
Definition: Fbo.h:193
~Obj()
Definition: Fbo.cpp:116
Fbo()
Creates a NULL FBO.
Definition: Fbo.h:95
void setMagFilter(GLenum magFilter)
Definition: Fbo.h:188
Texture & getDepthTexture()
Returns a reference to the depth texture of the FBO.
Definition: Fbo.cpp:342
static void unbindFramebuffer()
Unbinds the Fbo as the currently active framebuffer, restoring the primary context as the target for ...
Definition: Fbo.cpp:426
static GLint getMaxAttachments()
Returns the maximum number of color attachments the graphics card is capable of using for an Fbo...
Definition: Fbo.cpp:486
FboExceptionInvalidSpecification()
Definition: Fbo.h:268
int getHeight() const
Returns the height of the FBO in pixels.
Definition: Fbo.h:104
Represents an OpenGL Renderbuffer, used primarily in conjunction with FBOs. Supported on OpenGL ES bu...
Definition: Fbo.h:33
bool mDepthBufferAsTexture
Definition: Fbo.h:218
void init()
Definition: Fbo.cpp:174
void blitFromScreen(const Area &srcArea, const Area &dstArea, GLenum filter=GL_NEAREST, GLbitfield mask=GL_COLOR_BUFFER_BIT)
Copies from the screen from Area srcArea to dstArea using filter filter. mask allows specification of...
Definition: Fbo.cpp:518
GLuint mId
Definition: Fbo.h:240
bool hasMipMapping() const
Returns whether the contents of the FBO textures are mip-mapped.
Definition: Fbo.h:210
void setSamples(int samples)
Sets the number of samples used in MSAA-style antialiasing. Defaults to none, disabling multisampling...
Definition: Fbo.h:164
GLenum mWrapS
Definition: Fbo.h:220
virtual const char * what() const
Definition: Fbo.h:271
int getCoverageSamples() const
Returns the number of coverage samples used in CSAA-style antialiasing. Defaults to none...
Definition: Fbo.h:199
GLsizei samples
Definition: GLee.h:5425
bool mDepthBuffer
Definition: Fbo.h:218
Texture & getTexture(int attachment=0)
Returns a reference to the color texture of the FBO. attachment specifies which attachment in the cas...
Definition: Fbo.cpp:335
int getSamples() const
Returns the number of samples used in MSAA-style antialiasing. Defaults to none, disabling multisampl...
Definition: Fbo.h:62
void setCoverageSamples(int coverageSamples)
Sets the number of coverage samples used in CSAA-style antialiasing. Defaults to none. Note that not all implementations support CSAA, and is currenlty Windows-only Nvidia. Ignored on OpenGL ES.
Definition: Fbo.h:166
void enableDepthBuffer(bool depthBuffer=true, bool asTexture=true)
Enables or disables the creation of a depth buffer for the FBO. If asTexture the depth buffer is crea...
Definition: Fbo.cpp:159
GLenum mColorInternalFormat
Definition: Fbo.h:214
#define GL_RGBA8
Definition: gldx.h:1010
bool checkStatus(class FboExceptionInvalidSpecification *resultExc)
Definition: Fbo.cpp:431
Format mFormat
Definition: Fbo.h:239
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: GLee.h:5419
int mHeight
Definition: Fbo.h:238
bool mStencilBuffer
Definition: Fbo.h:218
GLenum getInternalFormat() const
Returns the internal format of the Renderbuffer.
Definition: Fbo.h:60
int GLint
Definition: gldx.h:51
GLenum GLsizei GLenum format
Definition: GLee.h:969
float getAspectRatio() const
Returns the aspect ratio of the FBO.
Definition: Fbo.h:110
GLuint dst
Definition: GLee.h:10536
int getWidth() const
Returns the width of the FBO in pixels.
Definition: Fbo.h:102
Vec2i getSize() const
Returns the size of the FBO in pixels.
Definition: Fbo.h:106
Represents an OpenGL Framebuffer Object. //! Represents an instance of a font at a point size...
Definition: Fbo.h:90
int getSamples() const
Returns the number of samples used in MSAA-style antialiasing. Defaults to none, disabling multisampl...
Definition: Fbo.h:197
GLenum mMinFilter
Definition: Fbo.h:221
bool mNeedsResolve
Definition: Fbo.h:247
unsigned int GLbitfield
Definition: gldx.h:48
GLenum getTarget() const
Returns the texture target for this FBO. Typically GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE_ARB.
Definition: Fbo.h:114
GLenum mDepthInternalFormat
Definition: Fbo.h:214
GLuint getResolveId() const
For antialiased FBOs this returns the ID of the mirror FBO designed for reading, where the multisampl...
Definition: Fbo.h:137
GLenum attachment
Definition: GLee.h:5383
const Format & getFormat() const
Returns the Fbo::Format of this FBO.
Definition: Fbo.h:112
static GLint getMaxSamples()
Returns the maximum number of samples the graphics card is capable of using per pixel in MSAA for an ...
Definition: Fbo.cpp:469
GLint GLint GLsizei GLsizei GLsizei depth
Definition: GLee.h:1161
std::shared_ptr< Obj > mObj
Definition: Fbo.h:250
void resolveTextures() const
Definition: Fbo.cpp:364
GLenum GLint GLuint mask
Definition: GLee.h:2241
void setColorInternalFormat(GLenum colorInternalFormat)
Sets the GL internal format for the color buffer. Defaults to GL_RGBA8 (and GL_RGBA on OpenGL ES)...
Definition: Fbo.h:160
void enableColorBuffer(bool colorBuffer=true, int numColorBuffers=1)
Enables or disables the creation of a color buffer for the FBO.. Creates multiple color attachments w...
Definition: Fbo.cpp:150
Renderbuffer mMultisampleDepthRenderbuffer
Definition: Fbo.h:243
GLenum getDepthInternalFormat() const
Returns the GL internal format for the depth buffer. Defaults to GL_DEPTH_COMPONENT24.
Definition: Fbo.h:195
unsigned int GLenum
Definition: gldx.h:46
void blitTo(Fbo dst, const Area &srcArea, const Area &dstArea, GLenum filter=GL_NEAREST, GLbitfield mask=GL_COLOR_BUFFER_BIT) const
Copies to FBO dst from srcArea to dstArea using filter filter. mask allows specification of color (GL...
Definition: Fbo.cpp:500
std::shared_ptr< Obj > Fbo::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: Fbo.h:257
float getAspectRatio() const
Returns the aspect ratio of the Renderbuffer.
Definition: Fbo.h:55
Vec2< int > Vec2i
Definition: Vector.h:1313
void setDepthInternalFormat(GLenum depthInternalFormat)
Sets the GL internal format for the depth buffer. Defaults to GL_DEPTH_COMPONENT24. Common options also include GL_DEPTH_COMPONENT16 and GL_DEPTH_COMPONENT32.
Definition: Fbo.h:162
GLuint color
Definition: GLee.h:3198