function threedshow

Fig = openfig('threedshow.fig');
H = guihandles(Fig);
set(Fig, 'Color', get(H.Light1, 'BackGroundColor'));

set(cameratoolbar, 'Parent', Fig);

LightingList = {'Flat', 'Gouraud', 'Phong'};
ColorList = {'Autumn', 'Bone', 'Cool', 'Copper', 'Gray', 'Hot', ...
    'HSV', 'Jet', 'Lines', 'Pink', 'Spring', 'Summer', 'Winter'};
ColorByList = {'X', 'Y', 'Z'};
StyleList = {'Points', 'Wireframe', 'Normal', 'Flat', 'One Color'};
BackFaceLightingList = {'Unlit', 'Lit', 'ReverseLit'};
MaterialList = {'Unknown', 'Metal', 'Shiny', 'Dull', 'Default'};
LightStyleList = {'Infinite', 'Local'};

LightingInit = 2;
ColorInit = 4;
ColorByInit = 3;
StyleInit = 3;
BackFaceLightingInit = 3;
MaterialInit = 1;
BottomInit = 0;
AlphaInit = 1;
ResolutionInit = 64;
AmbientStrengthInit = .3;
DiffuseStrengthInit = .6;
SpecularStrengthInit = .9;
SpecularExponentInit = 10;
SpecularColorReflectanceInit = 1;
Light1Init = 1;
Light2Init = 1;
Light3Init = 0;
Light1StyleInit = 1;
Light2StyleInit = 1;
Light3StyleInit = 1;

set(H.Lighting, 'String', LightingList, 'Value', LightingInit, 'CallBack', @LightingCallBack);
set(H.Color, 'String', ColorList, 'Value', ColorInit, 'CallBack', @ColorCallBack);
set(H.ColorBy, 'String', ColorByList, 'Value', ColorByInit, 'CallBack', @ColorByCallBack);
set(H.Style, 'String', StyleList, 'Value', StyleInit, 'CallBack', @StyleCallBack);
set(H.BackFaceLighting, 'String', BackFaceLightingList, 'Value', BackFaceLightingInit, 'CallBack', @BackFaceLightingCallBack);
set(H.Material, 'String', MaterialList, 'Value', MaterialInit, 'CallBack', @MaterialCallBack);
set(H.Bottom, 'Value', BottomInit, 'CallBack', @BottomCallBack);
set(H.Alpha, 'Value', AlphaInit, 'CallBack', @AlphaCallBack);
set(H.Resolution, 'Value', ResolutionInit, 'Min', 3, 'Max', 100, 'CallBack', @ResolutionCallBack);
set(H.AmbientStrength, 'Value', AmbientStrengthInit, 'CallBack', @AmbientStrengthCallBack);
set(H.DiffuseStrength, 'Value', DiffuseStrengthInit, 'CallBack', @DiffuseStrengthCallBack);
set(H.SpecularStrength, 'Value', SpecularStrengthInit, 'CallBack', @SpecularStrengthCallBack);
set(H.SpecularExponent, 'Value', SpecularExponentInit, 'Min', 5, 'Max', 25, 'CallBack', @SpecularExponentCallBack);
set(H.SpecularColorReflectance, 'Value', SpecularColorReflectanceInit, 'CallBack', @SpecularColorReflectanceCallBack);
set(H.AmbientLightColor, 'CallBack', @AmbientLightColorCallBack);
set(H.Light1, 'Value', Light1Init, 'CallBack', @Light1CallBack);
set(H.Light2, 'Value', Light2Init, 'CallBack', @Light2CallBack);
set(H.Light3, 'Value', Light3Init, 'CallBack', @Light3CallBack);
set(H.Light1Style, 'Value', Light1StyleInit, 'String', LightStyleList, 'CallBack', @Light1StyleCallBack);
set(H.Light2Style, 'Value', Light2StyleInit, 'String', LightStyleList, 'CallBack', @Light2StyleCallBack);
set(H.Light3Style, 'Value', Light3StyleInit, 'String', LightStyleList, 'CallBack', @Light3StyleCallBack);
set(H.Light1Color, 'CallBack', @Light1ColorCallBack);
set(H.Light2Color, 'CallBack', @Light2ColorCallBack);
set(H.Light3Color, 'CallBack', @Light3ColorCallBack);

% Initial drawing

[Xt, Yt, Zt, Xh, Yh, Zh, Xb, Yb, Zb] = queen(ResolutionInit);
Torso = surface(Xt, Yt, Zt);
Head = surface(Xh, Yh, Zh);
Bottom = patch(Xb, Yb, Zb);

