# Circle and sphere by NURBS functions of OpenGL

It is known that circles and spheres can be represented exactly by NURBS curve and surface respectively. I confirmed that this works correctly using the algorithm of NURBS. But, I could not obtain the correct results using the NURBS functions of OpenGL. I cannot understand why. Attached below is the image comparing the correct result and wrong result created by the OpenGL NURBS function.

The following is the OpenGL code.

``````void _OpenGLDrawCircleByNURBS(void)
{
GLfloat pXyz[9][4]={ 1.0, 0.0, 0.0,  1.0,
1.0, 1.0, 0.0,  0.707107,
0.0, 1.0, 0.0,  1.0,
-1.0, 1.0, 0.0,  0.707107,
-1.0, 0.0, 0.0,  1.0,
-1.0,-1.0, 0.0,  0.707107,
0.0,-1.0, 0.0,  1.0,
1.0,-1.0, 0.0,  0.707107,
1.0, 0.0, 0.0,  1.0};
GLfloat uKnotVect[]={0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0};
GLUnurbsObj *theNurb;

theNurb=gluNewNurbsRenderer();
gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_OUTLINE_PATCH);
gluBeginCurve(theNurb);
gluNurbsCurve(theNurb,
12,uKnotVect,
4,
&pXyz[0][0],
3,
GL_MAP1_VERTEX_4);
gluEndCurve(theNurb);
}
``````

Multiply X/Y/Z by W. E.g.

``````for (int i=0; i<9; i++)
for (int j=0; j<3; j++)
pXyz[i][j] *= pXyz[i][3];
``````

The book is treating w as a weight, i.e. <x,y,z,w> is interpreted as w*<x,y,z,1> = <w*x,w*y,w*z,w>.

A million thanks.
It works beautifully also in rendering sphere, cylinder, torus, etc. using gluNurbsSurface( ).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.