Transforming Objects Efficiently
Moving objects, for example by rotation, requires transforming the data that defines the objects. You can improve performance by taking advantage of the fact that graphics hardware can apply transforms to the data. You can then avoid sending the transformed data to the renderer. Instead, you send only the four-by-four transform matrix.
To realize the performance benefits of this approach, use the
to group the objects that you want to move.
The following examples define a sphere and rotate it using two techniques to compare performance:
rotatefunction transforms the sphere’s data and sends the data to the renderer thread with each call to
hgtransformfunction sends the transform matrix for the same rotation to the renderer thread.
|Code with Poor Performance||Code with Better Performance|
When object data is large, the update bottleneck becomes a limiting factor.
% Using rotate figure [x,y,z] = sphere(270); s = surf(x,y,z,z,'EdgeColor','none'); axis vis3d for ang = 1:360 rotate(s,[1,1,1],1) drawnow end
% Using hgtransform figure ax = axes; [x,y,z] = sphere(270); % Transform object contains the surface grp = hgtransform('Parent',ax); s = surf(ax,x,y,z,z,'Parent',grp,... 'EdgeColor','none'); view(3) grid on axis vis3d % Apply the transform tic for ang = linspace(0,2*pi,360) tm = makehgtform('axisrotate',[1,1,1],ang); grp.Matrix = tm; drawnow end toc