Carna  Version 3.0.1
Framebuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef FRAMEBUFFER_H_6014714286
13 #define FRAMEBUFFER_H_6014714286
14 
19 #include <Carna/Carna.h>
20 #include <Carna/base/noncopyable.h>
21 #include <Carna/base/math.h>
22 #include <stack>
23 #include <set>
24 
25 namespace Carna
26 {
27 
28 namespace base
29 {
30 
31 
32 
33 // ----------------------------------------------------------------------------------
34 // Framebuffer
35 // ----------------------------------------------------------------------------------
36 
49 class CARNA_LIB Framebuffer
50 {
51 
53 
54  math::Vector2ui size;
55 
56 public:
57 
61  const static unsigned int MAXIMUM_ALLOWED_COLOR_COMPONENTS = 8;
62 
68  Framebuffer( unsigned int width, unsigned int height, Texture< 2 >& renderTexture );
69 
76  static Texture< 2 >* createRenderTexture( bool floatingPoint = false );
77 
81  ~Framebuffer();
82 
86  bool hasRenderTexture( unsigned int location ) const;
87 
92  const Texture< 2 >& renderTexture( unsigned int location ) const;
93 
97  const unsigned int id;
98 
104  static void copyDepthAttachment( unsigned int srcId, unsigned int dstId, const Viewport& src, const Viewport& dst );
105 
112  static void copyColorAttachment
113  ( unsigned int srcId, unsigned int dstId
114  , const Viewport& src, const Viewport& dst
115  , unsigned int srcColorAttachment, unsigned int dstColorAttachment );
116 
121  static unsigned int currentId();
122 
126  void resize( const math::Vector2ui& size );
127 
130  void resize( unsigned int width, unsigned int height );
131 
135  inline unsigned int width() const
136  {
137  return size.x();
138  }
139 
143  inline unsigned int height() const
144  {
145  return size.y();
146  }
147 
148  // ------------------------------------------------------------------------------
149  // Framebuffer :: MinimalBinding
150  // ------------------------------------------------------------------------------
151 
162  class CARNA_LIB MinimalBinding
163  {
164 
166 
167  public:
168 
172  explicit MinimalBinding( Framebuffer& fbo );
173 
177  virtual ~MinimalBinding();
178 
188  void setColorComponent( Texture< 2 >& renderTexture, unsigned int location = 0 );
189 
195  void removeColorComponent( unsigned int location );
196 
203  Color readPixel( unsigned int x, unsigned int y, unsigned int location = 0 ) const;
204 
208  const Framebuffer& framebuffer() const;
209 
217  virtual void refresh() const;
218 
219  private:
220 
224  void bindFBO() const;
225 
226  protected:
227 
232 
233  }; // Framebuffer :: MinimalBinding
234 
235  // ------------------------------------------------------------------------------
236  // Framebuffer :: Binding
237  // ------------------------------------------------------------------------------
238 
246  class CARNA_LIB Binding : public MinimalBinding
247  {
248 
249  public:
250 
257  explicit Binding( Framebuffer& );
258 
259  protected:
260 
263  virtual void refresh();
264 
265  }; // Framebuffer :: Binding
266 
267  // ------------------------------------------------------------------------------
268 
269 private:
270 
271  Texture< 2 >* renderTextures[ MAXIMUM_ALLOWED_COLOR_COMPONENTS ];
272  const unsigned int depthBuffer;
273  std::set< unsigned int > boundColorBuffers;
274  class BindingStack;
275 
276  static void copy
277  ( unsigned int srcId, unsigned int dstId
278  , const Viewport& src, const Viewport& dst
279  , unsigned int flags
280  , unsigned int srcColorAttachment, unsigned int dstColorAttachment );
281 
282  static void copy
283  ( unsigned int srcId, unsigned int dstId
284  , unsigned int srcX0, unsigned int srcY0
285  , unsigned int dstX0, unsigned int dstY0
286  , unsigned int srcWidth, unsigned int srcHeight
287  , unsigned int dstWidth, unsigned int dstHeight
288  , unsigned int flags
289  , unsigned int srcColorAttachment, unsigned int dstColorAttachment );
290 
291 }; // Framebuffer
292 
293 
294 
295 // ----------------------------------------------------------------------------------
296 // CARNA_RENDER_TO_FRAMEBUFFER_EX
297 // ----------------------------------------------------------------------------------
298 
303 #define CARNA_RENDER_TO_FRAMEBUFFER_EX( fbo, binding, instructions ) \
304  { \
305  Carna::base::Framebuffer::Binding binding( fbo ); \
306  instructions; \
307  }
308 
309 
310 
311 // ----------------------------------------------------------------------------------
312 // CARNA_RENDER_TO_FRAMEBUFFER
313 // ----------------------------------------------------------------------------------
314 
317 #define CARNA_RENDER_TO_FRAMEBUFFER( fbo, instructions ) \
318  CARNA_RENDER_TO_FRAMEBUFFER_EX( fbo, fboBinding##__COUNTER__, instructions )
319 
320 
321 
322 } // namespace Carna :: base
323 
324 } // namespace Carna
325 
326 #endif // FRAMEBUFFER_H_6014714286
Maintains the binding of a Framebuffer in a RAII-manner.
Definition: Framebuffer.h:162
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
unsigned int height() const
Returns the framebuffer's height.
Definition: Framebuffer.h:143
Acts like MinimalBinding, but checks the bound framebuffer for validity and allows rendering...
Definition: Framebuffer.h:246
unsigned int width() const
Returns the framebuffer's width.
Definition: Framebuffer.h:135
const unsigned int id
Identifies the maintained framebuffer object.
Definition: Framebuffer.h:97
Represents a color. Objects from this class are copyable and assignable.
Definition: Color.h:41
Defines a rendering viewport. The viewport is a property of the current OpenGL context.
Definition: Viewport.h:48
Framebuffer & fbo
References the bound framebuffer.
Definition: Framebuffer.h:231
Eigen::Matrix< unsigned int, 2, 1 > Vector2ui
Defines vector.
Definition: math.h:200
Maintains a framebuffer object that supports up to 8 color components simultaneously.
Definition: Framebuffer.h:49
#define NON_COPYABLE
Features class it is placed in as non-copyable.
Definition: noncopyable.h:109