Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing transparent ShapeRenderer in libgdx

I have a drawn a filled circle using ShapeRenderer and now I want to draw this circle as a transparent one. I am using the following code to do that: But the circle is not coming as transparent. Also, I checked th libgdx API and from the wiki, it says that, need to Create CameraStrategy. Has somebody faced similar issue ever before? If so, please give me some clues. Thanks in advance.

 Gdx.gl.glEnable(GL10.GL_BLEND);
      Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
      drawFilledCircle();
      Gdx.gl.glDisable(GL10.GL_BLEND);

private void drawFilledCircle(){

   shapeRenderer.setProjectionMatrix(camera.combined);
   shapeRenderer.begin(ShapeType.FilledCircle);
   shapeRenderer.setColor(new Color(0, 1, 0, 1));
   shapeRenderer.filledCircle(470, 45, 10);
   shapeRenderer.end();

}
like image 828
UVM Avatar asked Feb 05 '13 05:02

UVM


3 Answers

The following code is working for me in this case, maybe it will help someone else:

Gdx.gl.glEnable(GL10.GL_BLEND);
Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.FilledCircle);
shapeRenderer.setColor(new Color(0, 1, 0, 0.5f));
shapeRenderer.filledCircle(470, 45, 10);
shapeRenderer.end();
Gdx.gl.glDisable(GL10.GL_BLEND);
like image 195
UVM Avatar answered Sep 22 '22 09:09

UVM


First we need to enable blending:

Gdx.gl.glEnable(GL10.GL_BLEND);

And make sure that you don't call SpriteBatch.begin() and SpriteBatch.end() between that line of code and your Shaperender.drawSomething() line of code. I don't know why but that's what works in my case

like image 37
Hải Phong Avatar answered Sep 26 '22 09:09

Hải Phong


Only this worked for me :

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
//Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);   // <<< this line here makes the magic we're after
game.shapeRenderer.setProjectionMatrix(camera.combined);
game.shapeRenderer.begin(ShapeType.Filled);
    go.drawShapes();
game.shapeRenderer.end();
//Gdx.gl.glDisable(GL20.GL_BLEND);
like image 20
Li3ro Avatar answered Sep 22 '22 09:09

Li3ro