Reflective Shaders

 

BMRT has the capability to produce ray traced images that include specular interreflections, but only if the objects have the proper surface shaders. For example, the normal surface shader "plastic" does not allow for specular interreflections.

/* plastic.sl - Standard plastic surface for RenderMan Interface.
 * (c) Copyright 1988, Pixar.
 *
 * The RenderMan (R) Interface Procedures and RIB Protocol are:
 *     Copyright 1988, 1989, Pixar.  All rights reserved.
 * RenderMan (R) is a registered trademark of Pixar.
 */
surface
plastic (float Ka = 1;
         float Kd = .5;
         float Ks = .5;
         float roughness = .1;
	 color specularcolor = 1;)
{
  normal Nf = faceforward (normalize(N),I);
  Oi = Os;
  Ci = Os * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
	specularcolor * Ks*specular(Nf,-normalize(I),roughness));
}

3_plastic_balls.jpg (24560 bytes)


The surface shader "shiny", included with BMRT uses the trace {} function to account for specular interreflections. It adds in the result of trace {} to the specular term of the local illumonation model:

 

/*
 * shiny.sl -- Shiny metal surface, using ray tracing.
 *
 * DESCRIPTION:
 *   Makes a smoothly polished metal, using ray tracing to calculate
 *   reflections of the environment.
 * 
 * PARAMETERS:
 *    Ka, Kd, Ks, roughness, specularcolor - The usual meaning
 *    Kr - coefficient for mirror-like reflections of environment
 *    blur - how blurry are the reflections? (0 = perfectly sharp)
 *    samples - set to higher than 1 for oversampling of blur
 *
 * AUTHOR: written by Larry Gritz, 1991
 *
 * HISTORY:
 *      Aug 1991 -- written by lg in C
 *      25 Jan 1994 -- recoded by lg in correct shading language.
 *
 * last modified 25 Jan 1994 by Larry Gritz
 */
surface
shiny ( float Ka = 1, Kd = 0.5, Ks = 1;
	float Kr = 0.5, roughness = 0.05, blur = 0;
	color specularcolor = 1;
	float samples = 1; )
{
    normal Nf;               /* Forward facing normal vector */
    vector IN;               /* normalized incident vector */
    vector uoffset, voffset; /* Offsets for blur */
    color ev;                /* Color of the reflections */
    vector R, Rdir;          /* Direction to cast the ray */
    uniform float i, j;
    
    /* Construct a forward facing surface normal */
    Nf = faceforward (normalize(N), I);
    IN = normalize (I);
    ev = 0;
    /* Calculate the reflection color */
    if (Kr > 0.001) {
	/* Rdir gets the perfect reflection direction */
	Rdir = normalize (reflect (IN, Nf));
	if (blur > 0) {
	    /* Construct orthogonal components to Rdir */
	    uoffset = blur * normalize (vector (zcomp(Rdir) - ycomp(Rdir),
					        xcomp(Rdir) - zcomp(Rdir),
					        ycomp(Rdir) - xcomp(Rdir)));
	    voffset = Rdir ^ uoffset;
	    for (i = 0;  i < samples;  i += 1) {
		for (j = 0;  j < samples;  j += 1) {
		    /* Add a random offset to the smooth reflection vector */
		    R = Rdir +
			((i + float random())/samples - 0.5) * uoffset +
			((j + float random())/samples - 0.5) * voffset;
		    ev += trace (P, normalize(R));
		}
	    }
	    ev *= Kr / (samples*samples);
	} else {
	    /* No blur, just do a simple trace */
	    ev = Kr * trace (P, Rdir);
	}
    }
    
    /* The rest is like the plastic shader */
    Oi = Os;
    Ci = Os * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
		specularcolor * (ev + Ks*specular(Nf,-IN,roughness)));
}

 3_shiny_balls.jpg (30254 bytes)


 

/*
 * shiny_pure_ref.sl -- Shiny metal surface, using ray tracing.
 *
 * DESCRIPTION:
 *   Makes a smoothly polished metal, using ray tracing to calculate
 *   reflections of the environment.
 * 
 * PARAMETERS:
 *    Ka, Kd, Ks, roughness, specularcolor - The usual meaning
 *    Kr - coefficient for mirror-like reflections of environment
 *    blur - how blurry are the reflections? (0 = perfectly sharp)
 *    samples - set to higher than 1 for oversampling of blur
 *
 * AUTHOR: written by Larry Gritz, 1991
 *
 * HISTORY:
 *      Aug 1991 -- written by lg in C
 *      25 Jan 1994 -- recoded by lg in correct shading language.
 *
 * last modified 25 Jan 1994 by Larry Gritz
 * Returns only the color of the reflection
 * GSO 6-21-98
 */
surface
shiny_pure_ref ( float Ka = 1, Kd = 0.5, Ks = 1;
	float Kr = 0.5, roughness = 0.05, blur = 0;
	color specularcolor = 1;
	float samples = 1; )
{
    normal Nf;               /* Forward facing normal vector */
    vector IN;               /* normalized incident vector */
    vector uoffset, voffset; /* Offsets for blur */
    color ev;                /* Color of the reflections */
    vector R, Rdir;          /* Direction to cast the ray */
    uniform float i, j;
    
    /* Construct a forward facing surface normal */
    Nf = faceforward (normalize(N), I);
    IN = normalize (I);
    ev = 0;
    /* Calculate the reflection color */
    if (Kr > 0.001) {
	/* Rdir gets the perfect reflection direction */
	Rdir = normalize (reflect (IN, Nf));
	if (blur > 0) {
	    /* Construct orthogonal components to Rdir */
	    uoffset = blur * normalize (vector (zcomp(Rdir) - ycomp(Rdir),
					        xcomp(Rdir) - zcomp(Rdir),
					        ycomp(Rdir) - xcomp(Rdir)));
	    voffset = Rdir ^ uoffset;
	    for (i = 0;  i < samples;  i += 1) {
		for (j = 0;  j < samples;  j += 1) {
		    /* Add a random offset to the smooth reflection vector */
		    R = Rdir +
			((i + float random())/samples - 0.5) * uoffset +
			((j + float random())/samples - 0.5) * voffset;
		    ev += trace (P, normalize(R));
		}
	    }
	    ev *= Kr / (samples*samples);
	} else {
	    /* No blur, just do a simple trace */
	    ev = Kr * trace (P, Rdir);
	}
    }
    
    /* The rest is like the plastic shader */
    Oi = Os;
    
    if (ev != 0) 
     { Ci = ev; } /* color is just the reflection */  
    else
    {Ci = Os * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
		specularcolor * (Ks*specular(Nf,-IN,roughness)));}
}
This image has Kr = 0.5
3_shiny_pure_ref__balls.jpg (30379 bytes)
This image has Kr = 1.0
3_shiny_pure_ref__ballsa.jpg (32199 bytes)


Main Renderman Tutorial Page
Main Renderman Page

Last changed July 20, 1999, G. Scott Owen, owen@siggraph.org