View Full Version : Centering a 3D model
11-06-2009, 02:07 PM
Any idea how to center a 3D model in the viewport in screenspace, rather than 3D space?
Trying to pisiton the center of the object at the origin in 3D does not make the object centered in viewport, depending on the shape of the object.
11-06-2009, 02:14 PM
Compute 'correct' center first (average vertex positions / center of mass / center of volume).
Then put it somewhere along the direction of the camera.
11-06-2009, 03:04 PM
What I did is computing the center of the bounding box of the object as follows:
center.X = (maxX + minX) / 2
center.Y = (maxY + minY) / 2
center.Z = (maxZ + minZ) / 2
where min/max are the min/max of the object coordinates.
Here's what I do: 1) Setup your camera and clipping planes so that a box with coordinates (-1 to +1) in all directions fits into the field of view nicely. Test this by drawing the wireframe box. I like to use the default camera, which is located at the origin looking into the screen (-Z direction). My cube is centered at the origin, so it must be translated in -Z to be seen by the camera (assuming perspective projection). 2) Find the geometric center of your object (as you have done) and translate the object by (-center.X, -center.Y, -center.Z), which centers it at the origin. 3) Find the scale factor which when applied to your object fits it into the cube defined in step 1. The scale factor (sf) is the maximum of dx, dy, and dz, where dx = (maxX - minX) / 2, dy = ...., and so on. 4) Scale your object by 1/sf. 5) Translate your scaled object by the amount you translate the cube to get it into the field of view of the camera.
11-08-2009, 06:48 PM
This is what you can do to know "how far" the camera should be:
camPos.Y = worldMin.Y - ((worldMax.X - worldMin.X) / 2) * 1.1f / tan((cameraFov/180.0f*(float)M_PI)/2);
Note: 1.1f is to add a 10% margin, the camera points towards positive Y, up is 0,0,1 and this is not taking aspect ratio into account...
11-09-2009, 11:32 AM
I understand centering in 3D which works for regular objects. Try to create a box with part of it's front sticking out toward the viewer, and it will not be centered in screen space. The extruded part will become closer and closer to one edge of the viewport.
Zooming out does not solve the problem because the centering is incorrect wrt X and Y.
What I'm trying to do is centering in screen space rather than 3D. :)
I think you'd better think about what you mean by 'centered in screen space'. Cause my algorithm gives it to you. What it means to me is that no matter how you rotate an object, no part of it will go out of the camera FOV, including forward or rear clipping planes.
James W. Walker
11-09-2009, 05:33 PM
When glfreak says "centered in screen space", I assume he/she means that the distance from the leftmost rendered pixel to the left edge of the screen equals the distance from the rightmost rendered pixel to the right edge of the screen, and similarly for the vertical axis.
If I understand correctly, your suggestion, MaxH, involves fitting the object in an axis-aligned bounding box which is in turn centered on the screen. Doing so will not necessarily center the pixels that are actually drawn by the object. I don't know a general way of doing that without rendering the object and looking at the pixels.
... Doing so will not necessarily center the pixels that are actually drawn by the object. Now we are getting down to semantics. My approach would center the pixels drawn for the union of all possible orientations of the object, but not for ONE specific orientation.
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.