1 #ifndef __GPU__FRAMEBUFFER_H__
2 #define __GPU__FRAMEBUFFER_H__
3 
4 
5 
6 
7 #include "commonDefs.h"
8 #include "Texture.h"
9 #include <map>
10 #include <vector>
11 
12 
13 /*
14  *
15  *  CLASS DECLARATION.
16  *
17  */
18 
19 namespace GPU
20 {
21     class RenderBuffer : public InstantiatedObject
22     {
23         /********************\
24         | Member variable(s) |
25         \********************/
26     private:
27         GLuint              m_Id;
28         unsigned int        m_Width;
29         unsigned int        m_Height;
30 
31         /*****************************\
32         | Constructor(s) / destructor |
33         \*****************************/
34     public:
RenderBuffer()35         inline              RenderBuffer() :
36             m_Id(0),
37             m_Width(0),
38             m_Height(0)
39         {}
RenderBuffer(const GLuint format,const unsigned int w,const unsigned int h)40         inline              RenderBuffer( const GLuint format,
41                                           const unsigned int w,
42                                           const unsigned int h ) :
43             m_Id(0),
44             m_Width(0),
45             m_Height(0)
46         {
47             Create( format, w, h );
48         }
~RenderBuffer()49         inline              ~RenderBuffer()                 { Release(); }
50 
51         /********************\
52         | Member function(s) |
53         \********************/
54     protected:
55         bool                Allocate();
56         bool                Unallocate();
57 
58     public:
59         bool                Create( const GLuint format,
60                                     const unsigned int w,
61                                     const unsigned int h );
62 
Width()63         inline unsigned int Width() const                   { return m_Width; }
Height()64         inline unsigned int Height() const                  { return m_Height; }
Id()65         inline GLuint       Id() const                      { return m_Id; }
66         GLint               InternalFormat() const;
67     };
68 
69 
70     class FrameBuffer
71     {
72         /******************\
73         | Internal type(s) |
74         \******************/
75     private:
76         typedef std::map<GLuint,InstantiatedObject*> BufferMap;
77 
78         /********************\
79         | Member variable(s) |
80         \********************/
81     private:
82         GLuint              m_Id;
83         GLuint              m_Width;
84         GLuint              m_Height;
85         BufferMap           m_Buffers;
86         std::vector<GLenum> m_EnabledBuffers;
87 
88         /*****************************\
89         | Constructor(s) / destructor |
90         \*****************************/
91     public:
FrameBuffer()92         inline  FrameBuffer() :
93             m_Id(0),
94             m_Width(0),
95             m_Height(0)
96         {}
FrameBuffer(const unsigned int w,const unsigned int h)97         inline  FrameBuffer( const unsigned int w,
98                              const unsigned int h ) :
99             m_Id(0),
100             m_Width(0),
101             m_Height(0)
102         {
103             Create(w,h);
104         }
~FrameBuffer()105         inline  ~FrameBuffer()                                          { Release(); }
106 
107         /********************\
108         | Member function(s) |
109         \********************/
110     public:
111         bool                Create( const unsigned int w,
112                                     const unsigned int h );
IsCreated()113         inline bool         IsCreated() const                           { return m_Id != 0; }
114         void                Release();
115 
Width()116         inline GLuint       Width() const                               { return m_Width; }
Height()117         inline GLuint       Height() const                              { return m_Height; }
Id()118         inline GLuint       Id() const                                  { return m_Id; }
119 
120         bool                Attach( const GLuint attachment,
121                                     const GLuint format );
122         bool                Attach( const GLuint attachment,
123                                     const GPU::RenderBuffer &buff );
Attach(const GLuint attachment,const GPU::RenderBuffer * buff)124         inline bool         Attach( const GLuint attachment,
125                                     const GPU::RenderBuffer *buff )     { return buff? Attach(attachment,*buff) : false; }
126         bool                Attach( const GLuint attachment,
127                                     const GPU::Texture2D &tex );
Attach(const GLuint attachment,const GPU::Texture2D * tex)128         inline bool         Attach( const GLuint attachment,
129                                     const GPU::Texture2D *tex )         { return tex? Attach(attachment,*tex) : false; }
130         void                Detach( const GLuint attachment );
131 
132         void                Enable ( const GLuint attachment );
133         void                Disable( const GLuint attachment );
134 
135         void                Bind() const;
Unbind()136         inline void         Unbind() const                              { glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ); }
137 
138         bool                DumpTo( const GLuint attachment,
139                                     void *buffer,
140                                     const GLuint format,
141                                     const GLuint type ) const;
142         bool                DumpTo( const GLuint attachment,
143                                     void *buffer,
144                                     const unsigned int x,
145                                     const unsigned int y,
146                                     const unsigned int w,
147                                     const unsigned int h,
148                                     const GLuint format,
149                                     const GLuint type ) const;
150 
151         const Texture2D*    AsTexture( const GLuint attachment ) const;
152         const RenderBuffer* AsBuffer( const GLuint attachment ) const;
153     };
154 };
155 
156 
157 
158 
159 #endif //__GPU__FRAMEBUFFER_H__
160