2012-01-14 70 views
3
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)。繪製網格和紋理libgdx

當我運行這個時,我只能看到屏幕上的紋理(圓圈)。我應該怎麼做才能使網格和紋理可見?

回答

4

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(); 
    mesh.render(GL10.GL_TRIANGLES, 0, 3); 
    
    batch.begin(); 
    batch.draw(texture, 10, 10); 
    batch.end(); 
    

    你必須重置日e)在begin()中批量設置投影和變換矩陣;因爲SpriteBatch.end()不會設置矩陣。