PDA

View Full Version : Catmull Rom spline Curve

09-30-2004, 02:16 AM
Dear All,

I am trying to render the Catmull Rom Spline curve. I generated the points using the Catmull Rom curve equation. But when I render these points, instead of curve, I am getting a straight line.

#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>

struct point
{
float x;
float y;
};

struct point CatmullRoll(float t, struct point p1, struct point p2, struct point p3, struct point p4)
{

float t2 = t*t;
float t3 = t*t*t;
struct point v; // Interpolated point

/* Catmull Rom spline Calculation */

v.x = ((-t3 + 2*t2-t)*(p1.x) + (3*t3-5*t2+2)*(p2.x) + (-3*t3+4*t2+t)* (p3.x) + (t3-t2)*(p4.x))/2;
v.y = ((-t3 + 2*t2-t)*(p1.y) + (3*t3-5*t2+2)*(p2.y) + (-3*t3+4*t2+t)* (p3.y) + (t3-t2)*(p4.y))/2;
printf("Values of v.x = %f and v.y = %f\n", v.x,v.y);

return v;
}

void initScene()
{
glClearColor(0.0,0.0,0.0,0.0);
}

void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
if(w<=h)
glOrtho(-50.0,50.0,-50.0*(GLfloat)h/(GLfloat)w,50.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);
else
glOrtho(-50.0,5.0,-50.0*(GLfloat)w/(GLfloat)h,50.0*(GLfloat)w/(GLfloat)h,-50.0,50.0);
glMatrixMode(GL_MODELVIEW);

}
void display(void)
{
float t;
struct point v; //Interpolated point
struct point p1,p2,p3,p4;

p1.x = 5.25;p1.y = 5.25;
p2.x = 10.5;p2.y = 10.5;
p3.x = 40.33;p3.y = 40.33;
p4.x = 50.66;p4.y = 50.66;

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,1.0f,1.0f);
glPointSize(8);

glBegin(GL_POINTS);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p3.x,p3.y);
glVertex2f(p4.x,p4.y);
glEnd();

for(t=0;t<1;t+=0.02)
{
v = CatmullRoll(t,p1,p2,p3,p4);
glBegin(GL_POINTS);
glVertex2f(v.x,v.y);
glEnd();
}
glFlush();

}

void main(int argc, char *argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Catmull Roll");
initScene();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

ZbuffeR
09-30-2004, 02:44 AM
p1.x = 5.25;p1.y = 5.25;
p2.x = 10.5;p2.y = 10.5;
p3.x = 40.33;p3.y = 40.33;
p4.x = 50.66;p4.y = 50.66;
Ok, so these points are aligned.

v = CatmullRoll(t,p1,p2,p3,p4);
glBegin(GL_POINTS);
glVertex2f(v.x,v.y);
glEnd();
And now you interpolate between them... so you will still get a straight line, but with a much higher resolution though :p
Try with more random coords for your control points !

By the way, this is not really OpenGL, may be more at its place on the GL Math & Algo forum ...

09-30-2004, 03:49 AM
also, you might get better results with a bezier curve:

float bezierCurve(float t, float p0, float p1, float p2, float p3)
{
float t2 = t * t;
float t3 = t2 * t;
return p0
+ 3*(p1 - p0) * t
+ 3*(p2 - 2*p1 + p0) * t2
+ (p3 - 3*(p2 - p1) - p0) * t3;
}