46

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

}
2
  • If you don't mind me asking, why do you want to draw a transparent circle?
    – Zach Latta
    Commented Feb 5, 2013 at 8: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
    Commented Feb 5, 2013 at 13:10

4 Answers 4

103

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);
7
  • 1
    Thanks it working but i hope i doesnt mess with other blend stuffs if any
    – PraveenMax
    Commented Apr 30, 2013 at 15:46
  • Enabling and disabling blend worked for me also.. however I share @Max_Dev's concern that this might not be the most efficient thing here. Commented May 26, 2013 at 14:43
  • 14
    It is VERY IMPORTANT to stop/pause the current SpriteBatch using mySpriteBatch.end() BEFORE enabling blending! Translucent drawing operations won't work otherwise! :D Commented Sep 9, 2013 at 8:51
  • 2
    Note: you shouldn't allocate new Color instance on each frame.
    – Miha_x64
    Commented Apr 22, 2017 at 17:13
  • 2
    Had to change GL10 to GL20 to get it working. Thanks!
    – Spikatrix
    Commented Aug 17, 2019 at 10:18
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

3
  • 6
    Replace GL10 with GL20 for newer versions of LibGDX.
    – Morgoth
    Commented Aug 7, 2018 at 8:13
  • 1
    Replace GL20 with GL30 for newer versions of LibGDX. Commented Jun 12, 2020 at 4:22
  • GL30 extends GL20 and GL30.GL_BLEND is a static reference to GL20.GL_BLEND.
    – dan1st
    Commented Jun 20, 2021 at 9: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);
1
  • 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. Commented Jun 11, 2015 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();
}
2
  • 3
    ,In my case with the above change, I cannot see the transparancy
    – UVM
    Commented Feb 6, 2013 at 4: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
    Commented Feb 17, 2015 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.