2009-06-11 15:49:36 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// SFML - Simple and Fast Multimedia Library
|
|
|
|
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
// In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
// subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented;
|
|
|
|
// you must not claim that you wrote the original software.
|
|
|
|
// If you use this software in a product, an acknowledgment
|
|
|
|
// in the product documentation would be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such,
|
|
|
|
// and must not be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Graphics/RenderImageImplFBO.hpp>
|
|
|
|
#include <SFML/Graphics/Image.hpp>
|
|
|
|
#include <SFML/Graphics/GLCheck.hpp>
|
2010-02-22 11:30:43 +00:00
|
|
|
#include <SFML/System/Err.hpp>
|
2009-06-11 15:49:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace sf
|
|
|
|
{
|
|
|
|
namespace priv
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
RenderImageImplFBO::RenderImageImplFBO() :
|
|
|
|
myFrameBuffer(0),
|
2010-01-19 22:07:31 +00:00
|
|
|
myDepthBuffer(0)
|
2009-06-11 15:49:36 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
RenderImageImplFBO::~RenderImageImplFBO()
|
|
|
|
{
|
|
|
|
// Destroy the depth buffer
|
|
|
|
if (myDepthBuffer)
|
|
|
|
{
|
2009-07-11 22:17:24 +00:00
|
|
|
GLuint depthBuffer = static_cast<GLuint>(myDepthBuffer);
|
|
|
|
GLCheck(glDeleteFramebuffersEXT(1, &depthBuffer));
|
2009-06-11 15:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the frame buffer
|
|
|
|
if (myFrameBuffer)
|
|
|
|
{
|
2009-07-11 22:17:24 +00:00
|
|
|
GLuint frameBuffer = static_cast<GLuint>(myFrameBuffer);
|
|
|
|
GLCheck(glDeleteFramebuffersEXT(1, &frameBuffer));
|
2009-06-11 15:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
bool RenderImageImplFBO::IsSupported()
|
|
|
|
{
|
|
|
|
// Make sure that GLEW is initialized
|
2009-10-29 09:32:17 +00:00
|
|
|
priv::EnsureGlewInit();
|
2009-06-11 15:49:36 +00:00
|
|
|
|
2009-11-06 10:38:07 +00:00
|
|
|
return GLEW_EXT_framebuffer_object != 0;
|
2009-06-11 15:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-11 22:17:24 +00:00
|
|
|
bool RenderImageImplFBO::Create(unsigned int width, unsigned int height, unsigned int textureId, bool depthBuffer)
|
2009-06-11 15:49:36 +00:00
|
|
|
{
|
2010-01-19 22:07:31 +00:00
|
|
|
// Create the framebuffer object
|
|
|
|
GLuint frameBuffer = 0;
|
|
|
|
GLCheck(glGenFramebuffersEXT(1, &frameBuffer));
|
|
|
|
myFrameBuffer = static_cast<unsigned int>(frameBuffer);
|
2009-06-11 15:49:36 +00:00
|
|
|
if (!myFrameBuffer)
|
|
|
|
{
|
2010-02-22 11:30:43 +00:00
|
|
|
Err() << "Impossible to create render image (failed to create the frame buffer object)" << std::endl;
|
2010-01-19 22:07:31 +00:00
|
|
|
return false;
|
2009-06-11 15:49:36 +00:00
|
|
|
}
|
|
|
|
GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFrameBuffer));
|
|
|
|
|
2010-01-19 22:07:31 +00:00
|
|
|
// Create the depth buffer if requested
|
2009-07-11 22:17:24 +00:00
|
|
|
if (depthBuffer)
|
2009-06-11 15:49:36 +00:00
|
|
|
{
|
2009-07-11 22:17:24 +00:00
|
|
|
GLuint depth = 0;
|
|
|
|
GLCheck(glGenRenderbuffersEXT(1, &depth));
|
|
|
|
myDepthBuffer = static_cast<unsigned int>(depth);
|
2009-06-11 15:49:36 +00:00
|
|
|
if (!myDepthBuffer)
|
|
|
|
{
|
2010-02-22 11:30:43 +00:00
|
|
|
Err() << "Impossible to create render image (failed to create the attached depth buffer)" << std::endl;
|
2009-06-11 15:49:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
GLCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, myDepthBuffer));
|
2009-07-11 22:17:24 +00:00
|
|
|
GLCheck(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height));
|
2009-06-11 15:49:36 +00:00
|
|
|
GLCheck(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, myDepthBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link the image to the frame buffer
|
2009-07-11 22:17:24 +00:00
|
|
|
GLCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0));
|
2009-06-11 15:49:36 +00:00
|
|
|
|
|
|
|
// A final check, just to be sure...
|
|
|
|
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
|
|
|
|
{
|
|
|
|
GLCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
|
2010-02-22 11:30:43 +00:00
|
|
|
Err() << "Impossible to create render image (failed to link the target image to the frame buffer)" << std::endl;
|
2009-06-11 15:49:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-11 22:17:24 +00:00
|
|
|
bool RenderImageImplFBO::Activate(bool active)
|
2009-06-11 15:49:36 +00:00
|
|
|
{
|
2010-01-19 22:07:31 +00:00
|
|
|
myContext.SetActive(active);
|
2009-06-11 15:49:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-01-19 22:07:31 +00:00
|
|
|
void RenderImageImplFBO::UpdateTexture(unsigned int)
|
2009-06-11 15:49:36 +00:00
|
|
|
{
|
2011-01-19 17:49:01 +00:00
|
|
|
glFlush();
|
2009-06-11 15:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace priv
|
|
|
|
|
|
|
|
} // namespace sf
|