Domanda

Mentre faccio la conoscenza di base di OpenGL che sto appena iniziando con libgdx.

La mia domanda è: perché, avendo lo stesso codice esatto, ma solo passare da ortograficoCamera a ProspectiveCamera ha l'effetto di non visualizzare più nessuno dei miei spritebatch?

Ecco il codice che uso:

Il metodo Crea ():

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

e il metodo 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();        
}
.

Ora, se nella mia ridimensiona (int larghezza, int altezza) Metodo I Configurazione della fotocamera come:

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

Io ottengo questo:

Inserisci Descrizione dell'immagine qui

Ma se cambio il tipo di telecamera:

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

Io ottengo questo:

Inserire l'immagine Descrizione qui

La ragione per cui sto chiedendo è perché mi è piaciuto molto la libgdx è costruita nella capacità di disegnare testo (font) in OpenGL. Ma nei loro esempi usano uno spritebatch che percorrono per l'istanza dei caratteri, e anche usano sempre la fotocamera Ortho. Mi piacerebbe sapere allora se SpriteBatch e Font Disegno funzionalità funzionano con ProspectiveCamera.

È stato utile?

Soluzione

Bene, OK, l'ho risolto:

risposta breve:

spritebatch utilizza un ortogonalperspective internamente. Se si utilizza PerspectiveCamera è necessario passare una matrice di visualizzazione personalizzata allo spritebatch. Puoi farlo nel metodo Ridimensione (...):

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

E poi non c'è bisogno di fare nient'altro con la matrice di proiezione di Sprite (a meno che non si desidera modificare il modo in cui lo sprite viene visualizzato sullo schermo):

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

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

Risposta lunga: dal momento che il mio obiettivo finale doveva essere in grado di utilizzare uno spritebatch per disegnare il testo, mentre con la modifica sopra menzionata al mio codice posso farlo, nel senso che sia il testo sullo sprite e la maglia con La trama è ora visibile, ho notato che se non specifico un colore per i vertici della mia maglia, detti vertici otterranno il colore che uso per il testo. In altre parole, con una maglia strutturata dichiarata come:

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

Anche Avere questo codice nel mio rendering (...) il metodo renderà la maglia rossa tinta:

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

La correzione a questo è per impostare i colori sui vertici della mesh direttamente dall'inizio:

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top