简体   繁体   English

绘制网格和纹理libgdx

[英]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();

  }

I'm trying to draw a triangle and a circle (From a png) on the screen, using libgdx. 我正在尝试使用libgdx在屏幕上绘制一个三角形和一个圆圈(从png开始)。

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 ? 为了使Mesh和Texture都可见,我该怎么办?

SpriteBatch uses orthographic projection matrix. SpriteBatch使用正交投影矩阵。 When you call batch.begin() then it applies its matrices (see SpriteBatch.setupMatrices(). 当你调用batch.begin()然后它应用它的矩阵(参见SpriteBatch.setupMatrices())。

So either: 所以要么:

  1. change vertices for mesh, so it is on screen: 更改网格的顶点,因此它在屏幕上:

     mesh.setVertices(new float[] { 100f, 100f, 0, 400f, 100f, 0, 250, 400f, 0 }); 
  2. move rendering of mesh out of batch rendering: 从批量渲染中移除网格渲染:

     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(); 

    you have to reset the projection and transformation matrices set by batch in begin(); 你必须在begin()中重置由批处理设置的投影和转换矩阵; because SpriteBatch.end() doesn't set matrices back. 因为SpriteBatch.end()没有设置矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM