PDA

View Full Version : fit to the screen

AeroSujan
10-30-2011, 09:04 AM
Dear Friends
How can I fit my 3d model on the screen... I am using perspective projection. Please give me some idea.

V-man
10-30-2011, 10:04 AM
I would project the vertices to window space. Use gluProject. If all the vertices are inside the viewport, move closer to the model. Compute vertices in window space again and check again if all vertices are inside.

AeroSujan
10-31-2011, 03:17 AM
I have a bounding box. minX, maxX, minY, maxY, minZ, maxZ...With these values how can I fit the bounding box onto the screen. Please help me. Thanks Sujan

thokra
10-31-2011, 04:46 AM
If you want to be exact, V-man's method is probably the best.

If you need only coarse fitting then you can simply treat the extents of the bounding box and treat it as the radius if the enclosing bounding sphere. From the top of my head I figured the following.

Assuming a symmetrical view frustum, the minimum distance to ensure the bounding sphere is tightly enclosed by the frustum can be computed using the field of view, the center and the radius of the bounding sphere.

Example:

Let

fov: opening angle of the view frustum
C: center of the sphere
d: min distance of the camera
E: eye of the camera
D: the viewing direction of the camera

r = |(maxX, maxY, maxZ)|

The distance of the camera can then be computed by

d = r / tan(fov / 2)

Assuming a simple shift parallel to the positive z-axis in world coordinates, one possible camera position and direction can be computed by

E = C + d * (0, 0, 1)
D = C - E / |C - E| (normalized)

The near and far clipping planes have to be at least

n = d - r
f = d + r

That should do. Try it and tell if it works for you.