[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.4.1 The RunProgram Procedural Primitive

This example shows how to write a procedural primitive in the form of an external program which generates RIB. It can be invoked in a RIB as such: Procedural "RunProgram" ["sphere" "0 0.5 1"] [-1 1 -1 1 -1 1] The 3 floats it receives as parameters represent a color. The program itself outputs a sphere of that color. This example also shows how to generate RIB using lib3delight. Note that on Windows 3Delight will search for sphere.exe if it doesn't find sphere in its procedural search path.

Note that a poorly written program (especially one which fails to output the \377 delimiter) may easily hang the renderer. Great care was taken in 3Delight to check for the most common errors but there are still some which are not caught. It also important that your program be written to accept multiple requests.

 
 
#include <stdio.h>
#include <stdlib.h>

#include "ri.h"

int main(int argc, char **argv)
{
    char buf[256];

    /*
      You can still use the standard error output to diagnose your program.
      This allows you to see that your program is started only once even if it
      is invoked several times in a frame.
    */
    fprintf(stderr, "diagnostic: sphere program started.\n");
    
    /*
      Requests from the renderer to the program are passed on the standard
      input. Each request is written on a single line. The detail level required
      is written first, followed by the arguments passed by the user in the RIB.
      The two are separated by a single space.
    */
    while (fgets(buf, 256, stdin) != NULL)
    {
        RtFloat detail;
        RtColor color = {1.0f, 1.0f, 1.0f};
        sscanf(buf, "%g %g %g %g", &detail, &color[0], &color[1], &color[2]);

        /*
          Calling RiBegin with a file name as a parameter causes the commands
          to be output as RIB to that file when using lib3delight. Using
		  "stdout" or "stderr" will output the RIB to the standard output or
		  error respectively.
          
          It is important to call RiBegin()/RiEnd() inside the loop (for each
          request) to ensure that the output is received properly by the
          renderer.
        */
        RiBegin("stdout");
        
        RiColor(&color[0]);
        RiSphere(1.0f, -1.0f, 1.0f, 360.0f, RI_NULL);
        
        /*
          Outputting a single 0xFF character (377 in octal) is the method to
          signal the renderer that the program has finished processing this
          request. This can also be done manually if you choose not to use the
          library to output your RIB.
        */
        RiArchiveRecord(RI_VERBATIM, "\377");
        
        RiEnd();
    }

    fprintf(stderr, "diagnostic: sphere program is about to end.\n");
    return 0;
}

This example can be compiled with the following commands:

Linux
g++ -o sphere -O3 -I$DELIGHT/include/ -L$DELIGHT/lib/ sphere.cpp -l3delight
Mac OS X
g++ -o sphere -O3 -I$DELIGHT/include/ -L$DELIGHT/lib/ sphere.cpp -l3delight
IRIX
CC -o sphere -O3 -n32 -TARG:isa=mips4 -TARG:processor=r10k -I$DELIGHT/include/ -L$DELIGHT/lib/ sphere.cpp -l3delight
Windows
CL /Ox /I%DELIGHT%\include\ sphere.cpp %DELIGHT%\lib\3Delight.lib

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Aghiles Kheffache on July, 31 2003 using texi2html
3Delight 1.0.0. Copyright 2000-2003 The 3Delight Team. All Rights Reserved.