2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#include "framebuffer.h"
|
|
|
|
#include "graphics.h"
|
2011-08-15 16:06:15 +02:00
|
|
|
#include "texture.h"
|
2012-03-21 13:41:43 +01:00
|
|
|
#include <framework/platform/platformwindow.h>
|
2011-08-15 16:06:15 +02:00
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
uint FrameBuffer::boundFbo = 0;
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
FrameBuffer::FrameBuffer()
|
|
|
|
{
|
2012-03-20 23:26:07 +01:00
|
|
|
internalCreate();
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
FrameBuffer::FrameBuffer(const Size& size)
|
|
|
|
{
|
2012-03-20 23:26:07 +01:00
|
|
|
internalCreate();
|
2011-12-25 00:14:12 +01:00
|
|
|
resize(size);
|
|
|
|
}
|
|
|
|
|
2012-03-20 23:26:07 +01:00
|
|
|
void FrameBuffer::internalCreate()
|
|
|
|
{
|
2012-04-20 12:16:03 +02:00
|
|
|
m_prevBoundFbo = 0;
|
|
|
|
m_fbo = 0;
|
2012-03-21 13:41:43 +01:00
|
|
|
if(g_graphics.canUseFBO()) {
|
2012-03-20 23:26:07 +01:00
|
|
|
glGenFramebuffers(1, &m_fbo);
|
|
|
|
if(!m_fbo)
|
|
|
|
logFatal("Unable to create framebuffer object");
|
2012-03-22 13:57:43 +01:00
|
|
|
}
|
2012-03-20 23:26:07 +01:00
|
|
|
}
|
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
FrameBuffer::~FrameBuffer()
|
|
|
|
{
|
2012-04-20 12:16:03 +02:00
|
|
|
if(m_fbo != 0)
|
2012-03-20 23:26:07 +01:00
|
|
|
glDeleteFramebuffers(1, &m_fbo);
|
2011-12-25 00:14:12 +01:00
|
|
|
}
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
void FrameBuffer::resize(const Size& size)
|
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
if(!size.isValid())
|
|
|
|
return;
|
|
|
|
|
2012-01-17 06:36:25 +01:00
|
|
|
if(m_texture && m_texture->getSize() == size)
|
|
|
|
return;
|
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
m_texture = TexturePtr(new Texture(size.width(), size.height(), 4));
|
2012-01-10 23:13:40 +01:00
|
|
|
m_texture->setSmooth(true);
|
2011-08-12 04:04:28 +02:00
|
|
|
|
2012-04-20 12:16:03 +02:00
|
|
|
if(m_fbo) {
|
2012-03-20 23:26:07 +01:00
|
|
|
internalBind();
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture->getId(), 0);
|
|
|
|
|
|
|
|
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
if(status != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
logFatal("Unable to setup framebuffer object");
|
|
|
|
internalRelease();
|
2012-04-20 12:16:03 +02:00
|
|
|
} else {
|
|
|
|
m_screenBackup = TexturePtr(new Texture(size.width(), size.height(), 4));
|
2012-03-20 23:26:07 +01:00
|
|
|
}
|
2012-04-04 22:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::clear(const Color& color, const Rect& rect)
|
|
|
|
{
|
|
|
|
bool clip = rect.isValid();
|
|
|
|
if(clip)
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setClipRect(Rect(0, 0, m_texture->getSize()));
|
2012-03-20 23:26:07 +01:00
|
|
|
|
2012-04-04 22:18:24 +02:00
|
|
|
glClearColor(color.rF(), color.gF(), color.bF(), color.aF());
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
if(clip)
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->resetClipRect();
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
|
|
|
|
2012-04-04 22:18:24 +02:00
|
|
|
void FrameBuffer::bind()
|
2010-11-23 16:30:43 +01:00
|
|
|
{
|
2011-12-25 00:14:12 +01:00
|
|
|
internalBind();
|
2012-04-19 01:03:43 +02:00
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
Matrix3 projectionMatrix = { 2.0f/m_texture->getWidth(), 0.0f, 0.0f,
|
|
|
|
0.0f, 2.0f/m_texture->getHeight(), 0.0f,
|
2012-04-19 01:03:43 +02:00
|
|
|
-1.0f, -1.0f, 1.0f };
|
|
|
|
g_painter->saveAndResetState();
|
|
|
|
g_painter->setProjectionMatrix(projectionMatrix);
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2011-12-25 00:14:12 +01:00
|
|
|
m_oldViewportSize = g_graphics.getViewportSize();
|
|
|
|
g_graphics.setViewportSize(m_texture->getSize());
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
void FrameBuffer::release()
|
2010-11-23 16:30:43 +01:00
|
|
|
{
|
2011-12-25 00:14:12 +01:00
|
|
|
internalRelease();
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->restoreSavedState();
|
2011-12-25 00:14:12 +01:00
|
|
|
g_graphics.setViewportSize(m_oldViewportSize);
|
2010-11-23 22:56:28 +01:00
|
|
|
}
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
void FrameBuffer::draw(const Rect& dest, const Rect& src)
|
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->drawTexturedRect(dest, m_texture, src);
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2011-12-07 01:31:55 +01:00
|
|
|
void FrameBuffer::draw(const Rect& dest)
|
2010-11-23 22:56:28 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->drawTexturedRect(dest, m_texture, Rect(0,0, getSize()));
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
2011-12-25 00:14:12 +01:00
|
|
|
|
|
|
|
void FrameBuffer::internalBind()
|
|
|
|
{
|
2012-04-20 12:16:03 +02:00
|
|
|
if(m_fbo) {
|
|
|
|
assert(boundFbo != m_fbo);
|
2012-03-20 23:26:07 +01:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
2012-04-20 12:16:03 +02:00
|
|
|
m_prevBoundFbo = boundFbo;
|
|
|
|
boundFbo = m_fbo;
|
|
|
|
} else {
|
|
|
|
// backup screen color buffer into a texture
|
|
|
|
m_screenBackup->bind();
|
|
|
|
Size size = getSize();
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, size.width(), size.height());
|
2012-03-22 13:57:43 +01:00
|
|
|
}
|
2011-12-25 00:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::internalRelease()
|
|
|
|
{
|
2012-04-20 12:16:03 +02:00
|
|
|
if(m_fbo) {
|
|
|
|
assert(boundFbo == m_fbo);
|
2012-03-20 23:26:07 +01:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_prevBoundFbo);
|
2012-04-20 12:16:03 +02:00
|
|
|
boundFbo = m_prevBoundFbo;
|
|
|
|
} else {
|
2012-03-21 13:41:43 +01:00
|
|
|
Size size = getSize();
|
2012-03-20 23:26:07 +01:00
|
|
|
|
2012-04-20 12:16:03 +02:00
|
|
|
// copy the drawn color buffer into the framebuffer texture
|
|
|
|
m_texture->bind();
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, size.width(), size.height());
|
2012-03-20 23:26:07 +01:00
|
|
|
|
2012-04-20 12:16:03 +02:00
|
|
|
// restore screen original content
|
2012-05-13 16:45:32 +02:00
|
|
|
glDisable(GL_BLEND);
|
2012-04-20 12:16:03 +02:00
|
|
|
g_painter->drawTexturedRect(Rect(0, 0, size), m_screenBackup, Rect(0, 0, size));
|
2012-05-13 16:45:32 +02:00
|
|
|
glEnable(GL_BLEND);
|
2012-03-20 23:26:07 +01:00
|
|
|
}
|
2011-12-25 00:14:12 +01:00
|
|
|
}
|
2012-03-21 13:41:43 +01:00
|
|
|
|
|
|
|
Size FrameBuffer::getSize()
|
|
|
|
{
|
2012-04-20 12:16:03 +02:00
|
|
|
if(m_fbo == 0) {
|
2012-03-21 13:41:43 +01:00
|
|
|
// the buffer size is limited by the window size
|
|
|
|
return Size(std::min(m_texture->getWidth(), g_window.getWidth()),
|
|
|
|
std::min(m_texture->getHeight(), g_window.getHeight()));
|
|
|
|
}
|
2012-03-22 13:57:43 +01:00
|
|
|
return m_texture->getSize();
|
2012-03-21 13:41:43 +01:00
|
|
|
}
|