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

RenderImageImplFBO.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2008 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/GraphicsContext.hpp>
00031 #include <iostream>
00032 
00033 
00034 namespace sf
00035 {
00036 namespace priv
00037 {
00041 RenderImageImplFBO::RenderImageImplFBO(Image& TargetImage) :
00042 RenderImageImpl(TargetImage),
00043 myFrameBuffer  (0),
00044 myDepthBuffer  (0)
00045 {
00046 
00047 }
00048 
00049 
00053 RenderImageImplFBO::~RenderImageImplFBO()
00054 {
00055     // Make sure we have a valid context
00056     priv::GraphicsContext Ctx;
00057 
00058     // Destroy the OpenGL depth buffer
00059     if (myDepthBuffer)
00060     {
00061         GLuint DepthBuffer = static_cast<GLuint>(myDepthBuffer);
00062         GLCheck(glDeleteFramebuffersEXT(1, &DepthBuffer));
00063     }
00064 
00065     // Destroy the OpenGL frame buffer
00066     if (myFrameBuffer)
00067     {
00068         GLuint FrameBuffer = static_cast<GLuint>(myFrameBuffer);
00069         GLCheck(glDeleteFramebuffersEXT(1, &FrameBuffer));
00070     }
00071 }
00072 
00073 
00077 bool RenderImageImplFBO::IsSupported()
00078 {
00079     // Make sure we have a valid context
00080     priv::GraphicsContext Ctx;
00081 
00082     // TODO : put it back :)
00083     return false;
00084     //return glewIsSupported("GL_EXT_framebuffer_object") != 0;
00085 }
00086 
00087 
00091 bool RenderImageImplFBO::Create(bool DepthBuffer)
00092 {
00093     // Make sure we have a valid context
00094     priv::GraphicsContext Ctx;
00095 
00096     // Create the framebuffer object if not already done
00097     if (!myFrameBuffer)
00098     {
00099         GLuint FrameBuffer = 0;
00100         GLCheck(glGenFramebuffersEXT(1, &FrameBuffer));
00101         myFrameBuffer = static_cast<unsigned int>(FrameBuffer);
00102         if (!myFrameBuffer)
00103         {
00104             std::cerr << "Impossible to create render image (failed to create the frame buffer object)" << std::endl;
00105             return false;
00106         }
00107     }
00108 
00109     // Bind the framebuffer
00110     GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFrameBuffer));
00111 
00112     // Create the depth buffer
00113     if (myDepthBuffer)
00114     {
00115         GLuint DepthBuffer = static_cast<GLuint>(myDepthBuffer);
00116         GLCheck(glDeleteRenderbuffersEXT(1, &DepthBuffer));
00117     }
00118     if (DepthBuffer)
00119     {
00120         GLuint DepthBuffer = 0;
00121         GLCheck(glGenRenderbuffersEXT(1, &DepthBuffer));
00122         myDepthBuffer = static_cast<unsigned int>(DepthBuffer);
00123         if (!myDepthBuffer)
00124         {
00125             std::cerr << "Impossible to create render image (failed to create the attached depth buffer)" << std::endl;
00126             return false;
00127         }
00128         GLCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDepthBuffer));
00129         GLCheck(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, myImage.GetWidth(), myImage.GetHeight()));
00130         GLCheck(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, myDepthBuffer));
00131     }
00132 
00133     // Link the image to the frame buffer
00134     GLCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, myImage.myTexture, 0));
00135 
00136     // A final check, just to be sure...
00137     if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
00138     {
00139         GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
00140         std::cerr << "Impossible to create render image (failed to link the target image to the frame buffer)" << std::endl;
00141         return false;
00142     }
00143 
00144     // Unbind the buffers
00145     Activate(false);
00146 
00147     return true;
00148 }
00149 
00150 
00154 bool RenderImageImplFBO::Activate(bool Active)
00155 {
00156     // Make sure we have a valid context
00157     priv::GraphicsContext Ctx;
00158 
00159     if (Active)
00160     {
00161         // Bind the buffers
00162         GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFrameBuffer));
00163         GLCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDepthBuffer));
00164     }
00165     else
00166     {
00167         // Unbind the buffers
00168         GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
00169         GLCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0));
00170 
00171         // After the RenderImage has been used, we have to notify
00172         // the underlying image that its pixels have been modified
00173         myImage.myNeedTextureUpdate = false;
00174         myImage.myNeedArrayUpdate   = true;
00175         myImage.myPixelsFlipped     = true;
00176     }
00177 
00178     return true;
00179 }
00180 
00181 } // namespace priv
00182 
00183 } // namespace sf

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