PDA

View Full Version : Catmull Rom Splines



stryker
02-26-2011, 03:30 AM
Hi guys, I'm trying to implement a series of functions to display a spline, but I really don't have any idea what I'm doing and am not sure if I'm even on the right track. It would be awesome if someone could take a look at these functions and see what I'm doing wrong or if I even understand the concepts behind this.

Note: splineCurve is a 2D array of floats with size[3][1000] that is supposed to hold the value of each coordinate of the spline. g_Splines is an array of spline control points.



float CatmullRom( float u, float x0, float x1, float x2, float x3 )
{
float u2 = u * u;
float u3 = u2 * u;
return ((2 * x1) +
(-x0 + x2) * u +
(2*x0 - 5*x1 + 4*x2 - x3) * u2 +
(-x0 + 3*x1 - 3*x2 + x3) * u3) * 0.5f;
}

void initSpline ()
{
for (int i = 1; i < g_iNumOfSplines; i++)
{

for (int u = 0; u < 1; u += 0.001)
{
splineCurve[0][u * 1000] = CatmullRom(u, g_Splines[i].points[i-1].x, g_Splines[i].points[i].x, g_Splines[i].points[i+1].x, g_Splines[i].points[i+2].x);
splineCurve[1][u * 1000] = CatmullRom(u, g_Splines[i].points[i-1].y, g_Splines[i].points[i].y, g_Splines[i].points[i+1].y, g_Splines[i].points[i+2].y);
splineCurve[2][u * 1000] = CatmullRom(u, g_Splines[i].points[i-1].z, g_Splines[i].points[i].z, g_Splines[i].points[i+1].z, g_Splines[i].points[i+2].z);
}
}
/*
for (int i = 0; i < g_iNumOfSplines; i++)
{
for (int k = 0; k < 4; k++)
{
glColor3f(0.5, 0.5, 0.5);
glVertex3f(g_Splines[i].points[k].x, g_Splines[i].points[k].y, g_Splines[i].points[0].z);
}
}
*/
}

void displaySpline()
{
glBegin(GL_LINES);
glColor3f(0, 0, 0);
for (int i = 0; i < 1000; i++)
{
glVertex3f(splineCurve[0][i], splineCurve[1][i], splineCurve[2][i]);
}
glEnd();
}

V-man
02-26-2011, 04:11 PM
for (int u = 0; u < 1; u += 0.001)

If u is an integer, than why are you adding 0.001 to u?

splineCurve[0][u * 1000]
For the above line, I don't recommend using floating point for accessing an array. Always use integers.