• No results found

LANGUAGE CONSTRUCTS

In document The RenderMan Interface pdf (Page 136-140)

Part II The RenderMan Shading Language

Section 13 LANGUAGE CONSTRUCTS

LANGUAGE CONSTRUCTS

13.1

Expressions

Expressions are built from arithmetic operators, function calls, and variables. The language supports the common arithmetic operators (+,-,*, and/) plus the vector operatorsˆ(cross product) and. (dot product), and the C conditional expression (binary relation? expr1: expr2).

When operating on points, vectors, normals, or colors, an arithmetic operation is per- formed in parallel on each component. If a binary operator involves a float and a multi- component type (such as a point, vector, normal, matrix, or color), the float is promoted to the appropriate type by duplicating its value into each component. It is illegal to perform a binary operation between a point and a color. Cross products only apply to vectors; dot products apply to both vectors and colors. Two points, two colors, or two strings can be compared using==and!=. Points cannot be compared to colors.

The usual common-sense mathematical rules apply to point/vector/normal arithmetic. For example, a vector added to a point yields a point, a vector added to a vector yields a vector, and a point subtracted from a point yields a vector. Mixing the types of vectors, normals, and points (for example, taking a cross product of two points, rather than two vectors) is allowed, but is discouraged. A particular implementation may choose to issue a compiler warning in such cases. Note that vectors and normals may be used nearly interchangably in arithmetic expressions, but care should be taken to distinguish between them when performing coordinate system transformations.

Matrix variables can be tested for equality and inequality with the==and!=boolean op- erators. The* operator between matrices denotes matrix multiplication, whilem1 / m2 denotes multiplyingm1by the inverse of matrixm2. Thus, a matrix can be inverted by writing1/m.

13.2

Standard Control Flow Constructs

The basic explicit control flow constructs are:

• conditional execution, • loops, and

• function calls.

These constructs are all modeled after C. Statement grouping allows a single statement to be replaced with a sequence of statements.

{

stmt; ...

stmt; }

Any variables declared within such a brace-delimited series of statements are only visible within that block. In other words, variables follow the same local scoping rules as in the C language.

Conditional execution is controlled by

if(boolean expression)stmtelsestmt

There are two loop statements,

while(boolean expression)stmt

and

for(expr;boolean expression;expr )stmt

A boolean expression is an expression involving a relational operator, one of: <,>,<=, >=,==, and!=. It is not legal to use an arbitraryfloat,pointorcolorexpression directly as a boolean expression to control execution. A boolean expression can not be used as a floating point expression.

The

break[n] and

continue[n]

statements cause either theforor thewhileloop at levelnto be exited or to begin the next iteration. The default value fornis 1 and refers to the immediately enclosing loop.

Built-in and user-progrmamed functions are called just as in C. The

returnexpr

statement is used to return a value to the caller. Any functions that do not return a value should be declared asvoid, just as in ANSI C, and should not contain areturnstatement.

13.3

Illuminance and Illuminate Statements

The Shading Language contains three new block statement constructs: illuminance, illu- minate, andsolar. illuminanceis used to control the integration of incoming light over a hemisphere centered on a surface in a surface shader.illuminateandsolarare used to spec- ify the directional properties of light sources in light shaders. If a light source does not have anilluminateorsolarstatement, it is a non-directional ambient light source.

Unlike other control statements, illuminance, illuminate, and solar statements cannot be nested. However, multipleilluminance,illuminate, orsolar statements may be given se- quentially within a single shader.

Theilluminancestatement controls integration of a procedural reflectance model over the incoming light. Inside theilluminanceblock two additional variables are defined: Clor light color, andLor light direction. The vectorLpoints towards the light source, but may not be normalized (see Figure 12.2). The arguments to theilluminancestatement specify a three-dimensional solid cone. The two forms of anilluminancestatement are:

illuminance( [string category,] pointposition)

statements

illuminance( [string category,] pointposition, vectoraxis, floatangle)

statements

The first form specifies that the integral extends over the entire sphere centered atposition. The second form integrates over a cone whose apex is on the surface atposition. This cone is specified by giving its centerline, and the angle between the side and the axis in radians. Ifangleis PI, the cone extends to cover the entire sphere and these forms are the same as the first form. Ifangleis PI/2, the cone subtends a hemisphere with the north pole in the directionaxis. Finally, ifangleis 0, the cone reduces to an infinitesimally thin ray.

Light shaders can specify “categories” to which they belong by declaring a string parame- ter named category(this name has two underscores), whose value is a comma-separated list of categories into which the light shader falls. When the illuminancestatement con- tains an optional string parametercategory, the loop will only consider lights for which thecategory is among those listed in its comma-separated categorylist. If the illumi- nancecategorybegins with a-character, then only lightsnotcontaining that category will be considered.

When the optionalcategorystring is omitted, anilluminanceloop will execute its body for every nonambient light source.

A Lambertian shading model is expressed simply using theilluminancestatement:

Nn = normalize(N); illuminance( P, Nn, PI/2 ){

Ln = normalize(L); Ci += Cs * Cl * Ln.Nn; }

This example integrates over a hemisphere centered at the point on the surface with its north pole in the direction of the normal. Since the integral extends only over the upper hemisphere, it is not necessary to use the customarymax(0,Ln.Nn)to exclude lights that are locally occluded by the surface element.

Theilluminateandsolarstatements are inverses of theilluminancestatement. They control the casting of light in different directions. The point variableLcorresponding to a particu- lar light direction is available inside this block. This vector points outward from the light source. The color variableClcorresponds to the color in this direction and should be set. Like theilluminancestatements,illuminateandsolarstatements cannot be nested.

Theilluminatestatement is used to specify light cast by local light sources. The arguments to theilluminatestatement specify a three-dimensional solid cone. The general forms are:

illuminate(position)stmt

illuminate(position, axis, angle)stmt

The first form specifies that light is cast in all directions. The second form specifies that light is cast only inside the given cone. The length ofL inside anilluminatestatement is equal to the distance between the light source and the surface currently being shaded. Thesolarstatement is used to specify light cast by distant light sources. The arguments to thesolarstatement specify a three-dimensional cone. Light is cast from distant directions inside this cone. Since this cone specifies only directions, its apex need not be given. The general forms of thesolarstatement are:

solar( )stmt

solar(axis, angle)stmt

The first form specifies that light is being cast from all points at infinity (e.g., an illumina- tion map). The second form specifies that light is being cast from only directions inside a cone.

An example of thesolarstatement is the specification of a distant light source:

solar( D, 0 )

Cl = intensity * lightcolor;

This defines a light source at infinity that sends light in the directionD. Since the angle of the cone is 0, all rays from this light are parallel.

An example of theilluminatestatement is the specification of a standard point light source:

illuminate( P )

Cl = (intensity * lightcolor) / (L.L)

This defines a light source at positionPthat casts light in all directions. The1/L.Lterm indicates an inverse square law fall off.

In document The RenderMan Interface pdf (Page 136-140)

Related documents