PDA

View Full Version : Problem with NURBS



TheBlackAdder
11-10-2005, 09:29 AM
I have a problem with my NURBS function. I am trying to make a spline between 7 controlpoints with an order of 4.
I made bezier and non/ uniform b-spline that works great.

But my NURBS functions makes strange approximations.
In my display function i call makeNURBS() that is supposed to make the spline...but something goes wrong.
I would appreciate a little help since im kinda stuck. Below i have first pasted the important function, and then below again the whole cpp file is listed.

Hope for a little input :) Thanx in advance.
Regards TheBlackAdder



void makeNURBS(void)
{
GLfloat NURBctrlpoints [7][4] = {
{-10.,0.,0.,1.},
{-10.,5.,0.,1.},
{-5.,5.,0.,1.},
{0.0,10.0,0.0,1.0},
{5.,5.,0.,1.},
{10.,5.,0.,1.},
{10.,0.,0.,1.}};

//GLfloat knots[]={0.0,0.0,0.0,0.0,1.0,2.0,3.0,4.0,4.0,4.0,4.0};
GLfloat knots[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,11,knots,3,(GLfloat*)NURBctr lpoints,4,GL_MAP1_VERTEX_4);
gluEndCurve(theNurb);
}

Here is the whole cpp file:




#include <GL/glut.h>
//#include <stdlib.h>
void makeBezier(void);
void makeUniformBSpline(void);
void makeNonUniformBSpline(void);
void makeNURBS(void);

GLfloat ctrlpoints [7][3] = {
{-10.,0.,0.},
{-10.,5.,0.},
{-5.,5.,0.},
{0.,10.,0.},
{5.,5.,0.},
{10.,5.,0.},
{10.,0.,0.}};


GLUnurbsObj * theNurb;
void init(void)
{
theNurb = gluNewNurbsRenderer();
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
gluNurbsProperty (theNurb, GLU_SAMPLING_METHOD, GLU_DOMAIN_DISTANCE);
gluNurbsProperty (theNurb, GLU_U_STEP, 100);
}

void display(void)
{
int i;

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (0., 5., 1., 0., 5., 0., 0., 1., 0.);

glPushMatrix();

/* The following code displays the control points as dots and line strip. */
glPointSize(5.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 7; i++)
glVertex3fv(&amp;ctrlpoints[i][0]);
glEnd();


glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i < 7; i++)
glVertex3fv(&amp;ctrlpoints[i][0]);
glEnd();

glColor3f(1.0,0.0,0.0);
makeBezier();
glColor3f(0.0,1.0,0.0);
makeUniformBSpline();
glColor3f(0.0,0.0,1.0);
makeNonUniformBSpline();
glColor3f(0.5,0.5,0.5);
makeNURBS();

glPopMatrix();
glFlush();
}

void makeBezier(void)
{
GLfloat knots[]={0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0};
GLfloat bezierCurve[]={
ctrlpoints[0][0],ctrlpoints[0][1],ctrlpoints[0][2],
ctrlpoints[1][0],ctrlpoints[1][1],ctrlpoints[1][2],
ctrlpoints[2][0],ctrlpoints[2][1],ctrlpoints[2][2],
ctrlpoints[3][0],ctrlpoints[3][1],ctrlpoints[3][2],

ctrlpoints[3][0],ctrlpoints[3][1],ctrlpoints[3][2],
ctrlpoints[4][0],ctrlpoints[4][1],ctrlpoints[4][2],
ctrlpoints[5][0],ctrlpoints[5][1],ctrlpoints[5][2],
ctrlpoints[6][0],ctrlpoints[6][1],ctrlpoints[6][2]
};

gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,8,knots,3,bezierCurve,4,GL_M AP1_VERTEX_3);
gluEndCurve(theNurb);

gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,8,knots,3,bezierCurve+4*3,4, GL_MAP1_VERTEX_3);
gluEndCurve(theNurb);
}

void makeUniformBSpline(void)
{
GLfloat knots[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,11,knots,3,(GLfloat*)ctrlpoi nts,4,GL_MAP1_VERTEX_3);
gluEndCurve(theNurb);
}
void makeNonUniformBSpline(void)
{
GLfloat knots[]={0.0,0.0,0.0,0.0,1.0,2.0,3.0,4.0,4.0,4.0,4.0};

gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,11,knots,3,(GLfloat*)ctrlpoi nts,4,GL_MAP1_VERTEX_3);
gluEndCurve(theNurb);
}

void makeNURBS(void)
{
GLfloat NURBctrlpoints [7][4] = {
{-10.,0.,0.,1.},
{-10.,5.,0.,1.},
{-5.,5.,0.,1.},
{0.0,10.0,0.0,1.0},
{5.,5.,0.,1.},
{10.,5.,0.,1.},
{10.,0.,0.,1.}};

//GLfloat knots[]={0.0,0.0,0.0,0.0,1.0,2.0,3.0,4.0,4.0,4.0,4.0};
GLfloat knots[]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,11,knots,3,(GLfloat*)NURBctr lpoints,4,GL_MAP1_VERTEX_4);
gluEndCurve(theNurb);
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (-12.0, 12.0, -12.0*(GLfloat)h/(GLfloat)w,
12.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D(-12.0*(GLfloat)w/(GLfloat)h,
12.0*(GLfloat)w/(GLfloat)h, -12.0, 12.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
// exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}