% Create the object's surface
function MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);

cla;

% Lighting
kd = .7 ; % Diffuse
ka = .1 ; % Ambient
ks = .7 ; ns = 10 ; % Specular

% Set up edges vs shading
if edgeflag == 1
Edgemode = 'black' ;
Lmode = 'none' ;
else
Edgemode = 'none' ;
Lmode = 'phong' ;
end

% Set up surface variables
n = length(pX);
xMat = zeros(n,n);
yMat = zeros(n,n);
zMat = zeros(n,n);

% Create the surface by scaling
if styleflag == 1
xMat = pX' * sX;
yMat = pY' * sX;
zMat = ones(n,1) * sY;

% Create the surface by rotation
else
for i = 1:n
% Calculate the components of slope
if (i == 1)
dx = pX(2) - pX(1);
dy = pY(2) - pY(1);
elseif (i == n)
dx = pX(n) - pX(n-1);
dy = pY(n) - pY(n-1);
else
dx = pX(i+1) - pX(i-1);
dy = pY(i+1) - pY(i-1);
end

% Specify rotation angle theta
if (dx == 0)
if (dy > 0)
theta = pi/2;
else
theta = 3*pi/2;
end
else
theta = atan(dy/dx);
if (dx < 0)
theta = theta + pi;
end
end

% Create a single cross-section
xMat(i,:) = -sX * sin(theta) + pX(i);
yMat(i,:) = sX * cos(theta) + pY(i);
zMat(i,:) = sY;
end

% If the path is closed, overlap the edges to ensure their connection
if (pX(1) > pX(n)-0.01) & (pX(1) < pX(n)+0.01) & (pY(1) > pY(n)-0.01) & (pY(1) < pY(n)+0.01)
xMat = [xMat;xMat(1,:)];
yMat = [yMat;yMat(1,:)];
zMat = [zMat;zMat(1,:)];
end
end

% Plot the surface
hndl = surfl(xMat,yMat,zMat);

% Specify surface characteristics
set(hndl,'EdgeColor',Edgemode);
set(hndl,'FaceColor',[1,.6,.4]);
set(hndl,'FaceLighting',Lmode);
set(hndl,'DiffuseStrength',kd);
set(hndl,'AmbientStrength',ka);
set(hndl,'SpecularStrength',ks);
set(hndl,'SpecularExponent',ns);

% Specify view parameters
view(azm, alt) ;
light ('position', [1, 1 ,0] )

% Set axes
axis([-size size -size size -size size])