无法使用纹理渲染 libGDX
can't render libGDX renderable with texture
我试图渲染一个带有纹理的网格(我以编程方式创建的)。一切正常,除了它不渲染纹理。它只是一个黑色三角形。这是代码的简化版本(也不起作用):
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();
}
我解决了这个问题。
我应该这样创建网格:
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。
我试图渲染一个带有纹理的网格(我以编程方式创建的)。一切正常,除了它不渲染纹理。它只是一个黑色三角形。这是代码的简化版本(也不起作用):
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();
}
我解决了这个问题。 我应该这样创建网格:
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。