3d 三维图形libgdx的正交投影

3d 三维图形libgdx的正交投影,3d,libgdx,2d,perspectivecamera,orthographic,3d,Libgdx,2d,Perspectivecamera,Orthographic,我正在实施这个简单的教程 blog.xoppa.com/basic-3d-using-libgdx-2/ 但是试图用正交的相机来代替透视相机 但是,我在理解相机位置如何工作时遇到了一些问题: 我补充说 cam2 = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); cam2.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeig

我正在实施这个简单的教程
blog.xoppa.com/basic-3d-using-libgdx-2/
但是试图用正交的相机来代替透视相机

但是,我在理解相机位置如何工作时遇到了一些问题:
我补充说

cam2 = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam2.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam2.position.set(0,0,10);
cam2.lookAt(0, 0, 0);
cam2.update()
我得到一个非常小的正方形。如果我改变位置

    cam2.position.set(0,0,100);
我什么都看不见,我想知道为什么正交摄影机应该忽略z轴。
基本上我需要的是创建一个3D形状,同时在两个不同的视图中显示它,一个使用透视图,另一个使用正交摄影机

谢谢

这是完整的代码

public class MyGdxGame implements ApplicationListener {
//  public PerspectiveCamera cam;
public OrthographicCamera cam2;

public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;

@Override
public void create() {
    modelBatch = new ModelBatch();

//      cam = new PerspectiveCamera(67, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
    cam2 = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam2.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

 //        cam.position.set(10f, 10f, 10f);
 //     cam.lookAt(0, 0, 0);
 //     cam.near = 1f;
 //     cam.far = 300f;
 //     cam.update();

    cam2.position.set(0, 0,0);
    cam2.lookAt(0, 0, 0);
    cam2.update();

    ModelBuilder modelBuilder = new ModelBuilder();
    model = modelBuilder.createBox(50f, 50f, 50f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
    instance = new ModelInstance(model);
}

@Override
public void render() {
    cam2.update();

     Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
 //     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

//      modelBatch.begin(cam);
//      modelBatch.render(instance);
//      modelBatch.end();
//      
//      Gdx.gl.glViewport(Gdx.graphics.getWidth()/2, 0, Gdx.graphics.getWidth()/2,
//              Gdx.graphics.getHeight()/2);
//      
    modelBatch.begin(cam2);
    modelBatch.render(instance);
    modelBatch.end();
}

@Override
public void dispose() {
    modelBatch.dispose();
    model.dispose();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {

}
} 

在正交摄影机上,请使用缩放。

正交摄影机(幸运的是)不会忽略z。简单地说,它基本上是一个不同的投影,即它有一个长方体而不是金字塔。查看本文以获得更深入的解释:

请注意,默认情况下,相机的近距离值为1f,远距离值为100f。在您的情况下,您正将100f单元从模型中移出。换句话说:模型在远平面上。根据各种因素,您将看到部分模型(如果有的话)

现在,当您执行此操作时:

new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
你基本上是说你的世界中的一个单位应该映射到一个像素。因此,您的模型(50x50x50框)的宽度/高度约为50像素。如果要将一个世界单位映射到例如两个像素,可以执行以下操作:

new OrthographicCamera(Gdx.graphics.getWidth() /2f, Gdx.graphics.getHeight() / 2f);
等等。如果您希望更好地控制这一点,可以考虑使用视口,请参阅:


另外,请注意不要将相机放在模型内,正如daniel所说,不要看你的
位置
,这会导致你的
方向
为零。

我知道我可以使用缩放,但根据许多像这样的教程,“正交相机,基本上忽略了z坐标。”…那么为什么更改z位置会得到不同的视图?如果位置相同且大小非常不同,这可能是一个错误,而且我注意到在查看之前或之后设置位置很重要。我没有更改任何内容:我首先编译应用程序设置cam2.position.set(0,0,10);然后使用cam2.position.set(0,0100)或cam2.position.set(0,0200);我什么也得不到。。。然后根据官方维基,相机的位置应该是(宽度/2,高度/2,0)[视口的中心],但我不工作…可能在代码中的某个地方你有错误,还要检查render()或update()方法,我对libgdx中的相机没有任何问题。将“注视”和“位置”都设置为“零”并不好。请删除“注视”以使ortho看到它的工作状态!