OpenGL拾取-光线/球体相交错误

OpenGL picking - Ray/sphere intersection wrong

本文关键字:错误 拾取 光线 OpenGL      更新时间:2023-10-16

我的光线拾取代码有问题。我的代码

我使用这个代码来选择计算:

/*-----------------------------------------------------------
Function:   GetViewportSystem
Returns:
    viewportCoordSystem
Get viewport coordinate system (only for reading)
Forward ray goes through origin
-------------------------------------------------------------*/
ViewportCoordSystem Camera::GetViewportSystem() const
{
    ViewportCoordSystem viewportCoord;
    viewportCoord.w = this->cameraPos;
    viewportCoord.w -= this->lookAt;
    viewportCoord.w.Normalize();
    viewportCoord.u = MyMath::Vector3::Cross(MyMath::Vector3::UnitY(), viewportCoord.w);
    viewportCoord.v = MyMath::Vector3::Cross(viewportCoord.w, viewportCoord.u);
    float d = (this->viewport.Height / 2.0f) * (1.0f / tanf(this->viewport.fov / 2.0f));
    viewportCoord.origin = this->cameraPos;
    viewportCoord.origin -= d * viewportCoord.w;
    return viewportCoord;
}
/*-----------------------------------------------------------
Function:   MapViewport2Dto3D
Parametrs:
    [in] viewportSystem - cameras viewport coordinate system
    [in] point - 2D point on image
Returns:
    3D mapped point in space
Map 2D image point to 3D space
Info about mapping 2D to 3D: http://meatfighter.com/juggler/
-------------------------------------------------------------*/
MyMath::Vector3 Camera::MapViewport2Dto3D(const ViewportCoordSystem & viewportSystem, const MyMath::Vector2 & point) const
{
    MyMath::Vector3 res = viewportSystem.origin;
    res += (point.X - this->viewport.Width * 0.5f) * viewportSystem.u;
    res += (this->viewport.Height * 0.5f - point.Y) * viewportSystem.v;
    return res;
}

自动分拣

ViewportCoordSystem vpSystem = this->camera->GetViewportSystem();
MyMath::Vector3 pos = this->camera->MapViewport2Dto3D(vpSystem, MyMath::Vector2(mouseX, mouseY));
this->ray.dir = pos - this->camera->GetPosition();
this->ray.dir.Normalize();
this->ray.origin = this->camera->GetPosition();

用这条射线,我计算了射线-球体相交测试。

bool BoundingSphere::RayIntersection(const MyMath::Ray & ray) const
{
    MyMath::Vector3 Q = this->sphereCenter - ray.origin;
    double c = Q.LengthSquared();
    double v = MyMath::Vector3::Dot(Q, ray.dir);
    double d = this->sphereRadius * this->sphereRadius - (c - v * v);
    if (d < 0.0) return false;
    return true;
}

问题是,我的代码工作不正确。如果我想象我的球体,并点击它们内部,我只得到了一半球体的正确答案。当我移动相机时,它会在球体外产生混乱和拾取反应。我的世界没有变换(所有的世界矩阵都是恒等式)。只有摄像机在移动。我正确地计算了OpenGL窗口中的鼠标位置(左上角有[0,0],转到[width,height])。

PS:我正在DirectX中成功地使用此代码进行光线投射/光线跟踪。我看不出它有什么问题。我的OpenGL渲染器使用的是左手系统(这对OpenGL来说不是自然的,但我希望它是那样的)

编辑:在可视化光线之后,当我向左/向右移动相机时,问题就出现了。射线中心与鼠标位置不一致。

好的。。发现问题。。。对于其他任何人来说,他们可能会被打扰

那两行不正确

viewportCoord.u = MyMath::Vector3::Cross(MyMath::Vector3::UnitY(), viewportCoord.w);
viewportCoord.v = MyMath::Vector3::Cross(viewportCoord.w, viewportCoord.u);

工作解决方案是

viewportCoord.u = MyMath::Vector3::Cross(viewportCoord.w, MyMath::Vector3::UnitY());
viewportCoord.u.Normalize();
viewportCoord.v = MyMath::Vector3::Cross(viewportCoord.u, viewportCoord.w);
viewportCoord.v.Normalize();