
                      Developing Image Processors
                    ===============================

Note: Image processors have many similarities to External Shaders, it would
      be a good idea to consult the Shaders development information as well.

Watcom C++ is the recommended compiler for PC systems, CL from the Win32
development kit is essential for MIPS processor development.

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

Each processor will have 4 files associated with it's development,
                   name.C
                   name.H
                   name.RC
                   name.BAT
The compiled processor will have the name "name.DLL"  this is the only
file that need be distributed. It should be located in
the XIMAGE subdirectory, but a separate directory can be used if there
are likely to be any conflicts in the name.

Each image processor also needs a file with name extension .XFX 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 set of TEMPLATE.???  files will serve as a starting point for developing
processors. Essentially there are number of functions located in the DLL
that are called from the Animator.

                      SetExternalParameters(...)

                      RenderImageProcess(...)

SetExternalParameters() is called from the Animator
to give user access to parameters that control the actions of the processor.
This will usually include a Dialog callback function. It also allocates and
writes all the parameters needed by the processor and passed them back to
the Animator to be saved in the STG file. It should also read parameters
passed in by the Animator and set the Dialog controls appropriately.


RenderImageProcess() is called after the image has been renderered into the
screen buffer.

NOTE: It is vital that the user has swithced on the Full Screen Buffer for
      the image post processors to do their job. Some processors may also
      need the Z buffer to be on as well. These use up quite a bit of
      additional memory so they default to off.

2 parameters are passed to this function, the Parameter list and a pointer
to an XIMAGE structure. The XIMAGE structure is defined in the file XIMAGE.H
and it contains pointer to vital information for the post processor.
The file STRUCT.H defines the data structures used by the renderer and
applicable to the image processors.
XIMAGE points to the Screen Buffer, Z buffer, Viewing Transformation,
and Lights data structure.

The build process can be executed by running the BAT file associated with
the processor. It requires a parameter to identify the compiler to be used
(At present ONLY Watcom C++ gives acceptable results). To execute it type;

           name WC

The users dialog is defined in the Resource Script file name.RC dialog item
identifies should be placed in the header file name.H.  This dialog is
called from the animator, the other function is called by the renderer.

Consult the Examples provided to see the use of the data structures.

Central to implementing postprocessor is understanding the data structures
in STRUCT.H and XIMAGE.H and the Animator co-ordinate system.

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.


The Co-ordinate system
----------------------
A long integer co-ordinate system (World co-ords) is used. This
is scaled to a floating point (User co-ords) system for use in dialogs and
the co-ords window. The user has
the option of setting the relationship between the User co-ords and the
World co-ords. The scale factor between the two systems in the variable
"ruler" passed as a parameter to the  SetExternalParameters() function.
Any distances set by the user in dialog boxes should be made in the
floating user scale. to convert from User units to World units use the
formule;

      d_world = (long)(d_user * (double)ruler);

to convert from World units to User units use the formulae;

      d_user = (double)d_world / (double)ruler;

