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 Comments

Could you show us how you set up the surface texture?
So basically I wanted the sphere with my image as its texture to rotate like the blue-and-red sphere is doing. I just don't know what to change/add in order for it to do that. I've been trying to read how to do this in the Mathworks manual, but this was the closest code I could find for this purpose on here and I just wanted to modify it a bit. Thanks for any help!
Can someone please tell me if this is at least unrealistic? Thanks!
Please explain, what "it doesn't work" exactly means.
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.
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 :|

Sign in to comment.

 Accepted Answer

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

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!