GLShadowFrameBuffer.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/GLShadowFrameBuffer.h>
00022 #include <GLEXT/GLStateExtension.h>
00023 
00024 GLShadowFrameBuffer::GLShadowFrameBuffer() : 
00025         frameBufferObject_(0), depthTextureObject_(0),
00026         width_(0), height_(0)
00027 {
00028 }
00029 
00030 GLShadowFrameBuffer::~GLShadowFrameBuffer()
00031 {
00032         destroy();
00033 }
00034 
00035 bool GLShadowFrameBuffer::create(int width, int height)
00036 {
00037         width_ = width;
00038         height_ = height;
00039 
00040         // Create texture
00041         glGenTextures(1, &depthTextureObject_);
00042         glBindTexture(GL_TEXTURE_2D, depthTextureObject_);
00043 
00044         glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, 
00045                 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);                
00046 
00047         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
00048         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
00049         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00050         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00051 
00052         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
00053         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
00054         glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY); 
00055 
00056         // Create framebuffer
00057         glGenFramebuffersEXT(1, &frameBufferObject_);
00058         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferObject_);
00059 
00060         glReadBuffer(GL_NONE);
00061         glDrawBuffer(GL_NONE);
00062 
00063         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, 
00064                 GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTextureObject_, 0);
00065         glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, 
00066                 GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthTextureObject_);
00067 
00068         GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);  
00069         if (status != GL_FRAMEBUFFER_COMPLETE_EXT) 
00070         {
00071                 return false;
00072         }
00073         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00074 
00075         return true;
00076 }
00077 
00078 void GLShadowFrameBuffer::bindDepthTexture()
00079 {
00080         glBindTexture(GL_TEXTURE_2D, depthTextureObject_);
00081         GLTexture::setLastBind(0);
00082 }
00083 
00084 void GLShadowFrameBuffer::destroy()
00085 {
00086         if (!bufferValid()) return;
00087 
00088         //Activate the frame buffer objcet
00089         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferObject_);
00090 
00091         //Detach our depth texture from the frame buffer object
00092         glFramebufferTexture2DEXT(
00093                 GL_FRAMEBUFFER_EXT,
00094                 GL_DEPTH_ATTACHMENT_EXT,
00095                 GL_TEXTURE_2D, 
00096                 0,
00097                 0);
00098         glFramebufferRenderbufferEXT(
00099                 GL_FRAMEBUFFER_EXT, 
00100                 GL_DEPTH_ATTACHMENT_EXT, 
00101                 GL_RENDERBUFFER_EXT, 
00102                 0);
00103 
00104         //DeActivate the frame buffer objcet
00105         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00106 
00107         //Delete the frame buffer object
00108         glDeleteFramebuffersEXT(1, &frameBufferObject_);
00109 
00110         frameBufferObject_ = 0;
00111 
00112         // Delete texture
00113         glDeleteTextures(1, &depthTextureObject_);
00114         depthTextureObject_ = 0;
00115 }
00116 
00117 void GLShadowFrameBuffer::bind()
00118 {
00119         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferObject_);
00120 }
00121 
00122 void GLShadowFrameBuffer::unBind()
00123 {
00124         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
00125 }

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