I’m loading an Obj file (using ObjLoader) to a TriMesh, in order to get its AABB (by calling calcBoundingBox()) which I need for some physics calculation.
However, the model needs to be manipulated before (scale, rotation and translation).
Is there any way to apply those transformation on the mesh that are not in the context of drawing?
(e.g something similar to chaining geom::Scale()/geom::Rotate()/geom::Translate()as with geom::Source) such that when calling tocalcBoundingBox()` the model would already be in its final configuration.
You can submit a mat4 for calcBoundingBox() and then your model will be transformed by that matrix when calculating the bounding box. Would this solve your problem?
Hi @gabor_papp thanks for the quick reply.
Could you elaborate please? Is there a way to build that matrix once and use it for both the drawing and the calculation of the AABB?
This can be slow if you transform many vertices, but if the model is static, you can calculate the bounding box once and transform it with the matrix every frame as the transformation changes.
// do this once when loading the mesh
mTriMeshBbox = mTriMesh->calcBoundingBox();
...
// update this every frame
AxisAlignedBox bbox = mTriMeshBbox.transformed( mTransform );
@gabor_papp Hah! it did the trick. I was sure I’ve tried it before…
The whole thing works now - thanks.
Still, I don’t understand how rotate() was found without the namespace prefix and scale() wasn’t (I don’t have other function with the same name in any scope)
before calling gl::multModelMatrix, make sure to store/push the current matrix so you can restore it when you’re done drawing the mesh. You can do this by calling gl::pushModelMatrix and gl::popModelMatrix, but a more convenient way is to call gl::ScopedModelMatrix scpModel; This will create an object which pushes the matrices, then pops them again when it goes out of scope, RAII style. There are many more scoped variations like that, all worth using. The following code is equivalent:
When multiplying matrices, the order is important. Transformations are performed from last to first in the line mTransform = translate * rotate * scale;. Usually, a mesh is first rotated and/or scaled, followed by a translation. That way, it rotates or scales around its own origin.