8

私は OpenGL の基本的な知識を持っていますが、libgdx から始めたばかりです。

私の質問は、まったく同じコードを使用しているのに、 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();        
}

ここで、 resize(int width, int height) メソッドの場合、次のようにカメラをセットアップします。

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

私はこれを得る:

ここに画像の説明を入力

しかし、カメラの種類を変更すると:

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

私はこれを得る:

ここに画像の説明を入力

私が尋ねている理由は、OpenGL でテキスト (フォント) を描画する libgdx の組み込み機能が本当に気に入ったからです。しかし、彼らの例では、Font インスタンスにパスする SpriteBatch を使用し、常に Ortho Camera も使用しています。SpriteBatch と Font の描画機能が PerspectiveCamera で動作するかどうかを知りたいです。

4

1 に答える 1

4

さて、わかりました、私はそれを解決しました:

短い答え:

SpriteBatchは、内部でOrthogonalPerspectiveを使用します。PerspectiveCameraを使用する場合は、カスタムビューマトリックスをSpriteBatchに渡す必要があります。あなたはresize(...)メソッドでそれを行うことができます:

@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});

また、render(...)メソッドにこのコードを含めると、メッシュが赤みを帯びます。

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});
于 2012-03-09T13:08:47.873 に答える