Pregunta

Mientras tengo el conocimiento básico de OpenGL, estoy empezando con LIBGDX.

Mi pregunta es: ¿Por qué, tener el mismo código exacto, pero solo cambiar de la cámaras ortográficas a perspectiveCamera tiene el efecto de ya no mostrar ninguno de mis spriteBatches?

Aquí está el código que utilizo:

El método Crear ():

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

y el método 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();        
}

Ahora, si en mi método de cambio de tamaño (ancho INT, ENT ALTE), configuro la cámara así:

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

obtengo esto:

ingrese la descripción de la imagen aquí

pero si cambio el tipo de cámara:

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

obtengo esto:

ingrese la descripción de la imagen aquí

La razón por la que estoy preguntando es porque realmente me gustó la capacidad incorporada de LIBGDX para dibujar texto (fuente) en OpenGL. Pero en sus ejemplos, usan un SpriteBatch que ruta a la instancia de fuente, y también usan siempre la cámara Ortho. Me gustaría saber, entonces si SpriteBatch y la funcionalidad de dibujo de fuentes trabajan con PerspectiveCamera.

¿Fue útil?

Solución

Bueno, OK, lo resolví:

Respuesta corta:

SpriteBatch usa un ortogonalperspective internamente. Si usa PerspectiveCamera, debe pasar una matriz de vista personalizada al SpriteBatch. Puede hacerlo en el método de cambio de tamaño (...):

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

y luego no hay necesidad de hacer nada más con la matriz de proyección de Sprite (a menos que desee cambiar cómo se muestra el sprite en la pantalla):

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

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

Respuesta larga: Dado que mi objetivo final era poder usar un SpriteBatch para dibujar texto, mientras que con la modificación mencionada anteriormente en mi código puedo hacerlo, en el sentido de que tanto el texto en el sprite y la malla con La textura ahora está visible, he notado que si no especifico un color para los vértices de mi malla, dichos vértices obtendrán el color que utilizo para el texto. En otras palabras, con una malla texturizada declarada así:

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

Tener este código en mi método de render (...) hará que la malla se tinte en rojo:

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

La solución a esto es establecer colores en los vértices de su malla desde el principio:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top