Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Catmull Rom spline Curve

  1. #1
    Guest

    Catmull Rom spline Curve

    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.

    Code :
     #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);
    	glShadeModel(GL_FLAT);
    }
     
     
    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);
    	glLoadIdentity();
     
    }
    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();
    }

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Catmull Rom spline Curve

    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
    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 ...

  3. #3
    Guest

    Re: Catmull Rom spline Curve

    also, you might get better results with a bezier curve:

    Code :
    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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •