fbo.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * MeshLab                                                           o o     *
00003 * An extendible mesh processor                                    o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2005, 2009                                          \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 #ifndef __FBO_H__
00025 #define __FBO_H__
00026 
00027 #pragma warning(disable : 4250)
00028 
00029 #include <map>
00030 #include <vector>
00031 
00032 #include <GL/glew.h>
00033 #include <GL/glut.h>
00034 
00035 #include "gl_object.h"
00036 
00037 class FrameBufferSemantic
00038 {
00039 public:
00040         typedef enum
00041         {
00042                 COLOR,
00043                 DEPTH,
00044                 STENCIL
00045         } FBSType;
00046 
00047         virtual FBSType Semantic(void) const = 0;
00048         virtual bool ValidateFormat(GLenum format) const = 0;
00049 
00050         static bool ValidateFormat(FBSType type, GLenum format)
00051         {
00052                 switch (type)
00053                 {
00054                         case COLOR   : return FrameBufferSemantic::ValidateColor(format);
00055                         case DEPTH   : return FrameBufferSemantic::ValidateDepth(format);
00056                         case STENCIL : return FrameBufferSemantic::ValidateStencil(format);
00057                         default      : return false;
00058                 }
00059         }
00060 
00061         static bool ValidateColor(GLenum type)
00062         {
00063                 return true;
00064         }
00065 
00066         static bool ValidateDepth(GLenum type)
00067         {
00068                 return  true;
00069         }
00070 
00071         static bool ValidateStencil(GLenum type)
00072         {
00073                 return true;
00074         }
00075 };
00076 
00077 class Texture : public GLObject, public Bindable, public FrameBufferSemantic
00078 {
00079 public:
00080         Texture(void) : GLObject(), Bindable(), FrameBufferSemantic()
00081         {
00082                 this->format = GL_NONE;
00083         }
00084 
00085         void Gen(void)
00086         {
00087                 this->Del();
00088                 glGenTextures(1, &(this->objectID));
00089         }
00090 
00091         void Del(void)
00092         {
00093                 if (this->objectID == 0) return;
00094                 glDeleteTextures(1, &(this->objectID));
00095                 this->objectID = 0;
00096         }
00097 
00098         GLenum Format(void) const
00099         {
00100                 return this->format;
00101         }
00102 
00103         virtual GLint Dimensions(void) const = 0;
00104 
00105         virtual GLsizei Size(const unsigned int i) const = 0;
00106 
00107         virtual GLenum Target(void) const = 0;
00108 
00109 protected:
00110         GLenum format;
00111 
00112         void DoBind(void)
00113         {
00114                 glBindTexture(this->Target(), this->objectID);
00115         }
00116 
00117         void DoUnbind(void)
00118         {
00119                 glBindTexture(this->Target(), 0);
00120         }
00121 };
00122 
00123 class ColorTexture : public virtual Texture
00124 {
00125 public:
00126         ColorTexture(void) : Texture()
00127         {
00128         }
00129 
00130         FrameBufferSemantic::FBSType Semantic(void) const
00131         {
00132                 return FrameBufferSemantic::COLOR;
00133         }
00134 
00135         bool ValidateFormat(GLenum format) const
00136         {
00137                 return FrameBufferSemantic::ValidateColor(format);
00138         }
00139 };
00140 
00141 class DepthTexture : public virtual Texture
00142 {
00143 public:
00144         DepthTexture(void) : Texture()
00145         {
00146         }
00147 
00148         FrameBufferSemantic::FBSType Semantic(void) const
00149         {
00150                 return FrameBufferSemantic::DEPTH;
00151         }
00152 
00153         bool ValidateFormat(GLenum format) const
00154         {
00155                 return FrameBufferSemantic::ValidateDepth(format);
00156         }
00157 };
00158 
00159 class StencilTexture : public virtual Texture
00160 {
00161 public:
00162         StencilTexture(void) : Texture()
00163         {
00164         }
00165 
00166         FrameBufferSemantic::FBSType Semantic(void) const
00167         {
00168                 return FrameBufferSemantic::STENCIL;
00169         }
00170 
00171         bool ValidateFormat(GLenum format) const
00172         {
00173                 return FrameBufferSemantic::ValidateStencil(format);
00174         }
00175 };
00176 
00177 class Texture1D : public virtual Texture
00178 {
00179 public:
00180         Texture1D(void) : Texture()
00181         {
00182                 this->dims[0] = 0;
00183                 this->wraps[0] = GL_CLAMP_TO_EDGE;
00184         }
00185 
00186         GLsizei Width(void) const
00187         {
00188                 return this->dims[0];
00189         }
00190 
00191         GLint Dimensions(void) const
00192         {
00193                 return 1;
00194         }
00195 
00196         GLsizei Size(const unsigned int i) const
00197         {
00198                 if (i > 0) return 0;
00199                 return this->dims[0];
00200         }
00201 
00202         GLenum Target(void) const
00203         {
00204                 return GL_TEXTURE_1D;
00205         }
00206 
00207         bool Set(GLint level, GLint internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid * pixels)
00208         {
00209                 if (!this->ValidateFormat(internalFormat)) return false;
00210 
00211                 this->Bind();
00212 
00213                 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00214                 glTexImage1D(GL_TEXTURE_1D, level, internalFormat, width, border, format, type, pixels);
00215 
00216                 this->Unbind();
00217 
00218                 this->format = internalFormat;
00219                 this->dims[0] = width;
00220 
00221                 return true;
00222         }
00223 
00224 protected:
00225         GLsizei dims[1];
00226         GLenum wraps[1];
00227 };
00228 
00229 class Texture2D : public virtual Texture
00230 {
00231 public:
00232         Texture2D(void) : Texture()
00233         {
00234                 this->dims[0] = 0;
00235                 this->dims[1] = 0;
00236         }
00237 
00238         GLsizei Width(void) const
00239         {
00240                 return this->dims[0];
00241         }
00242 
00243         GLsizei Height(void) const
00244         {
00245                 return this->dims[1];
00246         }
00247 
00248         GLint Dimensions(void) const
00249         {
00250                 return 2;
00251         }
00252 
00253         GLsizei Size(const unsigned int i) const
00254         {
00255                 if (i > 1) return 0;
00256                 return this->dims[i];
00257         }
00258 
00259         GLenum Target(void) const
00260         {
00261                 return GL_TEXTURE_2D;
00262         }
00263 
00264         bool Set(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * pixels)
00265         {
00266                 if (!this->ValidateFormat(internalFormat)) return false;
00267 
00268                 this->Bind();
00269 
00270                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00271                 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, border, format, type, pixels);
00272 
00273                 this->Unbind();
00274 
00275                 this->format = internalFormat;
00276                 this->dims[0] = width;
00277                 this->dims[1] = height;
00278 
00279                 return true;
00280         }
00281 
00282 protected:
00283         GLsizei dims[2];
00284 
00285         void DoBind(void)
00286         {
00287                 glBindTexture(GL_TEXTURE_2D, this->objectID);
00288         }
00289 
00290         void DoUnbind(void)
00291         {
00292                 glBindTexture(GL_TEXTURE_2D, 0);
00293         }
00294 };
00295 
00296 class Texture3D : public virtual Texture
00297 {
00298 public:
00299         Texture3D(void) : Texture()
00300         {
00301                 this->dims[0] = 0;
00302                 this->dims[1] = 0;
00303                 this->dims[2] = 0;
00304         }
00305 
00306         GLsizei Width(void) const
00307         {
00308                 return this->dims[0];
00309         }
00310 
00311         GLsizei Height(void) const
00312         {
00313                 return this->dims[1];
00314         }
00315 
00316         GLsizei Depth(void) const
00317         {
00318                 return this->dims[2];
00319         }
00320 
00321         GLint Dimensions(void) const
00322         {
00323                 return 3;
00324         }
00325 
00326         GLsizei Size(const unsigned int i) const
00327         {
00328                 if (i > 2) return 0;
00329                 return this->dims[i];
00330         }
00331 
00332         GLenum Target(void) const
00333         {
00334                 return GL_TEXTURE_3D;
00335         }
00336 
00337         bool Set(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid * pixels)
00338         {
00339                 if (!this->ValidateFormat(internalFormat)) return false;
00340 
00341                 this->Bind();
00342 
00343                 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00344                 glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, width, height, depth, border, format, type, pixels);
00345 
00346                 this->Unbind();
00347 
00348                 this->format = internalFormat;
00349                 this->dims[0] = width;
00350                 this->dims[1] = height;
00351                 this->dims[2] = depth;
00352 
00353                 return true;
00354         }
00355 
00356 protected:
00357         GLsizei dims[3];
00358 
00359         void DoBind(void)
00360         {
00361                 glBindTexture(GL_TEXTURE_3D, this->objectID);
00362         }
00363 
00364         void DoUnbind(void)
00365         {
00366                 glBindTexture(GL_TEXTURE_3D, 0);
00367         }
00368 };
00369 
00370 class ColorTexture1D : public virtual ColorTexture, public virtual Texture1D
00371 {
00372 public:
00373         ColorTexture1D(void) : ColorTexture(), Texture1D()
00374         {
00375         }
00376 };
00377 
00378 class ColorTexture2D : public virtual ColorTexture, public virtual Texture2D
00379 {
00380 public:
00381         ColorTexture2D(void) : ColorTexture(), Texture2D()
00382         {
00383         }
00384 };
00385 
00386 class ColorTexture3D : public virtual ColorTexture, public virtual Texture3D
00387 {
00388 public:
00389         ColorTexture3D(void) : ColorTexture(), Texture3D()
00390         {
00391         }
00392 };
00393 
00394 class DepthTexture2D : public virtual DepthTexture, public virtual Texture2D
00395 {
00396 public:
00397         DepthTexture2D(void) : DepthTexture(), Texture2D()
00398         {
00399         }
00400 };
00401 
00402 class StencilTexture2D : public virtual StencilTexture, public virtual Texture2D
00403 {
00404 public:
00405         StencilTexture2D(void) : StencilTexture(), Texture2D()
00406         {
00407         }
00408 };
00409 
00410 class FrameBuffer;
00411 
00412 class RenderTarget : public GLObject, public Bindable, public FrameBufferSemantic
00413 {
00414 friend class FrameBuffer;
00415 
00416 public:
00417         typedef enum
00418         {
00419                 BUFFER,
00420                 TEXTURE
00421         } RTStorageType;
00422 
00423         RenderTarget(void) : GLObject(), Bindable(), FrameBufferSemantic()
00424         {
00425                 this->frameBuffer = 0;
00426         }
00427 
00428         bool Attach(FrameBuffer * fb);
00429 
00430         bool Detach(void);
00431 
00432         FrameBuffer * GetFrameBuffer(void)
00433         {
00434                 return this->frameBuffer;
00435         }
00436 
00437         const FrameBuffer * GetFrameBuffer(void) const
00438         {
00439                 return this->frameBuffer;
00440         }
00441 
00442         virtual GLsizei Width(void) const = 0;
00443         virtual GLsizei Height(void) const = 0;
00444         virtual GLenum Format(void) const = 0;
00445 
00446         virtual GLenum Attachment(void) const = 0;
00447 
00448         virtual bool ValidateAttachment(GLenum attachment) const = 0;
00449 
00450         virtual RTStorageType StorageType(void) const = 0;
00451 
00452 protected:
00453         FrameBuffer * frameBuffer;
00454 
00455         virtual bool BindToFB(void) = 0;
00456 };
00457 
00458 class BufferRenderTarget : public virtual RenderTarget
00459 {
00460 public:
00461         BufferRenderTarget(void) : RenderTarget()
00462         {
00463                 this->width = 0;
00464                 this->height = 0;
00465                 this->format = GL_NONE;
00466         }
00467 
00468         void Gen(void)
00469         {
00470                 this->Del();
00471                 glGenRenderbuffersEXT(1, &(this->objectID));
00472         }
00473 
00474         void Del(void)
00475         {
00476                 if (this->objectID == 0) return;
00477                 glDeleteRenderbuffersEXT(1, &(this->objectID));
00478                 this->objectID = 0;
00479         }
00480 
00481         GLsizei Width(void) const
00482         {
00483                 return this->width;
00484         }
00485 
00486         GLsizei Height(void) const
00487         {
00488                 return this->height;
00489         }
00490 
00491         GLenum Format(void) const
00492         {
00493                 return this->format;
00494         }
00495 
00496         RTStorageType StorageType(void) const
00497         {
00498                 return RenderTarget::BUFFER;
00499         }
00500 
00501         bool Set(GLenum format, GLsizei width, GLsizei height)
00502         {
00503                 if (!this->ValidateFormat(format)) return false;
00504 
00505                 this->Bind();
00506 
00507                 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, format, width, height);
00508 
00509                 this->Unbind();
00510 
00511                 this->format = format;
00512                 this->width = width;
00513                 this->height = height;
00514 
00515                 return true;
00516         }
00517 
00518         bool BindToFB(void)
00519         {
00520                 if (this->frameBuffer == 0) return false;
00521                 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, this->Attachment(), GL_RENDERBUFFER_EXT, this->objectID);
00522 
00523                 return true;
00524         }
00525 
00526 protected:
00527         GLenum format;
00528         GLsizei width;
00529         GLsizei height;
00530 
00531         void DoBind(void)
00532         {
00533                 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, this->objectID);
00534         }
00535 
00536         void DoUnbind(void)
00537         {
00538                 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
00539         }
00540 };
00541 
00542 class TextureRenderTarget : public virtual RenderTarget
00543 {
00544 public:
00545         TextureRenderTarget(void) : RenderTarget()
00546         {
00547                 this->tex = 0;
00548                 this->level = 0;
00549         }
00550 
00551         void Gen(void)
00552         {
00553         }
00554 
00555         void Del(void)
00556         {
00557         }
00558 
00559         GLsizei Width(void) const
00560         {
00561                 if (this->tex == 0) return 0;
00562                 return this->tex->Width();
00563         }
00564 
00565         GLsizei Height(void) const
00566         {
00567                 if (this->tex == 0) return 0;
00568                 return this->tex->Height();
00569         }
00570 
00571         GLenum Format(void) const
00572         {
00573                 if (this->tex == 0) return GL_NONE;
00574                 return this->tex->Format();
00575         }
00576 
00577         RTStorageType StorageType(void) const
00578         {
00579                 return RenderTarget::TEXTURE;
00580         }
00581 
00582         void SetLevel(GLint level)
00583         {
00584                 if (level < 0) level = 0;
00585                 this->level = level;
00586         }
00587 
00588         bool Set(Texture2D * tex)
00589         {
00590                 this->Unset();
00591 
00592                 if (tex == 0) return true;
00593                 if (this->Semantic() != tex->Semantic()) return false;
00594 
00595                 this->tex = tex;
00596 
00597                 return true;
00598         }
00599 
00600         bool Unset(void)
00601         {
00602                 this->tex = 0;
00603 
00604                 return true;
00605         }
00606 
00607         Texture2D * GetTexture(void)
00608         {
00609                 return (this->tex);
00610         }
00611 
00612         bool BindToFB(void)
00613         {
00614                 if (this->frameBuffer == 0) return false;
00615                 if (this->tex == 0) return false;
00616 
00617                 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, this->Attachment(), GL_TEXTURE_2D, this->tex->ObjectID(), this->level);
00618 
00619                 return true;
00620         }
00621 
00622 protected:
00623 
00624         Texture2D * tex;
00625         GLint level;
00626 
00627         void DoBind(void)
00628         {
00629         }
00630 
00631         void DoUnbind(void)
00632         {
00633         }
00634 };
00635 
00636 class ColorRenderTarget : public virtual RenderTarget
00637 {
00638 public:
00639         ColorRenderTarget(void) : RenderTarget()
00640         {
00641                 this->attachment = GL_COLOR_ATTACHMENT0_EXT;
00642         }
00643 
00644         FrameBufferSemantic::FBSType Semantic(void) const
00645         {
00646                 return FrameBufferSemantic::COLOR;
00647         }
00648 
00649         bool ValidateFormat(GLenum format) const
00650         {
00651                 return FrameBufferSemantic::ValidateColor(format);
00652         }
00653 
00654         bool ValidateAttachment(GLenum attachment) const
00655         {
00656                 return (((GL_COLOR_ATTACHMENT0_EXT) <= attachment) && (attachment <= (GL_COLOR_ATTACHMENT0_EXT + 3)));
00657         }
00658 
00659         void SetAttachment(GLenum attachment)
00660         {
00661                 if (!this->ValidateAttachment(attachment)) return;
00662                 this->attachment = attachment;
00663         }
00664 
00665         GLenum Attachment(void) const
00666         {
00667                 return this->attachment;
00668         }
00669 
00670 protected:
00671         GLenum attachment;
00672 };
00673 
00674 class DepthRenderTarget : public virtual RenderTarget
00675 {
00676 public:
00677         DepthRenderTarget(void) : RenderTarget()
00678         {
00679         }
00680 
00681         FrameBufferSemantic::FBSType Semantic(void) const
00682         {
00683                 return FrameBufferSemantic::DEPTH;
00684         }
00685 
00686         bool ValidateFormat(GLenum format) const
00687         {
00688                 return FrameBufferSemantic::ValidateDepth(format);
00689         }
00690 
00691         bool ValidateAttachment(GLenum attachment) const
00692         {
00693                 return (attachment == GL_DEPTH_ATTACHMENT_EXT);
00694         }
00695 
00696         GLenum Attachment(void) const
00697         {
00698                 return GL_DEPTH_ATTACHMENT_EXT;
00699         }
00700 };
00701 
00702 class StencilRenderTarget : public virtual RenderTarget
00703 {
00704 public:
00705         StencilRenderTarget(void) : RenderTarget()
00706         {
00707         }
00708 
00709         FrameBufferSemantic::FBSType Semantic(void) const
00710         {
00711                 return FrameBufferSemantic::STENCIL;
00712         }
00713 
00714         bool ValidateFormat(GLenum format) const
00715         {
00716                 return FrameBufferSemantic::ValidateStencil(format);
00717         }
00718 
00719         bool ValidateAttachment(GLenum attachment) const
00720         {
00721                 return (attachment == GL_STENCIL_ATTACHMENT_EXT);
00722         }
00723 
00724         GLenum Attachment(void) const
00725         {
00726                 return GL_STENCIL_ATTACHMENT_EXT;
00727         }
00728 };
00729 
00730 class ColorRenderBuffer : public virtual ColorRenderTarget, public virtual BufferRenderTarget
00731 {
00732 public:
00733         ColorRenderBuffer(void) : ColorRenderTarget(), BufferRenderTarget()
00734         {
00735         }
00736 };
00737 
00738 class ColorRenderTexture : public virtual ColorRenderTarget, public virtual TextureRenderTarget
00739 {
00740 public:
00741         ColorRenderTexture(void) : ColorRenderTarget(), TextureRenderTarget()
00742         {
00743         }
00744 
00745         ColorRenderTexture(Texture2D * tex) : ColorRenderTarget(), TextureRenderTarget()
00746         {
00747                 this->Set(tex);
00748         }
00749 };
00750 
00751 class DepthRenderBuffer : public virtual DepthRenderTarget, public virtual BufferRenderTarget
00752 {
00753 public:
00754         DepthRenderBuffer(void) : DepthRenderTarget(), BufferRenderTarget()
00755         {
00756         }
00757 };
00758 
00759 class DepthRenderTexture : public virtual DepthRenderTarget, public virtual TextureRenderTarget
00760 {
00761 public:
00762         DepthRenderTexture(void) : DepthRenderTarget(), TextureRenderTarget()
00763         {
00764         }
00765 
00766         DepthRenderTexture(Texture2D * tex) : DepthRenderTarget(), TextureRenderTarget()
00767         {
00768                 this->Set(tex);
00769         }
00770 };
00771 
00772 class StencilRenderBuffer : public virtual StencilRenderTarget, public virtual BufferRenderTarget
00773 {
00774 public:
00775         StencilRenderBuffer(void) : StencilRenderTarget(), BufferRenderTarget()
00776         {
00777         }
00778 };
00779 
00780 class StencilRenderTexture : public virtual StencilRenderTarget, public virtual TextureRenderTarget
00781 {
00782 public:
00783         StencilRenderTexture(void) : StencilRenderTarget(), TextureRenderTarget()
00784         {
00785         }
00786 
00787         StencilRenderTexture(Texture2D * tex) : StencilRenderTarget(), TextureRenderTarget()
00788         {
00789                 this->Set(tex);
00790         }
00791 };
00792 
00793 class FrameBuffer : public GLObject, public Bindable
00794 {
00795 friend class RenderTarget;
00796 
00797 public:
00798         FrameBuffer(void) : GLObject(), Bindable()
00799         {
00800         }
00801 
00802         void Gen(void)
00803         {
00804                 this->Del();
00805                 glGenFramebuffersEXT(1, &(this->objectID));
00806         }
00807 
00808         void Del(void)
00809         {
00810                 if (this->objectID == 0) return;
00811                 glDeleteFramebuffersEXT(1, &(this->objectID));
00812                 this->objectID = 0;
00813         }
00814 
00815         bool DetachAll(void)
00816         {
00817                 return false;
00818         }
00819 
00820         bool IsValid(void) const
00821         {
00822                 const GLenum s = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
00823                 //return (s == GL_FRAMEBUFFER_COMPLETE_EXT);
00824 
00825                 switch (s)
00826                 {
00827                         case GL_FRAMEBUFFER_COMPLETE_EXT:
00828                                 printf("ok\n");
00829                                 break;
00830                         case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
00831                                 printf("i a\n");
00832                                 break;
00833                         case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
00834                                 printf("i m a\n");
00835                                 break;
00836                         case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
00837                                 printf("i d a\n");
00838                                 break;
00839                         case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
00840                                 printf("i d\n");
00841                                 break;
00842                         case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
00843                                 printf("i f\n");
00844                                 break;
00845                         case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
00846                                 printf("i d b\n");
00847                                 break;
00848                         case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
00849                                 printf("i r b\n");
00850                                 break;
00851                         case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
00852                                 printf("u\n");
00853                                 break;
00854                         default:
00855                                 printf("def\n");
00856                                 break;
00857                 }
00858 
00859                 return (s == GL_FRAMEBUFFER_COMPLETE_EXT);
00860         }
00861 
00862 protected:
00863         typedef std::map<GLenum, RenderTarget *> RTMap;
00864         typedef RTMap::iterator RTMap_i;
00865         typedef RTMap::const_iterator RTMap_ci;
00866 
00867         RTMap renderTargets;
00868 
00869         void DoBind(void)
00870         {
00871                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->objectID);
00872 
00873                 std::vector<GLenum> colorDrawBuffers;
00874                 colorDrawBuffers.reserve(this->renderTargets.size());
00875 
00876                 for (RTMap_i rt=this->renderTargets.begin(); rt!=this->renderTargets.end(); ++rt)
00877                 {
00878                         RenderTarget * prt = (*rt).second;
00879                         if (prt->Semantic() == FrameBufferSemantic::COLOR)
00880                         {
00881                                 colorDrawBuffers.push_back(prt->Attachment());
00882                         }
00883                         prt->BindToFB();
00884                 }
00885 
00886                 const GLsizei sz = (GLsizei)(colorDrawBuffers.size());
00887                 if (sz > 0)
00888                 {
00889                         glDrawBuffers(sz, &(colorDrawBuffers[0]));
00890                 }
00891         }
00892 
00893         void DoUnbind(void)
00894         {
00895                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00896         }
00897 
00898         bool AddRT(RenderTarget * rt)
00899         {
00900                 if (rt == 0) return false;
00901                 RTMap_i it = this->renderTargets.find(rt->Attachment());
00902                 if (it == this->renderTargets.end())
00903                 {
00904                         this->renderTargets.insert(std::make_pair(rt->Attachment(), rt));
00905                         return true;
00906                 }
00907                 return false;
00908         }
00909 
00910         bool RemoveRT(RenderTarget * rt)
00911         {
00912                 if (rt == 0) return false;
00913                 RTMap_i it = this->renderTargets.find(rt->Attachment());
00914                 if ((*it).second == rt)
00915                 {
00916                         this->renderTargets.erase(it);
00917                         return true;
00918                 }
00919                 return false;
00920         }
00921 };
00922 
00923 
00924 
00925 bool RenderTarget::Attach(FrameBuffer * fb)
00926 {
00927         this->Detach();
00928         if (fb == 0) return true;
00929 
00930         if (fb->AddRT(this))
00931         {
00932                 this->frameBuffer = fb;
00933                 return true;
00934         }
00935 
00936         return false;
00937 }
00938 
00939 bool RenderTarget::Detach(void)
00940 {
00941         if (this->frameBuffer == 0) return false;
00942         this->frameBuffer->RemoveRT(this);
00943         this->frameBuffer = 0;
00944 
00945         return true;
00946 }
00947 
00948 #endif  // __FBO_H__


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:30:58