1

レイ ピッキング コードに問題があります。私のコード

私は計算を選択するためにこのコードを使用しています:

/*-----------------------------------------------------------
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 レンダラーは Left-Handed システムを使用しています (OpenGL では自然ではありませんが、そのようにしたい)

編集: 光線を視覚化した後、カメラを左右に動かすと問題が発生します。光線の中心がマウスの位置と一致しません。

4

1 に答える 1

3

わかりました..問題を見つけました...興味があるかもしれない他の人のために

そのwo行は間違っています

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();
于 2013-07-24T18:05:55.220 に答える