SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

RenderImageImplFBO.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Graphics/RenderImageImplFBO.hpp>
00029 #include <SFML/Graphics/Image.hpp>
00030 #include <SFML/Graphics/GLCheck.hpp>
00031 #include <SFML/System/Err.hpp>
00032 
00033 
00034 namespace sf
00035 {
00036 namespace priv
00037 {
00041 RenderImageImplFBO::RenderImageImplFBO() :
00042 myFrameBuffer(0),
00043 myDepthBuffer(0)
00044 {
00045 
00046 }
00047 
00048 
00052 RenderImageImplFBO::~RenderImageImplFBO()
00053 {
00054     // Destroy the depth buffer
00055     if (myDepthBuffer)
00056     {
00057         GLuint depthBuffer = static_cast<GLuint>(myDepthBuffer);
00058         GLCheck(glDeleteFramebuffersEXT(1, &depthBuffer));
00059     }
00060 
00061     // Destroy the frame buffer
00062     if (myFrameBuffer)
00063     {
00064         GLuint frameBuffer = static_cast<GLuint>(myFrameBuffer);
00065         GLCheck(glDeleteFramebuffersEXT(1, &frameBuffer));
00066     }
00067 }
00068 
00069 
00073 bool RenderImageImplFBO::IsSupported()
00074 {
00075     // Make sure that GLEW is initialized
00076     priv::EnsureGlewInit();
00077 
00078     return GLEW_EXT_framebuffer_object != 0;
00079 }
00080 
00081 
00085 bool RenderImageImplFBO::Create(unsigned int width, unsigned int height, unsigned int textureId, bool depthBuffer)
00086 {
00087     // Create the framebuffer object
00088     GLuint frameBuffer = 0;
00089     GLCheck(glGenFramebuffersEXT(1, &frameBuffer));
00090     myFrameBuffer = static_cast<unsigned int>(frameBuffer);
00091     if (!myFrameBuffer)
00092     {
00093         Err() << "Impossible to create render image (failed to create the frame buffer object)" << std::endl;
00094         return false;
00095     }
00096     GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFrameBuffer));
00097 
00098     // Create the depth buffer if requested
00099     if (depthBuffer)
00100     {
00101         GLuint depth = 0;
00102         GLCheck(glGenRenderbuffersEXT(1, &depth));
00103         myDepthBuffer = static_cast<unsigned int>(depth);
00104         if (!myDepthBuffer)
00105         {
00106             Err() << "Impossible to create render image (failed to create the attached depth buffer)" << std::endl;
00107             return false;
00108         }
00109         GLCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDepthBuffer));
00110         GLCheck(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height));
00111         GLCheck(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, myDepthBuffer));
00112     }
00113 
00114     // Link the image to the frame buffer
00115     GLCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0));
00116 
00117     // A final check, just to be sure...
00118     if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
00119     {
00120         GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
00121         Err() << "Impossible to create render image (failed to link the target image to the frame buffer)" << std::endl;
00122         return false;
00123     }
00124 
00125     return true;
00126 }
00127 
00128 
00132 bool RenderImageImplFBO::Activate(bool active)
00133 {
00134     myContext.SetActive(active);
00135 
00136     return true;
00137 }
00138 
00142 void RenderImageImplFBO::UpdateTexture(unsigned int)
00143 {
00144     // Nothing to do: the FBO draws directly into the target image
00145 }
00146 
00147 } // namespace priv
00148 
00149 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::