GLFrameBufferObject.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <GLEXT/GLFrameBufferObject.h>
00022 #include <GLEXT/GLStateExtension.h>
00023 #include <common/DefinesAssert.h>
00024 
00025 GLFrameBufferObject::GLFrameBufferObject() : 
00026         frameBufferObject_(0), depthBufferObject_(0),
00027         texture_(0), bound_(false)
00028 {
00029 }
00030 
00031 GLFrameBufferObject::~GLFrameBufferObject()
00032 {
00033         destroy();
00034 }
00035 
00036 bool GLFrameBufferObject::create(GLTexture &texture, bool withDepth)
00037 {
00038         if (!GLStateExtension::hasFBO()) {
00039                 S3D::dialogExit("GLFrameBufferObject", "NO FBO");
00040         }
00041 
00042         texture_ = &texture;
00043 
00044         // create and bind depth buffer if requested
00045         if (withDepth) {
00046                 glGenRenderbuffersEXT(1, &depthBufferObject_);
00047                 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBufferObject_);
00048                 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT,
00049                         texture.getWidth(), texture.getHeight());
00050         }
00051 
00052         // create and bind FBO
00053         glGenFramebuffersEXT(1, &frameBufferObject_);
00054         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferObject_);
00055         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
00056                 GL_TEXTURE_2D, texture.getTexName(), 0);
00057 
00058         // attach depth buffer if requested
00059         if (withDepth) {
00060                 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
00061                         GL_RENDERBUFFER_EXT, depthBufferObject_);
00062         }
00063 
00064         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
00065         if(status != GL_FRAMEBUFFER_COMPLETE_EXT) {
00066                 destroy();
00067                 S3D::dialogExit("GLFrameBufferObject", "FBO Init");
00068         }
00069 
00070         // unbind for now
00071         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00072         glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
00073 
00074         return true;
00075 }
00076 
00077 void GLFrameBufferObject::destroy()
00078 {
00079         if (frameBufferObject_) glDeleteFramebuffersEXT(1, &frameBufferObject_);
00080         if (depthBufferObject_) glDeleteRenderbuffersEXT(1, &depthBufferObject_);
00081 
00082         frameBufferObject_ = 0;
00083         depthBufferObject_ = 0;
00084 }
00085 
00086 void GLFrameBufferObject::bind()
00087 {
00088         if (bound_) S3D::dialogExit("GLFrameBufferObject", "already bound");
00089 
00090         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferObject_);
00091         glPushAttrib(GL_VIEWPORT_BIT);
00092         glViewport(0, 0, texture_->getWidth(), texture_->getHeight());
00093         bound_ = true;
00094 }
00095 
00096 void GLFrameBufferObject::unBind()
00097 {
00098         if (!bound_) S3D::dialogExit("GLFrameBufferObject", "not bound");
00099 
00100         glPopAttrib();
00101         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00102         bound_ = false;
00103 }

Generated on Mon Feb 16 15:14:44 2009 for Scorched3D by  doxygen 1.5.3