我的问题
有人可以链接一篇好文章/教程/任何东西,或者甚至解释如何正确地从鼠标坐标投射光线以选择 3D 对象吗?
我已经有了射线和相交的作品,现在我只需要通过鼠标点击来创建射线。
我只是想要一些我知道实际上应该起作用的东西,这就是为什么我在这里问专业人士,而不是我一开始就不确定它是否正确的东西。
现在状态
我有一个射线类,如果我将原点和方向设置为与相机相同,它实际上可以工作并检测相交,所以当我移动相机时,它实际上选择了正确的东西。
现在我想用鼠标实际进行 3D 拾取,而不是相机移动。
我已经阅读了很多关于此的其他问题,2 个教程,尤其是许多不同的数学内容,因为我真的不擅长它。
但这对我没有多大帮助,因为那里的人经常使用一些“unproject”功能,这些功能似乎实际上已被弃用,我不知道如何使用也无权访问。
现在我将光线原点设置为相机位置,然后尝试从本教程中的计算中获取光线的方向。
它有点作用,这意味着当相机指向对象时选择起作用,有时也沿着整个 y 轴,我不知道发生了什么。
如果有人想立即查看我的代码:
public Ray2(Camera cam, float mouseX, float mouseY) {
origin = cam.getEye();
float height = 600;
float width = 600;
float aspect = (float) width / (float) height;
float x = (2.0f * mouseX) / width - 1.0f;
float y = 1.0f - (2.0f * mouseX) / height;
float z = 1.0f;
Vector ray_nds = vecmath.vector(x, y, z);
Vector4f clip = new Vector4f(ray_nds.x(), ray_nds.y(), -1.0f, 1.0f);
Matrix proj = vecmath.perspectiveMatrix(60f, aspect, 0.1f, 100f);
proj = proj.invertRigid();
float tempX = proj.get(0, 0) * clip.x + proj.get(1, 0) * clip.y
+ proj.get(2, 0) * clip.z + proj.get(3, 0) * clip.w;
float tempY = proj.get(0, 1) * clip.x + proj.get(1, 1) * clip.y
+ proj.get(2, 1) * clip.z + proj.get(3, 1) * clip.w;
float tempZ = proj.get(0, 2) * clip.x + proj.get(1, 2) * clip.y
+ proj.get(2, 2) * clip.z + proj.get(3, 2) * clip.w;
float tempW = proj.get(0, 3) * clip.x + proj.get(1, 3) * clip.y
+ proj.get(2, 3) * clip.z + proj.get(3, 3) * clip.w;
Vector4f ray_eye = new Vector4f(tempX, tempY, tempZ, tempW);
ray_eye = new Vector4f(ray_eye.x, ray_eye.y, -1.0f, 0.0f);
Matrix view = cam.getTransformation();
view = view.invertRigid();
tempX = view.get(0, 0) * ray_eye.x + view.get(1, 0) * ray_eye.y
+ view.get(2, 0) * ray_eye.z + view.get(3, 0) * ray_eye.w;
tempY = view.get(0, 1) * ray_eye.x + view.get(1, 1) * ray_eye.y
+ view.get(2, 1) * ray_eye.z + view.get(3, 1) * ray_eye.w;
tempZ = view.get(0, 2) * ray_eye.x + view.get(1, 2) * ray_eye.y
+ view.get(2, 2) * ray_eye.z + view.get(3, 2) * ray_eye.w;
tempW = view.get(0, 3) * ray_eye.x + view.get(1, 3) * ray_eye.y
+ view.get(2, 3) * ray_eye.z + view.get(3, 3) * ray_eye.w;
Vector ray_wor = vecmath.vector(tempX, tempY, tempZ);
// don't forget to normalise the vector at some point
ray_wor = ray_wor.normalize();
direction = ray_wor;
}