Rotating a 3D sphere through user interface
显示 更早的评论
Hey all, I am new to MATLAB and wanted to create a program with a 3-D sphere using a .png file as its surface texture while allowing a user to rotate it by use of a button.
by Julian Francisco and want to do something similar, but instead of a sphere create from MATLAB commands, wanted to use the .png. I've tried to change the 'surface' texture to the .png but it doesn't work. Can someone please show me how to do this?
Thanks!
function ex2
global state;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%GUI Controls %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fh = figure('Menu','none','Toolbar','none','Units','characters',...
'Renderer','OpenGL');
hPanAni = uipanel('parent',fh,'Units','characters','Position',...
[22.6 10.4 53 23],'title','Controls','FontSize',11,...
'FontAngle','italic','FontWeight','bold');
hIniAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
'Position',[0.14 0.75 0.5 0.12],'String','Rotate',...
'FontSize',10,'Callback',@hIniAniCallback);
hFinAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
'Position',[0.14 0.5 0.5 0.12],'String','Stop',...
'FontSize',10,'Callback',@hFinAniCallback);
hResetAni = uicontrol(hPanAni,'Style','pushbutton','Units','normalized',...
'Position',[0.14 0.25 0.5 0.12],'String','Reset',...
'FontSize',10,'Callback',@hResetAniCallback);
hPantSim = uipanel('Parent',fh,'Units','characters',...
'Position',[107.87 8 157.447 42],'title',...
'Screen','FontSize',11,'FontAngle','italic',...
'FontWeight','bold','BorderType','none');
hPantSimInt = uipanel('Parent',hPantSim,'Units','normalized','Position',...
[0 0 1 1],'BorderType','line','BackgroundColor','k');
axes('Parent',hPantSimInt,'Units','normalized','Position',[0 0 1 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%Placement of Stars %%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
stars = rand(1000,2);
scatter(stars(:,1),stars(:,2),6,'w','Marker','+');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis off;
ah4 = axes('Parent',hPantSimInt,'Units','normalized','Position',...
[0 0 1 1],'Color','none','Visible','off','DataAspectRatio',...
[1 1 1],'NextPlot','add');
hgrot = hgtransform('Parent',ah4);
T1 = 0:pi/1000:2*pi;
Fin = numel(T1);
if (Fin>1000)
Incr = floor(Fin/1000);
else
Incr = 1;
end
Y = zeros(numel(T1),3);
Y(:,1) = 7000*cos(T1);
Y(:,2) = 7000*sin(T1);
R_esf = 6378;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Draw Sphere %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[x_esf,y_esf,z_esf] = sphere(50);
x_esf = R_esf*x_esf;
y_esf = R_esf*y_esf;
z_esf = R_esf*z_esf;
xmin = min(x_esf);
xmax = max(z_esf);
ymin = min(y_esf);
ymax = max(y_esf);
zmin = min(z_esf);
zmax = max(z_esf);
s1 =surf(x_esf,y_esf,z_esf);
I=imread('mar-1.png');
I=double(I)/255;
colormap(jet);
props.EdgeColor = 'none';
props.CData = [1 0 1 1 0 1 1 0 1];
props.CDataMapping = 'scaled';
set(s1,'FaceColor','texturemap','cdata',I)
props.FaceColor = 'texturemap';
props.Parent = hgrot;
surface(x_esf,y_esf,z_esf,props);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handles.psat = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
'ZData',Y(1,3),'Marker','o', 'MarkerSize',10,'MarkerFaceColor','w');
handles.tray = line('parent',ah4,'XData',Y(1,1), 'YData',Y(1,2),...
'ZData',Y(1,3),'LineWidth',1,'Color','y');
set(ah4,'XLim',[min([-R_esf,xmin]) max([R_esf,xmax])],'YLim',...
[min([-1.55*R_esf,ymin]) max([1.55*R_esf,ymax])],'ZLim',[min([-1.55*R_esf,zmin]) ...
max([1.55*R_esf,zmax])]);
rotate3d on;
zoom off;
view([0 -10]);
k = 2;
ind_ini = 0;
state = 0;
az = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%Rotate Callback %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hIniAniCallback(hObject,evt)
tic;
if (ind_ini == 1)
return;
end
ind_ini = 1;
state = 0;
%Rotating sphere
while (k<=Fin)
az = az + 0.01745329252;
set(hgrot,'Matrix',makehgtform('zrotate',az));
%Tracing Line
set(handles.tray,'XData',Y(1:k,1),'YData',Y(1:k,2),'ZData',...
Y(1:k,3));
%Tracing Ball
set(handles.psat,'XData',Y(k,1),'YData',Y(k,2),'ZData',Y(k,3));
pause(eps);
if (k == Fin)
toc;
end
k = k + Incr;
if (state == 1)
state = 0;
break;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%Stop Callback %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hFinAniCallback(hObject,evt)
ind_ini = 0;
state = 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%Reset Callback %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hResetAniCallback(hObject,evt)
ind_ini = 0;
state = 1;
k = 2;
az = 0;
%%%Reset Axes
set(hgrot,'Matrix',makehgtform('zrotate',az));
set(handles.tray,'XData',Y(1,1), 'YData',Y(1,2),'ZData',Y(1,3));
set(handles.psat,'XData',Y(1,1), 'YData',Y(1,2),'ZData',Y(1,3));
view([0 -10]);
end
end
6 个评论
Walter Roberson
2012-4-27
Could you show us how you set up the surface texture?
Daimien Burks
2012-4-28
Daimien Burks
2012-4-28
Jan
2012-4-28
Please explain, what "it doesn't work" exactly means.
Daimien Burks
2012-4-28
Daimien Burks
2012-4-28
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!