绘制网格和纹理libgdx [英] Drawing a Mesh and a Texture libgdx

查看:169
本文介绍了绘制网格和纹理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();

  }

我想在屏幕上画一个三角形和一个圆(从PNG),使用libgdx。

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 ?

推荐答案

SpriteBatch使用正交投影矩阵。当你调用batch.begin(),然后应用其矩阵(见SpriteBatch.setupMatrices()。

SpriteBatch uses orthographic projection matrix. When you call batch.begin() then it applies its matrices (see SpriteBatch.setupMatrices().

因此​​,无论:

  1. 改变顶点网格,所以它在屏幕上:

  1. change vertices for mesh, so it is on screen:

mesh.setVertices(new float[] { 100f, 100f, 0, 
          400f, 100f, 0, 
          250, 400f, 0 });

  • 动呈现网状的出批渲染:

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

    您必须重新分批设置在开始投影和变换矩阵();因为SpriteBatch.end()不设置矩阵回来。

    you have to reset the projection and transformation matrices set by batch in begin(); because SpriteBatch.end() doesn't set matrices back.

    这篇关于绘制网格和纹理libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