我们正在开展一个增强现实项目,用户可以在其中虚拟地将 3d 家具对象放在相机上。
我们能够将 3d 对象(obj 文件)放置在捕获图像上,java 3d 允许我使用鼠标和 ViewingPlatform 类动态更改或重新定位对象,但我无法检测到对象的新位置.
你能告诉我如何检测 3d 对象的改变角度或位置吗?
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);
}
最佳答案
查看 KeyNavigator
的源代码,integrateTransformChanges()
方法中的代码更新转换组:
targetTG.setTransform(vpTrans);
请注意,这会更改应用于组的变换矩阵,它不会更改对象顶点或对象的原点本身 - 当渲染 while 场景时,变换矩阵会应用于每个 vector 。
所以一个简单的 objTrans.getTransform()
就可以解决这个问题。
[编辑] 要通过 OrbitBehavior
完成转换,请使用 viewingPlatform.getViewPlatformTransform().getTransform()
相关:
关于Java 3d 编程 - 无法访问 3d 对象的更改位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27685726/