framebuffer.h

Go to the documentation of this file.
00001 /*
00002  * framebuffer.h
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * 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 THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * 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
00026  * ON 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  * Simple framebuffer object (for offscreen drawing)
00031  */
00032 
00033 #ifndef WAVE_GLUT_EFFECTS_FRAMEBUFFER_H__
00034 #define WAVE_GLUT_EFFECTS_FRAMEBUFFER_H__
00035 
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "wave-glut/renderable.h"
00039 #include "wave-image/wave-image.h"
00040 
00041 
00042 namespace glut {
00043 
00044 
00045 /// \ingroup glut_effects
00046 /*@{*/
00047 
00048 
00049 
00050 /// provides a framebuffer of fixed size for offscreen rendering.
00051 /// To use:
00052 ///  - create the framebuffer with the set width and height
00053 ///  - create a texture of the same width and height
00054 ///  - call Framebuffer::setupForRendering() with the texture.  The perspective
00055 ///     matrix will be tweaked to account for the new aspect ratio, but the
00056 ///     modelview matrix will not change!
00057 ///  - perform any rendering you want with that render context.
00058 ///  - when finished, call Framebuffer::endRendering().  This will clean up
00059 ///             everythin and restore the original OpenGL transforms.
00060 ///  - the texture now contains whatever was rendered offscreen.
00061 ///
00062 /// However, once you create a Framebuffer, it is recommended that you use a
00063 ///     FramebufferContext to manage calling setupForRendering() and
00064 ///     endRendering() automatically.
00065 class Framebuffer {
00066 public:
00067         // virtual destructor --------------------------------------------------
00068         virtual ~Framebuffer(void) throw();
00069 
00070         // glut::Framebuffer class interface methods ---------------------------
00071         virtual int getWidth(void) const throw() = 0;
00072         virtual int getHeight(void) const throw() = 0;
00073         virtual void setupForRendering(IN int textureId,
00074                                 IN const render_context_t& rc) = 0;
00075         virtual void endRendering(void) throw() = 0;
00076 
00077         // public static methods (factory methods) -----------------------------
00078         static smart_ptr<Framebuffer> create(IN int width,
00079                                         IN int height);
00080 };
00081 
00082 
00083 
00084 /// simple helper class--put this on the stack and it will automatically call
00085 ///     Framebuffer::setupForRendering() and Framebuffer::endRendering() for
00086 ///     you.
00087 class FramebufferContext {
00088 public:
00089         /// constructor: you must provide the Framebuffer and texture, and the
00090         ///     render context will be prepared for you.
00091         FramebufferContext(IN Framebuffer * fbuffer,
00092                                 IN int textureId,
00093                                 IN const render_context_t& rc) {
00094                         ASSERT(fbuffer, "null");
00095                         ASSERT(textureId, "null");
00096                         m_framebuffer = fbuffer;
00097                         m_framebuffer->setupForRendering(textureId, rc);
00098                 }
00099 
00100         /// destructor: automatically cleans up
00101         ~FramebufferContext(void) throw() {
00102                         if (m_framebuffer) {
00103                                 m_framebuffer->endRendering();
00104                         }
00105                 }
00106 
00107 private:
00108         // private member data -------------------------------------------------
00109         Framebuffer *           m_framebuffer;  // weak pointer, does not delete
00110 };
00111 
00112 
00113 
00114 };      // glut namespace
00115 
00116 
00117 #endif  //WAVE_GLUT_EFFECTS_FRAMEBUFFER_H__
00118