Pergunta

I've created a simple "engine" in DirectX which can load and render models from .obj files. It works ok, for the objects I've created, e.g. a simple cube with coordinates within -1.0 to 1.0 range. However loading a model which uses different units and has coordinates within -2k to 2k range results in a huge something which doesn't even entirely fit in the depth buffer, so I see like one huge leg or a similar effect.

I remember reading somewhere about it, but as I went through a lot of DX stuff recently I can't remember where or what it was. So my question is, assuming I want to be able to load different models, which use different units, what is the best way to ensure they all have a similar size and fit nicely on the screen?

I could try to convert the values upon loading the object, but maybe there is some more DirectX way which uses transformation matrices or shaders?

Foi útil?

Solução

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).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top