45

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();

}
UVM
  • 9,776
  • 6
  • 41
  • 66
  • If you don't mind me asking, why do you want to draw a transparent circle? – Zach Latta Feb 05 '13 at 08:12
  • @crynix, basically I need to understand how can I use ShapeRenderer to display transparancy. I do not want to use a transparent texture here for that. So I wanted to do using ShapeRenederer. – UVM Feb 05 '13 at 13:10

4 Answers4

102

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);
Alex Crist
  • 1,059
  • 2
  • 12
  • 22
UVM
  • 9,776
  • 6
  • 41
  • 66
13

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

Hải Phong
  • 5,094
  • 6
  • 31
  • 49
6

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);
Li3ro
  • 1,837
  • 2
  • 27
  • 35
  • 1
    Same thing happened with me. LibGDX discontinued support of OpenGL version 1.x sometime last year. This is likely just the result of using a newer version. – Robert Krupp Jun 11 '15 at 18:14
5

Well, there is not really a point in drawing something fully transparent. If you did want to make a half transparent circle, you would have to clear the color buffer by glClearColor before each frame and set Color alpha component to 0.5f.

If you wouldn't clear the buffer, after few render draws, the circle would blend into one with almost solid color.

private void drawFilledCircle(Camera camera){
    Gdx.gl.glClearColor(0, 0, 0, 0); 
    Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT  );

    shapeRenderer.setProjectionMatrix(camera.combined);
    shapeRenderer.begin(ShapeType.FilledCircle);
    shapeRenderer.setColor(new Color(0, 1, 0, 0.5f)); // last argument is alpha channel
    shapeRenderer.filledCircle(470, 45, 10);
    shapeRenderer.end();
}
jellyfication
  • 1,595
  • 1
  • 16
  • 37
  • 3
    ,In my case with the above change, I cannot see the transparancy – UVM Feb 06 '13 at 04:25
  • For some reason alpha in color works in debug mode (drawing borders of scene2ui elements). But without debug mode I got solid non-transparent colors (same result when alpha is 1f). I'm clearing buffer before every frame. – monnef Feb 17 '15 at 14:55