%% Handle Graphics Objects
%
% This example shows visualization of graphical objects created in MATLAB
% and visualized in the F3D Viewer.
%
% For visualization of Handle Graphics objects, the load method is available.
%
% The displayImage method is used to apply the MATLAB figure graphics as a
% texture on the scene floor surface.

%   Copyright 1998-2021 HUMUSOFT s.r.o.

%% Initialize the 3D Foundation object
FW = F3D.World(mfilename);
initialize(FW, [0 -10 5]);

% Set the viewport properties
VP = FW.Viewports.Main;
VP.Title = mfilename;
Path = fileparts(which(mfilename));
setLogo(VP, fullfile(Path, 'data', 'MatlabLogo.png'));

% Use OnClose callback of the viewport to close figures
VP.OnClose = 'close(findall(0, ''Tag'', ''F3D_HGO''));';

% Create a Patch object
hF1 = figure('Tag', 'F3D_HGO');
colormap('jet');
[verts, faces, cindex] = teapotGeometry;
P = patch('Faces',faces,'Vertices',verts,'FaceVertexCData',cindex,'FaceColor','interp');
view(3);
axis equal

% Import the Patch object
Patch1 = addChild(FW.Root, 'Patch');
load(Patch1, P, 1);
Patch1.Location = [4 0 0];
Patch1.Flat = 0.5;
Patch1.Shininess = 0.8;

% Create a Surf object
figure('Tag', 'F3D_HGO', 'Position', hF1.Position + [100 -100 0 0]);
colormap('hsv');
[X,Y] = meshgrid(-5:0.2:5);
Z = cos((X.^2 + Y.^2)/5)*10./(2 + sqrt(X.^2 + Y.^2));
S = surf(X, Y, Z);

% Import the Surf object
Surf1 = addChild(FW.Root, 'Surf');
load(Surf1, S, 0.5);
Surf1.Location = [-4 1 0];

% Create a Graph object
figure('Tag', 'F3D_HGO', 'Position', hF1.Position + [200 -200 0 0]);
t = 0:360;
plot(t, sind(t), t, cosd(t));
grid on
xlim([0 360]);

% Import the graph as texture
Graph1 = addChild(FW.Root, 'Graph');
createShape(Graph1, 'plane', [16 12 0]);
Graph1.Location = [0 0 -4];
Graph1.TextureMapping.Blend = 1;
displayImage(Graph1, gcf);
Graph1.Shininess = 0;