Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Painting with libgdx makes blinking objects

I'm trying to make something like a very simple painting app using Libgdx. I've been searching on the intertubes for a few days now trying to solve this issue, which is probably due to my openGL noobness.

When I paint an object on the screen, as long as the render() method is running, the stuff that I've painted blinks very quickly (I assume it's every time the render() method is called). If I disable continuous rendering, the blinking stops until I draw something else (again, render() isn't being called).

Assume that I'm loading a new Texture into a Sprite appropriately - it does draw, afer all - and all I'm doing in my render() method is this:

batch.begin();
myShape.setPosition(Gdx.input.getX(), Gdx.input.getY());
batch.setColor(Color.BLUE);
myShape.draw(batch);
batch.end();

I'm not calling glClear because I don't (think I) want to clear the screen every render. I have blindly experimented with various glEnable and glDisable's for culling, blending, dithernig, etc., but nothing has helped the problem.

What in the world am I doing wrong, or just not understanding here? Did I not set something up right?

like image 770
Dave Avatar asked Nov 03 '22 23:11

Dave


1 Answers

That's because of double buffering.

With double buffering enabled, you are not drawing over previous frame, but over pre-previous. That means, even and odd frames are drawn to two different buffers. Changing those buffers creates flickering.

I see three solutions:

  • You can disable double buffering. I'm not sure if that's possible in libgdx.

  • You can clear the screen and draw everything every frame. This will get slower as you draw.

  • You can save the image to texture and draw it back to screen at the beginning of every frame. This is the preferred solution, although it might require some work. This technique is called "render to texture".

like image 165
Piotr Praszmo Avatar answered Nov 08 '22 04:11

Piotr Praszmo