function out=plotNUBSsurface(p,q,U,V,CP,num)

% PLOTNUBSSurface generates a polygonal surface that approximates a
% nonuniform b-spline surface of degree (p,q) given by the control
% net CP and knot sequences U and V. The polygonal surface will have
% (num - 1)^2 quadrilaterals.

if (nargin ~= 6)
  error('Incorrect number of arguments');
end

n1 = size(CP,2);
n2 = size(CP,3);

X = zeros(num,num);
Y = zeros(num,num);
Z = zeros(num,num);

for j = 1 : num
  s = (j - 1)/(num - 1);
  for i = 1 : num
    t = (i - 1)/(num - 1);
    pt = pointonsurface(p,q,U,V,CP,t,s);
    X(i,j) = pt(1);
    Y(i,j) = pt(2);
    Z(i,j) = pt(3);
  end
end

surf(X,Y,Z)
shading interp

out = [X, Y, Z];

return