Question

I'm rendering a libgdx mesh using shader with alpha color less than 1.0.

When rendering the first frame, the alpha value set in shader is being ignored and rendered as 1.0. All following rendering frames are fine.

The same happened to me in previous project drawing lines and shapes using glDrawArrays and I haven't found the solution yet.

Code in libgdx render() loop:

    Gdx.gl20.glClearColor(0, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    Gdx.gl20.glEnable(GL20.GL_BLEND);
    MeshManager_colorFill.meshShader.begin();
    MeshManager_colorFill.meshShader.setUniformMatrix("u_worldView", Snappr.camMatrix);

    meshArray_colorFill.render(MeshManager_colorFill.meshShader, GL20.GL_TRIANGLE_FAN, 0, meshArray_colorFill.get(i).getNumVertices()); 

    MeshManager_colorFill.meshShader.end();
    Gdx.gl20.glDisable(GL20.GL_BLEND);

My shader (compiled in create(){}):

  public static final String meshVertexShader_colorFill =

                    "attribute vec2 a_position;    \n" + 
                    "uniform mat4 u_worldView;\n" + 
                    "void main()                  \n" + 
                    "{                            \n" + 
                    "   gl_Position =  u_worldView * vec4(a_position.xy, 0, 1);  \n"      + 
                    "}                            \n" ;

  public static final String meshFragmentShader_colorFill =

                    "precision mediump float;\n" +  
                    "void main()                                  \n" + 
                    "{                                            \n" + 
                    "  gl_FragColor = vec4(1,1,1,0.2);\n" +
                    "}";

How do I render the very first frame as it should be?

Thanks

Was it helpful?

Solution

glBlendFunc in create() does the trick, specifically: "Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, L20.GL_ONE_MINUS_SRC_ALPHA);" I'm using SpriteBatch to render a font, right after the mesh. glBlendFunc is contained internally within the SpriteBatch, looks like that's why all the other frames were fine.

OTHER TIPS

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

Source https://stackoverflow.com/a/66820414/2413469

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top