(See the examples STARS or FOG for this conversion in action.

Animations are set up and Models are built in Internal units with a
co-ordinate system in which  X is +ve to the right Y is +ve in the forward
direction and Z is +ve in the up direction. Animation take place in this
integer world where the farthest anything can be is +/-2**31. The actors
and camera are positioned in this absolute frame of reference. As each frame
is renderered a transformation is derived that move the camera to (0,0,0)
looking in the direction (0,1,0) with up being (0,0,1).
A 4x4 transformation matrix is calculated that when applied to the vertices in a
model or light will reposition them according to this frame of reference.
This ammounts to a matrix multiplication  X' = T*X where X is the original
point in a model and X' is the new point. Where this point will be on
the screen is determined by the Aspect Ration / Lens length of the camera.
The screen co-ordinates are determined by code such as-;

   /* given point (xp,yp,zp), transform to  (x,y,z) */

   TransformIntoView(lpXimage->ViewTransform,xp,yp,zp,&x,&y,&z);

   */ if(y > 1.0) then point is in front of the Camera */

   if(y >= 1.000){  /* all points in front of viewer */

    /* scale to appear in buffer (image size is  Xmax by Ymax  */

     xx=(double)lpXimage->Xmax/2.0 + (lpXimage->Xscale*x/y);
     yy=(double)lpXimage->Ymax/2.0 - (lpXimage->Yscale*z/y);

     /* xx and yy are integer co--ordinates to pixels in the output */
     /* image.                                                      */

     if((long)xx >= 0 && (long)xx < lpXimage->Xmax &&
        (long)yy >= 0 && (long)yy < lpXimage->Ymax){

      /* this point is visible on screen, it falls into image area  */


      /* DrawPointInBuffer( lpXimage->Screen, ...

      }

   }

Note the use of the lpXimage-> pointer to access the information stored
in the XIMAGE struxture.

This brief few lines of code is all that is needed to bring stars, particles
light halos for other pixel based effects.

Data Stuctures
--------------
The most important data structure is the XIMAGE data structure;

typedef struct tagXIMAGE {
  double ViewTransform[4][4];  /* viewing transformation matrix            */
  double Xscale,Yscale;        /* scaling values for camera field of view  */
  long Frame;                  /* frame being rendered                     */
  long Xmax,Ymax;              /* size of the image                        */
  long Nlights;                /* number of lights present                 */
  light *Lights;               /* pointer to array of light structures     */
  long Ntargets;               /* not used yet                             */
  vector *Targets;             /* not used yet                             */
  fullscreenbuffer *Screen;    /* pointer to screen buffer                 */
  double *Zbuffer;             /* pointer to Z depth buffer NULL if absent */
} XIMAGE;

The ViewTransform, Xscale, Yscale, Xmax and Ymax variables have been used
in the example above. The  *Screen pointer points to an array of Xmax * Ymax
fullscreenbuffer structures,

typedef struct SCREENBUFFER {
 unsigned char A,  /* Alpha channel (not available yet) */
               R,  /* red value  range - 255            */
               G,  /* green value  range - 255          */
               B;  /* blue value  range - 255           */
} fullscreenbuffer;


For example to set a pixel at screen location (x,y) to bright blue use,

 (Screen + y*Xmax + x)->R=0;
 (Screen + y*Xmax + x)->G=0;
 (Screen + y*Xmax + x)->B=255;
 (Screen + y*Xmax + x)->A=0;

or equivalent! Note x +ve to right y +ve down.


The information about lights is given in the Light structure. The *Lights
pointer in the XIMAGE data structure gives the locations of an array
of "light" structures. The light structure is,

typedef struct {
        vector p,  /* p is the position of the light, it has been     */
                   /*   transformed for the camera position and       */
                   /*    direction and may be used as is!             */
               d;  /* d is normalized dirn. for spots                 */
        double dot1,                  /* not useful for image process */
               falloff,
               dPhi,
               dTheta,
               dAlpha,
               cone1,
               cone2,
               dot2,
               internal_size,
               scale;
        short type;
        float *shadow_buffer;
        double shadow_transform[4][4];
        unsigned char color[3],internal_type,*object_buffer;
        unsigned short *face_buffer;
        double Ire,Igr,Ibl;          /* normalized light intensity   */
} light;


Of the this only the vector p is of much use for post-processing. (Note a
vector data type is defined in STRUCT.H and is equivalent to a an array
of 3 floats). P is co-ordinate of the light AFTER it has been transformed
to the Camera co-ordinate system. This means that light coordinates do NOT
have to be TransformedIntoView, to get their Screen co-ordinates.

For example if there are 2 lights the screen pixel address (u,v) for the
second light is given by;

   x=lpXimage->Lights[1].p[0];
   y=lpXimage->Lights[1].p[1];
   z=lpXimage->Lights[1].p[2];
   if(y >= 1.000){
     u=lpXimage->Xmax/2 +
       (long)(lpXimage->Xscale*x/y);
     v=lpXimage->Ymax/2 -
       (long)(lpXimage->Yscale*z/y);
   }


Z buffer
--------
If the user has set the flag for a Z buffer then the XIMAGE structure
pointer "Zbuffer" will point to an arrary of "float" variables. The size
of the array is Xmax * Ymax, that is one real number for each pixel on the
screen. The order is conventional (same as screen buffer)  first
element is pixel (0,0) top,left, next element is (1,0), order proceeds
by row then column,

       0,1,2,3,            ....  ,Xmax-1,
       Xmax, Xmax+1,       ....  ,2*Xmax-1,
       2*Xmax,             ....
       .
       .
       (Ymax-1)*Xmax,      ....  ,(Ymax-1)*(Xmax-1)

The floating point value at screen coordinate (u,v) is the depth away from
the Camera (located at 0,0 in World co-ordinates) of surface visible in
that pixel, if nothing is visible in that pixel (except the Sky) the value
is set to the constant FARAWAY which is defined in UTILS.H

The Z buffer can be used to put the post process rendering "into" the scene,
see the FOG.C program as an example.


Look at the examples in the order.

                            FILTER
                            STARS
                            HALO
                            FOG

END OF DOCUMENT
