% eye = [...];
target = [0 0 0];
up = [0 0 1];

%
%   generate some dummy data
%

figure(1);
clf;
subplot(1, 2, 1);
scatter3(0,0,0,'x');
hold on;
n_pts = 100;
points = zeros(3, n_pts);
for i = 1:n_pts
    points(:, i) = [cos(i/n_pts*2*pi), .25 * rand(), sin(i/n_pts*2*pi)];
    scatter3(points(1, i), points(2, i), points(3, i));
end
xlabel('X');
ylabel('Y');
zlabel('Z');
daspect([1 1 1]);
xlim([-4, 4]);
ylim([-4, 4]);



%
%   animate the camera
%

n_camera_pos = 32;

filename1 = 'camera_anim.gif';
clear imind

for i = 1:n_camera_pos
    
    eye = [4 * sin((i-1)/n_camera_pos*2*pi), 3 * cos((i-1)/n_camera_pos*2*pi), 1];

    subplot(1, 2, 1);
    scatter3(eye(1), eye(2), eye(3), '+');
    hold on

    camera_matrix = lookAt(eye, target, up);
    camera_matrix = camera_matrix(:,1:3);

    subplot(1, 2, 2);
    cla
    
    for j = 1:n_pts
        p = camera_matrix' * [points(1, j) points(2, j) points(3, j) 1]';
                
        hold on
        scatter(p(1) / p(3), p(2) / p(3));
    end

    daspect([1 1 1]);
    xlim([-1 1])
    ylim([-1 1])
    drawnow

    frame = getframe(1);
    im = frame2im(frame);
    [im, cm] = rgb2ind(im, 256);
    if i == 1
        imwrite(im, cm, filename, 'gif', 'writemode', 'overwrite', 'LoopCount', inf, 'DelayTime', .1);
    else
        imwrite(im, cm, filename, 'gif', 'writemode','append', 'DelayTime', .1);
    end
    
    % pause(.1);
end