I know TransformGroup and Transform3D are a very good way to get performances and Transform trees are sometimes useful… But that code is just not cool :
TransformGroup objTranslate = new TransformGroup();
Transform3D translate = new Transform3D();
translate.setTranslation(new Vector3f(-2f, -5f, 0f));
objTranslate.setTransform(objTranslate);
TransformGroup objRotate = new TransformGroup();
Transform3D rotate = new Transform3D();
rotate.rotXYZ((float)Math.toRadians(270f), 0f, 0f);
objRotate.setTransform(rotate);
TransformGroup objScale = new TransformGroup();
Transform3D scale = new Transform3D();
scale.setScale(0.06f);
objScale.setTransform(scale);
objScale.addChild(amodel);
objRotate.addChild(objScale);
objTranslate.addChild(objRotate);
addChild(objTranslate);
It just does translate, then rotate, then scale, the “amodel” Shape3D.
I already did a proposal for cascade calls, which would reduce the code to :
TransformGroup objTranslate = new TransformGroup();
Transform3D translate = new Transform3D();
translate.setTranslation(new Vector3f(-2f, -5f, 0f));
objTranslate.setTransform(objTranslate);
TransformGroup objRotate = new TransformGroup();
Transform3D rotate = new Transform3D();
rotate.rotXYZ((float)Math.toRadians(270f), 0f, 0f);
objRotate.setTransform(rotate);
TransformGroup objScale = new TransformGroup();
Transform3D scale = new Transform3D();
scale.setScale(0.06f);
objScale.setTransform(scale);
addChild(objTranslate).addChild(objRotate).addChild(objScale).addChild(amodel);
Hmm… still not that good. maybe an helper method in Transform3D something like getTG() that creates a new transform group and set the Transform3D… that would reduce code to :
Transform3D translate = new Transform3D();
translate.setTranslation(new Vector3f(-2f, -5f, 0f));
Transform3D rotate = new Transform3D();
rotate.rotXYZ((float)Math.toRadians(270f), 0f, 0f);
Transform3D scale = new Transform3D();
scale.setScale(0.06f);
addChild(translate.getTG()).addChild(rotate.getTG()).addChild(scale.getTG()).addChild(amodel);
And now we could add an addChild(Transform3D t) method so it calls getTG() automatically, that would reduce code to :
Transform3D translate = new Transform3D();
translate.setTranslation(new Vector3f(-2f, -5f, 0f));
Transform3D rotate = new Transform3D();
rotate.rotXYZ((float)Math.toRadians(270f), 0f, 0f);
Transform3D scale = new Transform3D();
scale.setScale(0.06f);
addChild(translate).addChild(rotate).addChild(scale).addChild(amodel);
And we could also provide cool constructors for Transform3D :
Transform3D translate = new Transform3D(new Vector3f(-2f, -5f, 0f));
Transform3D rotate = new Transform3D((float)Math.toRadians(270f), 0f, 0f);
Transform3D scale = new Transform3D(scale.setScale(0.06f));
addChild(translate).addChild(rotate).addChild(scale).addChild(amodel);
4 lines vs 12 lines with the original version !! And much clearer… Of course it breaks a bit of API strict definitions but It could be really convenient. If there’s no objection I may implement it.
Note about performances : implementation requires that Transform3D contains a “TransformGroup tg = null” field. This doesn’t consume more memory as long as tg equals null, right ? And it would lazy-create it only when needed
Note about bugs… If you call addChild(translate) two times it would fail. So the solution is to catch the IllegalScenegraphOperationException in the addChild(Transform3D t) method and to call the transform3d.getNewTG() method, which instanciate a new TransformGroup… and then we would need a Vector in the Transform3D class
Then when you would call something like rotXYZ(), setTranslation() or setScale(), the transform3D would update the TransformGroup (transformGroup.setTransform(this)) automagically so there are no more surprises…
Pheww… a whole programme, heh ? ;D