PDA

View Full Version : Catmull-Rom Spline woes



swirly
10-06-2015, 06:55 AM
Hello,

After spending several days coming up dry on Google, I'm hoping someone here can help me solve figure out what's wrong with my Catmull-Rom Splines. The intention is the classical rollercoaster track.


vector<glm::vec3> cubePositions;
glm::vec3 currentpos = glm::vec3(-2.0f, 0.0f, -2.0f);
deque<glm::vec3> controls;

for (pointVectorIter ptsiter = g_Track.points().begin(); ptsiter != g_Track.points().end(); ptsiter++)
{
/* get the next point from the iterator */
glm::vec3 pt(*ptsiter);
controls.push_back(pt);

if (controls.size() == 4)
{
for (GLfloat i = 0.0f; i < 1.0f; i += 0.25f)
{
glm::vec3 mid = glm::vec3(
cmrSpline(controls[0].x, controls[1].x, controls[2].x, controls[3].x, i),
cmrSpline(controls[0].y, controls[1].y, controls[2].y, controls[3].y, i),
cmrSpline(controls[0].z, controls[1].z, controls[2].z, controls[3].z, i)

);
cubePositions.push_back(mid*2.0f);

}
controls.pop_front();

}
currentpos += pt;
// Mutliplying by two and translating (in initialization) just to move the boxes further apart.
cubePositions.push_back(currentpos*2.0f);

}

GLfloat cmrSpline(GLfloat p0, GLfloat p1, GLfloat p2, GLfloat p3, GLfloat u)
{
glm::vec4 ut = glm::vec4(1.0f, u, u*u, u*u*u);
glm::mat4x4 M = glm::mat4x4(0.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.0f, 0.5f, 0.0f, 1.0f, -2.5f, 2.0f, -0.5f, -0.5f, 1.5f, -1.5f, 0.5f);
glm::vec4 p = glm::vec4(p0, p1, p2, p3);
glm::vec4 output = ut*M*p;
return (output[0] + output[1] + output[2] + output[3]);
}


Thank you in advance

GClements
10-06-2015, 10:12 AM
Your matrix is transposed. The arguments to glm::mat4() are in column-major order (i.e. the first four values specify the left-hand column).

Transposing the matrix or reversing the order of the multiplications (i.e. p*M*ut) should produce the correct result (unless there's some other issue I've missed).

swirly
10-06-2015, 10:19 AM
That seems to have solved it. Thank you very much.