Set Camera Position and Orientation to fit Mesh

boyamer

30-04-2009 16:03:02

How can i set the camera position and orieantation to fit perfectly whole mesh?

I have different meshes with different size and i'd like to view the whole mesh...

currently i'm doing this,but it doesn't fit well the mesh:


//Adjust camera to fit all
float l = (ent.BoundingBox.Maximum - ent.BoundingBox.Minimum).Length;

renderCamera.SetPosition(0,0,l + 10f);
renderCamera.SetAutoTracking(true,Renderer.Instance.SceneManager.RootSceneNode);

Beauty

04-05-2009 02:25:56

Sorry, I don't know the answer.

Suggestions:
* choose a name like size instead of l, because it's looking like the number 1. So I was confused in the first moment.
* choose a factor instead of an addition (which is better for different sizes I suppose) SetPosition(0, 0, size * 1.2f);
* maybe it's possible to get the BoundingBox (AABB) of a SceneNode? Would be better because of scale, childs or multiple entities.

Problem: your camera only moves on the Z axis.
Maybe use the position vector of the SceneNode, reduce the length (downsize x/y/z) and set the camera to there.
Then you can use Camera.LookAt(...) to set the orientation.

For Camera.SetAutoTracking(...) it would be better to use the SceneNode of the object.
(Often objects aren't attached to the RootSceneNode)

Also you can ask in the main forum. Because this is a common question, not Mogre related. In the main forum are much more people who can help. Porting the code of small snippet should be easy. Maybe there is still a solution - search for it in the main forum.

If you got a working solution, it would be nice if you post it here :wink:

jmix90

04-05-2009 13:43:21

Hello,

I don't have the answer too :P !

But this can help you: the "width of your mesh" is not the only things to consider. take also a look at the FOV factor. You can set it by adjusting the near clippling plane and far clipping plane...

Tell me if I am wrong :) !

Good luck ;-)

Beauty

18-05-2009 13:21:07

Ok, this is also new for me, but nice to know. FOV means Field Of View.
A description of Camera.setFOVy is here:
http://www.ogre3d.org/docs/api/html/cla ... bf635e0c75

Also important is the Camera.setNearClipDistance param:
http://www.ogre3d.org/docs/api/html/cla ... 8a237eb9d3

thatnerdyguy

23-05-2009 22:36:23

If you know your FOV, and the radius of the bounding sphere of your mesh, a little trig will get you your answer:

AxisAlignedBox bbox = // Get the bouding box of the thing you want to look at

Vector3 offset = bbox.Maximum - bbox.Center; // Radius of bounding sphere
Vector3 center = bbox.Center;

viewcam.Position = center;
float oneOverSine = 1.0f / (float)System.Math.Sin(viewcam.FOVy.ValueRadians / 2.0); // 1 / sin = adjacent / opposite
float distanceToCenter = offset.Length * oneOverSine; // (adjacent / opposite) * opposite = adjacent
viewcam.Position += (new Vector3(1, 1, 1).NormalisedCopy * distanceToCenter);
viewcam.LookAt(center);

Beauty

23-05-2009 22:44:02

This would be a usefull snippet for the wiki.
I put it to my todo list :wink:

dphil

28-05-2011 03:42:48

Er, shouldn't that be tan, not sin?
Sin = opposite/hypotenuse
Tan = opposite/adjacent

I use tan and get appropriate-looking results.