Reputation: 494
We are working on a augmented reality project wherein the user will be able to virtually place 3d furniture objects on camera.
We are able to place the 3d object (obj file) on capture image, java 3d allows me dynamically change or re-position the object using mouse and ViewingPlatform class, but I am not able to detect the new position of the object.
Can you tell me on how to detect the Changed Angle or position of the 3d object?
public BranchGroup createSceneGraph(String file) {
objScale = new TransformGroup();
objTrans = new TransformGroup();
trans = new Transform3D();
BranchGroup objRoot = new BranchGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(scaling);
trans.setTranslation(new Vector3f(xloc, yloc, 0.0f));
objScale.setTransform(trans);
objRoot.addChild(objScale);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
objTrans.setCapability(Group.ALLOW_CHILDREN_EXTEND);
objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);
objTrans.setCapability(Group.ALLOW_BOUNDS_WRITE);
objTrans.setCapability(Group.ALLOW_BOUNDS_READ);
objScale.addChild(objTrans);
int flags = ObjectFile.RESIZE;
flags |= ObjectFile.TRIANGULATE;
flags |= ObjectFile.STRIPIFY;
ObjectFile f = new ObjectFile(flags,
(float) (creaseAngle * Math.PI / 180.0));
System.out.println(60 * Math.PI / 180.0);
try {
loadscente = f.load(filename);
} catch (Exception e) {
System.err.println(e);
}
objTrans.addChild(loadscente.getSceneGroup());
bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 50.0);
System.out.println(bounds + "Bounds");
getBackgroundImage();
objRoot.addChild(bgNode);
addLightsToUniverse();
return objRoot;
}
public void addCompInPanel() {
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
c = new Canvas3D(config);
jPanel2.add("Center", c);
c.addKeyListener(this);
c.addMouseListener(this);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph(filename);
u = new SimpleUniverse(c);
// add mouse behaviors to the ViewingPlatform
ViewingPlatform viewingPlatform = u.getViewingPlatform();
PlatformGeometry pg = new PlatformGeometry();
// Set up the ambient light
Color3f ambientColor = new Color3f(Color.RED);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
pg.addChild(ambientLightNode);
// Set up the directional lights
Color3f light1Color = new Color3f(Color.RED);
Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
pg.addChild(light1);
DirectionalLight light2 = new DirectionalLight(light2Color,
light2Direction);
light2.setInfluencingBounds(bounds);
pg.addChild(light2);
viewingPlatform.setPlatformGeometry(pg);
keyNavBeh = new KeyNavigatorBehavior(objTrans);
keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(),
1000.0));
System.out.println(keyNavBeh.getSchedulingBounds() + " keyNavBeh.getSchedulingBounds();");
objTrans.addChild(keyNavBeh);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
viewingPlatform.setNominalViewingTransform();
OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(orbit);
u.addBranchGraph(scene);
jSlider1StateChanged(null);
}
Upvotes: 2
Views: 366
Reputation: 328556
Looking at the source code for KeyNavigator
, the code in the method integrateTransformChanges()
updates the transform group:
targetTG.setTransform(vpTrans);
Note that this changes the transform matrix applied to the group, it doesn't change object vertices or the object's origin as such - when the while scene is rendered, the transformation matrix is applied to every vector.
So a simple objTrans.getTransform()
will do the trick.
[EDIT] To get the transformations done by the OrbitBehavior
, use viewingPlatform.getViewPlatformTransform().getTransform()
Related:
Upvotes: 1