How to render BSpline surface using gluNurbs?

Hi all,
I am writing about displaying Nurbs surface from TopAbs_FACE using gluNurbs.
I searched about it from google. and I got snipped code.
But I couldn't display nurbs surface on my program from snipped code.
Please let me know what am I wrong.

	Handle(Geom_Surface) aGeomSurface = BRep_Tool::Surface(aFace);
	Handle(Geom_BSplineSurface) aBSplineSurface;
	Standard_Boolean ok = false;
	if(aGeomSurface->DynamicType() != STANDARD_TYPE(Geom_BSplineSurface)) {
		// try to convert into nurbs
		BRepBuilderAPI_NurbsConvert nbscv;
		nbscv.Perform(aFace);
		if (nbscv.IsDone()){
			aGeomSurface = BRep_Tool::Surface(TopoDS::Face(nbscv.Shape()));
			ok = true;
		}
	} else {
		ok = true;
	}

	aBSplineSurface = Handle(Geom_BSplineSurface)::DownCast(aGeomSurface);

	if (ok) {
		int myNbUPoles = aBSplineSurface->NbUPoles();
		int myNbVPoles = aBSplineSurface->NbVPoles();

		TColgp_Array2OfPnt aPoles(1, myNbVPoles, 1, myNbUPoles);

		TColStd_Array1OfReal aVKnots(1, aBSplineSurface->NbVKnots());
		TColStd_Array1OfReal aUKnots(1, aBSplineSurface->NbUKnots());

		aBSplineSurface->Poles(aPoles);
		aBSplineSurface->UKnots(aUKnots);
		aBSplineSurface->VKnots(aVKnots);

		// Push the nurbs in OpenGL
		// --------------------------------------------------------
		// Control Point
		Standard_Integer i, j, aCounter;
		int aLength = myNbVPoles * myNbUPoles; //Create array of control points and their weights

		//SbVec4f* mySurfaceCtrlPoints = new SbVec4f[aLength];
		glm::vec4* mySurfaceCtrlPoints = new glm::vec4[aLength];		
		if(mySurfaceCtrlPoints == NULL) return false;

		aCounter = -1;

		for(j = 1; j <= myNbVPoles; j++) {
			for(i = 1; i <= myNbUPoles; i++) {
				const gp_Pnt& aPoint = aBSplineSurface->Pole(i, j); //Control point (U,V)
				float x = (float)(aPoint.X());
				float y = (float)(aPoint.Y());
				float z = (float)(aPoint.Z());
				float w = (float)(aBSplineSurface->Weight(i, j));

				mySurfaceCtrlPoints[++aCounter] = glm::vec4(x, y, z, w);
			}
		}

		TColStd_ListOfReal aListOfKnot;
		TColStd_ListIteratorOfListOfReal aListOfKnotIterator;

		try {
			//Fill the knot`s array taking into account multiplicities

			// -----------------------------------
			// VKnots
			for (i=aVKnots.Lower(); i <= aVKnots.Upper(); i++) {
				for (j = 1; j <= aBSplineSurface->VMultiplicity(i); j++) {
					aListOfKnot.Append(aVKnots(i));
				}
			}

			mySurfaceVKnots = new float[aListOfKnot.Extent()];
			if(mySurfaceVKnots == NULL) return false;

			aCounter = -1;
			for(aListOfKnotIterator.Initialize(aListOfKnot); 
					aListOfKnotIterator.More(); aListOfKnotIterator.Next()) {
				mySurfaceVKnots[++aCounter] = (float)(aListOfKnotIterator.Value());
			}
			myNbVKnots = aCounter+1;

			// -----------------------------------
			// UKnots
			aListOfKnot.Clear();
			for (i=aUKnots.Lower(); i <= aUKnots.Upper(); i++) {
				for (j = 1; j <= aBSplineSurface->UMultiplicity(i); j++) {
					aListOfKnot.Append(aUKnots(i));
				}
			}
			mySurfaceUKnots = new float[aListOfKnot.Extent()];
			if(mySurfaceUKnots == NULL) return false;

			aCounter = -1;
			for(aListOfKnotIterator.Initialize(aListOfKnot); 
					aListOfKnotIterator.More(); aListOfKnotIterator.Next()) {
				mySurfaceUKnots[++aCounter] = (float)(aListOfKnotIterator.Value());
			}
			myNbUKnots = aCounter+1;
		}
		catch(Standard_Failure) {
			continue;
		}

		// --------------------------------------------
		int dim = 4;		
		GLfloat * ptr = (GLfloat *)glm::value_ptr(mySurfaceCtrlPoints[0]);

		int ustride = dim;
		int vstride = dim * myNbUPoles;
		int numuknot = myNbUKnots;
		float *uknotvec = (float*) &mySurfaceUKnots[0];
		int numvknot = myNbVKnots;
		float *vknotvec = (float*) &mySurfaceVKnots[0];
		int numuctrlpts = myNbUPoles;
		int numvctrlpts = myNbVPoles;

		gluBeginSurface(nurbsrenderer);
		gluNurbsSurface(nurbsrenderer,
				numuknot, (GLfloat*) uknotvec,
				numvknot, (GLfloat*) vknotvec,
				ustride, vstride, ptr,
				numuknot - numuctrlpts, numvknot - numvctrlpts,
				(dim == 3) ? GL_MAP2_VERTEX_3 : GL_MAP2_VERTEX_4);
		gluEndSurface(nurbsrenderer);

		delete [] mySurfaceVKnots;
		delete [] mySurfaceUKnots;
		delete [] mySurfaceCtrlPoints;
	}
}

Kirill Gavrilov's picture

GLU library has been written against legacy OpenGL functionality and has been deprecated more than a decade ago. I'm not sure that it worth trying to solve your problem using this very old approach.

In-Hak Min's picture

Hi Kirill,

I know. I Just want to verification of displaying nurbs surface with my OpenGL program. And I will conversion to shader later.