function [bw, im] = projection2d(filename, direction, show)
% FUNCTION [bw im] = projection2d(filename, rotdirection, <show>) 
% computes the parallel-projective view of 3ds-model (filename)
% input: 3ds-file
%        rotdirection: angles of rotation around x and y axis
%                      a rotation around these angles is computed
%                      before the projection
%        optional: show
% output:
%        bw: foreground / background matrix. foreground = 1, bg = 0
%        im: image of projection


% read 3d model
tic
[vcell tcell] = read3ds(filename,1,0);

t = tcell{1};
v = vcell{1};


% 3d rotation of vertices
% rotate around x and y axis
a = direction(1);
b = direction(2);

RX = [1 0 0;0 cos(a) sin(a); 0 -sin(a) cos(a)];
RY = [cos(b) 0 sin(b);0 1 0;-sin(b) 0  cos(b)];
v = v * RX * RY;


% plot all triangles projected to the XY - plane
% ( = omitting the Z - coordinate)

for i = 1:length(t(:,1))
    X = v(t(i,1:3),1);
    Y = v(t(i,1:3),2);
    if (i==2)
        hold on
        set(gcf,'renderer','painters');
    end
    fill(X,Y,'k');
end
hold off

% get the image !

axis equal
axis off
im = frame2im(getframe);
bw = im2bw(im);
bw=im2double(bw);
bw = -bw + 1;   %invert

tm = toc;
fprintf('time: %f\n',tm);

% don't close figure if 'show' is given
if exist('show')==0
    close
end