【问题标题】:LIBGDX: How do you horizontally/vertically flip a camera view?LIBGDX:你如何水平/垂直翻转相机视图?
【发布时间】:2016-06-14 23:05:32
【问题描述】:

我正在尝试创建一个四向对称程序,它有 1 个摄像头查看特定场景,但只是翻转 4 个面板中每个面板的摄像头视图。

例如:symmetry

象限 1 将是世界的常规方向,正 x 向右,正 y 向上。 象限 2 将是左侧的正 x 和向上的正 y,依此类推。

我想出了一种方法,通过多次启动和结束我的 spritebatch(不确定这是否是一件坏事,但这是我让它工作的方式)并更改 glViewport 来在多个面板中绘制相同的相机视图每个面板。

    batch.setProjectionMatrix(cam.combined);

    batch.begin();        
    Gdx.gl.glViewport(0,Gdx.graphics.getHeight()/2,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.draw(img, 0, 0);
    batch.end();

    batch.begin();
    Gdx.gl.glViewport(0,0,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.draw(img, 0, 0);
    batch.end();        

    batch.begin();
    Gdx.gl.glViewport(Gdx.graphics.getWidth()/2,0,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.draw(img, 0, 0);
    batch.end();

    batch.begin();
    Gdx.gl.glViewport(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.draw(img, 0, 0);
    batch.end();

我认为线性变换可以做到这一点,但我对 libgdx 或线性代数的使用还不足以立即看到明确的答案。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    有几种方法可以做到这一点。

    翻转相机

    保留一个矩阵,以便在不弄乱相机的情况下进行翻转。

    private final Matrix4 tmpM = new Matrix4();
    

    然后你可以将它翻转为三个翻转的象限:

    Gdx.gl.glViewport(0,Gdx.graphics.getHeight()/2,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.setProjectionMatrix(cam.combined);
    batch.begin();        
    batch.draw(img, 0, 0);
    batch.end();
    
    Gdx.gl.glViewport(0,0,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.setProjectionMatrix(tmpM.set(cam.combined).scl(1, -1, 1));
    batch.begin();        
    batch.draw(img, 0, 0);
    batch.end();
    
    Gdx.gl.glViewport(Gdx.graphics.getWidth()/2,0,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.setProjectionMatrix(tmpM.set(cam.combined).scl(-1, -1, 1));
    batch.begin();        
    batch.draw(img, 0, 0);
    batch.end();
    
    Gdx.gl.glViewport(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
    batch.setProjectionMatrix(tmpM.set(cam.combined).scl(-1, -1, 1));
    batch.begin();        
    batch.draw(img, 0, 0);
    batch.end();
    

    帧缓冲区

    如果您的场景很复杂,这可能会更快,因为实际场景只绘制一次。在调整大小时创建帧缓冲区对象。 try/catch 是因为一些旧手机或廉价手机不支持帧缓冲区对象中的 RGBA8888 颜色。

    public void resize (int width, int height){
        if (frameBuffer != null) frameBuffer.dispose();
        try {
            frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width/2, height/2, false); 
        } catch (GdxRuntimeException e) {
            frameBuffer = new FrameBuffer(Pixmap.Format.RGB565, width/2, height/2, false);
        }
    }
    

    然后按如下方式使用:

    frameBuffer.begin();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.setProjectionMatrix(cam.combined);
    batch.begin();        
    batch.draw(img, 0, 0);
    batch.end();
    frameBuffer.end();
    
    Texture fbTex = frameBuffer.getColorBufferTexture();
    batch.getProjectionMatrix().idt();
    batch.begin();
    batch.draw(fbTex, -1, 1, 1, -1);
    batch.draw(fbTex, -1, 0, 1, 1);
    batch.draw(fbTex, 0, 0, 1, 1);
    batch.draw(fbTex, 0, 1, 1, -1);
    batch.end();
    

    【讨论】:

      【解决方案2】:

      你可以使用TextureRegion翻转

          void flip(boolean x, boolean y);
      

      batch.draw() 之前的方法。

      如果您的 img 是 Texture 实例,您可以使用以下构造函数将其投射到 TextureRegion 之一

          public TextureRegion(Texture texture)
      

      【讨论】:

        猜你喜欢
        • 2017-03-27
        • 2016-07-29
        • 1970-01-01
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        相关资源
        最近更新 更多