tibia-client/src/framework/graphics/framebuffer.cpp

100 lines
3.2 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* 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"
2011-12-25 00:14:12 +01:00
uint FrameBuffer::boundFbo = 0;
2010-11-23 16:30:43 +01:00
2011-12-25 00:14:12 +01:00
FrameBuffer::FrameBuffer(const Size& size)
{
2011-12-07 01:31:55 +01:00
glGenFramebuffers(1, &m_fbo);
if(!m_fbo)
logFatal("Unable to create framebuffer object");
2010-11-23 16:30:43 +01:00
2011-12-25 00:14:12 +01:00
resize(size);
}
FrameBuffer::~FrameBuffer()
{
glDeleteFramebuffers(1, &m_fbo);
}
2010-11-23 16:30:43 +01:00
2011-12-25 00:14:12 +01:00
void FrameBuffer::resize(const Size& size)
{
internalBind();
m_texture = TexturePtr(new Texture(size.width(), size.height(), 4));
m_texture->enableBilinearFilter();
2011-12-07 01:31:55 +01:00
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture->getId(), 0);
2011-08-12 04:04:28 +02:00
2011-12-07 01:31:55 +01:00
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
2011-12-25 00:14:12 +01:00
logFatal("Unable to setup framebuffer object");
internalRelease();
2010-11-23 16:30:43 +01:00
}
2011-12-25 00:14:12 +01:00
void FrameBuffer::bind(bool clear)
2010-11-23 16:30:43 +01:00
{
2011-12-25 00:14:12 +01:00
internalBind();
Matrix3 projectionMatrix = { 2.0f/m_texture->getWidth(), 0.0f, 0.0f,
0.0f, 2.0f/m_texture->getHeight(), 0.0f,
-1.0f, -1.0f, 0.0f };
2010-11-23 16:30:43 +01:00
2011-12-25 00:14:12 +01:00
m_oldProjectionMatrix = g_painter.getProjectionMatrix();
m_oldViewportSize = g_graphics.getViewportSize();
g_painter.setProjectionMatrix(projectionMatrix);
g_graphics.setViewportSize(m_texture->getSize());
if(clear)
glClear(GL_COLOR_BUFFER_BIT);
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();
g_painter.setProjectionMatrix(m_oldProjectionMatrix);
g_graphics.setViewportSize(m_oldViewportSize);
}
2010-11-23 16:30:43 +01:00
2011-12-07 01:31:55 +01:00
void FrameBuffer::draw(const Rect& dest)
{
2011-12-07 01:31:55 +01:00
g_painter.drawTexturedRect(dest, m_texture);
2010-11-23 16:30:43 +01:00
}
2011-12-25 00:14:12 +01:00
void FrameBuffer::internalBind()
{
if(boundFbo == m_fbo)
return;
assert(boundFbo != m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
m_prevBoundFbo = boundFbo;
boundFbo = m_fbo;
}
void FrameBuffer::internalRelease()
{
assert(boundFbo == m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_prevBoundFbo);
boundFbo = m_prevBoundFbo;
}