%Demo the hierarchical building system

clear all;
clf;

%build a ground plane
ground.facecolor=[.1 .9 .1];
ground.vertices= ...
   [-10, -10, 0;
   -10, 10, 0;
   10, 10 ,0;
   10, -10, 0];
ground.faces=[1 2 3 4];

%tower
tower=translate(scale(UnitCube,1,1,4),8,8,4);
tower.facelighting='flat';
tower.facecolor=[.9,.2,.2];

%build the fusilage
sphere1=UnitSphere(2);
sphere1.facecolor='white';
nose=translate(scale(sphere1,3,1,1),-4.5,0,1.01);
tailcap=translate(scale(sphere1,2,1,1),4.5,0,1.01);
cockpit=translate(scale(UnitSphere(1.5),1.5,.75,.5),-2.5,0,2.0);
cockpit.facecolor='none';
cockpit.edgecolor='black';
cyl1=UnitCylinder(2);
cyl1=translate(rotateY(scale(cyl1,1,1,5),90),0,0,1.01);
%cyl1.facelighting='flat';
cyl1.facecolor='white';
fusilage=combine(cyl1, nose, tailcap, cockpit);

%wing and tail
wing=translate(scale(UnitSphere(3),1,10,.2),-2,0,1);
wing.facecolor='white';
htail=translate(scale(UnitSphere(2),1,4,.2),5,0,1);
vtail=translate(rotateY(scale(UnitSphere(2),1,.2,2),30),5,0,2);
wingtail=combine(wing, htail, vtail);

for time=0:.1:1
   
   %motors and propellors
   motor=scale(UnitSphere(2),1.5,.5,.5);
   motor.facecolor='white';
   motors={ translate(motor,-2,3,1) translate(motor,-2,-3,1) };
   prop=rotateX(scale(UnitSphere(1.5),.1,2,.2), time*90);
   prop.facecolor='black';
   props=combine(...
      translate(prop,-3.1,-3,1),...
      translate(prop,-3.1,3,1) );
   motorprop=combine( motors, props );
   
   plane=combine( fusilage, wingtail, motorprop);
   plane=translate(plane,0,0,1);
   
   clf  
   scene=combine( ground, tower, plane );
   count=renderpatch(scene);
   
   axis off;
   grid on
   daspect([1 1 1])
   
   light('position',[10,-10,10])
   %light('position',[10, 10, 10])
   
   %Do a persptective transform
   set(gca,'projection','perspective')
   set(gca,'CameraViewAngle',6)
   
   %The frame background color
   set(gcf,'color', [.6,.8,.8])
   xlabel('x');ylabel('y');zlabel('z'); 
   view(-40,20)
   drawnow;
   
end %for  

rotate3d on
