LIBGDX:你如何水平/垂直翻转相机视图?

时间:2016-06-13 23:55:18

标签: java libgdx

我正在尝试创建一个四路对称程序,其中有一台摄像机可以查看特定场景,但只需翻转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或线性代数足以看到一个明确的答案。

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

您可以使用

翻转TextureRegion
lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)

Sub Macro1() 'Make a variable to store the cell found Dim lastCell as Range 'find the last cell in Column A of the active sheet lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1) 'Paste in the I16 value lastCell.value = RangE("I16").value 'Grab whatever is hanging out in Column B next to the last cell and stick it in J20 Range("J20").value = lastCell.Offset(0,1).value 'Test to see if I16 has value "R" If Range("I16").value = "R" Then 'Find the last row in Sheet7, Column B and store it to the variable lastCell = Range("B" & Rows.Count).End(xlUp).Offset(1) 'Copy J20 value to the lastCell in Sheet 7, Column B lastCell = Range("J20").value End if End Sub 之前的方法。

如果您的 void flip(boolean x, boolean y); 是纹理实例,则可以使用以下构造函数 转换为纹理实例

batch.draw()