2

I'm currently programming a 2D-sidescroll game with c++ and OpenGL. To get the window I'm using SFML, but that should'nt be relevant as I draw all my quads directly with OpenGL. Now the problem is when I use a Framebufferobject to draw some light-textures on it and then use it as a mask over the scene, the framerate drops down. Normally the game reaches its limit at 60 fps, but with the fbo, its about 40 fps, sometimes even 30. The effect looks nice thought :D I have never used a framebuffer befor, and this: Framebuffer FBO render to texture is very slow, using OpenGL ES 2.0 on Android, why? doesn't seem to help. So my question: What am I doing wrong? Or is there even a much simpler way to reach the same effect? I'm running it on Ubuntu 12.04. My code:

    void init() {
      GLuint fbo, mask; //framebufferobject and texture
      glewInit();
      glGenTextures(1, &mask);
      glBindTexture(GL_TEXTURE_2D, mask);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1024, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE,
      0);
      glGenFramebuffers(1, &fbo);
      glBindFramebuffer(GL_FRAMEBUFFER, fbo);
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mask,
      0);
      glClearColor(darkness, darkness, darkness, 1);
      if (glCheckFramebufferStatus(GL_FRAMEBUFFER) !=
      GL_FRAMEBUFFER_COMPLETE)CMain::game.usefbo = false;
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
    }

    void render() {
      //render usual stuff, then
      glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
      glClear(GL_COLOR_BUFFER_BIT);
      glPushAttrib(GL_VIEWPORT_BIT);
      glViewport(0, 0, 1024, 600);
      glBlendFunc(GL_SRC_COLOR, GL_ONE);
      //render lights
      glPopAttrib();
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
      glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
      //render fbo over the screen:
      glBindTexture(GL_TEXTURE_2D, mask);
      glTranslatef(0, 0, 9);
      glBegin(GL_QUADS);
        glTexCoord2f(0, 0);     glVertex2i(0, 0);
        glTexCoord2f(0, 1);     glVertex2i(0, 600);
        glTexCoord2f(1, 1);     glVertex2i(1024, 600);
        glTexCoord2f(1, 0);     glVertex2i(1024, 0);
      glEnd();
      glLoadIdentity();
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      //display
    }

Edit: At //render lights three light-textures (round, radial, black-to-white) are rendered to the fbo via glBegin(GL_QUADS) etc. Later of course I want to use much more lights. I need to refresh the fbo every frame cause the lights are moving and overlapping each other.

render lights:

for(std::vector<LightSource*>::iterator it = lights.begin(); it != lights.end(); it++) {
    (*it)->draw();
}

where lights is a vector of LightsSources. Lightssource::draw():

void LightsSource::draw() {
  if (visible()) {
      if (activated) {
          glTranslatef(this->x, this->y, 0);
          glBindTexture(GL_TEXTURE_2D, this->texture);
          glBegin(GL_QUADS);
              glTexCoord2f(0, 0);     glVertex2i(0, 0);
              glTexCoord2f(0, 1);     glVertex2i(0, height);
              glTexCoord2f(1, 1);     glVertex2i(width, height);
              glTexCoord2f(1, 0);     glVertex2i(width, 0);
          glEnd();
          glLoadIdentity();
      }
  }
}

visible() and activated are usually true.

Community
  • 1
  • 1
user1943758
  • 21
  • 1
  • 3
  • Honestly, talking about "fast" and "slow" doesn't make any sense when you're using the FFP. You should probably fix that first. – Cubic Feb 14 '13 at 16:08
  • sorry, don't know what you mean. please explain yourself. – user1943758 Feb 14 '13 at 16:25
  • Nothing here sticks out as a potential problem. I'm assuming //render lights is where you do some rendering to the FBO. What's going on in there? – Aeluned Feb 14 '13 at 16:50
  • Yes, that's true. I edited – user1943758 Feb 14 '13 at 17:00
  • post what's in render lights, please. – Aeluned Feb 14 '13 at 18:34
  • I edited again. I don't thing that's the problem though because as i comment it out I get about 50 fps. more then with them, but that means even without rendering anything to the fbo, 10 fps got lost. – user1943758 Feb 14 '13 at 19:08
  • You're losing 10fps by drawing 3 quads?! Technically, the fixed function pipeline (which you are using) has been deprecated. This means that your OpenGL implementation may not support it in hardware (however I've never seen this happen). What graphics device / driver are you using? Perhaps you're rendering in software? There's nothing here that should be accounting for fps drops of 30-50% like you are reporting. – Aeluned Feb 14 '13 at 19:18
  • graphics card: AMD Radeon HD 6470M graphics-driver: ATI/AMD FGLRX – user1943758 Feb 14 '13 at 19:33
  • so what would be the alternative of FFP? could you just give me a hint, then I can search for that, please? I'm quite new to that. – user1943758 Feb 14 '13 at 19:38
  • well, it's all shaders now. That's another topic entirely. I've just gotten a warning that we're having an 'extended discussion' and you don't have enough rep to chat. Unfortunately, I'm at a loss here. I can't imagine that this is due to you using the FFP even though you're on a mobile chip. [click here for a tutorial on modern graphics programming](http://www.arcsynthesis.org/gltut/index.html) Sorry, and good luck :( – Aeluned Feb 14 '13 at 19:57
  • Dude, if this is resolved for you, why don't you mark it as answered? – Tara Sep 06 '13 at 12:04

1 Answers1

0

What kind of GPU are you using?
The only thing I see right now is that you're using the texture format "GL_RGBA" instead of "GL_BGRA", which is considered the fastest format on many GPUs. So if you GPU is very old, that might be the issue.

Tara
  • 1,673
  • 22
  • 30
  • I ported to shaders and am trying to not use the FFP anymore. It's much more performant. So there's no need to use a framebuffer anymore. I researched a lot since I posted and now see how I use fixed functions everywhere. It could have been a lot. Thanks for your reply, though. – user1943758 Feb 22 '13 at 18:05
  • I see... Good that you don't use fixed functions anymore^^ They're sometimes a pain in the ass... – Tara Feb 23 '13 at 19:30