%-------------------------------------------------------------------------------
% use camera control functions to take complete control of what is drawn
%-------------------------------------------------------------------------------

% set the plotting parameters and figure properties
feature('UseGenericOpenGL', 1);
set( ...
  gcf, 'Renderer', 'OpenGL', 'Units', 'pixels', 'Color', [.98 .98 .98] ...
);
% calulate the size of the current axes so they fill the whole figure
figrect = get(gcf,'Position');
axisrect = [[1 1] figrect(3:4)];

% set the units and appearance of the current axes
set(gca, ...
  'Units', 'pixels', 'Position', axisrect, ...
  'DataAspectRatio', [1 1 1], 'DataAspectRatioMode', 'manual', ...
  'PlotBoxAspectRatio', [4 3 2], 'PlotBoxAspectRatioMode', 'manual', ...
  'Box', 'on', 'TickLength', [0 0], ...
  ...
  'XGrid', 'on', 'XLim', [-2 2], 'XLimMode', 'manual', ...
  'XTick', [-2:0.5:2], 'XTickLabel', [], ...
  'XTickLabelMode', 'manual', 'XTickMode', 'manual', ...
  ...
  'YGrid', 'on', 'YLim', [-1.5 1.5], 'YLimMode', 'manual', ...
  'YTick', [-1.5:0.5:1.5], 'YTickLabel', [], ...
  'YTickLabelMode', 'manual', 'YTickMode', 'manual', ...
  ...
  'ZGrid', 'on', 'ZLim', [-1 1], 'ZLimMode', 'manual', ...
  'ZTick', [-1:0.5:1], 'ZTickLabel', [], ...
  'ZTickLabelMode', 'manual', 'ZTickMode', 'manual' ...
);

%-------------------------------------------------------------------------------
% geometry

% define the vertices of four squares lying in the xy plane
v1 = [
  -1   0   0  -1
  -1  -1   0   0
   0   0   0   0
   1   1   1   1
];
v2 = [
   0   1   1   0
  -1  -1   0   0
   0   0   0   0
   1   1   1   1
];
v3 = [
   0   1   1   0
   0   0   1   1
   0   0   0   0
   1   1   1   1
];
v4 = [
  -1   0   0  -1
   0   0   1   1
   0   0   0   0
   1   1   1   1
];

% define their colors
clr(1,1,1:3) = [.7 .5 .5];
clr(1,2,1:3) = [.5 .7 .5];
clr(1,3,1:3) = [.5 .5 .7];
clr(1,4,1:3) = [.7 .7 .7];

% draw a multi-colored patch using these vertices
h = patch( ...
  [v1(1,:)' v2(1,:)' v3(1,:)' v4(1,:)'], ...
  [v1(2,:)' v2(2,:)' v3(2,:)' v4(2,:)'], ...
  [v1(3,:)' v2(3,:)' v3(3,:)' v4(3,:)'], ...
  clr ...
);
set(h, 'FaceAlpha', .5);

%-------------------------------------------------------------------------------
% rendering

% create the avi file and set its parameters
video = avifile( ...
  'changeview.avi','fps', 10, 'keyframe', 10, ...
  'quality', 100, 'compression', 'none' ...
);
frames = 75;

% setup camera movement parameters
dtheta = -90/75; dphi = .25;
dx = 0; dy = 0; dz = .01;

% render frames while changing the viewpoint
campos([0 5 1]); camtarget([0 0 0]); camproj('perspective'); camva(45);
for (i = 1:frames)
  % get a frame of given size and add it to the avi file as usual
  video = addframe(video, getframe(gcf, axisrect));
  % move camera to next position
  camorbit(dtheta, dphi, 'data', 'z');
  camdolly(dx, dy, dz, 'fixtarget', 'camera');
end
video = close(video); % finis
