position camera for object to fit in screen height

Started by aeroxr1, September 24, 2014, 01:47:38 AM

Previous topic - Next topic

aeroxr1

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 :
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 :

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 ?

EgonOlsen

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?