无法使用纹理渲染libGDX渲染

时间:2015-07-19 12:48:28

标签: java opengl libgdx

我试图用纹理渲染一个网格(我用编程方式创建)。一切正常,但它不会渲染纹理。它只是一个黑色三角形。这是代码的简化版本(也不起作用):

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

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 我应该用这种方式创建网格:

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

不同之处在于,现在纹理坐标属性中的别名是a_texCoord0而不是a_texCoord。