我一直在努力解决以下问题:我有一个程序允许用户绘制各种长度的Bezier曲线(第一个为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 请注意,我将其称为图像中的“偏斜对称方法”。 我对算法的实现如下,并且如前所述,它不能正常工作: (请注意,此代码替换了上一个示例中的代码的中间部分,并且在保持相同之前和之后使用了循环)
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);
有了这一切,我希望分析为什么我的解决问题的尝试失败了,和/或一个能正确旋转顶点的解决方案。
谢天谢地。
答案 0 :(得分:0)
看评论......切线空格和四元数似乎已经完成了这个技巧