clear all;

% Create a figure to plot onto
fig = figure;

% Define the sphere we want to plot onto
[x y z] = sphere(24);
    % 24 divides the sphere into 15 degree sections

% Read in the image data, including its colourmap
[I,map] = imread('C:\Documents and Settings\ajones\My Documents\SVN WC\trunk\aliens_txkri\worldgen8_2.gif');

% Generate a surface plot of the sphere we've created, coloured with the
% image we've just read in (which forms a texture map); flipud is necessary
% to get the output world to be the right way up!
surface(x, y, z, ...
        'FaceColor',    'texture', ...
        'CDataMapping', 'direct',  ...
        'Cdata',        flipud(I)  ...
        );

% Equalise the three axes so it appears as a sphere
axis equal;

% Set the colour map to be that of the image, so it is actually coloured
% the same as the original image
colormap(map);

% Turn off the axes so it's just a sphere on a blank background
axis off;

% Set the axis limits to the range of the data.
axis tight;

% Freeze aspect ratio properties to enable rotation of 3-D objects and
% override stretch-to-fill
axis vis3d;

% Set the axis limits and PlotBoxAspectRatio so that the axes fill the
% position rectangle
axis fill;

% Set the figure position and size to be what we want
figposn = get(fig, 'Position');
set(fig, 'Position', [figposn(1) figposn(2) 440 440]);

% And the size of the axes within the figure
set(gca, 'Position', [0.05 0.05 0.9 0.9]);
    % This is relative to the size of the figure by default, so 0 is the
    % extreme left/bottom and 1 the extreme right/top...

% And set the background to be black
set(gcf, 'Color', 'black');

%=========================================================================
% Now make a movie of it!

% Turn on flash-free rendering for simple animations
set(fig,'DoubleBuffer','on');

% create a file to output to
themovie = avifile('C:\Documents and Settings\ajones\My Documents\SVN WC\trunk\aliens_txkri\orbit_around_world_s_lit.avi', ...
                   'fps',       30, ...
                   'quality',   50, ...
                   'videoname', 'Flying over an alien world. Copyright © A. R. Jones, 2008');

% Intialise the viewpoint and lighting
view(0,0);
lightangle(0,0);

% Then move the viewpoint around the northern then the southern hemispheres
% and back to the start point; the light illuminating the globe stays in
% the plane of the equator however.
for el = 0:0.5:45
    view(0,el);
    lightangle(0,0);
    MovieFrame = getframe(gcf);
    themovie = addframe(themovie, MovieFrame);
end
for az = 0:0.5:360
    view(az,45)
    lightangle(az,0);
    MovieFrame = getframe(gcf);
    themovie = addframe(themovie, MovieFrame);
end
for el = 45:-0.5:-45
    view(0,el);
    lightangle(0,0);
    MovieFrame = getframe(gcf);
    themovie = addframe(themovie, MovieFrame);
end
for az = 0:0.5:360
    view(az,-45)
    lightangle(az,0);
    MovieFrame = getframe(gcf);
    themovie = addframe(themovie, MovieFrame);
end
for el = -45:0.5:0
    view(0,el);
    lightangle(0,0);
    MovieFrame = getframe(gcf);
    themovie = addframe(themovie, MovieFrame);
end

% And we're done...
themovie = close(themovie);
