Author Topic: position camera for object to fit in screen height  (Read 598 times)

Offline aeroxr1

  • int
  • **
  • Posts: 82
    • View Profile
position camera for object to fit in screen height
« on: September 24, 2014, 01:47:38 am »
Hi :)

I have an 3d model and I want to set my camera to fit all the object in screen height.

I did in this way .

I have calculated bounding box :
Code: [Select]
protected float[] calcBoundingBox() {
float[] box = null;

for (Animated3D skin : modello) {
float[] skinBB = skin.getMesh().getBoundingBox();

if (box == null) {
box = skinBB;
} else {
// x
box[0] = Math.min(box[0], skinBB[0]);
box[1] = Math.max(box[1], skinBB[1]);
// y
box[2] = Math.min(box[2], skinBB[2]);
box[3] = Math.max(box[3], skinBB[3]);
// z
box[4] = Math.min(box[4], skinBB[4]);
box[5] = Math.max(box[5], skinBB[5]);
}
}
return box;
}

and after :

Code: [Select]
float[] bb = calcBoundingBox();
box_h = (bb[3] - bb[2]); // model height


Camera camera = world.getCamera();
if(width < height)
{
camera.setPosition(0,-box_h/2,0);
camera.moveCamera(Camera.CAMERA_MOVEOUT, 70);  //la sposto indietro
camera.lookAt(new SimpleVector(0, -box_h/2, 0));
}
else
{
float fovy=camera.getYFOV();


double cameraDistance = ( box_h / 2 ) / Math.tan( fovy / 2 );


camera.setPosition(0,-box_h/2,(float)cameraDistance-100);


camera.lookAt(new SimpleVector(0, -box_h/2, 0));


}

With the -100 offset the 3d model correctly fits in both the portrait mode than landscape mode. But I didn't understand why I have to pur the negative offset -100..

Where did I do wrong ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: position camera for object to fit in screen height
« Reply #1 on: September 24, 2014, 08:56:57 pm »
I'm not sure...might be caused by the near clipping plane. Maybe your calculation assumes it to be a z=0 but it's actually at z=1 by default. Anyway, i wouldn't bother with this too much. If it works this way, all all is fine, isn't it?