user interface - MATLAB: How to make camera light follow 3D Rotation -


i have come problem when trying rotate 3d objects. building gui , have separate figure object plotted. in figure, allow user use matlab's built-in rotate button move object around. however, unable have light follow rotation seems fixed on 1 part of object. create light, using

c=camlight('right'); set(c,'style','infinite'); 

one solution have thought of add light whenever user releases rotate button, not nice. not know how use rotate callbacks when button in separate figure.

does know how make light "track" 3d rotation, current view illuminated?

thanks!

implement own rotation functionality

you can implement own functionality adjust axes , lights @ same time. way light gets adjusted continuously while rotating axes.

function follow_me_1     figure     axes('buttondownfcn', @buttondownfcn);  % assign callback     set(gca,'nextplot','add');              % add next plot current axis     surf(peaks,'hittest','off');            % hittest -> off important     view(3);                                % view start     c = camlight('headlight');              % add light     set(c,'style','infinite');              % set style of light      function buttondownfcn(ax,~)         fig = ancestor(ax,'figure');        % figure handle         [oaz, oel] = view(ax);              % current azimuth , elevation         oloc = get(0,'pointerlocation');    % starting point         set(fig,'windowbuttonmotionfcn',{@rotationcallback,ax,oloc,oaz,oel});         set(fig,'windowbuttonupfcn',{@donecallback});     end      function rotationcallback(~,~,ax,oloc,oaz,oel)         locend = get(0, 'pointerlocation'); % mouse location         dx = locend(1) - oloc(1);           % calculate difference x         dy = locend(2) - oloc(2);           % calculate difference y         factor = 2;                         % correction mouse -> rotation         newaz = oaz-dx/factor;              % calculate new azimuth         newel = oel-dy/factor;              % calculate new elevation         view(ax,newaz,newel);               % adjust view         c = camlight(c,'headlight');        % adjust light     end      function donecallback(src,~)         fig = ancestor(src,'figure');           % figure handle         set(fig,'windowbuttonmotionfcn',[]);    % unassign windowbuttonmotionfcn         set(fig,'windowbuttonupfcn',[]);        % unassign windowbuttonupfcn     end  end 

using rotate3d

this example uses built-in rotate3d , assigned callback functions. 'not nice'-solution takes lines of code.

function follow_me_2     surf(peaks);                  % load demo data     c = camlight('headlight');    % create light     set(c,'style','infinite');    % set style     h = rotate3d;                 % create rotate3d-handle     h.actionpostcallback = @rotationcallback; % assign callback-function     h.enable = 'on';              % no need click ui-button      % sub function callback     function rotationcallback(~,~)         c = camlight(c,'headlight');     end end 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -