The RenderMan Interface - Paul Bourke
The RenderMan Interface - Paul Bourke
The RenderMan Interface - Paul Bourke
- No tags were found...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
14.2 Functions<br />
Functions are similar to shaders except they can be called from shaders or other functions,<br />
but cannot be instanced from the <strong>RenderMan</strong> <strong>Interface</strong>. Functions in the Shading Language<br />
are also much like functions in C. For example, the following defines a function normalize<br />
that returns a unit vector in the direction V:<br />
vector<br />
normalize ( vector V )<br />
{<br />
return V/length(V);<br />
}<br />
Function calls use the traditional C syntax, except for the issues noted below.<br />
Functions may return any of the basic types (float, color, point, vector, normal, matrix,<br />
string). Functions may not return arrays, though they make take arrays as parameters.<br />
Functions must have exactly one return statement, except for those functions declared with<br />
type void, which may not have any return statement.<br />
Function parameters are passed by reference; in other words, parameters are not copied<br />
into private data areas of the function. Nevertheless, function parameters are not writable<br />
unless specified with the output keyword. For example:<br />
void<br />
normalize inplace ( output vector V )<br />
{<br />
V = V/length(V);<br />
}<br />
Functions in the Shading Language cannot be called recursively.<br />
Functions may be polymorphic — that is, you may define multiple functions with the same<br />
name, as long as they take different types or numbers of arguments. Functions may even<br />
be polymorphic based on return type. For example:<br />
float snoise (point p)<br />
{<br />
return 2 * (float noise(p)) - 1;<br />
}<br />
float snoise (float s, float t)<br />
{<br />
return 2 * (float noise(s, t)) - 1;<br />
}<br />
vector snoise (point p)<br />
{<br />
131