Pregunta

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

  }

Estoy tratando de dibujar un triángulo y un círculo (de un PNG) en la pantalla, usando libgdx.

Cuando ejecuto esto, solo puedo ver la textura (círculo) en la pantalla. ¿Qué debo hacer para hacer visible tanto la malla como la textura?

¿Fue útil?

Solución

Spritebatch utiliza matriz de proyección ortográfica. Cuando llama a Batch.Begin (), aplica sus matrices (ver spritebatch.setupMatrices ().

Entonces, o:

  1. Cambie los vértices para la malla, por lo que está en la pantalla:

    mesh.setVertices(new float[] { 100f, 100f, 0, 
              400f, 100f, 0, 
              250, 400f, 0 });
    
  2. Mueve la representación de la malla fuera de la representación por lotes:

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

    Debe restablecer las matrices de proyección y transformación establecidas por lote en begin (); Porque spritebatch.end () no retrocede las matrices.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top