2011-08-14 04:09:11 +02:00
|
|
|
#include "framebuffer.h"
|
|
|
|
#include "graphics.h"
|
2011-04-17 21:14:24 +02:00
|
|
|
#include <core/platform.h>
|
2011-08-14 04:09:11 +02:00
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
#include <GL/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
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// create FBO texture
|
|
|
|
glGenTextures(1, &m_fboTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_fboTexture);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// we want bilinear filtering (for a smooth framebuffer)
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// use FBO ext only if supported
|
2011-08-12 03:38:54 +02: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-03-19 01:22:29 +01:00
|
|
|
oglFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_fboTexture, 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);
|
2010-11-23 16:30:43 +01:00
|
|
|
} else {
|
|
|
|
// otherwise fallback to copy texture from screen implementation
|
|
|
|
m_fallbackOldImp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FrameBuffer::~FrameBuffer()
|
|
|
|
{
|
2010-11-23 22:56:28 +01:00
|
|
|
glDeleteTextures(1, &m_fboTexture);
|
2010-11-23 16:30:43 +01:00
|
|
|
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);
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
// setup framebuffer viewport
|
|
|
|
glViewport(0, 0, m_width, m_height);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
gluOrtho2D(0.0f, m_width, 0, m_height);
|
|
|
|
|
|
|
|
// back to model view
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2010-11-23 16:30:43 +01:00
|
|
|
// clear framebuffer
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
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
|
2010-11-23 22:56:28 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_fboTexture);
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, m_width, m_height);
|
|
|
|
|
|
|
|
// restore graphics viewport
|
|
|
|
g_graphics.restoreViewport();
|
2010-11-23 16:30:43 +01:00
|
|
|
|
|
|
|
// clear 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);
|
|
|
|
}
|
2010-11-23 22:56:28 +01:00
|
|
|
}
|
2010-11-23 16:30:43 +01:00
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
void FrameBuffer::draw(int x, int y, int width, int height)
|
|
|
|
{
|
2011-08-12 04:04:28 +02:00
|
|
|
glColor4ubv(Color::white.rgbaPtr());
|
2010-11-23 22:56:28 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_fboTexture);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2i(0, 0); glVertex2i(x, y);
|
|
|
|
glTexCoord2i(0, 1); glVertex2i(x, y+height);
|
|
|
|
glTexCoord2i(1, 1); glVertex2i(x+width, y+height);
|
|
|
|
glTexCoord2i(1, 0); glVertex2i(x+width, y);
|
|
|
|
glEnd();
|
2010-11-23 16:30:43 +01:00
|
|
|
}
|