生成球体的法线贴图

时间:2013-12-02 17:00:34

标签: matlab

我搜索了很多网络,但很难找到我们如何能够真正找到对象的法线贴图。现在,我必须编写一个生成球体法线贴图的代码。我的意思是this one。我的球体应该用方位角和倾斜度来定义,但我们说数字(方位角和倾斜度)应该在0到100之间。有人有任何想法吗?

1 个答案:

答案 0 :(得分:0)

Here是一个解释,可以很好地了解法线贴图是什么以及如何创建它。

使用笛卡尔坐标系在Matlab中创建球体法线贴图的一种方法是:

% X and Y components of the normals vectors are between -1 and 1.
% The step number specifies the size of the output image.
range = -1:0.01:1;
% Repeat the range to build a matrix
X = repmat(range, [size(range, 2), 1]);
Y = flipud(repmat(range', [1, size(range, 2)]));
% These normals must be of unit norm, so find an Z component which
% solves sqrt(X^2 + Y^2 + Z^2) = 1
Z = (1 - (X.^2 + Y.^2)).^0.5;
% Change the display range of X and Y to 0..1 instead of -1..1
X = (X + 1) / 2;
Y = (Y + 1) / 2;
Z = (Z + 1) / 2;
% Mask pixels outside the sphere (in which the Z component is complex)
X(Z ~= real(Z)) = 0;
Y(Z ~= real(Z)) = 0;
Z(Z ~= real(Z)) = 0;
% Build the map
map = cat(3,X,Y,Z);
imshow(map);

这构建了一个矩阵map,其中包含球体法线的X,Y和Z分量。您可以使用sph2cart函数将方位角和高程转换为等效的X,Y和Z分量。

请注意,与通常的普通地图相比,您引用的法线贴图会颠倒翻转,其中绿色应位于左上角,红色位于右下角。