如何正确地旋转向量以匹配另一个向量?(OPENGL)

3
我已经为以下问题苦苦挣扎了一段时间:我有一个程序,允许用户绘制不同长度的贝塞尔曲线(第一个4个点,其余3个点,将曲线连接在一起)。我需要沿着曲线放置小矩形轨道,以便让用户骑上他们制作的轨道。我已经定义了顶点,并制作了一个方法来正确地将它们放置在线上,但旋转证明是棘手的。对于平滑曲线,我的当前实现效果良好,但是锐角会导致轨道不再对齐,所有通过那个角的轨道都具有相同的旋转,完全破坏了它。下面是所有相关代码:
用于填充曲线 std::vector 的代码:
p0 = glm::vec2(pointVertexData.at(0), pointVertexData.at(1));
p1 = glm::vec2(pointVertexData.at(3), pointVertexData.at(4));
p2 = glm::vec2(pointVertexData.at(6), pointVertexData.at(7));
p3 = glm::vec2(pointVertexData.at(9), pointVertexData.at(10));
curveVertexData = Subdivide(0.0f, 1.0f, 0.05f, curveVertexData);
for (int i = 0; i < timesToLoop; i++)
{
    p0 = p3;
    p1 = glm::vec2(pointVertexData.at(n), pointVertexData.at(n+1));
    p2 = glm::vec2(pointVertexData.at(n+3), pointVertexData.at(n+4));
    p3 = glm::vec2(pointVertexData.at(n+6), pointVertexData.at(n+7));
    std::vector<GLfloat> tempVec = Subdivide(0.0f, 1.0f, 0.05f, tempVec);
    curveVertexData.insert(curveVertexData.end(), tempVec.begin()+3, tempVec.end());
}

细分代码:

std::vector<GLfloat> Subdivide(GLfloat u0, GLfloat u1, GLfloat maxLineLength, std::vector<GLfloat> recurVertices)
{
    GLfloat umid = (u0 + u1) / 2.0;
    glm::vec2 x0 = Interpolate(p0, p1, p2, p3, u0, pFinal);
    glm::vec2 x1 = Interpolate(p0, p1, p2, p3, u1, pFinal);
    GLfloat length = sqrt(pow((x1.x - x0.x), 2) + pow((x1.y - x0.y), 2));
    if (length > maxLineLength)
    {
        std::vector<GLfloat> firstVertices = Subdivide(u0, umid, maxLineLength, firstVertices);
        std::vector<GLfloat> secondVertices = Subdivide(umid, u1, maxLineLength, secondVertices);
        secondVertices.insert(secondVertices.begin(), firstVertices.begin(), firstVertices.end()-3);
        recurVertices = secondVertices;
        return recurVertices;
    }
    else
    {
        recurVertices.push_back(x0.x);
        recurVertices.push_back(x0.y);
        recurVertices.push_back(0.1f);
        recurVertices.push_back(x1.x);
        recurVertices.push_back(x1.y);
        recurVertices.push_back(0.1f);
        numberOfVertices += 6;
        return recurVertices;
    }
}

设置包含轨迹顶点的std::vector的代码:

    std::vector<GLfloat> tempVertices;
    numberOfTrackVertices = 0;
    for (int i = 0; i < curveVertexData.size() - 2; i+=3)
    {
        std::cout << "Now calculating point # " << i << " : ";
        if(i != 0 && i < curveVertexData.size() - 5)
            shiftVertices(trainVertices, curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i + 3], curveVertexData[i + 4], curveVertexData[i + 5], curveVertexData[i - 3], curveVertexData[i - 2], curveVertexData[i - 1], &tempVertices);
        else if (i == 0)
            shiftVertices(trainVertices, curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i + 3], curveVertexData[i + 4], curveVertexData[i + 5], curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], &tempVertices);
        else
            shiftVertices(trainVertices, curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i - 3], curveVertexData[i - 2], curveVertexData[i - 1], &tempVertices);
    }

