绘制网格和纹理 libgdx

发布于 2024-12-27 04:58:24 字数 836 浏览 3 评论 0原文

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2025-01-03 04:58:24

SpriteBatch 使用正交投影矩阵。当您调用batch.begin()时,它会应用其矩阵(请参阅SpriteBatch.setupMatrices())。

因此,要么:

  1. 更改网格的顶点,以便它出现在屏幕上:

    mesh.setVertices(new float[] { 100f, 100f, 0, 
              400f、100f、0、 
              250, 400f, 0 });
    
  2. 将网格的渲染移出批量渲染:

    Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);
    Gdx.gl10.glLoadIdentity();
    Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);
    Gdx.gl10.glLoadIdentity();
    网格渲染(GL10.GL_TRIANGLES, 0, 3);
    
    批处理.开始();
    批处理.draw(纹理, 10, 10);
    批处理结束();
    

    必须在begin()中重置批量设置的投影和变换矩阵;因为 SpriteBatch.end() 不会重新设置矩阵。

SpriteBatch uses orthographic projection matrix. When you call batch.begin() then it applies its matrices (see 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(); because SpriteBatch.end() doesn't set matrices back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文