简体   繁体   中英

can't render libGDX renderable with texture

I was trying to render a mesh (that i created programmatically) with textures. Everything works fine except that it doesn't render the texture. It's just a black triangle. Here is a simplified version of the code (which doesn't work either):

public ModelBatch batch;
public OrthographicCamera cam;
public Renderable renderable;

@Override
public void create () {
    cam = new OrthographicCamera(5, 5);
    batch = new ModelBatch();

    Mesh mesh = new Mesh(true, 3, 3,
            new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_position"),
            new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoord")
    );

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

    renderable = new Renderable();
    renderable.primitiveType = GL20.GL_TRIANGLES;
    renderable.mesh = mesh;
    renderable.meshPartOffset = 0;
    renderable.meshPartSize = mesh.getNumIndices();
    renderable.material = new Material(TextureAttribute.createDiffuse(new Texture("badlogic.jpg")));
}

@Override
public void render () {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin(cam);
    batch.render(renderable);
    batch.end();
}

I solved the problem. I should have created the mesh this way:

Mesh mesh = new Mesh(true, 3, 3,
        new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_position"),
        new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoord0")
);

The difference is that now the alias in the texture coordinates attribute is a_texCoord0 instead of a_texCoord.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM