/************************************************ ** framebufferObject.h ** ** ------------------- ** ** ** ** This is the frame-work for general purpose ** ** initialization of a framebuffer object, ** ** as specified in the OpenGL extension: ** ** GL_EXT_FRAMEBUFFER_OBJECT ** ** ** ** Since this is an OpenGL extension, not WGL, ** ** it should be much more portable (and ** ** supposedly) faster than p-buffers and ** ** render-to-texture. ** ** ** ** Chris Wyman (4/27/2005) ** ************************************************/ #ifndef ___FRAMEBUFFER_OBJECT_H #define ___FRAMEBUFFER_OBJECT_H #include class FrameBuffer { private: GLuint ID; // from glGenFramebuffersEXT() GLuint *colorIDs; // these are the GL texture IDs for the color buffers GLuint depthID; // the GL texture ID for the depth buffer (_OR_ the depth buffer ID from glBindRenderbufferEXT) GLuint stencilID; // the GL buffer ID for the stencil buffer (from glBindRenderbufferEXT) GLint maxColorBuffers; // the max number of color buffer textures (i.e., size of colorIDs[] array) GLuint prevFrameBuf; // a very primitive method for allowing nested BindBuffer() calls. (BE CAREFUL) int width, height; // width & height of ALL textures/buffers in framebuffer object. (MAY BE INCORRECT if used with the default Framebuffer() constructor!) char fbName[80]; // a simple ascii text name for the buffer -- useful for debugging public: FrameBuffer( char *name=0 ); // avoid the use of this constructor FrameBuffer( int width, int height, char *name=0 ); // use this constructor if you want to setup the textures yourself FrameBuffer( int width, int height, GLuint colorBufType, int numColorBufs, // creates a complete FBO with a basic setup for you int hasZbuf, char *name=0 ); ~FrameBuffer(); // Check to see if there are any errors. Returns GL_FRAMEBUFFER_COMPLETE_EXT if OK. // Prints messages to stdout if printMessage == 1 GLenum CheckFramebufferStatus( int printMessage=0 ); // Attach textures to various attachment points int AttachColorTexture( GLuint colorTexID, int colorBuffer=0 ); int AttachDepthTexture( GLuint depthTexID ); int AttachStencilTexture( GLuint stencilTexID ); // CURRENTLY UNSUPPORTED. DO NOT USE! // Attach renderbuffers to various attachment points // (note these SHOULD replace textures, but it may not be guaranteed, // so you might want to unbind textures before binding a renderbuffer) int AttachColorBuffer( GLuint colorBufID, int colorBuffer=0 ); // Use only if you know what you're doing! int AttachDepthBuffer( GLuint depthBufID ); // Use only if you know what you're doing! int AttachStencilBuffer( GLuint stencilBufID ); // Use only if you know what you're doing! // Functionality for drawing custom mipmap levels. void DrawToColorMipmapLevel( GLuint colorBuffer, GLuint level ); // Use only if you know what you're doing! void DoneDrawingMipmapLevels( void ); // Use only if you know what you're doing! // Bind/unbind the current framebuffer. These functions store the currently // bound framebuffer during a BindBuffer() and rebind it upon an UnbindBuffer() // (which allows for a very basic nesting of BindBuffer() calls) GLuint BindBuffer( void ); // Commands after this draw into THIS framebuffer. int UnbindBuffer( void ); // Commands after this draw into the normal framebuffer (e.g., screen) // Queries to return the texture/renderbuffer ID of the various attachments // // You may thus use one of the color buffers as a GL texture using: // glBindTexture( GL_TEXTURE_2D, framebuffer->GetColorTextureID( 0 ) ); // Beware you may NOT have this framebuffer bound (via BindBuffer()) at the // same time you're using one of the textures (via glBindTexture()). This means // when you're done using framebuffer->GetColorTextureID(0), you should call // glBindTexture( GL_TEXTURE_2D, 0 ); inline GLuint GetColorTextureID( int level=0 ) { return (level < maxColorBuffers && level >= 0 ? colorIDs[level] : -1); } inline GLuint GetDepthTextureID( void ) { return depthID; } inline GLuint GetStencilTextureID( void ) { return stencilID; } // Gets some identifying information about the framebuffer object. inline int GetWidth( void ) { return width; } inline int GetHeight( void ) { return height; } inline GLuint GetBufferID( void ) { return ID; } inline char *GetName( void ) { return fbName; } // Allows you to set the size, particularly useful if you use the default constructor, // and want to set it up correctly afterwards! inline void SetSize( int newWidth, int newHeight ) { width = newWidth; height = newHeight; } }; #endif