矢量和面之间的 OpenGL 交集

OpenGL intersection between vector and face

我有一个地形和一个应该在地形上移动的物体,所以我做了一个函数,可以检测哪个面是上面物体的原点,所以我应该将物体的 Y 设置为准确的高度来自垂直于面部的对象原点的向量之间的交点,我有面部的三个顶点,因此我可以计算它的法线和原点以及最大和最小点(边界框)。

enter image description here

编辑: 另一种形式的问题: 如果我知道 3D 中三角形上的一个点的 x 和 z space 如果它是三角形中的一个点,我怎么知道它?

the Möller–Trumbore intersection algorithm

这是实现:

bool EngineItem::checkIntersection(glm::vec3& rayOrigin, glm::vec3& rayVector, Face& face, glm::vec3& point) {
    const float EPSILON = 0.0000001;
    glm::vec3 vertex0 = face.v1;
    glm::vec3 vertex1 = face.v2;
    glm::vec3 vertex2 = face.v3;
    glm::vec3 edge1, edge2, h, s, q;
    float a, f, u, v;
    edge1 = vertex1 - vertex0;
    edge2 = vertex2 - vertex0;
    h = glm::cross(rayVector, edge2);
    a = glm::dot(edge1, h);
    if (a > -EPSILON && a < EPSILON)
        return false;
    f = 1 / a;
    s = rayOrigin - vertex0;
    u = f * glm::dot(s, h);
    if (u < 0.0 || u > 1.0)
        return false;
    q = glm::cross(s, edge1);
    v = f * glm::dot(rayVector, q);
    if (v < 0.0 || u + v > 1.0)
        return false;
    float t = f * glm::dot(edge2, q);
    if (t > EPSILON) {
        glm::vec3 plus = glm::normalize(rayVector) * (t * glm::length(rayVector));
        point = rayOrigin + plus;
        return true;
    } else
        return false;
}