private Mesh mesh;
private Texture texture;
private SpriteBatch batch;
@Override
public void create() {
if (mesh == null) {
mesh = new Mesh(true, 3, 3, new VertexAttribute(Usage.Position, 3,
"a_position"));
mesh.setVertices(new float[] { -0.5f, -0.5f, 0,
0.5f, -0.5f, 0,
0, 0.5f, 0 });
mesh.setIndices(new short[] { 0, 1, 2 });
texture = new Texture(Gdx.files.internal("data/circle.png"));
batch = new SpriteBatch();
}
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
mesh.render(GL10.GL_TRIANGLES, 0, 3);
batch.draw(texture, 10, 10);
batch.end();
}
我正在尝试使用libgdx在屏幕上绘制一个三角形和一个圆圈(来自png)。
当我运行它时,我只能在屏幕上看到纹理(圆圈)。为了使Mesh和Texture都可见,我该怎么办?
答案 0 :(得分:4)
SpriteBatch使用正交投影矩阵。当你调用batch.begin()然后它应用它的矩阵(参见SpriteBatch.setupMatrices()。
所以:
更改网格的顶点,因此它在屏幕上:
mesh.setVertices(new float[] { 100f, 100f, 0,
400f, 100f, 0,
250, 400f, 0 });
将网格渲染移出批处理渲染:
Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);
Gdx.gl10.glLoadIdentity();
Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);
Gdx.gl10.glLoadIdentity();
mesh.render(GL10.GL_TRIANGLES, 0, 3);
batch.begin();
batch.draw(texture, 10, 10);
batch.end();
你必须在begin()中重置由批处理设置的投影和转换矩阵;因为SpriteBatch.end()没有设置矩阵。