绘制网格和纹理 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)。
当我运行它时,我只能在屏幕上看到纹理(圆圈)。我应该怎么做才能使网格和纹理都可见?
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();
}
I'm trying to draw a triangle and a circle (From a png) on the screen, using libgdx.
When I run this, I can only see the Texture (circle) on the screen. What should I do in order to make both Mesh and the Texture visible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SpriteBatch 使用正交投影矩阵。当您调用batch.begin()时,它会应用其矩阵(请参阅SpriteBatch.setupMatrices())。
因此,要么:
更改网格的顶点,以便它出现在屏幕上:
将网格的渲染移出批量渲染:
必须在begin()中重置批量设置的投影和变换矩阵;因为 SpriteBatch.end() 不会重新设置矩阵。
SpriteBatch uses orthographic projection matrix. When you call batch.begin() then it applies its matrices (see SpriteBatch.setupMatrices().
So either:
change vertices for mesh, so it is on screen:
move rendering of mesh out of batch rendering:
you have to reset the projection and transformation matrices set by batch in begin(); because SpriteBatch.end() doesn't set matrices back.