Java 三维空间中鼠标位置的计算——OpenGL

Java 三维空间中鼠标位置的计算——OpenGL,java,opengl,lwjgl,Java,Opengl,Lwjgl,我正在尝试创建一条光线,将鼠标坐标转换为3d世界坐标 Cx = Mx / screenWidth * 2 - 1 Cy = -( My / screenHeight * 2 - 1 ) vNear = InverseViewProjectionMatrix * ( Cx, Cy, -1, 1 ) VFar = InverseViewProjectionMatrix * ( Cx, Cy, 1, 1 ) vNear /= vNear.w vFar /= vFar.w 测试后,射线的vFar似

我正在尝试创建一条光线,将鼠标坐标转换为3d世界坐标

Cx = Mx / screenWidth * 2 - 1
Cy = -( My / screenHeight * 2 - 1 )

vNear = InverseViewProjectionMatrix * ( Cx, Cy, -1, 1 )
VFar = InverseViewProjectionMatrix * ( Cx, Cy, 1, 1 )

vNear /= vNear.w
vFar /= vFar.w
测试后,射线的vFar似乎总是来自同一个大致方向

似乎我需要添加摄影机透视图,因为我希望vFar始终位于摄影机后面

我不完全确定应该如何添加。这是我的测试代码

public void mouseToWorldCordinates(Window window,Camera camera, Vector2d mousePosition){
    float normalised_x = (float)((mousePosition.x / (window.getWidth()*2)) -1);
    float normalised_y = -(float)((mousePosition.y / (window.getHeight()*2)) -1);

    Vector4f mouse = new Vector4f(normalised_x,normalised_y,-1,1);

    Matrix4f projectionMatrix = new Matrix4f(transformation.getProjectionMatrix()).invert();
    Matrix4f mouse4f = new Matrix4f(mouse,new Vector4f(),new Vector4f(),new Vector4f());
    Matrix4f vNear4f = projectionMatrix.mul(mouse4f);
    Vector4f vNear = new Vector4f();
    vNear4f.getColumn(0,vNear);

    mouse.z = 1f;

    projectionMatrix = new Matrix4f(transformation.getProjectionMatrix()).invert();
    mouse4f = new Matrix4f(mouse,new Vector4f(),new Vector4f(),new Vector4f());
    Matrix4f vFar4f = projectionMatrix.mul(mouse4f);
    Vector4f vFar = new Vector4f();
    vFar4f.getColumn(0,vFar);

    vNear.div(vNear.w);
    vFar.div(vFar.w);

    lines[0] = vNear.x;
    lines[1] = vNear.y;
    lines[2] = vNear.z;

    lines[3] = vFar.x;
    lines[4] = vFar.y;
    lines[5] = vFar.z;

}

normalized_x
normalized_y
的计算是错误的。标准化设备坐标在[-1.0,1.0]范围内:

float normalized_x=2.0f*(float)mousePosition.x/(float)window.getWidth()-1.0f;
float normalized_y=1.0f-2.0f*(float)mousePosition.y/(float)window.getHeight();

我想你想做的就是所谓的?@Neil“挑选”这个词在这里并不常见。请看,还有这些:这就是问题所在!非常感谢你。