I'm implementing this simple tutorial
blog.xoppa.com/basic-3d-using-libgdx-2/
but trying to substitute the perspective camera with an orthographic one.
However, I've some problems in understanding how the camera position works:
I added
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()
and I get a very tiny square. If I change the position.
cam2.position.set(0,0,100);
I can't see anything, and I wonder why since the orthographic camera should ignore the z.
Basically what I need is to create a 3D shape and display it at the same time in two different views, one using a perspective came and the other one using a orthographic camera.
Thanks!
Here is the full code
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() {
}
}
An orthographic camera (luckily) does not ignore z. Simply said, it is basically a different projection, namely it has a box frustum instead of a pyramid. Check this article for a more in depth explanation: http://www.badlogicgames.com/wordpress/?p=1550
Note that by default the camera near value = 1f and the far value = 100f. In your case, you're exactly moving 100f units away from your model. In other words: the model is on the far plane. Depending on various factors you'll see the model partially, if at all.
Now, when you do this:
new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
You're basically saying that one unit in your world should map to one pixel. Thus your model (a 50x50x50 box) will be about 50 pixels wide/height. If you want to map one world unit to e.g. two pixels, you could do:
new OrthographicCamera(Gdx.graphics.getWidth() /2f, Gdx.graphics.getHeight() / 2f);
And so forth. If you want better control on this, you could consider using a Viewport, see: https://github.com/libgdx/libgdx/wiki/Viewports
Also be careful to not place your camera inside your model and, as daniel said, dont lookAt
your position
, it will cause your direction
to be zero.