如何在Java3D中找到直接从虚拟世界指向屏幕的法线向量?
时间:2020-03-05 18:46:35 来源:igfitidea点击:
我认为可以通过将场景图的变换矩阵应用于z-normal(0、0、1)来完成,但这是行不通的。我的代码是这样的:
Vector3f toScreenVector = new Vector3f(0, 0, 1); Transform3D t3d = new Transform3D(); tg.getTransform(t3d); //tg is Transform Group of all objects in a scene t3d.transform(toScreenVector);
然后我也尝试过类似的方法:
Point3d eyePos = new Point3d(); Point3d mousePos = new Point3d(); canvas.getCenterEyeInImagePlate(eyePos); canvas.getPixelLocationInImagePlate(new Point2d(Main.WIDTH/2, Main.HEIGHT/2), mousePos); //Main is the class for main window. Transform3D motion = new Transform3D(); canvas.getImagePlateToVworld(motion); motion.transform(eyePos); motion.transform(mousePos); Vector3d toScreenVector = new Vector3f(eyePos); toScreenVector.sub(mousePos); toScreenVector.normalize();
但这仍然无法正常工作。我认为必须有一种简单的方法来创建这样的向量。我们知道我的代码有什么问题或者更好的方法吗?
解决方案
回答
如果我做对了,我们想要一个垂直于屏幕平面但在世界坐标系中的向量吗?
在这种情况下,我们想从(World,> Screen)转换为((0,0,-1)或者((0,0,1))的Screen-> World的转换,具体取决于哪个轴屏幕指向下方。
由于ModelView矩阵只是一个旋转矩阵(忽略齐次变换部分),因此我们可以通过旋转部分的转置来简单地将其拉出,或者将其转置到下图的" Z"坐标列中的底行中进行简单的读取换位。
回答
是的,问题没错。抱歉,昨天我有点困惑。现在,我已经按照建议将问题中的两段代码混合在一起,从而更正了代码:
Vector3f toScreenVector = new Vector3f(0, 0, 1); Transform3D t3d = new Transform3D(); canvas.getImagePlateToVworld(t3d); t3d.transform(toScreenVector); tg.getTransform(t3d); //tg is Transform Group of all objects in a scene t3d.transform(toScreenVector);
谢谢你。