4

Building open this tutorial - https://code.google.com/p/libgdx/wiki/ProjectionViewportCamera I have constructed a class for generating polygon meshes. But i cannot figure out how to render semi transparency, even though the mesh in the tutorial takes a color object with an alpha channel.

I am rendering with squareMesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4); This is the code used for initializing the mesh

if (squareMesh == null) {
    squareMesh = new Mesh(true, 4, 4, 
                 new VertexAttribute(Usage.Position,    3, "a_position"),
                 new VertexAttribute(Usage.ColorPacked, 4, "a_color"));

    squareMesh.setVertices(new float[] {
        -0.5f, -0.5f, 0, Color.toFloatBits(128, 0, 0, 255),
         0.5f, -0.5f, 0, Color.toFloatBits(192, 0, 0, 255),
        -0.5f,  0.5f, 0, Color.toFloatBits(192, 0, 0, 255),
         0.5f,  0.5f, 0, Color.toFloatBits(255, 0, 0, 255) });   
    squareMesh.setIndices(new short[] { 0, 1, 2, 3});
}
  • What else are you rendering? (What do you expect to see through your transparent mesh?). Also, `255` is fully opaque, so presumably you set the alpha to something lower? – P.T. Sep 08 '13 at 15:33
  • Yes the alpha value does not change anything, I would expect to see the background through the mesh. for example: with a white mesh and a black background I would expect to see a grey square if the alpha values were set to 128. – Anton Christensen Sep 09 '13 at 00:10
  • I had the same problem and resolved it by using 2 `Color` objects. The first is the desired color, the other is just a temporary color where I put all values of desired color multiplied by its alpha, and then I update the vertices of the `Mesh` with it. This is a bit tricky/dirty, so if you find a *clean* solution let me know please. – Mickäel A. Sep 13 '13 at 12:42
  • @miNde - wouldn't that just render a mesh with colors closer to black instead of actually blending the color with the background? – Anton Christensen Sep 14 '13 at 10:56
  • Ew... That's true. I didn't notice that because it was a test and the background was black. Maybe you should open a ticket on the issue tracker of LibGDX on GitHub. – Mickäel A. Sep 14 '13 at 11:32

2 Answers2

3

Many of the Libgdx infrastructure classes enable blending automatically. If you're drawing meshes yourself, you will probably need to turn blending on manually:

Gdx.graphics.getGL10().glEnable(GL10.GL_BLEND); // Or GL20
P.T.
  • 24,557
  • 7
  • 64
  • 95
  • Nothing really changed, would you happen to know where i could read more on this or how this should be correctly implemented? – Anton Christensen Sep 09 '13 at 05:15
  • Maybe make the blending function explicit too? `Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);` This is mostly regular OpenGL as wrapped by LWJGL, so its not really covered in the Libgdx docs (AFAICT). If that doesn't fix it, please show the code that clears the screen and maybe try rendering two meshes to see if you can blend them. – P.T. Sep 09 '13 at 06:48
  • I initialise with `Gdx.gl.glClearColor(0.2f, 0, 0.1f, 1);` and clear the screen with `Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);` and two meshes with alpha set to 0 changes nothing. – Anton Christensen Sep 09 '13 at 07:20
  • I found that this was the solution, but if rendering through a spriteBatch, for some reason you have to disable blending for the sprite batch for the mesh transparency to take effect – Anton Christensen Sep 20 '13 at 01:00
1

I found that ModelBatch.begin(...) will disable Blending by calling Gdx.gl.glDisable(GL20.GL_BLEND). So make sure to enable blending AFTER calling ModelBatch.begin().

modelBatch.begin(camera); // resets Blending to false
// enable Blending
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
// draw mesh
mesh.render(shader, GL20.GL_TRIANGLES);
modelBatch.end();

There is also an equivalent method to enable blending in ModelBatch

modelBatch.getRenderContext().setBlending(true, GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Syscall
  • 19,327
  • 10
  • 37
  • 52
Alex
  • 390
  • 4
  • 14