POV-Ray : Newsgroups : povray.text.tutorials : Q: Macro tutorial? : Re: Q: Macro tutorial? Server Time
28 Apr 2024 07:06:28 EDT (-0400)
  Re: Q: Macro tutorial?  
From: Nieminen Juha
Date: 1 Feb 2000 04:23:49
Message: <3896a625@news.povray.org>
Jerry's tutorial is good. If you want a short answer, I'll try to write
something:

  If you have ever coded in C or C++, macros are like #defines which take
arguments. The general syntax is:

#macro MyMacro(argument1, argument2, argument3)
  (some pov-script here)
#end

  There can be any number of arguments. These can be used to generate the
pov-script. For example:

#macro WhiteSphere(Location, Radius)
  sphere { Location, Radius pigment { rgb 1 } }
#end

  Now if you write:

WhiteSphere(<1,2,3>, 4)

the povray parser will internally substitute with the following:

sphere { <1,2,3>, 4 pigment { rgb 1 } }

  You could also use macros as functions. For example:

#macro Double(Var)
  (Var)*2
#end

  Now if you type something like:

#declare Radius = Double(3);

it will be the same as:

#declare Radius = (3)*2;

  (Note the parentheses; guess why they are important?)


  Sometimes one wants a kind of return value from a complex calculation.
It can be achieved like this:

#macro ComplexCalculation(A, B, C)
  #local result = 0;
  #while(A>0)
    #local result = result + B*C;
    #local A = A-1;
  #end
  result
#end

  Note the last line which only has the identifier 'result' in it. It means
that the value of the whole macro will be the value of 'result' at the end
of the macro. Now you can use ComplexCalculation() as any other function.

  (Ok, this is not a very short answer after all...)

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):5;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.