簡體   English   中英

如何將屏幕上的鼠標坐標轉換為3D坐標

[英]How to convert mouse coordinate on screen to 3D coordinate

我正在使用C ++中的GLUT創建3D應用程序。

現在,我想實現一個與此類似的方法:

Vector3* MyClass::get3DObjectfromMouse(int mouseX, int mouseY);

如何實現此方法?

正如Andon M. Coleman所評論的那樣,實現此目的的一種方法是通過使用未投影的屏幕坐標進行射線/對象相交測試。 這項技術通常稱為采摘

用於選擇的偽C ++代碼:

假設我們有一個3D對象類型/類:

class Object3D { ... };

3D拾取功能將返回由一條線相交的所有對象的列表,該線從近平面中的給定2D點到遠平面中的同一點。

struct LineSegment 
{
    Vector3 start;
    Vector3 end;
};

Object3D[] Pick(float x, float y)
{
    LineSegment lineSeg;
    Object3D[] intersectedObjs;

    // Do both un-projections for z-near (0) and z-far (1).
    // This produces a line segment going from z-near to far.
    UnProject(x, y, /* z = */ 0.0, modelViewMatrix, projectionMatrix, viewport, lineSeg.start);
    UnProject(x, y, /* z = */ 1.0, modelViewMatrix, projectionMatrix, viewport, lineSeg.end);

    // Iterate all object in the scene or in the current view:
    for (Object3D obj : scene)
    {
        if (TestLineIntersection(obj, lineSeg))
        {
            // This object is crossed by the picking line.
            intersectedObjs.Add(obj);
        }
    }

    // Optionally you might want sort them from distance 
    // to the camera/viewer before returning the intersections.
    return intersectedObjs;
}

UnProject()函數將如下所示:

bool UnProject(float winX, float winY, float winZ,
               const Matrix4 & modelView, const Matrix4 & projection,
               const ScreenRect viewport, Vector3 & worldCoordinates)
{
    // Compute (projection x modelView) ^ -1:
    const Matrix4 m = inverse(projection * modelView);

    // Need to invert Y since screen Y-origin point down,
    // while 3D Y-origin points up (this is an OpenGL only requirement):
    winY = viewport.Height() - winY;

    // Transformation of normalized coordinates between -1 and 1:
    Vector4 in;
    in[0] = (winX - viewport.X()) / viewport.Width()  * 2.0 - 1.0;
    in[1] = (winY - viewport.Y()) / viewport.Height() * 2.0 - 1.0;
    in[2] = 2.0 * winZ - 1.0;
    in[3] = 1.0;

    // To world coordinates:
    Vector4 out(m * in);
    if (out[3] == 0.0) // Avoid a division by zero
    {
        worldCoordinates = Vector3Zero;
        return false;
    }

    out[3] = 1.0 / out[3];
    worldCoordinates[0] = out[0] * out[3];
    worldCoordinates[1] = out[1] * out[3];
    worldCoordinates[2] = out[2] * out[3];
    return true;
}

為了明確TestLineIntersection()TestLineIntersection()了線對AABB交點測試。 邊界框應轉換為世界空間,因為它通常表示為局部模型空間中的一組點。

bool TestLineIntersection(const Object3D & obj, const LineSegment & lineSeg)
{
    AABB aabb = obj.GetAABB();
    aabb.TransformBy(obj.modelMatrix);
    return aabb.LineIntersection(lineSeg.start, lineSeg.end);
}

// AABB.cpp:
bool AABB::LineIntersection(const Vector3 & start, const Vector3 & end) const
{
    const Vector3 center     = (mins + maxs) * 0.5;
    const Vector3 extents    = maxs - center;
    const Vector3 lineDir    = 0.5 * (end - start);
    const Vector3 lineCenter = start + lineDir;
    const Vector3 dir        = lineCenter - center;

    const float ld0 = Mathf::Abs(lineDir[0]);
    if (Mathf::Abs(dir[0]) > (extents[0] + ld0))
    {
        return false;
    }

    const float ld1 = Mathf::Abs(lineDir[1]);
    if (Mathf::Abs(dir[1]) > (extents[1] + ld1))
    {
        return false;
    }

    const float ld2 = Mathf::Abs(lineDir[2]);
    if (Mathf::Abs(dir[2]) > (extents[2] + ld2))
    {
        return false;
    }

    const Vector3 vCross = cross(lineDir, dir);
    if (Mathf::Abs(vCross[0]) > (extents[1] * ld2 + extents[2] * ld1))
    {
        return false;
    }
    if (Mathf::Abs(vCross[1]) > (extents[0] * ld2 + extents[2] * ld0))
    {
        return false;
    }
    if (Mathf::Abs(vCross[2]) > (extents[0] * ld1 + extents[1] * ld0))
    {
        return false;
    }

    return true;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM