1

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() {

}
} 

2 Answers 2

2

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.

0
0

On OrthographicCamera use zoom instead.

5
  • I knew I can use zoom, but according to many tutorials like this github.com/libgdx/libgdx/wiki/Projection,-viewport,-%26-camera "orthographic camera, which basically ignores the z coordinate."...so why by changing the z position I get a different view? Commented Apr 24, 2014 at 13:54
  • That might be bug if position is the same and size is very different, also I have notices setting position before or after lookAt is important.
    – daniel
    Commented Apr 24, 2014 at 14:15
  • I don't change anything: I compile the application first setting cam2.position.set(0,0,10); and then using cam2.position.set(0,0,100) or cam2.position.set(0,0,200); I get nothing... then according to the official wiki github.com/libgdx/libgdx/wiki/Orthographic-camera the position of the camera should be (WIDTH / 2, HEIGHT / 2, 0) [center of the viewport] but I doesn't work... Commented Apr 24, 2014 at 14:30
  • probably in your code somewhere you have wrong, also check render() or update() method, i have no problem with camera in libgdx.
    – daniel
    Commented Apr 24, 2014 at 14:43
  • it is not good to have both lookat and position at zero remove lookat for ortho see its working!
    – daniel
    Commented Apr 24, 2014 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.