java-绘制网格和纹理libgdx

java-绘制网格和纹理libgdx,第1张

概述privateMeshmesh;privateTexturetexture;privateSpriteBatchbatch;@Overridepublicvoidcreate(){if(mesh==null){mesh=newMesh(true,3,3,newVertexAttribute(Usage.Position,3,"a_position"));

  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).

运行此命令时,我只能在屏幕上看到“纹理”(圆形).为了使“网格”和“纹理”都可见,该怎么办?

解决方法:

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()不会重新设置矩阵.

总结

以上是内存溢出为你收集整理的java-绘制网格和纹理libgdx全部内容,希望文章能够帮你解决java-绘制网格和纹理libgdx所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1085057.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存