%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Author: Roland Miezianko (rmiezian@astro.temple.edu)
% Date: October 6, 2002
% Instructor: Dr.Longin Jan Latecki
% Course: CIS 581
% Project : Assignment 1 (Create Movie Part)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start fresh, cleanup
clear all;
clc;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define avi file and other movie params frame counter
aviFileName = 'cube.avi';
framesPerSec = 10; 
aviobj = avifile(aviFileName,'fps',framesPerSec);
frame = 1;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add movie title
a = 0; b = 255; c = 200;
x = a + (b-a) * rand(c);
x = (b/2) + zeros(c);

for i = 1:15
    
    % show image
    imagesc(x);
    colormap(gray);
    axis off;
    
    % show text
    text(100,50,'Adventures of','FontSize',24,'FontWeight','bold','Color','b','HorizontalAlignment','center');
    text(100,80,'The Cube','FontSize',24,'FontWeight','bold','Color','b','HorizontalAlignment','center','FontAngle','italic');
    text(100,130,'Roland Miezianko','FontSize',16,'FontWeight','normal','Color','k','HorizontalAlignment','center');
    text(100,150,'CIS 581','FontSize',16,'FontWeight','normal','Color','k','HorizontalAlignment','center');

    % store frame
    aviobj = addframe(aviobj,getframe);
	frame = frame + 1;
    clf;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define translation paramters
tx = 0.5;
ty = 0.0;
tz = 0.0;

% define translation matrix
T = [   1 0 0 tx;
        0 1 0 ty;
        0 0 1 tz;
        0 0 0 1];

   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define scaling parameter
sx = 0.5;
sy = 0.5;
sz = 0.5;

% define scaling matrix
S = [   sx  0   0   0;
        0   sy  0   0;
        0   0   sz  0;
        0   0   0   1];
    
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define rotation around x-axis parameter (in rad)
xang = 0;

% define rotation around x-axis matrix
rx = [  1        0           0           0;
        0        cos(xang)   -sin(xang)  0;
        0        sin(xang)   cos(xang)   0;
        0        0           0           1];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define rotation around y-axis parameter (in rad)
yang = 0;

% define rotation around y-axis matrix
ry = [  cos(yang)   0       sin(yang)   0;
        0           1       0           0;
        -sin(yang)  0       cos(yang)   0;
        0           0       0           1];
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define rotation around z-axis parameter (in rad)
zang = 0;

% define rotation around z-axis matrix
rz = [  cos(zang)  -sin(zang)   0       0;
        sin(zang)  cos(zang)    0       0;
        0          0            1       0;
        0          0            0       1];
       
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define initial cube vertices
v  = [  0   0   0;
        1   0   0;
        1   1   0;
        0   1   0;
        0   0   1;
        1   0   1;
        1   1   1;
        0   1   1];

% row vector to add to v to create 8x4 matrix for translation/rotation/scaling    
vw = [1 1 1 1 1 1 1 1]';
    
% define the cube faces
f = [   1 2 6 5;
        2 3 7 6;
        3 4 8 7;
        4 1 5 8;
        1 2 3 4;
        5 6 7 8];

% define the cube    
fvc = [ 0 0 0;
        0 0 1;
        0 1 0;
        0 1 1;
        1 0 0;
        1 0 1];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show original cube
