The simplest way is to find on which component the model contains the largest range, and use that value to scale the model uniformly. To find the largest range, compute the model bounding box (unless wavefront obj. files provide it already, but maybe you'd like to verify it anyway), and use the box dimension as the range to compare. This would scale all imported models down to the unit range.
For instance, if you have a model with a bounding box covering 374 * 820 * 512 units, the largest one is the Y axis with 820, so you just have to scale it down by 820 (divide all the components by 820). This can be conveniently achieved with a uniform scaling matrix, which can be initialized with the D3DXMatrixScaling
, if I am not mistaken.
Assuming that the bounding box is given by 2 vectors (max and min vertices), compute the difference of these vectors, then pick the largest value, and use its inverse as scaling factor for your matrix.
D3DMatrix mScale;
D3DVector mx, mn;
obj->getBoundingBox(&mn,&mx);
float scale = 1.0 / max(mx.x - mn.x, max(mx.y - mn.y, mx.z -mn.z));
D3DXMatrixScaling( &mScale, scale, scale,scale);
All is left to do is to insert the matrix in your rendering pipeline.
Regarding the computation of bounding box, a simple loop on all the vertices of the model, keeping the minimun and maximum components (independently of each others), should do it.
D3DVector mn, mx;
mn.x = mn.y = mn.z = MAX_FLOAT;
mx.x = mx.y = mx.z = - MAX_FLOAT;
for (int i = 0; i < numVertices ; i++) {
mn.x = min(mn.x, vertex[i].x);
mn.y = min(mn.y, vertex[i].y);
mn.z = min(mn.z, vertex[i].z);
// same for mx using max
}
Of course, you might need to use some scaling factor if your view covers a larger area than the unit box (but apparently this is what you are using).