簡體   English   中英

libgdx:使用PerspectiveCamera不顯示SpriteBatch

[英]libgdx: SpriteBatch not displayed with PerspectiveCamera

雖然我確實掌握了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);
}       

我明白了:

在此輸入圖像描述

我問的原因是因為我真的很喜歡libgdx內置的在OpenGL中繪制文本(字體)的能力。 但是在他們的例子中,他們使用SpriteBatch,它們可以通向Font實例,並且它們也總是使用Ortho Camera。 我想知道SpriteBatch和Font繪圖功能是否適用於PerspectiveCamera。

好吧,好吧,我解決了它:

簡短回答:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM