00001
00002 #ifndef _TP_FRAMEBUFFER_H
00003 #define _TP_FRAMEBUFFER_H
00004
00005 #include "GL/GLPlatform.h"
00006 #include "GLResource.h"
00007 #include "GL/TPTexture.h"
00008
00009
00010 namespace gk {
00011
00012
00013 enum
00014 {
00015 COLOR0= 0,
00016 COLOR1= 1,
00017 COLOR2= 2,
00018 COLOR3= 3,
00019 COLOR4= 4,
00020 COLOR5= 5,
00021 COLOR6= 6,
00022 COLOR7= 7,
00023 LAST_COLOR= 8,
00024 DEPTH= 9,
00025 LAST= 10
00026 };
00027
00028
00029 enum
00030 {
00031 COLOR0_BIT= 1<<COLOR0,
00032 COLOR1_BIT= 1<<COLOR1,
00033 COLOR2_BIT= 1<<COLOR2,
00034 COLOR3_BIT= 1<<COLOR3,
00035 COLOR4_BIT= 1<<COLOR4,
00036 COLOR5_BIT= 1<<COLOR5,
00037 COLOR6_BIT= 1<<COLOR6,
00038 COLOR7_BIT= 1<<COLOR7,
00039 DEPTH_BIT= 1<<DEPTH
00040 };
00041
00042
00043 enum
00044 {
00045 DRAW0= 0,
00046 DRAW1= 1,
00047 DRAW2= 2,
00048 DRAW3= 3,
00049 DRAW4= 4,
00050 DRAW5= 5,
00051 DRAW6= 6,
00052 DRAW7= 7,
00053 };
00054
00055
00056
00057 class GLRendertarget : public GLResource
00058 {
00059
00060 GLRendertarget( const GLRendertarget& );
00061 GLRendertarget& operator= ( const GLRendertarget& );
00062
00063 protected:
00064 std::vector<GLTexture *> m_textures;
00065 std::vector<GLenum> m_draw_buffers;
00066 int m_width;
00067 int m_height;
00068
00069 template< typename Texture >
00070 int attach_buffer( const unsigned int buffer, Texture *texture )
00071 {
00072 if(m_name == 0)
00073 return -1;
00074 if(buffer >= LAST)
00075 return -1;
00076 if(texture == NULL)
00077 return -1;
00078
00079 #ifdef VERBOSE_DEBUG
00080 if(buffer < LAST_COLOR)
00081 printf("GLFramebuffer::attachTexture( ): buffer 0x%x, texture %d.\n",
00082 GL_COLOR_ATTACHMENT0 + buffer, texture->name());
00083 else if(buffer == DEPTH)
00084 printf("GLFramebuffer::attachTexture( ): buffer 0x%x, texture %d\n.",
00085 GL_DEPTH_ATTACHMENT, texture->name());
00086 #endif
00087
00088
00089 if(m_width > 0 && texture->width() != m_width)
00090 return -1;
00091 m_width= texture->width();
00092 if(m_height > 0 && texture->height() != m_height)
00093 return -1;
00094 m_height= texture->height();
00095
00096
00097 m_textures[buffer]= texture;
00098 if(buffer >= LAST_COLOR)
00099 return GL_DEPTH_ATTACHMENT;
00100
00101
00102 int draw_buffer= -1;
00103 m_draw_buffers.clear();
00104 for(int i= 0; i < LAST_COLOR; i++)
00105 if(m_textures[i] != NULL)
00106 {
00107 if(i == buffer)
00108 draw_buffer= GL_COLOR_ATTACHMENT0 + (unsigned int) m_draw_buffers.size();
00109 m_draw_buffers.push_back(GL_COLOR_ATTACHMENT0 + (unsigned int) m_draw_buffers.size());
00110 }
00111
00112
00113 return draw_buffer;
00114 }
00115
00116 public:
00117
00118 GLRendertarget( );
00119
00120
00121
00122 int attachTexture( const GLenum target, const unsigned int buffer, GLTexture2D *texture, const int level= 0 );
00123 int attachTexture( const GLenum target, const unsigned int buffer, GLDepthTexture *texture, const int level= 0 );
00124 int attachTexture( const GLenum target, const unsigned int buffer, GLTexture2DArray *texture, const int layer, const int level= 0 );
00125 int attachTexture( const GLenum target, const unsigned int buffer, GLTextureCube *texture, const GLenum face, const int level= 0 );
00126
00127
00128
00129
00130
00131
00132
00133
00134 GLRendertarget( const GLenum target, const int w, const int h, const unsigned int buffer_bits );
00135
00136
00137 virtual ~GLRendertarget( );
00138
00139
00140 int createGLResource( )
00141 {
00142 return (m_name != 0) ? 0 : -1;
00143 }
00144
00145
00146 int releaseGLResource( )
00147 {
00148 return (m_name != 0) ? 0 : -1;
00149 }
00150
00151
00152 int validate( const GLenum target );
00153
00154
00155 int width( ) const
00156 {
00157 return m_width;
00158 }
00159
00160
00161 int height( ) const
00162 {
00163 return m_height;
00164 }
00165
00166
00167 GLTexture *texture( const unsigned int buffer );
00168
00169
00170 GLDepthTexture *zbuffer( );
00171
00172
00173
00174
00175
00176
00177
00178
00179 const std::vector<GLenum>& drawBuffers( ) const
00180 {
00181 return m_draw_buffers;
00182 }
00183 };
00184
00185
00186 class GLFramebuffer : public GLRendertarget
00187 {
00188 public:
00189
00190 GLFramebuffer( )
00191 :
00192 GLRendertarget( )
00193 {}
00194
00195
00196 GLFramebuffer( const int w, const int h, const unsigned int buffer_bits )
00197 :
00198 GLRendertarget(GL_DRAW_FRAMEBUFFER, w, h, buffer_bits)
00199 {}
00200
00201
00202 ~GLFramebuffer( ) {}
00203
00204
00205 int attachTexture( const unsigned int buffer, GLTexture2D *texture, const int level= 0 )
00206 {
00207 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, level);
00208 }
00209
00210 int attachTexture( const GLenum target, const unsigned int buffer, GLDepthTexture *texture, const int level= 0 )
00211 {
00212 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, level);
00213 }
00214
00215 int attachTexture( const GLenum target, const unsigned int buffer, GLTexture2DArray *texture, const int layer, const int level= 0 )
00216 {
00217 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, layer, level);
00218 }
00219
00220 int attachTexture( const GLenum target, const unsigned int buffer, GLTextureCube *texture, const GLenum face, const int level= 0 )
00221 {
00222 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, face, level);
00223 }
00224
00225
00226 int validate( )
00227 {
00228 return GLRendertarget::validate(GL_DRAW_FRAMEBUFFER);
00229 }
00230 };
00231
00232
00233 class GLReadFramebuffer : public GLRendertarget
00234 {
00235 public:
00236
00237 GLReadFramebuffer( )
00238 :
00239 GLRendertarget()
00240 {}
00241
00242
00243 GLReadFramebuffer( const int w, const int h, const unsigned int buffer_bits )
00244 :
00245 GLRendertarget(GL_READ_FRAMEBUFFER, w, h, buffer_bits)
00246 {}
00247
00248
00249 ~GLReadFramebuffer( ) {}
00250
00251
00252 int attachTexture( const unsigned int buffer, GLTexture2D *texture, const int level= 0 )
00253 {
00254 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, level);
00255 }
00256
00257 int attachTexture( const GLenum target, const unsigned int buffer, GLDepthTexture *texture, const int level= 0 )
00258 {
00259 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, level);
00260 }
00261
00262 int attachTexture( const GLenum target, const unsigned int buffer, GLTexture2DArray *texture, const int layer, const int level= 0 )
00263 {
00264 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, layer, level);
00265 }
00266
00267 int attachTexture( const GLenum target, const unsigned int buffer, GLTextureCube *texture, const GLenum face, const int level= 0 )
00268 {
00269 return GLRendertarget::attachTexture(GL_DRAW_FRAMEBUFFER, buffer, texture, face, level);
00270 }
00271
00272
00273 int validate( )
00274 {
00275 return GLRendertarget::validate(GL_READ_FRAMEBUFFER);
00276 }
00277 };
00278
00279 }
00280
00281 #endif