Documente Academic
Documente Profesional
Documente Cultură
% Annotations:
% ---------------------------------------------------------------------
% The file is by no means a good example for matlab programming, its main
% purpos was to draw a xmas tree.
% The povray source is from xtree.inc
% from Remco de Korte 1998 <remcodek@xs4all.nl>
% the decoration part was inspired by Ken Allred's (2002) additions to
% xtree.inc
% <SNIP xtree.inc>
% //The xtree-object is a simple firtree, starting at <0,0,0> up to maxheight
% //You can change the tree by altering the rand-seed or the height, or by changing
the
% //tow increment-steps indeicated below. The setting as it is now is working
allright for
% //me, changing it can increase the render-time dramatically.
% <SNAP>
% povray uses a differently oriented coordinate system, therefore rotations
% around y and z axis are interchanged.
%
% The rotation subroutines are copied from CAD2MATDEMO.M
% by Don Riley from the FEX
%
% Happy Christmas *
% /\
% /^^\
% Marc /^°^°\
% ||
% ---------------------------------------------------------------------
if length(reg)==1
maxHeight = reg{1};
elseif length(reg)~=0
maxHeight = 30;
disp('XMasTree uses input of the form XMasTree([height],''Properties'',...)');
warning(['input ' mat2str([reg{2:end}]) ' is ignored.']);
else
maxHeight = 30;
end
%% a template ball
[x,y,z] = sphere(nPoints);
tmpBall.Vertices = [x(:),y(:),z(:)]*decorationsSize;
tmpBall.Faces = convhulln(tmpBall.Vertices);
%% a template needle
% #declare needle=sphere{<0,0,0>,1 scale<.75,.1,.1> pigment{rgb<.2,.5,.2>}}
% //here you can change the size of the needles - they're quite large and a
% bit 'fat' now
[x,z,y] = ellipsoid(0,0,0, 0.5,0.1,0.1, nPoints);
tmpNeedle.Vertices = [x(:),y(:),z(:)];
tmpNeedle.Faces = convhulln(tmpNeedle.Vertices);
%% the top
% sphere{<0,maxheight,0>,maxheight/175 texture{branch_text}}
[x,y,z] = ellipsoid(0,0,maxheight,
maxheight/175,maxheight/175,maxheight/175,nPoints);
top.Vertices = [x(:),y(:),z(:)];
top.Faces = convhulln(top.Vertices);
% //this starts the branches of at 1/15th of the tree's size off the ground.
% #declare cc=maxheight/15;
cc=maxheight/15;
tree = povUnion;
% #while (cc<maxheight)
while (cc<maxheight)
% start a branch:
% union{
branch = povUnion;
% #declare i=0;
i = 0;
% #declare dd=(maxheight*.95-cc)/2.5;
dd = (maxheight*.95-cc)/2.5;
% //put needles on the branch:
% #while (i<dd)
while (i<dd)
% union{
branchPart = povUnion;
% sphere{<0,0,0>,.2 scale <1,2.5,1> texture{branch_text}}
tmp = tmpBranch;
branchPart = povUnion(branchPart,tmp,BRANCHTYPE);
% //put the needles all around:
% #declare j=60*rand(r);
j = 60*rand;
% #while (j<360)
while (j<360)
% object{needle translate -x rotate z*-15-i*45/dd rotate y*j+30*rand(r)}
hasLight = rand;
if hasLight<=NeedlePropability
tmp = tmpNeedle;
hasLight = 0;
else
tmp = tmpBall;
hasLight = 1;
end
% translate -x rotate z*-15-i*45/dd rotate y*j+30*rand(r)
tmp.Vertices(:,1) = tmp.Vertices(:,1)-0.75;
tmp = c_rotateYAxis(tmp,-(-15-i*45/dd));
tmp = c_rotateZAxis(tmp,j+30*rand);
if hasLight==0
branchPart = povUnion(branchPart,tmp,NEEDLETYPE);
else
branchPart = povUnion(branchPart,tmp,BALLTYPE);
end
% #declare j=j+60;
j = j + 60;
% #end
end
% scale 1-.25*i/dd
branchPart.Vertices = branchPart.Vertices * (1-0.25*i/dd);
% translate y*i
branchPart.Vertices(:,3) = branchPart.Vertices(:,3)+i;
% }
branch = povUnion(branch,branchPart);
% the frequency of the needles (~the amount of needles) depends on this step
% if you make it smaller you have more needles and a much more rendertime
% #declare i=i+.4;
i = i + 0.4;
% #end
end %(i<dd)
if ~isempty(branch.Vertices)
% //this makes the branch point up a bit
% (depending on how high it is on the tree)
% rotate z*(-75+cc/2)
branch = c_rotateYAxis(branch,-75+cc/2);
tree = povUnion(tree,branch);
end
% }
if nargin<3
Type = NaN;
end
tmp = unique(faces(types==selectType,:));
tmpMap = [tmp [1:length(tmp)]'];
map = ones(length(vertices),1)*NaN;
map(tmpMap(:,1)) = tmpMap(:,2);
newVertices = vertices(tmp,:);
newFaces = map(faces(types==selectType,:));
newTypes = ones(size(newFaces,1),1)*selectType;
end % of function c_seperateType
% ---------------------------------------------------------------------------
function colorDecorations(object,handle,BALLTYPE,tmpBall,decorationsColorArray)
if ~isempty(object.Types==BALLTYPE)
decoIdx = find(object.Types==BALLTYPE);
numBalls = length(decoIdx)/length(tmpBall.Faces);
numFaces = length(decoIdx);
tmpFaceVertexCData = repmat([0.0, 0.9, 0.0],numFaces,1);
for iBalls=1:numBalls
rColor = rand*length(decorationsColorArray);
rColor = min(ceil(rColor),size(decorationsColorArray,1));
color = repmat(decorationsColorArray(rColor,:),length(tmpBall.Faces),1);
tmpFaceVertexCData(1+(iBalls-1)*length(tmpBall.Faces):...
iBalls*length(tmpBall.Faces),:) = color;
end
faceVertexCData=get(handle,'FaceVertexCData');
faceVertexCData(decoIdx,:) = tmpFaceVertexCData;
set(handle,'FaceVertexCData',faceVertexCData);
% use metalic looking balls if
% all faces belong to one decoration patch
if size(object.Faces,1)==numFaces
set(handle,...
'AmbientStrength',0.4,...
'DiffuseStrength',0.7,...
'SpecularStrength',8.0,...
'SpecularExponent',5,...
'SpecularColorReflectance',0.6);
end
end
end % of function colorDecorations
% ---------------------------------------------------------------------------