最后,我认为最有可能引起问题的代码是用于转动轨道的代码。 我目前的算法如下: (请注意,“currentOrientation”设置为顶点的前两个元素相减等于彼此的原因是因为它们表示矩形的后下角,当它们相互相减时,会得到一个表示盒子朝向的向量)

    void shiftVertices(GLfloat inVertices[], GLfloat x, GLfloat y, GLfloat z, GLfloat rx, GLfloat ry, GLfloat rz, GLfloat qx, GLfloat qy, GLfloat qz, std::vector<GLfloat> *container)
{
    glm::vec3 tempVectors[36];
    glm::vec3 moveVector = glm::vec3(x, y, z);
    glm::vec3 rotateVector = glm::normalize(glm::vec3(rx - qx, ry - qy, rz - qz));
    rotateVector = glm::normalize(glm::cross(rotateVector, UP));
    bool unFilled = true;
    int i = 0;
    int n = 0;
    while(unFilled)
    {
        tempVectors[n].x = inVertices[i];
        i++;
        tempVectors[n].y = inVertices[i];
        i++;
        tempVectors[n].z = inVertices[i];
        i++;
        n++;
        if (n >= 36)
            unFilled = false;
    }
    glm::vec3 currentOrientation = glm::normalize(tempVectors[0] - tempVectors[1]);
    GLfloat angleToRotate = glm::acos(glm::dot(currentOrientation, rotateVector));
    angleToRotate = (180.0f * angleToRotate) / PI;
    std::cout << angleToRotate << "\n";
    glm::mat4 rotationMatrix;
    rotationMatrix = glm::rotate(rotationMatrix, angleToRotate, UP);
    for (int u = 0; u < 36; u++)
    {
        tempVectors[u] = glm::vec3(rotationMatrix * glm::vec4(tempVectors[u], 1.0));
        tempVectors[u] = tempVectors[u] + moveVector;
    }
    i = 0;
    n = 0;
    unFilled = true;
    while (unFilled)
    {
        container->push_back(tempVectors[n].x);
        container->push_back(tempVectors[n].y);
        container->push_back(tempVectors[n].z);
        numberOfTrackVertices++;
        n++;
        if (n >= 36)
            unFilled = false;
    }
}

该实现的结果如下: http://imgur.com/a/8OI2E(除了最后一张图片。抱歉,无法嵌入图片)
我查阅了许多资源,但效果不佳。其中一个实现是Jur van den Berg在这个问题的回答:https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d 请注意,在图片中我将其称为“skew-symmetric method”。 我的算法实现如下,并且正如之前所述,未能正常运行: (请注意,此代码替换了上一个示例中的中间部分代码,循环前后保持不变)
        glm::vec3 crossVector = glm::cross(currentOrientation, rotateVector);
        GLfloat sineAngle = crossVector.length();
        GLfloat cosAngle = glm::dot(currentOrientation, rotateVector);
        glm::mat3 experimentalRMatrix;
        glm::mat3 skewSymmetric = { 0, (-1.0f * crossVector.z), crossVector.y,
            crossVector.z, 0, (-1.0f *crossVector.x),
            (-1.0f * crossVector.y), crossVector.x, 0 };
        glm::mat3 skewSecond = skewSymmetric * skewSymmetric;
        skewSecond = skewSecond * ((1.0f - cosAngle) / (sineAngle * sineAngle));
        experimentalRMatrix = glm::mat3() + skewSymmetric + skewSecond;
        testVector = experimentalRMatrix * currentOrientation;
        rotationMatrix = glm::mat4(experimentalRMatrix);

考虑到这一切,我希望能够分析为什么我的解决问题的尝试失败了,以及一个能够正确旋转顶点的解决方案。

非常感谢。


你需要的是沿着曲线的良好“框架”。经典解决方案是弗雷内框架。https://en.wikipedia.org/wiki/Frenet%E2%80%93Serret_formulas - starmole
1
你需要的是沿着曲线的一个好的“框架”。一个经典的解决方案是弗雷内框架。https://en.wikipedia.org/wiki/Frenet%E2%80%93Serret_formulas 但在实践中可能并不有用。如果你是交互式的,最好的方法就是使用https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process,并选择一个屏幕空间向上的向量。 在实践中,取出曲线上的切线。将其转换为视图空间(tv)。然后选择屏幕空间向上为nv =(0,1,0)。计算bv = tv x nv。(x是叉积)。然后nv2 = bv x tv。你的框架就是(tv,bv,nv2)。 - starmole
2
切向空间是这里的正确选择,但两条曲线相交的地方应该是不连续的(输入曲线2)。你可以通过(当用户连接两条曲线时)从你要合并的曲线中注入控制点来缓解这种情况(对于两个曲线都要注入!)。此外,你正在询问如何“正确旋转向量”,事实上的答案应该是使用四元数。另外,如果你切换到四元数,你将免费获得slerp。而且,glm使它们变得容易 :) - svenevs
@sjm324 非常感谢!我已经转而使用四元数,效果非常好!即使在锐角处,它也会自动纠正。我非常感谢您和starmole提供的解决方案,并将确保从现在开始使用四元数。 - Disva Dravir
耶!我不确定那是否是你的问题,但我很高兴它起作用了,四元数是纯粹的魔法。我提供了一个“答案”,这样在SO方面就可以关闭了。 - svenevs
1个回答

0

请看注释...切空间和四元数似乎解决了问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接