Programming models and shaders II

April 02 , 2006 Posted by Ana Nikoloska

This is an example of the way tcl scripting can be used.

If I have written a shader in renderman, and want to be able to use it in houdini with the mantra renderer, since RSL and VEX have syntax similarities, this is possible.

The main difference in syntax is:

RSL   VEX
 
color   vector
normal   vector
faceforward   frontface
Ci   Cf
Oi   Of

The main catch though is that in RSL the function call for assigning color is the same as the variable call:

in RSL : color c1= color (1, 0, 0);

would be : vector c1= set (1, 0, 0); in vex

That means that when writting the tcl code it is very important to make a difference between searching for the string color.

That is what regular expressions are for.

my regular expression for searching for color as a variable call is :

set tockazap ";"
set zag "\\("

set pattern ((^|(\\t)+)(color)(?=(\\s)*)(?!$zag))|($tockazap)((\\s)*)(color)(?=(\\s)*)(?!$zag)

this means that the regular expression searches for any color that is either in the begining of the line or a little bit ofsetted (I needed the tab because of the call color in the definition of the shader), and that doesnt have parentheses after it (not even after an empty space), and that color call can also be after a semicolon in the same line (if the programmer is not very tidy).

The catch here is that TCl doesn't have syntax for lookbehind assertion, and that is why when I would find the color that is after a semicolon it replaces the semicolon too.

This can be solved in the regsub call:

regsub -all $pattern $copied1 \\5\\6$replacement copied1

with the help of back reference

This back reference referes to the parentheses in the pattern variable that checks for the semicolon.

Similar call is used for the normal reference.

As for the color function call, the regular expression is:

set pattern2 ((color)(?=((\\s)*)$zag))

which referes to a color string that is followed by an open parenthesis.

 

I have used back refference for replacing Ci and Oi, so that I can use them in a same regexp expression.

As for replacing Cs to Cd, (and Os) I have noticed that my code doesn't compile right so in this particular case I have "intelligently" avoided this by choosing a shader that I have written (an adaptation of the slim assignment,) where i don't have a Cs call.

While compiling, I have also noticed that VEX doesn't have the same function call for the step function in RSL, it does have some min max or clamp equivalents, but just so that I can demonstrate the tcl interpreter I have simplified my function a little and got rid of the step function.

The original render in the rib scene with anaPlastic shader is:

 

 

And the equivalent anaPlastic in vex, that is obtained using the tcl interpreter is:

No comments | Leave a comment