# Ray-Tracing Your Animation

Once you are satisfied with the MGED wireframe preview of an animation script, you can use the BRL-CAD ray-tracer to compute an optical rendering of the animation.

A typical call to rt might look something like the following:

    %> rt -M -o foo.pix -s 200 moss.g all.g 2>>foo.log < foo.script &

The -M option tells the ray-tracer to read instructions from the script file on standard input. The argument to the -o option gives the basename for the image files that will be created. For example, if foo.script contains instructions for ray-tracing frames zero through two, the image files are named foo.pix, foo.pix.1, and foo.pix.2. The size of each image, specified by the -s option, is 200x200 pixels. After the options, rt expects the name of the geometry database to be ray-traced (in this case "moss.g") and a list of objects to be ray-traced (in this case only the group "all.g"). It is important to include in this list all of the objects you wish to see in the final images. The ray-tracer produces a lot of diagnostic information on standard error, which in this case is redirected to the file foo.log. Ray-tracing animations can take a long time, so it is a good idea to run rt as a background process. You can refer to the rt manual page for complete information on ray-tracer options.

Small images such as the ones you created in the above example can be previewed on a fast framebuffer with the pixtile and fbanim commands, as outlined in Animation Techniques in BRL-CAD.

When preparing animations that will eventually be stored on videotape, frames are typically computed at a resolution of 1440 by 972 pixels, then reduced to 720 by 486 pixels using pixhalve. This combination of high resolution computation and filtering helps to eliminate aliasing artifacts. It is also important to compress the images, because at three bytes per pixel, each 720x486 frame of an animation takes up a megabyte of disk space.

A common technique for doing the filtering and compression is to have rt call a shell script after each frame is computed. A convenient way to add shell script calls to the animation script is with scriptsort. Scriptsort takes as input an animation script in which the frames are possibly disordered, and produces a sorted animation script as output. With the -f option, scriptsort also inserts a call to the shell script end_of_frame.sh after each frame of the animation.

    %> head -n 10 anim.script
    start 0;clean ;
    anim all.g/cone.r matrix lmul -0.268148 -0.841061 -0.469801 17.995962 0.889157 -0.403767 0.215341 -112.596747 -0.370805 -0.359984 0.856105 -3.321956 0.000000 0.000000 0.000000 1.000000 ;
    end;
    start 1;clean ;
    anim all.g/cone.r matrix lmul -0.261689 -0.843380 -0.469285 15.768705 0.890764 -0.398235 0.218973 -111.614872 -0.371563 -0.360719 0.855466 0.195392 0.000000 0.000000 0.000000 1.000000 ;
    end;
    start 2;clean ;
    anim all.g/cone.r matrix lmul -0.264301 -0.842990 -0.468521 13.970193 0.887275 -0.402938 0.224462 -110.642745 -0.378004 -0.356382 0.854462 4.031175 0.000000 0.000000 0.000000 1.000000 ;
    end;
    start 3;clean ;
    %> scriptsort -f < anim.script > anim_and_shell.script
    %> head -n 10 anim_and_shell.script
    start 0;clean ;
    anim all.g/cone.r matrix lmul -0.268148 -0.841061 -0.469801 17.995962 0.889157 -0.403767 0.215341 -112.596747 -0.370805 -0.359984 0.856105 -3.321956 0.000000 0.000000 0.000000 1.000000 ;
    end;
    !end_of_frame.sh 0
    start 1;clean ;
    anim all.g/cone.r matrix lmul -0.261689 -0.843380 -0.469285 15.768705 0.890764 -0.398235 0.218973 -111.614872 -0.371563 -0.360719 0.855466 0.195392 0.000000 0.000000 0.000000 1.000000 ;
    end;
    !end_of_frame.sh 1
    start 2;clean ;
    anim all.g/cone.r matrix lmul -0.264301 -0.842990 -0.468521 13.970193 0.887275 -0.402938 0.224462 -110.642745 -0.378004 -0.356382 0.854462 4.031175 0.000000 0.000000 0.000000 1.000000 ;

The next step is to create a shell script called end_of_frame.sh which does the work you want it to do. The following is a typical example:

end_of_frame.sh

    #!/bin/sh
    #END_OF_FRAME.SH - script to be called by rt after each frame is computed.
    #Usage:
    # end_of_frame.sh framenumber

    #IMPORTANT - User should put correct values here before calling this script
    BASE="foo.pix"
    TESTWIDTH="1440"
    TESTHEIGHT="972"
    #BASE is pathname of pix files, minus the index
    #TESTWIDTH and TESTHEIGHT are dimensions of pix files

    echo $0 $* START

    #special handling for frame 0
    if test $1 = 0
    then
    	FILE=$BASE
    else
    	FILE=${BASE}.$1
    fi

    if test ! -f "$FILE"
    then
    	echo $0 $FILE does not exist, aborting
    	exit 1
    fi

    eval `pixautosize -b 3 -f $FILE`	# Sets WIDTH, HEIGHT
    if test "$WIDTH" != "$TESTWIDTH" -o "$HEIGHT" != "$TESTHEIGHT"
    then
    	echo "File size was $WIDTH by $HEIGHT, should be $TESTWIDTH by $TESTHEIGHT ."
    	exit 1
    fi

    if test $1 = 0
    then
    	# Special case for frame 0.  Hard link, and use new name.
    	ln $FILE ${FILE}.0
    	FILE=${FILE}.0
    fi

    # Add NTSC gamma correction and reduce in size
    bwmod -d255 -r2.2 -m255 < $FILE | pixhalve -w $WIDTH > $FILE.sm

    # compress
    chmod 444 $FILE.sm
    gzip -9 $FILE.sm
    rm -f $FILE
    ln $FILE.sm.gz $FILE

    echo $0 $* END

Finally, do the high resolution ray-tracing.

    %> rt -M -w 1440 -n972 -V1440:972 -o foo.pix moss.g all.g 2>>foo.log < anim_and_shell.script &

For a more general discussion of animation ray-tracing considerations, see the appropriate section of Animation Techniques in BRL-CAD.

Index