add framebuffer support for old video cards

This commit is contained in:
Eduardo Bart 2011-11-25 20:29:45 -02:00
parent 7e0618b247
commit 1386064d71
4 changed files with 36 additions and 9 deletions

View File

@ -79,6 +79,9 @@ FrameBuffer::FrameBuffer(int width, int height)
// otherwise fallback to copy texture from screen implementation // otherwise fallback to copy texture from screen implementation
m_fallbackOldImp = true; m_fallbackOldImp = true;
} }
if(m_fallbackOldImp)
logInfo("Framebuffers not supported, falling back to old implementation.");
} }
FrameBuffer::~FrameBuffer() FrameBuffer::~FrameBuffer()
@ -92,13 +95,23 @@ void FrameBuffer::bind()
if(!m_fallbackOldImp) { if(!m_fallbackOldImp) {
// bind framebuffer // bind framebuffer
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo); oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
} 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);
} }
// setup framebuffer viewport // setup framebuffer viewport
glViewport(0, 0, m_texture->getWidth(), m_texture->getHeight()); glViewport(0, 0, m_texture->getWidth(), m_texture->getHeight());
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
glOrtho(0.0f, m_texture->getWidth(), 0, m_texture->getHeight(), -1, 1); glOrtho(0.0f, m_texture->getWidth(), m_texture->getHeight(), 0, -1, 1);
// back to model view // back to model view
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@ -127,13 +140,14 @@ void FrameBuffer::unbind()
// restore graphics viewport // restore graphics viewport
g_graphics.restoreViewport(); g_graphics.restoreViewport();
// clear screen // restore screen
glClearColor(0.0, 0.0, 0.0, 1.0); glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
g_graphics.drawTexturedRect(Rect(Point(0,0), g_graphics.getScreenSize()), m_screenBackup, Rect(), true);
} }
} }
void FrameBuffer::draw(const Rect& screenCoords, const Rect& framebufferCoords) void FrameBuffer::draw(const Rect& screenCoords, const Rect& framebufferCoords)
{ {
g_graphics.drawTexturedRect(screenCoords, m_texture, framebufferCoords); g_graphics.drawTexturedRect(screenCoords, m_texture, framebufferCoords, true);
} }

View File

@ -44,6 +44,7 @@ public:
private: private:
TexturePtr m_texture; TexturePtr m_texture;
TexturePtr m_screenBackup;
uint m_fbo; uint m_fbo;
bool m_fallbackOldImp; bool m_fallbackOldImp;
}; };

View File

@ -132,7 +132,8 @@ void Graphics::endRender()
void Graphics::drawTexturedRect(const Rect& screenCoords, void Graphics::drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,
const Rect& textureCoords) const Rect& textureCoords,
bool upsideDown)
{ {
if(screenCoords.isEmpty() || texture->getId() == 0) if(screenCoords.isEmpty() || texture->getId() == 0)
return; return;
@ -151,13 +152,23 @@ void Graphics::drawTexturedRect(const Rect& screenCoords,
if(textureCoords.isEmpty()) { if(textureCoords.isEmpty()) {
textureRight = texture->getWidth() / (float)textureSize.width(); textureRight = texture->getWidth() / (float)textureSize.width();
textureBottom = texture->getHeight() / (float)textureSize.height(); if(upsideDown) {
textureTop = 0.0f; textureBottom = 0.0f;
textureTop = texture->getHeight() / (float)textureSize.height();
} else {
textureBottom = texture->getHeight() / (float)textureSize.height();
textureTop = 0.0f;
}
textureLeft = 0.0f; textureLeft = 0.0f;
} else { } else {
textureRight = (textureCoords.right() + 1) / (float)textureSize.width(); textureRight = (textureCoords.right() + 1) / (float)textureSize.width();
textureBottom = (textureCoords.bottom() + 1) / (float)textureSize.height(); if(upsideDown) {
textureTop = textureCoords.top() / (float)textureSize.height(); textureTop = (textureCoords.bottom() + 1) / (float)textureSize.height();
textureBottom = textureCoords.top() / (float)textureSize.height();
} else {
textureBottom = (textureCoords.bottom() + 1) / (float)textureSize.height();
textureTop = textureCoords.top() / (float)textureSize.height();
}
textureLeft = textureCoords.left() / (float)textureSize.width(); textureLeft = textureCoords.left() / (float)textureSize.width();
} }

View File

@ -56,7 +56,8 @@ public:
// drawing API // drawing API
void drawTexturedRect(const Rect& screenCoords, void drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,
const Rect& textureCoords = Rect()); const Rect& textureCoords = Rect(),
bool upsideDown = false);
void drawRepeatedTexturedRect(const Rect& screenCoords, void drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,