for i = 1:20

    % show the actual new cube coordinates
	patch('Vertices', v, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none') % , 'HandleVisibility', 'on'
    text(0,0,2,'Original Cube','Color','b');
	alpha(.5);
	view(3);
	axis([-2 2 -2 2 -2 2]);
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
	frame = frame + 1;
	clf;
    
end  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show translating the  cube
for i = 0.0 : 0.02 : 0.7
    
    % new scaling factor
	tx = 0;
	ty = i;
	tz = 0;
	T = [   1 0 0 tx;
            0 1 0 ty;
            0 0 1 tz;
            0 0 0 1];
    
    % do the matrix computation and get new cube coordinates
	Vw = [v vw]';
	Vcomp = ( T * Vw )';
	Vnew = Vcomp(:,1:3);

    % show the actual new cube coordinates
	patch('Vertices', Vnew, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none')
    text(0,0,2,'Translate Cube','Color','b');
    alpha(.5);  
	view(3);
	axis([-2 2 -2 2 -2 2]);
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
    frame = frame + 1;
    
    clf
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show scaling the cube
for i = 1.0 : -0.02 : 0.1
    
    % new scaling factor
	sx = i;
	sy = i;
	sz = i;
	S = [   sx  0   0   0;
            0   sy  0   0;
            0   0   sz  0;
            0   0   0   1];
    
    % do the matrix computation and get new cube coordinates
	Vw = [v vw]';
	Vcomp = ( S * Vw )';
	Vnew = Vcomp(:,1:3);

    % show the actual new cube coordinates
	patch('Vertices', Vnew, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none')
    text(0,0,2,'Scale Cube','Color','b');
    alpha(.5);  
	view(3);
	axis([-2 2 -2 2 -2 2]);
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
    frame = frame + 1;
    
    clf;
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show rotating the cube around X axis
for i = 0 : pi/10 : 2*pi
    
    % new rotation factor
	xang = i;
	rx = [  1        0           0          0;
            0        cos(xang)   -sin(xang) 0;
            0        sin(xang)   cos(xang)  0;
            0        0           0          1];
   
    % do the matrix computation and get new cube coordinates
	Vw = [v vw]';
	Vcomp = ( rx * Vw )';
	Vnew = Vcomp(:,1:3);
	
    % show the actual new cube coordinates
	patch('Vertices', Vnew, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none')
    text(0,0,2,'Rotate Cube (X-axis)','Color','b');
    alpha(.5);  
	view(3);
	axis([-2 2 -2 2 -2 2]);
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
    frame = frame + 1;
    
    clf;
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show rotating the cube around Y axis
for i = 0 : pi/10 : 2*pi
    
    % new rotation factor
	yang = i;
	ry = [  cos(yang)   0       sin(yang)   0;
            0           1       0           0;
            -sin(yang)  0       cos(yang)   0;
            0           0       0           1];
   
    % do the matrix computation and get new cube coordinates
	Vw = [v vw]';
	Vcomp = ( ry * Vw )';
	Vnew = Vcomp(:,1:3);
	
    % show the actual new cube coordinates
	patch('Vertices', Vnew, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none')
    text(0,0,2,'Rotate Cube (Y-axis)','Color','b');
    alpha(.5);  
	view(3);
	axis([-2 2 -2 2 -2 2])
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
    frame = frame + 1;
    
    clf;
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show rotating the cube around Z axis
for i = 0 : pi/10 : 2*pi
    
    % new rotation factor
	zang = i;
	rz = [  cos(zang)  -sin(zang)   0       0;
            sin(zang)  cos(zang)    0       0;
            0          0            1       0;
            0          0            0       1];
       
   
    % do the matrix computation and get new cube coordinates
	Vw = [v vw]';
	Vcomp = ( rz * Vw )';
	Vnew = Vcomp(:,1:3);
	
    % show the actual new cube coordinates
	patch('Vertices', Vnew, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none')
    text(0,0,2,'Rotate Cube (Z-axis)','Color','b');
    alpha(.5);  
	view(3);
	axis([-2 2 -2 2 -2 2]);
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
    frame = frame + 1;
    
    clf;
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show rotating the cube around all axis
for i = 0 : pi/10 : 2*pi
    
    % new rotation factor for all matrices
	ra = [  cos(i)   -sin(i)  sin(i)  0;
            sin(i)   cos(i)   -sin(i) 0;
            -sin(i)  sin(i)   cos(i)  0;
            0        0        0       1];
   
    % do the matrix computation and get new cube coordinates
	Vw = [v vw]';
	Vcomp = ( ra * Vw )';
	Vnew = Vcomp(:,1:3);
	
    % show the actual new cube coordinates
	patch('Vertices', Vnew, 'Faces', f, 'FaceVertexCData', fvc,...
          'FaceColor', 'flat', 'EdgeColor', 'none')
    text(0,0,2,'Fly Cube','Color','b');
    alpha(.5);  
	view(3);
	axis([-2 2 -2 2 -2 2]);
	axis off;
	hidden on;
    
    % store figure in movie file
    aviobj = addframe(aviobj,getframe);
    frame = frame + 1;
    
    clf;
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add movie ending
for i = 1:15
    
    % show image
    imagesc(x);
    colormap(gray);
    axis off;
    
    % show text
    text(100,100,'The End','FontSize',24,'FontWeight','bold','Color','b','HorizontalAlignment','center');

    % store frame
    aviobj = addframe(aviobj,getframe);
	frame = frame + 1;
    clf;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% movie(M)
% close the avi movie object
aviobj = close(aviobj);



