renderTarget.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_VIEW_RENDERING_RENDERTARGET_H
00006 #define BALL_VIEW_RENDERING_RENDERTARGET_H
00007 
00008 #include <BALL/COMMON/global.h>
00009 #include <BALL/COMMON/exception.h>
00010 
00011 #include <assert.h>
00012 #include <vector>
00013 #include <boost/smart_ptr.hpp>
00014 #include <boost/shared_array.hpp>
00015 
00016 #include <BALL/VIEW/RENDERING/pixelFormat.h>
00017 
00018 namespace BALL
00019 {
00020   namespace VIEW
00021   {
00022 
00024     struct Resolution
00025     {
00026       unsigned int width;
00027       unsigned int height;
00028 
00029       Resolution() : width(0), height(0) { }
00030         
00031       Resolution(const unsigned width, const unsigned int height) : width(width),height(height) { }
00032     };
00033 
00034 
00038     class FrameBufferFormat
00039     {
00040     public:
00041 
00043       FrameBufferFormat() : 
00044         width(0), height(0), pitch(0), pixelFormat(PixelFormat::RGB_32)
00045       { }
00046 
00047       FrameBufferFormat(const unsigned int width,
00048                 const unsigned int height, const PixelFormat &pixelFormat)
00049         : width(width), height(height), 
00050           pitch(width * pixelFormat.computeByteSize()), 
00051           pixelFormat(pixelFormat)
00052       {
00053         assert(pitch >= width * pixelFormat.computeByteSize());
00054       }
00055 
00056       FrameBufferFormat(const unsigned int width, const unsigned int height,
00057                 const unsigned int pitch, const PixelFormat &pixelFormat )
00058         : width(width), height(height), pitch(pitch), pixelFormat(pixelFormat)
00059       {
00060         assert(pitch >= width * pixelFormat.computeByteSize());
00061       }
00062 
00063       FrameBufferFormat(const FrameBufferFormat &format)
00064         : width(format.width), height(format.height), 
00065           pitch(format.pitch), pixelFormat(format.pixelFormat)
00066       {
00067       }
00068 
00069       const FrameBufferFormat &operator=(const FrameBufferFormat &format)
00070       {
00071         width = format.width;
00072         height = format.height;
00073         pitch = format.pitch;
00074         pixelFormat = format.pixelFormat;
00075         return *this;
00076       }
00077 
00078       bool operator==(const FrameBufferFormat& format) const
00079       {
00080         return width==format.width && height==format.height &&
00081              pitch==format.pitch && pixelFormat==format.pixelFormat;
00082       }
00083 
00084       bool operator!=(const FrameBufferFormat& format) const
00085       {
00086         return width!=format.width || height!=format.height ||
00087              pitch!=format.pitch || pixelFormat!=format.pixelFormat;
00088       }
00089 
00090       bool isValid() const { return width > 0 && height > 0 && pitch > 0; }
00091 
00095       unsigned int getWidth() const { return width; }
00096 
00100       void setWidth(unsigned int width) { this->width = width; }
00101 
00103       unsigned int getHeight() const { return height; }
00104 
00106       void setHeight(unsigned int height) { this->height = height; }
00107 
00109       unsigned int getPitch() const { return pitch; }
00110 
00112       void setPitch(unsigned int pitch) { this->pitch = pitch; }
00113 
00115       const PixelFormat &getPixelFormat() const { return pixelFormat; }
00116         
00118       void setPixelFormat(const PixelFormat &pixelFormat)
00119       { this->pixelFormat = pixelFormat; }
00120 
00124       size_t computeSize() const
00125       {
00126         // pitch is in bytes
00127         return isValid() ? (getPitch() * getHeight()) : 0;
00128       }
00129 
00131       void resize(const unsigned int newWidth, const unsigned int newHeight)
00132       {
00133         this->width = newWidth;
00134         this->height = newHeight;
00135       }
00136 
00140       const FrameBufferFormat resized(const unsigned int newWidth, 
00141                       const unsigned int newHeight) const
00142       {
00143         return FrameBufferFormat(newWidth, newHeight, pitch, pixelFormat);
00144       }
00145 
00146     private:
00148       unsigned int width;
00149 
00151       unsigned int height;
00152 
00159       unsigned int pitch;
00160 
00161       PixelFormat pixelFormat;
00162     };
00163 
00164     inline std::ostream &operator<<(std::ostream &o, const FrameBufferFormat &f)
00165     {
00166       o << f.getWidth() << "x" << f.getHeight() << ", pitch " << f.getPitch()
00167         << ", pixel format: " << f.getPixelFormat();
00168       return o;
00169     }
00170 
00171     typedef std::vector<FrameBufferFormat> BufferFormatList;
00172 
00173     //-----------------------------------------------------------------------------
00174 
00175     class FrameBuffer
00176     {
00177     public:
00178 
00182       FrameBuffer( void* data, const FrameBufferFormat &format ) :
00183         data(data), format(format)
00184       {
00185       }
00186 
00190       virtual ~FrameBuffer() {}
00191 
00193       void *getData() { return data; }
00194 
00196       const void *getData() const { return data; }
00197 
00199       const FrameBufferFormat &getFormat() const { return format; }
00200 
00201     protected:
00202 
00204       void setData(void *data) { this->data = data; }
00205 
00207       void setFormat(const FrameBufferFormat &format) { this->format = format; }
00208 
00209     private:
00214       void *data;
00215 
00219       FrameBufferFormat format;
00220     };
00221 
00222     typedef boost::shared_ptr<FrameBuffer> FrameBufferPtr;
00223     
00224     //-----------------------------------------------------------------------------
00225 
00226     class RenderTarget
00227     {
00228       public:
00229 
00230       virtual ~RenderTarget() { }
00231 
00240       virtual FrameBufferPtr getBuffer() throw(BALL::Exception::NoBufferAvailable) = 0;
00241 
00242       virtual FrameBufferFormat getFormat() const = 0;
00243 
00250       virtual void releaseBuffer(FrameBufferPtr buffer) = 0;
00251 
00254       virtual bool init() = 0;
00255 
00258       virtual bool resize(const unsigned int width, const unsigned int height) = 0;
00259 
00262       virtual void refresh() = 0;
00263 
00264       /* Prepare the window for rendering, e.g., make it current if necessary.
00265        */
00266       virtual void prepareRendering() = 0;
00267     };
00268 
00269   } //namespace VIEW
00270 
00271 } // namespace BALL
00272 
00273 #endif // BALL_VIEW_RENDERING_RENDERTARGET_H