Catmull-Rom Spline woes


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);
        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)

        currentpos += pt;
        //  Mutliplying by two and translating (in initialization) just to move the boxes further apart.


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

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. pMut) should produce the correct result (unless there’s some other issue I’ve missed).

That seems to have solved it. Thank you very much.