OpenGL挑选 - 射线/球体交叉错误

时间:2013-07-24 16:13:30

标签: c++ opengl

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

我正在使用此代码来挑选卡路里:

/*-----------------------------------------------------------
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]并转到[宽度,高度])。

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

修改 在对摄像机进行可视化之后,当我向右移动摄像机时,问题就出现了问题。射线中心与小鼠位置不相符。

1 个答案:

答案 0 :(得分:3)

好的..发现问题......对于其他任何可能被强迫的人

那些窝线不正确

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();