0

I'm working on an android app using libgdx, and I'm trying to draw some type of wind particles that leave a trail behind, something similar to this: http://hint.fm/wind/.

I've managed to draw the particles, which are lines that connect the previous position to the newest. However, if I clean the screen every time, it will only draw the current positions, which will be similar to little dots moving, as expected.

What I want to do is to draw a trail behind those particles, that fades away. To do this, I tried creating a list of the previous particle positions and drawing them with a decreasing alpha, but there are to many particles to draw and it proved to be very slow. So I searched, and there is another way to achieve this, which is clearing a screen with some some transparency while I draw the new particles. My solution right now is using this: Unexpected results implementing simple motion blur in Libgdx, and while my desktop rendering works as expected, in the android there is this purple background formed by the white particles that I don't understand. How can I solve this?

This are the screenshots of the android and desktop respectively: https://i.stack.imgur.com/lxY46.jpg

Code:

Pixmap screenClearPixmap = new Pixmap(480, 548, Format.RGBA8888);
screenClearPixmap.setColor(Color.rgba8888(0f, 0f, 0f, 0.1f));
screenClearPixmap.fill();
screenClearTexture = new Texture(screenClearPixmap);
screenClearSprite = new Sprite(screenClearTexture);
screenClearSprite.setSize(480, 548);
screenClearPixmap.dispose();

public void render() {

batch.begin();
screenClearSprite.draw(batch);
batch.end();
animate();
}

public void animate() {
moveParticles();
drawParticles();
}

public void drawParticles() {

shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(Color.WHITE);
...
shapeRenderer.line((float) particle.getOldX(), (float) particle.getOldY(), (float) particle.getX(), (float) particle.getY());
Gdx.gl20.glLineWidth(Global.LINEWIDTH);
shapeRenderer.end();
}
Community
  • 1
  • 1
rgom
  • 1
  • 1
  • Have you tryed using the Libgdx `ParticleEffect`s (http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/ParticleEffect.html)? – Robert P Apr 22 '14 at 13:34
  • Well I have looked the ParticleEffect class, but I will need to need around 200 to 500 particles, is it viable in performane wise? The thing is that the desktop rendering is the visualization I need, but in the android it's just messed up. I feel that by doing some kind of alpha blending I can achieve this, I just don't know how.. – rgom Apr 22 '14 at 16:24
  • 1
    200-500 particles is nothing... – noone Apr 23 '14 at 04:45

1 Answers1

0

I know this is several years late, but as this worked great for me, I'm posting this in the hopes that it helps someone.

Most Android devices I've seen (excluding emulators) require you to call Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) or you can get some weird results. This way allows you to clear the screen, but still get the trail effect to work by doing your drawing to a frame buffer object (FBO), which doesn't get cleared by glClear(), and then drawing the FBO to the screen.

  • Create an FBO: FrameBuffer fbo;

  • Create a SpriteBatch to draw the FBO: SpriteBatch batch;

  • In create(): fbo = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false); batch = new SpriteBatch();

  • In render(): Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glEnable(GL20.GL_BLEND); Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); fbo.begin(); //This must be called before your drawing code shapeRenderer.begin(); shapeRenderer.setColor(0, 0, 0, 0.2f); //You can change the trail length by changing the alpha shapeRenderer.rect(0, 0, screenWidth, screenHeight); //Draw a translucent rectangle to the FBO. ... Your drawing code goes here ... shapeRenderer.end(); fbo.end(); //Make sure this is called after shapeRenderer.end() or shapeRenderer.flush(); batch.begin(); batch.draw(fbo.getColorBufferTexture(), 0, 0); //Draw the FBO batch.end(); Gdx.gl.glDisable(GL20.GL_BLEND);

  • Remember to call fbo.dispose() in your application's dispose() method;

MinteZ
  • 86
  • 4