MATLAB: RGB-images on a 3D cube

3d3d plotscube

Hey,
For a a presentation, I would like to make a 3D cube in Matlab (that's pretty simple I know), but I would also like for the surface of it to be an RGB image (the image repeats on each of the 3 visible sides), and this, I can't figure out how to do. So can anyone help me? Converting it to some sort of indexed colorscale would be fine as well, but it should preferably stay in roughly the same colors.

Best Answer

This is an application for texture mapping. You can create six surfaces -- one for each face of the cube -- and apply your RGB image(s) as the texture map for each. You just have to make sure you flip around the RGB image data so that you're looking at it from the "right side", when assembled into a cube.
Here's a full example:
cdata = flipdim( imread('peppers.png'), 1 );
cdatar = flipdim( cdata, 2 );
% bottom
surface([-1 1; -1 1], [-1 -1; 1 1], [-1 -1; -1 -1], ...
'FaceColor', 'texturemap', 'CData', cdatar );
% top
surface([-1 1; -1 1], [-1 -1; 1 1], [1 1; 1 1], ...
'FaceColor', 'texturemap', 'CData', cdata );
% font
surface([-1 1; -1 1], [-1 -1; -1 -1], [-1 -1; 1 1], ...
'FaceColor', 'texturemap', 'CData', cdata );
% back
surface([-1 1; -1 1], [1 1; 1 1], [-1 -1; 1 1], ...
'FaceColor', 'texturemap', 'CData', cdatar );
% left
surface([-1 -1; -1 -1], [-1 1; -1 1], [-1 -1; 1 1], ...
'FaceColor', 'texturemap', 'CData', cdatar );
% right
surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
'FaceColor', 'texturemap', 'CData', cdata );
view(3);
If you're not sure how it all works, try adding one surface at a time -- it might be more clear that way.