Question

Pendant que je reçois la connaissance de base de OpenGL, je commence juste avec libgdx.

Ma question est la suivante: pourquoi, avoir exactement le même code, mais seulement la commutation d'orthographiccamera à Perspectivecamera a pour effet de ne plus afficher aucun de mes gardes Sprantits?

Voici le code que j'utilise:

la méthode 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();        
}

et la méthode rendu ():

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

Maintenant, si dans ma méthode de redimensionner (int largeur, int hauteur), je configure la caméra comme suit:

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

Je reçois ceci:

Entrez la description de l'image ici

Mais si je change le type de caméra:

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

Je reçois ceci:

Entrez la description de l'image ici

La raison pour laquelle je demande est parce que j'ai vraiment aimé la capacité intégrée de libgdx à dessiner du texte (police) dans OpenGL. Mais dans leurs exemples, ils utilisent un gardes SpritTbatch qu'ils appartiennent à l'instance de police et utilisent également toujours une caméra ortho. J'aimerais savoir alors si SpritBatch et la fonctionnalité de dessin de police fonctionnent avec Perspectivecamera.

Était-ce utile?

La solution

Eh bien, ok, je l'ai résolu:

Réponse courte:

spritbatch utilise un orthogonalperspensé en interne. Si vous utilisez Perspectivecamera, vous devez transmettre une matrice de vue personnalisée sur le SpritTbatch. Vous pouvez le faire dans la méthode Redimension (...):

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

Et puis il n'est pas nécessaire de faire quoi que ce soit d'autre avec cette matrice de projection de cette Sprite (sauf si vous souhaitez modifier la manière dont le sprite est affiché à l'écran):

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

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

longue réponse: puisque mon objectif final était de pouvoir utiliser un Spritbatch pour dessiner du texte, alors que la modification mentionnée ci-dessus à mon code, je peux le faire, dans le sens où le texte sur le sprite et le maillage avec La texture est maintenant visible, j'ai remarqué que si je ne spécifie pas de couleur pour les sommets de mon maillage, lesdits sommets obtiennent la couleur que j'utilise pour le texte. En d'autres termes, avec un maillage texturé déclaré comme:

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

Avoir aussi ce code dans ma méthode de rendu (...) fera le maillage rouge-teinté:

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

Le correctif à ceci est de définir des couleurs sur les sommets de votre maillage dès le début:

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top