00001
00002 #ifndef _TP_FRAMEBUFFER_H
00003 #define _TP_FRAMEBUFFER_H
00004
00005 #include "GLResource.h"
00006 #include "GL/GLPlatform.h"
00007 #include "GL/TPTexture.h"
00008
00009
00010 namespace gk {
00011
00012 class GLRendertarget;
00013
00014
00015 class FramebufferState
00016 {
00017 GLRendertarget *m_framebuffer;
00018 GLenum m_target;
00019 int m_index;
00020
00021 FramebufferState( const int index, const GLenum target )
00022 :
00023 m_framebuffer(NULL),
00024 m_target(target),
00025 m_index(index)
00026 {}
00027
00028 public:
00029
00030 FramebufferState( )
00031 :
00032 m_framebuffer(NULL),
00033 m_target(GL_NONE),
00034 m_index(-1)
00035 {}
00036
00037
00038 ~FramebufferState( ) {}
00039
00040
00041 FramebufferState& operator= ( const FramebufferState& b );
00042
00043
00044 FramebufferState& setFramebuffer( GLRendertarget *framebuffer );
00045
00046 FramebufferState& reset( );
00047
00048
00049 GLRendertarget *getFramebuffer( )
00050 {
00051 return m_framebuffer;
00052 }
00053
00054
00055 const GLRendertarget *getFramebuffer( ) const
00056 {
00057 return m_framebuffer;
00058 }
00059
00060
00061 GLenum target( ) const
00062 {
00063 assert(m_index != -1);
00064 return m_target;
00065 }
00066
00067
00068 static int init( );
00069 };
00070
00071 enum
00072 {
00073 READ= 0,
00074 DRAW= 1
00075 };
00076
00077 extern FramebufferState Framebuffers[2];
00078 extern FramebufferState ActiveFramebuffers[2];
00079
00080
00081 FramebufferState& setFramebuffer( GLRendertarget *framebuffer );
00082
00083 FramebufferState& resetFramebuffer( );
00084
00085
00086 FramebufferState& setReadFramebuffer( GLRendertarget *framebuffer );
00087
00088 FramebufferState& resetReadFramebuffer( );
00089
00090
00091 int BlitFramebuffer( const int sx_min, const int sy_min, const int sx_max, const int sy_max,
00092 const int dx_min, const int dy_min, const int dx_max, const int dy_max,
00093 const GLbitfield buffer_bits= GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, const GLenum filter= GL_NEAREST );
00094
00095
00096 inline
00097 int BlitFramebuffer( const int sx_min, const int sy_min, const int width, const int height,
00098 const int dx_min, const int dy_min,
00099 const GLbitfield buffer_bits= GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, const GLenum filter= GL_NEAREST )
00100 {
00101 return BlitFramebuffer(
00102 sx_min, sy_min, sx_min + width, sy_min + height,
00103 dx_min, dy_min, dx_min + width, dy_min + height, buffer_bits, filter );
00104 }
00105
00106
00107 enum
00108 {
00109 COLOR0= 0,
00110 COLOR1= 1,
00111 COLOR2= 2,
00112 COLOR3= 3,
00113 COLOR4= 4,
00114 COLOR5= 5,
00115 COLOR6= 6,
00116 COLOR7= 7,
00117 LAST_COLOR= 8,
00118 DEPTH= 9,
00119 LAST= 10
00120 };
00121
00122
00123 enum
00124 {
00125 COLOR0_BIT= 1<<COLOR0,
00126 COLOR1_BIT= 1<<COLOR1,
00127 COLOR2_BIT= 1<<COLOR2,
00128 COLOR3_BIT= 1<<COLOR3,
00129 COLOR4_BIT= 1<<COLOR4,
00130 COLOR5_BIT= 1<<COLOR5,
00131 COLOR6_BIT= 1<<COLOR6,
00132 COLOR7_BIT= 1<<COLOR7,
00133 DEPTH_BIT= 1<<DEPTH
00134 };
00135
00136
00137 enum
00138 {
00139 DRAW0= 0,
00140 DRAW1= 1,
00141 DRAW2= 2,
00142 DRAW3= 3,
00143 DRAW4= 4,
00144 DRAW5= 5,
00145 DRAW6= 6,
00146 DRAW7= 7,
00147 };
00148
00149
00150
00151 class GLRendertarget : public GLResource
00152 {
00153 protected:
00154 std::vector<GLTexture *> m_textures;
00155 std::vector<GLenum> m_draw_buffers;
00156 int m_draw_buffers_count;
00157 int m_width;
00158 int m_height;
00159 unsigned int m_name;
00160
00161
00162 GLRendertarget( const GLRendertarget& );
00163 GLRendertarget& operator= ( const GLRendertarget& );
00164
00165 public:
00166
00167 GLRendertarget( );
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 int attachTexture( const unsigned int state, const unsigned int buffer, GLTexture *texture );
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 GLRendertarget( const unsigned int state, const int w, const int h, const unsigned int buffer_bits );
00200
00201
00202 virtual ~GLRendertarget( );
00203
00204
00205 int createGLResource( )
00206 {
00207 return (m_name != 0) ? 0 : -1;
00208 }
00209
00210
00211 int releaseGLResource( )
00212 {
00213 return (m_name != 0) ? 0 : -1;
00214 }
00215
00216
00217 int validate( const unsigned int state );
00218
00219
00220 int bind( const unsigned int state );
00221
00222 int unbind( const unsigned int state );
00223
00224
00225 GLuint name( ) const
00226 {
00227 return m_name;
00228 }
00229
00230
00231 int width( ) const
00232 {
00233 return m_width;
00234 }
00235
00236
00237 int height( ) const
00238 {
00239 return m_height;
00240 }
00241
00242
00243 GLTexture *texture( const unsigned int buffer );
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 int setBuffer( const unsigned int state, const unsigned int draw_buffer, const unsigned int buffer, const unsigned int level= 0 );
00278
00279 int setFaceBuffer( const unsigned int state, const GLenum face, const unsigned int draw_buffer, const unsigned int buffer, const unsigned int level= 0 );
00280
00281 int setLayerBuffer( const unsigned int state, const unsigned int layer, const unsigned int draw_buffer, const unsigned int buffer, const unsigned int level= 0 );
00282
00283
00284 int resetBuffer( const unsigned int state, const unsigned int draw_buffer );
00285
00286
00287 int updateBufferMipmap( const unsigned int buffer, const unsigned int unit= 0 );
00288 };
00289
00290
00291 class GLFramebuffer : public GLRendertarget
00292 {
00293 public:
00294
00295 GLFramebuffer( )
00296 :
00297 GLRendertarget( )
00298 {}
00299
00300
00301 GLFramebuffer( const int w, const int h, const unsigned int buffer_bits )
00302 :
00303 GLRendertarget(DRAW, w, h, buffer_bits)
00304 {}
00305
00306
00307 ~GLFramebuffer( ) {}
00308
00309
00310 int attachTexture( const unsigned int buffer, GLTexture *texture )
00311 {
00312 return GLRendertarget::attachTexture(DRAW, buffer, texture);
00313 }
00314
00315
00316 int validate( )
00317 {
00318 return GLRendertarget::validate(DRAW);
00319 }
00320
00321
00322 int bind( )
00323 {
00324 return GLRendertarget::bind(DRAW);
00325 }
00326
00327 int unbind( )
00328 {
00329 return GLRendertarget::unbind(DRAW);
00330 }
00331
00332
00333 int setDrawbuffer( const unsigned int draw_buffer, const unsigned int buffer, const unsigned int level= 0 )
00334 {
00335 return setBuffer(DRAW, draw_buffer, buffer, level);
00336 }
00337
00338 int setFaceDrawbuffer( const GLenum face, const unsigned int draw_buffer, const unsigned int buffer, const unsigned int level= 0 )
00339 {
00340 return setFaceBuffer(DRAW, face, draw_buffer, buffer, level);
00341 }
00342
00343 int setLayerDrawbuffer( const unsigned int layer, const unsigned int draw_buffer, const unsigned int buffer, const unsigned int level= 0 )
00344 {
00345 return setLayerBuffer(DRAW, layer, draw_buffer, buffer, level);
00346 }
00347
00348
00349 int updateMipmap( const unsigned int buffer, const unsigned int unit= 0 )
00350 {
00351 return updateBufferMipmap(buffer, unit);
00352 }
00353
00354
00355 int resetDrawbuffer( const unsigned int draw_buffer )
00356 {
00357 return resetBuffer(DRAW, draw_buffer);
00358 }
00359
00360
00361 int setReadbuffer( const unsigned int buffer, const unsigned int level= 0 )
00362 {
00363 return setBuffer(READ, DRAW0, buffer, level);
00364 }
00365
00366
00367 int setFaceReadbuffer( const GLenum face, const unsigned int buffer, const unsigned int level= 0 )
00368 {
00369 return setFaceBuffer(READ, face, DRAW0, buffer, level);
00370 }
00371
00372
00373 int setLayerReadbuffer( const unsigned int layer, const unsigned int buffer, const unsigned int level= 0 )
00374 {
00375 return setLayerBuffer(READ, layer, DRAW0, buffer, level);
00376 }
00377 };
00378
00379
00380 class GLReadFramebuffer : public GLRendertarget
00381 {
00382 public:
00383
00384 GLReadFramebuffer( )
00385 :
00386 GLRendertarget()
00387 {}
00388
00389
00390 GLReadFramebuffer( const int w, const int h, const unsigned int buffer_bits )
00391 :
00392 GLRendertarget(READ, w, h, buffer_bits)
00393 {}
00394
00395
00396 ~GLReadFramebuffer( ) {}
00397
00398
00399 int attachTexture( const unsigned int buffer, GLTexture *texture )
00400 {
00401 return GLRendertarget::attachTexture(READ, buffer, texture);
00402 }
00403
00404
00405 int validate( )
00406 {
00407 return GLRendertarget::validate(READ);
00408 }
00409
00410
00411 int bind( )
00412 {
00413 return GLRendertarget::bind(READ);
00414 }
00415
00416
00417 int unbind( )
00418 {
00419 return GLRendertarget::unbind(READ);
00420 }
00421
00422
00423 int setReadbuffer( const unsigned int buffer, const unsigned int level= 0 )
00424 {
00425 return GLRendertarget::setBuffer(READ, DRAW0, buffer, level);
00426 }
00427
00428
00429 int setFaceReadbuffer( const GLenum face, const unsigned int buffer, const unsigned int level= 0 )
00430 {
00431 return GLRendertarget::setFaceBuffer(READ, face, DRAW0, buffer, level);
00432 }
00433
00434
00435 int setLayerReadbuffer( const unsigned int layer, const unsigned int buffer, const unsigned int level= 0 )
00436 {
00437 return GLRendertarget::setLayerBuffer(READ, layer, DRAW0, buffer, level);
00438 }
00439 };
00440
00441 }
00442
00443 #endif