我已经为以下问题苦苦挣扎了一段时间:我有一个程序,允许用户绘制不同长度的贝塞尔曲线(第一个4个点,其余3个点,将曲线连接在一起)。我需要沿着曲线放置小矩形轨道,以便让用户骑上他们制作的轨道。我已经定义了顶点,并制作了一个方法来正确地将它们放置在线上,但旋转证明是棘手的。对于平滑曲线,我的当前实现效果良好,但是锐角会导致轨道不再对齐,所有通过那个角的轨道都具有相同的旋转,完全破坏了它。下面是所有相关代码:
用于填充曲线 std::vector 的代码:
该实现的结果如下: 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”。 我的算法实现如下,并且正如之前所述,未能正常运行: (请注意,此代码替换了上一个示例中的中间部分代码,循环前后保持不变)
用于填充曲线 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);
考虑到这一切,我希望能够分析为什么我的解决问题的尝试失败了,以及一个能够正确旋转顶点的解决方案。
非常感谢。
glm
使它们变得容易 :) - svenevs