如何使用近处和远处位置识别3D对象内部或外部3D对象的单击

Ram*_*sad 5 android opengl-es glsurfaceview opengl-es-2.0 raycasting

我正在使用带有Android,Java代码的OpenGLES 2.0进行3D对象渲染.如何使用以下代码在近处和远处位置识别3D对象内部或外部3D对象?

public static PointF screenToWorld(float[] viewMatrix,
                                       float[] projMatrix, float screenX, float screenY) {
        float[] nearPos = unProject(viewMatrix, projMatrix, screenX, screenY, 0);
        float[] farPos = unProject(viewMatrix, projMatrix, screenX, screenY, 1);

        Log.d(LOGTAG,"nearPos ->"+nearPos.length+" "+nearPos);
        Log.d(LOGTAG,"farPos ->"+farPos.length+" "+farPos);

        // The click occurred in somewhere on the line between the two points
        // nearPos and farPos. We want to find
        // where that line intersects the plane at z=0
        float distance = nearPos[2] / (nearPos[2] - farPos[2]); // Distance between nearPos and z=0
        float x = nearPos[0] + (farPos[0] - nearPos[0]) * distance;
        float y = nearPos[1] + (farPos[1] - nearPos[0]) * distance;
        return new PointF(x, y);
    }

    private static float[] unProject(float[] viewMatrix,
                                     float[] projMatrix, float screenX, float screenY, float depth) {
        float[] position = {0, 0, 0, 0};
        int[] viewPort = {0, 0, 1, 1};
        GLU.gluUnProject(screenX, screenY, depth, viewMatrix, 0, projMatrix, 0,
                viewPort, 0, position, 0);
        position[0] /= position[3];
        position[1] /= position[3];
        position[2] /= position[3];
        position[3] = 1;
        return position;

    }
Run Code Online (Sandbox Code Playgroud)

Rab*_*d76 5

如何识别 3D 对象内部或 3D 对象外部的点击?

您必须验证是否命中了对象的任何基元。

近平面上的点和远平面上的点定义了一条穿过世界的射线:

float[] nearPos = unProject(viewMatrix, projMatrix, screenX, screenY, 0);
float[] farPos  = unProject(viewMatrix, projMatrix, screenX, screenY, 1);
Run Code Online (Sandbox Code Playgroud)

伪代码:

R0 = nearPos
D  = normalize(farPos - nearPos)
Run Code Online (Sandbox Code Playgroud)


找到一条射线与一个基元的交点

为了找到被射线击中的表面,必须计算每个表面(基元)与射线的交点和射线起点的距离。具有最低距离(在光线方向)的表面被击中。

要找到一条射线与三角形基元的交点的距离,必须完成以下步骤:

  1. 找到射线与由三角形基元的 3 个点定义的平面的交点。
  2. 计算交点与射线起点之间的距离。
  3. 测试交点是否在光线的方向(而不是相反的方向)
  4. 测试交点是否在三角形轮廓内或上。

求交点和交点距离:

在此处输入图片说明

平面由范数向量 ( NV)和平面上的点 ( P0) 定义。如果三角形由 3 个点PA,PB和 给出 PC,则平面可以计算如下:

P0 = PA
NV = normalize( cross( PB-PA, PC-PA ) )
Run Code Online (Sandbox Code Playgroud)

射线与平面的交点是通过将射线
P_isect = dist * D + R0方程代入平面方程来计算的dot( P_isect - P0, NV ) == 0
它跟随:

dist_isect = dot( P0 - R0, NV ) / dot( D, NV ) 
P_isect    = R0 + D * dist_isect
Run Code Online (Sandbox Code Playgroud)

测试交点是否在光线方向:

如果`dist_isect 大于或等于0.0,则交点在光线的方向上。

测试交点是否在三角形轮廓内或上

要找出一个点是否在三角形内,必须测试从角点到交点的线是否在连接到角点的两条腿之间:

bool PointInOrOn( P1, P2, A, B )
{
    CP1 = cross( B - A, P1 - A )
    CP2 = cross( B - A, P2 - A )
    return dot( CP1, CP2 ) >= 0
}

bool PointInOrOnTriangle( P, A, B, C )
{
    return PointInOrOn( P, A, B, C ) &&
           PointInOrOn( P, B, C, A ) &&
           PointInOrOn( P, C, A, B )
} 
Run Code Online (Sandbox Code Playgroud)


另请参阅:是否可以在 OpenGL 中单击立方体的哪个表面?