Question

i am trying to render a textured mesh along with stage in Libgdx. I have problem rendering texture when i put the stage.draw on top of mesh.render().

Case 1:

public void render(float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);

    stage.act(delta);
    stage.draw();
    texture.bind();
    mesh.render(GL10.GL_TRIANGLES, 0, 3);

}

// -- This is in my create method:
        mesh = new Mesh(true, 3, 3, 
                new VertexAttribute(Usage.Position, 3, "a_position"),
                new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
                new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords"));

        mesh.setVertices(new float[] { 50, 50, 0,Color.toFloatBits(255, 0, 0, 255),0,1,
                                        50, 500, 0,Color.toFloatBits(0, 255, 0, 255),0,0,
                                        500, 50, 0, Color.toFloatBits(0, 0, 255, 255),1,1                                                   });

        mesh.setIndices(new short[] { 0, 1, 2 });

In the above case I am not able to render texture but able to render gradient triangle.

Case 2:

public void render(float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);

    texture.bind();
    mesh.render(GL10.GL_TRIANGLES, 0, 3);
    stage.act(delta);
    stage.draw();
}

In this case i am able to render texture properly on the triangle.

My Questions:

  1. Why does the order of stage.draw() and mesh.render() affect the texture although I can draw the triangle.
  2. How do I draw the mesh with a texture in an actor's draw method?
Was it helpful?

Solution

OpenGL is a a "stateful" library (so changes made by calls into OpenGL persist until changed again). So, something in the stage.draw() call is changing the OpenGL state that makes your mesh's textures not work.

You might try moving the:

Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);

call down with your:

texture.bind();
mesh.render(GL10.GL_TRIANGLES, 0, 3);

I didn't think the Stage would un-enable texturing, but it looks like it might.

Additionally, if you're trying to draw meshes directly from within an Actor's draw callback, you may need to end the batch and restart around your non-batch drawing. The batch construction makes assumptions about OpenGL state between its begin and end calls, so it may get confused if you change the state. See libgdx - ShapeRenderer in Group.draw renders in wrong colour for a related example.

OTHER TIPS

Im sorry, this is just a guess: try adding

Gdx.gl10.glActiveTexture(GL10.GL_TEXTURE0);

between drawing mesh and stage, or after, or before... i had a similar problem, where i wanted to use shaders and BitmapFonts

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