You are on page 1of 4

Chapter 18

My First Shader

Introduction
In this chapter we will compile and view a simple shader. In doing so, you will become familiar with the shader writing tools and processes, which we will apply to more complex examples in later chapters.

Writing the Code


RenderMan shaders are written using a special programming language know as SL (Shading Language). The code descrbing a shader called mySurface will typically be found in the file mySurface.sl. While this naming convention is not strictly necessary, it is a very sensible convention to adopt, and we will assume it here. The code for our first shader is in Listing 18.1. In SL anything between /* and */ is a comment, so the first line of code is simply a description of the files contents. The second line indicates that this is a shader of type surface and has the name first. This is the name that it will be referred to by, in a RIB file or animation package. The rest of the file will be considered in greater detail in the following chapter.

Listing 18.1 A first shader.


/* A Simple Shader Shader written in SL */ surface first() { Oi= Os; Ci= Cs * Oi; }

131

132

Essential RenderMan

The shader source code is simply text, and can be entered using any text editor, such as textedit (MacOSX), kate (Linux), or notepad (Windows). It should be saved in a text file with the name first.sl. The exact layout is not important, so you can add new lines, or spaces wherever you choose, but it is important to lay the code out so that it is as clear and readable as possible.

Preparing the Shader for Use


In order to use the shader with a particular renderer it first needs to be transformed from the text format of the shading language into a more basic format that can be understood by the renderera processed known known as compilation. This is done by a program (known as a compiler) which is unique to each RenderMan renderer. You should refer to the documentation of your renderer for the information on its compiler, but the most common examples are: Renderer PRMan RenderDotC Air 3Delight Aqsis Angel Compiler Name shader shaderdc shaded shaderdl aqsl giles File Extension .slo .system dependant .slb .sdl .slx .slc

We will use the command shader (the PRMan shader compiler), but you should substitute for whatever is required by your renderer. When this program is run a new file is created which contains a version of the shader optimised for a particular renderer. You would therefore compile your new surface first for use by PRMan using the command shader first.sl to create the file first.slo. Each renderer uses a unique file extension for its compiled shaders, so you can compile a shader for several renderers and keep the results in the same folder. If a compiled shader is not generated, this usually means there is a mistake somewhere in your shader. This will happen, even when you are proficient at shader writing, as the compiler insists that the shader be exactly right before it can be used to create an image. In the event of an error the compiler will usually print a list of problems, along with the line number of the code which caused problems. Tackle the errors one at a time, starting with the first, as a problem on one line will often confuse the compiler, making it think there are far more problems than there arefixing the first error will often solve all of them. If you cannot see a problem on the line that the compiler is reporting an error on, check the line before, as sometimes the compiler does not notice a mistake until slightly later than it should.

My First Shader

133

Viewing the Results


At this stage it would be possible to go into the modeling package of your choice, and use the shader. During the process of writing a shader, however, you will probably need to do this hundreds of times. It, therefore, makes sense to streamline the process as much as possible (and also avoid tying up a potentially valuable modeling licence). A good method of doing this is to either manually or using your modeling software create a RIB file which has the new shader attached to a simple object (a sphere or a plane), as in Listing 18.2. This can be rendered quickly and easily from the command line to produce something like Figure 18.1.

Listing 18.2 A test scene


Display first.tiff file rgba Projection perspective fov [45] LightSource ambientlight 1 intensity [0.2] LightSource spotlight 2 from [1 1 0 ] to [0 0 3] intensity [3] Translate 0 0 3 WorldBegin Color [1 0 0] Surface first Sphere 1 1 1 360 WorldEnd

FIGURE 18.1. A first shader

134

Essential RenderMan

Starting with simple geometry allows you to understand the behavior of your shader better than a complex model would. The development process therefore becomes: edit myShader.sl shader myShader.sl render testMyShader.rib viewer testMyShader.tiff rinse and repeat . . . A typical shader development process might use a simple RIB file for the initial development. Once the basic look has been established a more complex RIB file can be used, perhaps containing the object for which the shader is being developed, so the appearance can be evaluated in situ. Only for final tweaking and production testing need the shader actually be loaded into the modeling package.

Summary
The shader development process is summarized in Figure 18.2.

Shader first.sl Go Back and Improve the shader RIB File first.rib

Shader Compiler shader

Compiled Shader shader.slo

Renderer render

Image File first.tiff

View Image

FIGURE 18.2. Compiling shaders

You might also like