2012-03-09 14 views
6

OpenGLの基本的な知識はありますが、私はlibgdxから始めるところです。libgdx:PerspectiveCameraでSpriteBatchが表示されない

私の質問は、まったく同じコードで、OrthographicCameraからPerspectiveCameraに切り替えるだけで、私のSpriteBatchesが表示されなくなったということです。

ここで私が使用するコードです:

create()メソッド:

public void create() { 
    textureMesh = new Texture(Gdx.files.internal("data/texMeshTest.png")); 
    textureSpriteBatch = new Texture(Gdx.files.internal("data/texSpriteBatchTest.png")); 

    squareMesh = new Mesh(true, 4, 4, 
      new VertexAttribute(Usage.Position, 3, "a_position") 
      ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords") 

    ); 

    squareMesh.setVertices(new float[] { 
      squareXInitial, squareYInitial, squareZInitial,    0,1, //lower left 
      squareXInitial+squareSize, squareYInitial, squareZInitial, 1,1, //lower right 
      squareXInitial, squareYInitial+squareSize, squareZInitial, 0,0, //upper left 
      squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,1,0}); //upper right 

    squareMesh.setIndices(new short[] { 0, 1, 2, 3}); 

    spriteBatch = new SpriteBatch();   
} 

とrender()メソッド:

public void render() { 
    GLCommon gl = Gdx.gl; 

    camera.update(); 
    camera.apply(Gdx.gl10); 
    spriteBatch.setProjectionMatrix(camera.combined); 

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glEnable(GL10.GL_DEPTH_TEST); 

    gl.glEnable(GL10.GL_TEXTURE_2D); 
    textureMesh.bind(); 
    squareMesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4); 

    spriteBatch.begin(); 
    spriteBatch.draw(textureSpriteBatch, -10, 0); 
    spriteBatch.end();   
} 

、私のリサイズ(int型の幅であればは、 、int height)メソッドカメラを次のように設定しました。

public void resize(int width, int height) { 
     float aspectRatio = (float) width/(float) height; 
     camera = new OrthographicCamera(cameraViewHeight * aspectRatio, cameraViewHeight); 

私はこの取得:

enter image description here

をしかし、私はカメラの種類変更した場合:

public void resize(int width, int height) { 
    float aspectRatio = (float) width/(float) height; 
    camera = new PerspectiveCamera(64, cameraViewHeight * aspectRatio, cameraViewHeight); 
}  

を、私はこれを取得:

enter image description here

私が求めている理由は、私はOpenGLでテキスト(フォント)を描く能力を持つlibgdxの機能が本当に好きだったからです。しかし、それらの例では、SpriteBatchを使用してFontインスタンスに渡り、常にOrtho Cameraを使用します。 SpriteBatchとFont描画機能がPerspectiveCameraで動作するかどうかを知りたい。

+0

私は[デカール]だと思うPerspectiveCameraを使用している場合(http://code.google.com/p/libgdx-users/wiki/Decals)とDecalBatchクラスは、libgdx作成者が私たちが意図したものです。 – Sundae

+0

コードには長すぎる行が含まれています(スクロールコードは水平ではありません)(答えも) –

+0

ちょうど2台のカメラを使うことができます。抽象化。 –

答えて

4

はまあは、[OK]を、私はそれを解決:

短い答え:

SpriteBatchが内部OrthogonalPerspectiveを使用しています。 PerspectiveCameraを使用する場合、カスタムビューマトリックスをSpriteBatchに渡す必要があります。

@Override 
public void resize(int width, int height) { 
    float aspectRatio = (float) width/(float) height; 
    camera = new PerspectiveCamera(64, cameraViewHeight * aspectRatio, cameraViewHeight); 
    viewMatrix = new Matrix4(); 
    viewMatrix.setToOrtho2D(0, 0,width, height); 
    spriteBatch.setProjectionMatrix(viewMatrix); 
} 

をし、そのスプライトの投影行列で何かを(あなたはスプライトが画面に表示されている方法を変更しない限り)を実行する必要はありません:あなたは、サイズ変更(...)メソッドでそれを行うことができます。

public void render() { 
    GLCommon gl = Gdx.gl; 

    camera.update(); 
    camera.apply(Gdx.gl10); 
    //this is no longer needed: 
    //spriteBatch.setProjectionMatrix(camera.combined); 
    //... 

長い答え:私のコードに上記の修正を加えて、私はスプライトのテキストの両方という意味で、それを行うことができますが、私の最終目標は、テキストを描画するSpriteBatchを使用できるようにしたので、メッシュの頂点の色を指定しなければ、頂点は私がそのテキストに使う色を取得することに気付きました。つまり、次のように宣言されたテクスチャメッシュを使用します。

squareMesh = new Mesh(true, 4, 4, 
      new VertexAttribute(Usage.Position, 3, "a_position") 
      ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords") 

    ); 

    squareMesh.setVertices(new float[] { 
      squareXInitial, squareYInitial, squareZInitial,    0,1, //lower left 
      squareXInitial+squareSize, squareYInitial, squareZInitial, 1,1, //lower right 
      squareXInitial, squareYInitial+squareSize, squareZInitial, 0,0, //upper left 
      squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,1,0}); //upper right 

    squareMesh.setIndices(new short[] { 0, 1, 2, 3}); 

)メソッドは、メッシュが赤がかったようになります:

font.setColor(Color.RED); 
spriteBatch.draw(textureSpriteBatch, 0, 0); 
font.draw(spriteBatch, (int)fps+" fps", 0, 100); 

これに修正プログラムが最初からあなたのメッシュの頂点に色を設定することです:

squareMesh = new Mesh(true, 4, 4, 
     new VertexAttribute(Usage.Position, 3, "a_position") 
     ,new VertexAttribute(Usage.ColorPacked, 4, "a_color") 
     ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords") 

); 

squareMesh.setVertices(new float[] { 
     squareXInitial, squareYInitial, squareZInitial,       Color.toFloatBits(255, 255, 255, 255), 0,1, //lower left 
     squareXInitial+squareSize, squareYInitial, squareZInitial,    Color.toFloatBits(255, 255, 255, 255), 1,1, //lower right 
     squareXInitial, squareYInitial+squareSize, squareZInitial,    Color.toFloatBits(255, 255, 255, 255), 0,0, //upper left 
     squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial, Color.toFloatBits(255, 255, 255, 255), 1,0}); //upper right 

squareMesh.setIndices(new short[] { 0, 1, 2, 3}); 
関連する問題