• Create Account

## How to set camera to maximize the mesh view into the screen?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1jor1980  Members

152
Like
0Likes
Like

Posted 11 April 2014 - 07:46 AM

Hi.

I am making a program using jmonkey to preview the meshes contained into a filetype. Each file has a mesh of diferent sizes and positions, I want to make my program focus the mesh contained in the file trying to view it in a way that the mesh uses almost all the space in the viewport.

I would like to know if there is a mathematical way to make this, and achieve to preview the meshes using as much space in the screen as possible.

### #2Stainless  Members

1875
Like
3Likes
Like

Posted 11 April 2014 - 09:27 AM

Couple of ways of doing this, both need to know the size of the mesh.

So you need to first generate a bounding box or a bounding sphere. (there are lots of online resources to help you with this)

Then you can either ...

1) Move the camera forward until the mesh fills the screen.

To work out the distance to place the camera you calculate distance = bounding_sphere.radius / tan(field_of_view)

2) You change the field of view in your projection matrix

To work out the field of view required you calculate fov = atan(bounding_sphere.radius/camera_distance)

Both might need a little adjustment , but it's a good starting point.

### #3NotYourAverageUser  Members

500
Like
0Likes
Like

Posted 11 April 2014 - 12:40 PM

Couple of ways of doing this, both need to know the size of the mesh.

So you need to first generate a bounding box or a bounding sphere. (there are lots of online resources to help you with this)

Then you can either ...

1) Move the camera forward until the mesh fills the screen.

To work out the distance to place the camera you calculate distance = bounding_sphere.radius / tan(field_of_view)

2) You change the field of view in your projection matrix

To work out the field of view required you calculate fov = atan(bounding_sphere.radius/camera_distance)

Both might need a little adjustment , but it's a good starting point.

Personally, I like changing the distance of the camera approach.  Messing with the FOV too much also messes with perspective.  And sometimes that's just plain funny looking.

### #4Stainless  Members

1875
Like
0Likes
Like

Posted 12 April 2014 - 07:38 AM

Yes it can do.

But at times it's great. For example on a recent racing game I used the FOV technique to give you the feeling of zooming in on a car.

Completely screwed up my shadows though.

### #5jor1980  Members

152
Like
0Likes
Like

Posted 12 April 2014 - 08:57 AM

As i am new in game programming, i was trying to obtain the field of view of my jme3 camera but i don´t know how to do it. Here is the camera class definition: