
                Developing External Effects
              ===============================


To develope external effects a 32 bit compiler is required. Watcom C++32
is recommended because it is used in the Renderer itself and is FAST
(17.5 % faster than Visual C++).
Visual C++ versions 2.0 4.0 and 5.0 have been tested and Watcom C++
versions 10.5, 10.6 and 11.0 should all work.

External effecst are implemented as Windows NT dynamic link libraries (DLL's)
they may be used in Windows if the Win32s is used.
That is, if Win32s has been installed.

Each effect will have 4 files associated with it's development,
                   name.C
                   name.H
                   name.RC
                   makefile
The compiled effect will have the name  "name.DLL"  this and the matching
.EFX (see below) are the only files that need be distributed.
They should be located in the EFFECTS subdirectory, but a separate directory
can be used if there are likely to be any conflicts in the name of effects.

Each effect also needs a file with name extension .EFX is needed
to accompany each of the .DLL library files. This is a dummy file and need
not contain any data. It is just a name holder to allow the selection dialog
to pick up the correct type of DLL's and not any other!

The makefile is used to build all the modules from the command line using
the NMAKE utility that comes with Microsoft C (if using Watcom's compiler
make sure NMAKE is still on the path). The nmake utility is called by:
      nmake efx=<name of effect>  <compilerID>=1

eg.     nmake efx=template vc=1     ( build template with Visual c)
        nmake efx=wave     wc=1     ( build wave effect with Watcom C)
        nmake efx=flap   cpu=alpha  ( build wave for DEC alpha processor)

The set of TEMPLATE.???  files will serve as a starting point for developing
effects. Essentially there are three functions located in the Effects DLL
that are called.

long _RenderExternalEffect(char *PrmList, sfxinfo *SFXinfo,
                                   vertex *Vlist);

void _PreviewExternalEffect(char *parameters, sfxinfo *SFXinfo,
                                    Svertex *v);

char * _SetExternalParameters(char *Op, HWND Hwnd, long ruler, char *name);


The function RenderExternalEffect( ... ) executes the action of the
effect. It is called from the Renderer after the model to which it is
applied is loaded but before the model is transformed by the viewing
transformation.
It should read the effects parameters from the parameter list
PrmList, use the additional data in the "sfxinfo" structure to move
the vertex coordinates (given in the vertex list Vlist) as dictated by
the algorithm.

The function PreviewExternalEffect( ... ) is called from the Animator to
render the real time preview of the effect, the function must be present
but it may do nothing. It may also give an approximation to the effect if
the calculation of the effect are likely to be time consuming.

The function SetExternalParameters( ... ) is called by the Animator to
give the user the opportunity to set/modify parameters for the effect.

Allocating and releasing memory passed back and forward to the caller
---------------------------------------------------------------------
Use the macros

   CALL_FREE( );
   CALL_MALLOC(  );

to allocate and free any memory that is passed to or returned to the
calling process. Any other memory used within the processes can be allocated
in the normal way.
These macros and their function pointers are picked up by including the
header files

#include "......memory.h"
#include "......memdef.h"

in the code. See the example shaders and effects. This is only needed in
the part of the code with the setup dialog.

Designing Effects
-----------------
The idea behind the effects is to move the vertices in a model according
to some algorithm rather than by user interaction. Systematic waves or
distortions are ideal candidates for implementation by external effect.

One important point that must be made is that the model being "effected"
must have enough vertices so that the effect will be visible. There is
no point in applying a "wave" to a square with 4 vertices and 2 faces it
will look very wierd! Basically the effects "length" should be 5 or 10
times the inter-vertex spacing in a model.

The vertex co-ordinates passed to the Effect procedures are floating point
values equivalent to the (long int) co-ordinates of the vertices
stored in the models SCE file. The effect is applied in this co-ordinate
frame of reference before the renderer moves the model to the offset and
orientation specified by the animator. Read the section "The Co-ordinate
system" in the Ximage readme.txt file.

Because the effect manifests itself over the time interval of the
channel(time-line) to which it is applied a variable "t" in the sfxinfo
structure (0.0->1.0) gives the proportion of the effect that should be
put to the model. The sfxinfo structure also provides a bounding box which,
for example, may be used to anchor the effect to the bottom of the model.
The last member of the sfxinfo structure "origin" specifies the origin
point of the model.

Note: It is very important to appreciate that the effect is applied
      BEFORE the model it is moved, rotated or scaled by the to
      animator settings. All vertex co-ordinates are exactly as used in
      the modeler.
      The vector "p" is a floating point co-ordinate triple (x,y,z)

                x = right/left  +ve to right
                Y = front/back  +ve to front
                Z = up/down     +ve up

Using the functions
-------------------
The parameters of the function SetExternalParameters are-:

  char *Op   /* string for the parameters                  */
  HWND hWnd  /* parent window                              */
  long ruler /* ruler scale value to facilitate scaling    */
  char *name /* name of DLL file with the effect           */

The first parameter *Op is a pointer to a char array. This array holds
the settings for the effect. When SetExternalParameters is called for
a new effect Op is NULL. SetExternalParameters usually calls up a dialog
box in which the user enters the control settings. If Op is not NULL
then the dialog box controls should be filled with values read from the
string pointed to by Op. Use the function sscanf(Op," ", ...); to do this.
The dialog box should have buttons for Ok and Cancel and if Cancel is
clicked then the function should return the value of Op passed to it.
If Ok is clicked the function should free the memory pointed to by Op
allocate enough memory to hold the new settings write to it using sprintf()
and return a pointer to this new block of memory. See twist.c as an example.
Note the first item written to this string must be the name of the
DLL given by the functions third parameter *name.
The second parameter is the handle of the parent window, use this in
calls to the DialogBox function.

The parameters of the function RenderExternalEffect() are;

 char *PrmList,     /* The control settings string, a pointer to a string
                       holding the information in the retured string from
                       SetExternalParameters. */
 sfxinfo *SFXinfo,  /* Pointer to sfxinfo structure, the sfxinfo structure
                       holds information giving time, and bound information
                       for the application of the effect. */
 vertex *Vlist      /* Pointer to the list of vertices of the model. This
                       structure is defined in PSTRUCT.H It is the task of
                       the animation effect to change the structure member
                       "p" (vertex co-ordinates) according to the algorithm
                       of the effect subject to the data in *SFXinfo and
                       settings from *PrmList.
                       Consult TWIST.C for example. */

The parameters for the function PreviewExternalEffect() are;

 char *PrmList,      /* Same as PrmList above */
 sfxinfo *SFXinfo,   /* Same as SFXinfo ablve */
 Svertex *Vlist      /* Svertex is defined as an array of 3 floats, it is
                        the same as a vector in this context, but is used
                        differently in the animator so it must be defined
                        separately. */

The actions of PreviewExternalEffect ideally are exactly the same as
RenderExternalEffect but whereas RenderExternalEffect is called from the
renderer PreviewExternalEffect is called from the Animator.
PreviewExternalEffect must be fast it is used in the realtime camera window
so it the effect is computationally intensive PrevewExternalEffect should
implement a simplified version of the Effect. In the limit it can return
without doing anything.

Structures
----------
The PC (80x86 processor family) renderer uses float data types throughout
so the best way to have compatibility across platforms is to include the
definition below and make all real number data types "double"

#define double float

typedef double vector[3]; /* a vector is defined as a floating point triple */

typedef float Svertex[3]; /* structure for preview vertex positions         */
                          /* exactly equivalent to the vector type defined  */
                          /* above for PC systems                           */

typedef struct VERTEX { /* structure for each vertex                        */
 vector p;              /* real position  3D space                          */
 double x,y;            /* screen location pixel space - NOT used in effect */
} vertex;

typedef struct {
 long nvert;            /* number of vertices in the model                  */
 long ltime;            /* integer equivalent of member "time",used in DOS  */
                        /* program not needed for Windows version           */
 long vmin[3];          /* array with minimum x,y,z co-ordinate             */
 long vmax[3];          /* array with maximum x,y,z co-ordinate             */
                        /* vmin and vmax define a bounding box that         */
                        /* contains the model                               */
 long origin[3];        /* co-ordinates of origin point of model. This is   */
                        /* the location of the Yellow Cross in the Modeler  */
 double time;           /* proportion of elapsed time on actor channel      */
} sfxinfo;


