The shading language is a language quite similar to C but it lacks most of the more complicated features and has some special constructs that simplify the writing of shader.
The language contains 4 data types only: strings, floats, colors and points. While strings can only be set and accessed, all standard operators ( +, -, *, ...) are available for the other data types. There are no complex data types like arrays, structures or pointers. The usual control constructs like `` for'' loops or the `` if'' statements are available.
There is also a set of predefined convenience functions like `` reflect()'' which calculates the reflected vector according to a surface normal, and so on. User defined functions are available as well, with all parameters passed by reference. An example surface shader taken from is given in below:
surface matte (float Ka = 1; float Kd = 1;) { point Nf; Nf = faceforward(N,I); Oi = Os; Ci = Os * Cs * (Ka * ambient() + Kd * diffuse(Nf)); }A shader is a special type of function and belongs to exactly one of the six shader RenderMan shader classes . Shader parameters are defined in the header of the shader. A default value must be specified. The values can be changed using token-value pairs
Shaders communicate with the rendering system through a set of global variables. At a shader call the input variables are preset by the renderer and the shader uses this information to calculate its return values which are then passed to the renderer in other global variables.
The shader shown above uses the global variables containing the surface normal `` N'', the direction of the incident ray `` I'', and the surface color and opacity ( Cs and Os). These values are used to calculate the reflected color `` Ci'' and opacity `` Oi'' of the surface. The ambient() and diffuse() functions return the illumination of the surface.
There are 6 different classes of shaders in the shading language. The classes differ from each other in the set of global variables that can be accessed from the shader and in some functions which can only be called by a specific shader class.
While the illuminate() construct defines a local light source, in point light sources the solar() construct describes a lightsource positioned at infinity, with the emission only depending on the direction from which it is viewed (all light rays are parallel).
The shading language uses two classes of variables: uniform and varying variables. Uniform variables are constant for a specific instance of a shader. Thus, they can be calculated during the instantiation of the shader. A variable is declared uniform by preceding the type name with the keyword `` uniform'', for example `` uniform float''.
By default variables defined in the body of a shader are varying while the parameters are uniform. A parameter can be declared varying by preceding the type name with `` varying''. A parameter must be declared varying if automatically interpolated values should be passed (see Section
Maps are used to access external resources such as texture or environment maps. The different map types differ in the way the data is accessed. The texture coordinates s and t are used in the case of texture ( texture()) and bump maps ( bump()). The shadow ( shadow()) and environment ( environment()) maps are indexed using a point (a direction in this case). The type of the return value depends on the type of the map. Texture and environment maps return either float or color values, shadow maps return floats and bump maps points. There are no limitations on the use of these functions, i.e. every map type can be accessed from any shader type.
Shader that are attached to surfaces (surface, area light source and
displacement shaders) offer functions to calculate the derivative of
an arbitrary expression along the u ( Du) and v ( Dv)
parametric direction of the surface. This is convenient in conjunction
with displacement mapping, in which the normal can be obtained by
simply calculating the cross product of the two partial derivatives of
the displaced positions: N = Du(P) x Dv(P). Other applications
allow for a convenient filtering of (procedural) textures.
Copyright - Sudhir R Kaushik (sudhir@cs.wpi.edu)