00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
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
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 }