function eye=DrawEyes(eyes)
% r = radius of eyeballs

ebr = eyes.radius;
if isfield(eyes,'nsurface')
    [x z y] = sphere(eyes.nsurface);
else
    [x z y] = sphere(50);
end
x = x.*ebr; y=y.*ebr; z = z.*ebr;
for thiseye=1:length(eyes.eyepos)
    [f,v,c]=surf2patch(x + eyes.eyepos(thiseye).nodal(1),y + eyes.eyepos(thiseye).nodal(2),z + eyes.eyepos(thiseye).nodal(3),z);
    eye(thiseye) = patch('faces',f,'vertices',v,'facecol',eyes.eyeballcolor,'edgecol','none','facealpha',eyes.eyeballtransparency,'edgealpha',eyes.eyeballtransparency);
    % If asked, draw in an iris and pupil
    if isfield(eyes,'iriscolor')
        X = get(eye(thiseye),'Xdata')-eyes.eyepos(thiseye).nodal(1);
        Y = get(eye(thiseye),'Ydata')-eyes.eyepos(thiseye).nodal(2);
        Z = get(eye(thiseye),'Zdata')-eyes.eyepos(thiseye).nodal(3);
        [n,m]=size(Z);
        c = repmat(eyes.eyeballcolor,m,1);
        Rot = inv(RotationMatrix(eyes.eyepos(thiseye)));
        R = Rot * [X(1,:);Y(1,:);Z(1,:)];
        Z = R(3,:);
        j = find(Z>eyes.irisradius);
        c(j,:) = repmat(eyes.iriscolor,length(j),1);
        j = find(Z>eyes.pupilradius);
        c(j,:) = repmat(eyes.pupilcolor,length(j),1);
        set(eye(thiseye),'facecolor','flat','edgecol','none','facevertexcdata',c);
    end
end

