[英]Drawing a Mesh and a Texture libgdx
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都可見,我該怎么辦?
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()沒有設置矩陣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.