Search code examples
matlabimage-processing3d2dtexture-mapping

Assigning a 2D image for every face of a 3D cube: MATLAB


I want to build a cube in MATLAB and assign different 2D images for its faces. I think this is called texture mapping. I've searched for such a code, but what I found is a code that is able to assign a single image to all of the cube faces, the code is available here (http://www.mathworks.com/matlabcentral/answers/32070-rgb-images-on-a-3d-cube). Here is the code,

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);

I want to assign different pictures for different faces, I've tried to set different cdata variables as cdata1, cdata2,..., cdata6 each has a different image path, but I got an error indicating that cdata1 is not defined.

EDIT: Here is what I've tried using only 2 images as an example,

cdata1 = flipdim( imread('face1.jpg'), 1 );
cdatar1 = flipdim( cdata1, 2 );

cdata2 = flipdim( imread('interface 1.png'), 1);
cdatar2 = flipdim( cdata2, 2 );

% bottom
surface([-1 1; -1 1], [-1 -1; 1 1], [-1 -1; -1 -1], ...
    'FaceColor', 'texturemap', 'CData1', cdatar1 );
% top
surface([-1 1; -1 1], [-1 -1; 1 1], [1 1; 1 1], ...
    'FaceColor', 'texturemap', 'CData2', cdata2 );

% font
surface([-1 1; -1 1], [-1 -1; -1 -1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData1', cdata1 );
% back
surface([-1 1; -1 1], [1 1; 1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData2', cdatar2 );

% left
surface([-1 -1; -1 -1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData1', cdatar2 );
% right
surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
    'FaceColor', 'texturemap', 'CData1', cdata1 );

view(3);

The code above yields an error saying that CData1 is not defined.

Can anyone tell me how to get the above code capable of assigning different images to different faces of the cube?

Thanks.


Solution

  • It should be straightforward!

    cdata1 = flipdim( imread('peppers.png'), 1 );
    cdata2 = rgb2gray(flipdim( imread('peppers.png'), 1 ));
    

    It seems that you just did not create the variables with the correct name (cdata1, cdata2, cdata3 ,...)

    Additionally:

    the name of the property you wan to set in each of the surface calls does not change. You always want to set CData to something. There is no property in surface called CData1 as there is no property called PutImageHere ! replace all the calls with 'CData' as in:

    surface([1 1; 1 1], [-1 1; -1 1], [-1 -1; 1 1], ...
        'FaceColor', 'texturemap', 'CData', yourvariablename); 
    % in this case yourvariablename is cdata1