To Render the function in Matlab, I used the peaks function (substitute your function/functions for this) with surf() and set the view to to be 90 azimuth, 0 horizon, and a interpolated color map set to gray mapping in the Z direction.
From there its a matter of using the print function to print out the image to an image of the appropriate format and possibly cropping it (I wrote the crop code in matlab, but Gimp and Photoshop work just as well). If you don’t want Matlab to do the cropping (I did not write a very intelligent algorithm), just delete all the code below the “process out white border” comment.
It saves out the raw file (without cropping) as heightmap.png, and the cropped file as hmap.png. Those names are definitely modifiable as well as many of the settings in the script. Just use the Matlab Docs if you want to change stuff, or post back and test my Matlab knowledge
.
Fortunately, I saved the Matlab Code I used for this (thought I had deleted it):
clear
clc
close all
% Example Code For Matlab to Blender workflow for heightmaps
% Copyright 2007
%Example Function
[xx, yy, zz] = peaks(500);
%Plot The Function Using a Gray Map
pHandle = surf(xx, yy, zz, zz);
colormap('gray')
%Make The Shading Smooth
shading interp
%View From Top
view(0, 90)
%Clean up
grid off
axis off
axis square
%Save out as .png
fileName = 'heightmap';
%Change 300 to set Resolution
print('-dpng', '-r300', fileName)
%Process Out White Border
map = imread([fileName '.png']);
[rows, cols, colors] = size(map);
hMid = round(cols/2);
vMid = round(rows/2);
topRow = 0;
for i = 1:round(rows/2)
if (map(i, hMid, 1) ~= 255) || (map(i, hMid, 2) ~= 255) || (map(i, hMid, 3) ~= 255)
topRow = i;
break;
end
end
bottomRow = 0;
for i = rows:-1:round(rows/2)
if (map(i, hMid, 1) ~= 255) || (map(i, hMid, 2) ~= 255) || (map(i, hMid, 3) ~= 255)
bottomRow = i;
break;
end
end
leftCol = 0;
for i = 1:round(cols/2)
if (map(vMid, i, 1) ~= 255) || (map(vMid, i, 2) ~= 255) || (map(vMid, i, 3) ~= 255)
leftCol = i;
break;
end
end
rightCol = 0;
for i = cols:-1:round(cols/2)
if (map(vMid, i, 1) ~= 255) || (map(vMid, i, 2) ~= 255) || (map(vMid, i, 3) ~= 255)
rightCol = i;
break;
end
end
hmap = map(topRow:bottomRow, leftCol:rightCol, 1:3);
imwrite(hmap, 'hmap.png')
In Blender, you import the image as a texture and apply it to a heavily subdivided plane using the displace modifier. You can find a little more description and example files at:
http://wiki.blender.org/index.php/Manual/Displace_Modifier
Like I previously said blurring may help, and I believe there are functions in Matlab that can do this (imfilter() and fspecial() perhaps? or just code your own).