我们正在致力于一个增强现实项目,在这个项目中,用户将能够将3d家具物体虚拟地放置在相机上。
我们可以将3d对象(obj文件)放置在捕获图像上,java3D允许我使用鼠标和ViewingPlatform类动态地更改或重新定位对象,但是我无法检测对象的新位置。
你能告诉我如何检测改变的角度或位置的三维物体吗?
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);
}发布于 2014-12-29 15:21:31
查看KeyNavigator的源代码,integrateTransformChanges()方法中的代码更新转换组:
targetTG.setTransform(vpTrans);请注意,这会改变应用于组的转换矩阵,它不会改变对象顶点或对象的原点--当呈现时间场景时,转换矩阵将应用于每个向量。
因此,一个简单的objTrans.getTransform()就能做到这一点。
编辑以获取由OrbitBehavior完成的转换,使用viewingPlatform.getViewPlatformTransform().getTransform()
相关信息:
https://stackoverflow.com/questions/27685726
复制相似问题