Rotating a 3D sphere through user interface

5 次查看(过去 30 天)
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 个评论
Daimien Burks
Daimien Burks 2012-4-28
Basically, when it starts, the sphere appears and is textured with my image, but when I hit "Rotate" a new semi-transparent sphere of blue and red lines appears on top of my sphere and *that's* what rotates, not the actual sphere itself. My sphere just stays still like it's not connected to the button.
Daimien Burks
Daimien Burks 2012-4-28
Edit: Added formatted code. Sorry I posted it sloppily in the comment section and left out useful info! I just read the tutorial on how to ask questions acceptably :|

请先登录,再进行评论。

采纳的回答

Daimien Burks
Daimien Burks 2012-4-28
Figured it out! Except now the figure zooms in and out and I can't figure out to make it stop. Any ideas?
function ex2
global state;
global marker;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%& GUI Menu 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);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%Draw Tracing Line & Ball %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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;
hold on;
view([0 -10]);
k = 2;
ind_ini = 0;
state = 0;
marker = 0;
az = 0;
set(gca,'CameraViewAngle', 15)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%Rotate Callback %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function hIniAniCallback(hObject,evt)
state = 0;
for k=marker:-10:-360
view(k,-10)
axis equal
pause(0.1)
if (state == 1)
marker = k;
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;
marker = 0;
k = 0;
az = 0;
view(0, -10);
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by