%Author: Jie Zhang
%Date:    Feb. 21, 2013
%Purpose: CDS301 class HW#2 demonstration
%Visualize a 2-D Gaussian function in 3-D in two different lighting mode

%clear the variables and figure space
clear 
clf reset

%define the mesh
%x: [-1.2, 1.2], and y: [-1.2,1.2]
[x,y]=meshgrid([-1.5:0.1:1.5],[-1.5:0.1:1.5]);

%define the Gaussian function
z=exp(-x.^2-y.^2);  

%visualize the first Figure: flat or constant interpolation on the surface cells

%Show the Figure in Window 1
hf_1= figure(1);   %obtain the handle for Figure 1
set(hf_1,'Position',[250 300 500 500]);  %explic
hs_1= surf(x,y,z); %obtain the handle for the surface

%remove the edge lines
set(hs_1,'EdgeColor','none')

%set the window background color to be white
set(hf_1,'Color',[1,1,1]);

%map to a single green color
colormap([0,1,0])

%set up the light
light('Position',[-2,-2,0.6],'Style','infinite');

%set the lighting effect to be flat
lighting flat

%tuning the lighting effect
%reduce the specular light
set(hs_1,'AmbientStrength',0.4,'DiffuseStrength',0.5','SpecularStrength',0.1,'SpecularExponent',10)

%remove the axis
axis off

%add the grid
hold on  %hold the figure

z_m=zeros(size(z));
hm_1 = mesh(x,y,z_m,'EdgeColor','k','LineWidth',1.1)

%plot the domain bounday in red color
sz=size(z);  %index size of the array
sz_line=sz(1)  %index size of the 1-D array
z_line=zeros(sz_line) ;
plot3(x(1:1,:),y(1:1,:),z_line,'Color',[1,0,0],'LineWidth',2)
plot3(x(sz_line:sz_line,:),y(sz_line:sz_line,:),z_line,'Color',[1,0,0],'LineWidth',2)
plot3(y(1:1,:),x(1:1,:),z_line,'Color',[1,0,0],'LineWidth',2)
plot3(y(sz_line:sz_line,:),x(sz_line:sz_line,:),z_line,'Color',[1,0,0],'LineWidth',2)

hold off

print -dpng 'Gaussian_2D_flat.png'

%
%make the second Figure
%use Gouraud shaing, and half transparency of the surface
%
%Show the Figure in Window 1
hf_2= figure(2)
clf reset
set(hf_2,'Position',[800 300 500 500]);  %explicitly set the position
hs_2= surf(x,y,z); %obtain the handle for the surface

%remove the edge lines
set(hs_2,'EdgeColor','none')

%set the window background color to be white
set(hf_2,'Color',[1,1,1]);

%map to a single green color
colormap([0,1,0])

%set up the light 
%make the light position lower in Z
light('Position',[-2,-2,0],'Style','infinite');

%set the lighting effect to be flat
lighting gouraud

%tuning the lighting effect
%increase the alpha value to 0.9
set(hs_2,'AmbientStrength',0.7,'DiffuseStrength',0.7,...
'SpecularStrength',0.5,'SpecularExponent',5,...
'FaceAlpha',0.9)


%remove the axis
axis off

%add the grid
hold on  %hold the figure

z_m=zeros(size(z));
hm_2 = mesh(x,y,z_m,'EdgeColor','k','LineWidth',1.1)

%plot the domain bounday in red color
sz=size(z);  %index size of the array
sz_line=sz(1)  %index size of the 1-D array
z_line=zeros(sz_line) ;
plot3(x(1:1,:),y(1:1,:),z_line,'Color',[1,0,0],'LineWidth',2)
plot3(x(sz_line:sz_line,:),y(sz_line:sz_line,:),z_line,'Color',[1,0,0],'LineWidth',2)
plot3(y(1:1,:),x(1:1,:),z_line,'Color',[1,0,0],'LineWidth',2)
plot3(y(sz_line:sz_line,:),x(sz_line:sz_line,:),z_line,'Color',[1,0,0],'LineWidth',2)

print -dpng 'Gaussian_2D_Gouraud.png'