axis off equal vis3d;
campos([-130 -720 320]);
axis([-25 25 -25 25 0 70]);
Light1 = camlight;
Light2 = camlight('left');
Light3 = camlight('headlight');

LightingCallBack;
ColorCallBack;
% ColorByCallBack; % ResolutionCallBack calls this
StyleCallBack;
BackFaceLightingCallBack;
MaterialCallBack;
BottomCallBack;
AlphaCallBack;
ResolutionCallBack;
AmbientStrengthCallBack;
DiffuseStrengthCallBack;
SpecularStrengthCallBack;
SpecularExponentCallBack;
SpecularColorReflectanceCallBack;
Light1CallBack;
Light2CallBack;
Light3CallBack;
Light1StyleCallBack;
Light2StyleCallBack;
Light3StyleCallBack;

    function LightingCallBack(varargin)
        set([Torso, Head, Bottom], 'FaceLighting', LightingList{get(H.Lighting, 'Value')});
    end;
    
    function ColorCallBack(varargin)
        colormap(ColorList{get(H.Color, 'Value')});
    end;
    
    function ColorByCallBack(varargin)
        Data = [ColorByList{get(H.ColorBy, 'Value')}, 'Data'];
        set(Torso, 'CData', get(Torso, Data));
        set(Head, 'CData', get(Head, Data));
        set(Bottom, 'CData', get(Bottom, Data));
    end;
    
    function StyleCallBack(varargin)
        switch StyleList{get(H.Style, 'Value')}
            case 'Points'
                set([Torso, Head, Bottom], ...
                    'Marker', '.', 'MarkerEdgeColor', 'k', ...
                    'LineStyle', 'None', 'FaceColor', 'None');
            case 'Wireframe'
                set([Torso, Head, Bottom], ...
                    'LineStyle', '-', 'EdgeColor', 'k', ...
                    'Marker', 'None', 'FaceColor', 'None');
            case 'Normal'
                set([Torso, Head, Bottom], ...
                    'Marker', 'None', 'LineStyle', 'None', ...
                    'FaceColor', 'Interp');
            case 'Flat'
                set([Torso, Head, Bottom], ...
                    'Marker', 'None', 'LineStyle', 'None', ...
                    'FaceColor', 'Flat');
            case 'One Color'
                c = get(Torso, 'FaceColor');
                if ischar(c)
                    c = [1, .7, 0];
                end;
                c = uisetcolor(c, 'Set Face Color');
                set([Torso, Head, Bottom], ...
                    'Marker', 'None', 'LineStyle', 'None', ...
                    'FaceColor', c);
        end
    end;
    
    function BackFaceLightingCallBack(varargin)
        set([Torso, Head, Bottom], 'BackFaceLighting', BackFaceLightingList{get(H.BackFaceLighting, 'Value')});
    end;
    
    function MaterialCallBack(varargin)
        m = MaterialList{get(H.Material, 'Value')};
        if ~isequal(m, 'Unknown')
            material(MaterialList{get(H.Material, 'Value')});
            resetmaterial('AmbientStrength');
            resetmaterial('DiffuseStrength');
            resetmaterial('SpecularStrength');
            resetmaterial('SpecularExponent');
            resetmaterial('SpecularColorReflectance');
        end;
        function resetmaterial(p)
            x = get(Torso, p);
            set(H.(p), 'Value', x);
            set(H.([p, 'Text']), 'String', num2str(x));
        end;
    end;
    
    function BottomCallBack(varargin)
        if get(H.Bottom, 'Value')
            set(Bottom, 'Visible', 'On');
        else
            set(Bottom, 'Visible', 'Off');
        end;
    end;

    function AlphaCallBack(varargin)
        x = get(H.Alpha, 'Value');
        set(H.AlphaText, 'String', num2str(x));
        alpha(x);
    end;

    function ResolutionCallBack(varargin)
        x = round(get(H.Resolution, 'Value'));
        set(H.ResolutionText, 'String', num2str(x));
        [Xt, Yt, Zt, Xh, Yh, Zh, Xb, Yb, Zb] = queen(x);
        set(Torso, 'XData', Xt, 'YData', Yt, 'ZData', Zt);
        set(Head, 'XData', Xh, 'YData', Yh, 'ZData', Zh);
        set(Bottom, 'XData', Xb, 'YData', Yb, 'ZData', Zb);
        ColorByCallBack;
    end;

    function AmbientStrengthCallBack(varargin)
        x = get(H.AmbientStrength, 'Value');
        set(H.AmbientStrengthText, 'String', num2str(x));
        set([Torso, Head, Bottom], 'AmbientStrength', x);
        SetMaterialUnknown;
    end;

    function DiffuseStrengthCallBack(varargin)
        x = get(H.DiffuseStrength, 'Value');
        set(H.DiffuseStrengthText, 'String', num2str(x));
        set([Torso, Head, Bottom], 'DiffuseStrength', x);
        SetMaterialUnknown;
    end;

    function SpecularStrengthCallBack(varargin)
        x = get(H.SpecularStrength, 'Value');
        set(H.SpecularStrengthText, 'String', num2str(x));
        set([Torso, Head, Bottom], 'SpecularStrength', x);
        SetMaterialUnknown;
    end;

    function SpecularExponentCallBack(varargin)
        x = get(H.SpecularExponent, 'Value');
        set(H.SpecularExponentText, 'String', num2str(x));
        set([Torso, Head, Bottom], 'SpecularExponent', x);
        SetMaterialUnknown;
    end;

    function SpecularColorReflectanceCallBack(varargin)
        x = get(H.SpecularColorReflectance, 'Value');
        set(H.SpecularColorReflectanceText, 'String', num2str(x));
        set([Torso, Head, Bottom], 'SpecularColorReflectance', x);
        SetMaterialUnknown;
    end;
    
    function Light1CallBack(varargin)
        if get(H.Light1, 'Value')
            set(Light1, 'Visible', 'On');
        else
            set(Light1, 'Visible', 'Off');
        end;
    end;
    
    function Light2CallBack(varargin)
        if get(H.Light2, 'Value')
            set(Light2, 'Visible', 'On');
        else
            set(Light2, 'Visible', 'Off');
        end;
    end;
    
    function Light3CallBack(varargin)
        if get(H.Light3, 'Value')
            set(Light3, 'Visible', 'On');
        else
            set(Light3, 'Visible', 'Off');
        end;
    end;
    
    function Light1StyleCallBack(varargin)
        set(Light1, 'Style', LightStyleList{get(H.Light1Style, 'Value')});
    end;
    
    function Light2StyleCallBack(varargin)
        set(Light2, 'Style', LightStyleList{get(H.Light2Style, 'Value')});
    end;
    
    function Light3StyleCallBack(varargin)
        set(Light3, 'Style', LightStyleList{get(H.Light3Style, 'Value')});
    end;
    
    function Light1ColorCallBack(varargin)
        SetLightColor(Light1);
    end;

    function Light2ColorCallBack(varargin)
        SetLightColor(Light2);
    end;

    function Light3ColorCallBack(varargin)
        SetLightColor(Light3);
    end;

    function SetLightColor(L)
        c = get(L, 'Color');
        c = uisetcolor(c, 'Set Light 1 Color');
        set(L, 'Color', c);
    end;

    function AmbientLightColorCallBack(varargin)
        c = get(H.Axes, 'AmbientLightColor');
        c = uisetcolor(c, 'Set Ambient Light Color');
        set(H.Axes, 'AmbientLightColor', c);
    end;

    function SetMaterialUnknown
        set(H.Material, 'Value', 1);
    end;

    function [Xt, Yt, Zt, Xh, Yh, Zh, Xb, Yb, Zb] = queen(n)

        t = [c(15, 15, 2), c(15, 13, 2), c(13, 15, 2), c(15, 15, 1), c(15, 10, 6), c(10, 10, 4), c(10, 5, 20), ...
            c(5, 12, 2), c(12, 8, 2), c(8, 8, 2), c(8, 6, 2), c(6, 6, 3), c(6, 7, 1), c(7, 6, 1), c(6, 11, 9), ...
            c(11, 2, 2), c(2, 2, 2)];

        [Xt, Yt, Zt] = cylinder(t, n);
        Zt = 63*Zt;

        [Xh, Yh, Zh] = sphere(n);
        Xh = 3 * Xh; Yh = 3 * Yh; Zh = 3 * Zh;
        Zh = Zh + 64;

        t = linspace(0, 2*pi, n+1);
        Xb = 15 * cos(t);
        Yb = 15 * sin(t);
        Zb = zeros(size(Xb));

        function t = c(t0, t1, n)
            k = 1;
            t = linspace(t0, t1, k*n);
        end;
    end;

end
