Author Topic: Screen orientation and camera  (Read 4920 times)

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Screen orientation and camera
« on: September 25, 2012, 07:15:47 pm »
Hi there,

How do I make the scene look the same in both portrait and landscape orientation ? If I setup my scene in portrait mode, when I switch to landscape, the objects don't fit the screen and look bigger, as if the camera position was changed. In the other 3d engine I'm using (Rajawali), the scene looks the same in both landscape and portrait mode.

Thank you.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Screen orientation and camera
« Reply #1 on: September 25, 2012, 08:44:42 pm »
The scene can never look the same if you switch from x*y to y*x. If it would, switching resolutions would be pointless. I think you mean that the size of objects in the scene doesn't change, right? If so...well, in jPCT you can set the fov (field of view). The field of view is indepent of the resolution or the orientation. If you look at the rendered image, you'll notice that the field of view (i.e. how wide is the viewing angle between the left and the right egde) always stays the same. The logical consequence of this is, that objects get bigger on screen if the x-resolution increases (i.e. if you switch to landscape). Any other behaviour would render a fov setting useless. If you want different behaviour, you have to change the fov between landscape and portrait mode.

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Re: Screen orientation and camera
« Reply #2 on: September 26, 2012, 01:45:56 am »
Thank you Egon, then I should use Camera.setFOV() in onSurfaceChanged() ? What value should I pass to setFOV() ? it's all about trial and error ?

Offline OneManSitting

  • byte
  • *
  • Posts: 9
    • View Profile
Re: Screen orientation and camera
« Reply #3 on: September 26, 2012, 05:16:41 pm »
well i mean you can calculate the field of view. :-\

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Re: Screen orientation and camera
« Reply #4 on: September 27, 2012, 05:06:47 pm »
Egon, this is what the Rajawali engine does onSurfaceChanged():

Code: [Select]
public void onSurfaceChanged(GL10 gl, int width, int height) {
mViewportWidth = width;
mViewportHeight = height;
mCamera.setProjectionMatrix(width, height);
GLES20.glViewport(0, 0, width, height);
}

And this is the camera setProjectionMatrix() method referenced above:

Code: [Select]
public void setProjectionMatrix(int width, int height) {
float ratio = (float) width / height;
float frustumH = MathUtil.tan(getFieldOfView() / 360.0f * MathUtil.PI) * getNearPlane();
float frustumW = frustumH * ratio;

Matrix.frustumM(mProjMatrix, 0, -frustumW, frustumW, -frustumH,
frustumH, getNearPlane(), getFarPlane());
}


Can I replicate this in JPCT-AE ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Screen orientation and camera
« Reply #5 on: September 27, 2012, 09:42:35 pm »
That's actually the exact same thing that jPCT-AE does except that jPCT-AE assumes that fov means the field of view in x-direction while this code assumes that fov defines the field of view in y-direction. Personally, i find this highly unintuitive, because nobody defines his field of view in up/down-direction. Anyway, you can get behaviour by setting the fov values for x and y explicitly. To do this, you can do something like:

Code: [Select]
Camera cam = world.getCamera();
float fov=1.25f;
if (w > h) {
cam.setFOV(fov);
cam.setYFOV(fov * ((float)h / (float)w));
} else {
cam.setFOV(fov * ((float)w / (float)h));
cam.setYFOV(fov);
}

Hope this helps.

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Re: Screen orientation and camera
« Reply #6 on: September 28, 2012, 12:29:26 am »
That's actually the exact same thing that jPCT-AE does except that jPCT-AE assumes that fov means the field of view in x-direction while this code assumes that fov defines the field of view in y-direction. Personally, i find this highly unintuitive, because nobody defines his field of view in up/down-direction. Anyway, you can get behaviour by setting the fov values for x and y explicitly. To do this, you can do something like:

Code: [Select]
Camera cam = world.getCamera();
float fov=1.25f;
if (w > h) {
cam.setFOV(fov);
cam.setYFOV(fov * ((float)h / (float)w));
} else {
cam.setFOV(fov * ((float)w / (float)h));
cam.setYFOV(fov);
}

Hope this helps.

Thank you very much Egon ! :)