我正在尝试创建一种四向对称程序,该程序具有1个摄像机查看特定场景,但只是翻转4个面板中每个摄像机的视图。
例如:symmetry
第1象限是世界的常规方向,右边的x值为正,y值为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或线性代数进行足够的工作,无法立即获得明确的答案。
任何帮助,将不胜感激。谢谢。
最佳答案
您可以通过几种方法来执行此操作。
翻转相机
围绕矩阵进行翻转,而不会弄乱相机。
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();
帧缓冲区
如果场景很复杂,这可能会更快,因为实际场景仅绘制一次。以调整大小创建帧缓冲区对象。尝试/捕获是因为某些旧式或廉价电话在帧缓冲区对象中不支持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();