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"
|
|
|
|
|
|
|
|
#include <framework/platform/platform.h>
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#include <GL/gl.h>
|
2011-11-23 01:21:33 +01:00
|
|
|
#include <framework/thirdparty/glext.h>
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2011-03-19 01:22:29 +01:00
|
|
|
PFNGLGENFRAMEBUFFERSPROC oglGenFramebuffers = 0;
|
|
|
|
PFNGLBINDFRAMEBUFFERPROC oglBindFramebuffer = 0;
|
|
|
|
PFNGLFRAMEBUFFERTEXTURE2DPROC oglFramebufferTexture2D = 0;
|
|
|
|
PFNGLDELETEFRAMEBUFFERSPROC oglDeleteFramebuffers = 0;
|
|
|
|
PFNGLCHECKFRAMEBUFFERSTATUSPROC oglCheckFramebufferStatus = 0;
|
2011-03-19 00:49:30 +01:00
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
FrameBuffer::FrameBuffer(int width, int height)
|
2010-11-23 16:30:43 +01:00
|
|
|
{
|
2010-11-23 22:56:28 +01:00
|
|
|
m_fbo = 0;
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// create FBO texture
|
2011-08-15 16:06:15 +02:00
|
|
|
m_texture = TexturePtr(new Texture(width, height, 4));
|
|
|
|
m_texture->enableBilinearFilter();
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// use FBO ext only if supported
|
2011-10-31 00:13:22 +01:00
|
|
|
if(g_graphics.isExtensionSupported("GL_ARB_framebuffer_object")) {
|
2010-11-23 16:30:43 +01:00
|
|
|
m_fallbackOldImp = false;
|
2011-03-19 01:22:29 +01:00
|
|
|
if(!oglGenFramebuffers) {
|
2011-08-14 04:09:11 +02:00
|
|
|
oglGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)g_platform.getExtensionProcAddress("glGenFramebuffers");
|
|
|
|
oglBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)g_platform.getExtensionProcAddress("glBindFramebuffer");
|
|
|
|
oglFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)g_platform.getExtensionProcAddress("glFramebufferTexture2D");
|
|
|
|
oglDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)g_platform.getExtensionProcAddress("glDeleteFramebuffers");
|
|
|
|
oglCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)g_platform.getExtensionProcAddress("glCheckFramebufferStatus");
|
2011-03-19 00:49:30 +01:00
|
|
|
}
|
2011-04-06 22:53:00 +02:00
|
|
|
|
2010-11-23 16:30:43 +01:00
|
|
|
// generate FBO
|
2011-03-19 01:22:29 +01:00
|
|
|
oglGenFramebuffers(1, &m_fbo);
|
|
|
|
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
|
2010-11-23 16:30:43 +01:00
|
|
|
|
|
|
|
// attach 2D texture to this FBO
|
2011-08-15 16:06:15 +02:00
|
|
|
oglFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_texture->getId(), 0);
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2011-03-19 01:22:29 +01:00
|
|
|
GLenum status = oglCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
|
2010-11-23 16:30:43 +01:00
|
|
|
switch(status) {
|
|
|
|
case GL_FRAMEBUFFER_COMPLETE_EXT:
|
|
|
|
//ok
|
|
|
|
break;
|
2010-11-23 22:56:28 +01:00
|
|
|
default: // fallback to old implementation
|
2010-11-23 16:30:43 +01:00
|
|
|
m_fallbackOldImp = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-12 04:04:28 +02:00
|
|
|
|
|
|
|
// restore back buffer
|
|
|
|
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
|
2011-08-15 16:06:15 +02:00
|
|
|
glDrawBuffer(GL_BACK);
|
|
|
|
glReadBuffer(GL_BACK);
|
2010-11-23 16:30:43 +01:00
|
|
|
} else {
|
|
|
|
// otherwise fallback to copy texture from screen implementation
|
|
|
|
m_fallbackOldImp = true;
|
|
|
|
}
|
2011-11-25 23:29:45 +01:00
|
|
|
|
|
|
|
if(m_fallbackOldImp)
|
|
|
|
logInfo("Framebuffers not supported, falling back to old implementation.");
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FrameBuffer::~FrameBuffer()
|
|
|
|
{
|
|
|
|
if(m_fbo)
|
2011-03-19 01:22:29 +01:00
|
|
|
oglDeleteFramebuffers(1, &m_fbo);
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::bind()
|
|
|
|
{
|
|
|
|
if(!m_fallbackOldImp) {
|
|
|
|
// bind framebuffer
|
2011-03-19 01:22:29 +01:00
|
|
|
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
|
2011-11-25 23:29:45 +01:00
|
|
|
} else {
|
|
|
|
int screenWidth = g_graphics.getScreenSize().width();
|
|
|
|
int screenHeight = g_graphics.getScreenSize().height();
|
|
|
|
|
|
|
|
if(!m_screenBackup || m_screenBackup->getSize() != g_graphics.getScreenSize())
|
|
|
|
m_screenBackup = TexturePtr(new Texture(screenWidth, screenHeight, 4));
|
|
|
|
|
|
|
|
// save screen state
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_screenBackup->getId());
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, screenWidth, screenHeight);
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// setup framebuffer viewport
|
2011-08-15 16:06:15 +02:00
|
|
|
glViewport(0, 0, m_texture->getWidth(), m_texture->getHeight());
|
2010-11-23 22:56:28 +01:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2011-11-25 23:29:45 +01:00
|
|
|
glOrtho(0.0f, m_texture->getWidth(), m_texture->getHeight(), 0, -1, 1);
|
2010-11-23 22:56:28 +01:00
|
|
|
|
|
|
|
// back to model view
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2010-11-23 16:30:43 +01:00
|
|
|
// clear framebuffer
|
2011-10-31 00:13:22 +01:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
2010-11-23 16:30:43 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::unbind()
|
|
|
|
{
|
|
|
|
if(!m_fallbackOldImp) {
|
|
|
|
// bind back buffer again
|
2011-03-19 01:22:29 +01:00
|
|
|
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
|
2010-11-23 16:30:43 +01:00
|
|
|
glDrawBuffer(GL_BACK);
|
|
|
|
glReadBuffer(GL_BACK);
|
2010-11-23 22:56:28 +01:00
|
|
|
|
|
|
|
// restore graphics viewport
|
|
|
|
g_graphics.restoreViewport();
|
2010-11-23 16:30:43 +01:00
|
|
|
} else {
|
|
|
|
// copy screen to texture
|
2011-08-15 16:06:15 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texture->getId());
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_texture->getWidth(), m_texture->getHeight());
|
2010-11-23 22:56:28 +01:00
|
|
|
|
|
|
|
// restore graphics viewport
|
|
|
|
g_graphics.restoreViewport();
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2011-11-25 23:29:45 +01:00
|
|
|
// restore screen
|
2010-11-23 22:56:28 +01:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
2010-11-23 16:30:43 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2011-11-25 23:29:45 +01:00
|
|
|
g_graphics.drawTexturedRect(Rect(Point(0,0), g_graphics.getScreenSize()), m_screenBackup, Rect(), true);
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
2010-11-23 22:56:28 +01:00
|
|
|
}
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
void FrameBuffer::draw(const Rect& screenCoords, const Rect& framebufferCoords)
|
2010-11-23 22:56:28 +01:00
|
|
|
{
|
2011-11-25 23:29:45 +01:00
|
|
|
g_graphics.drawTexturedRect(screenCoords, m_texture, framebufferCoords, true);
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|