
                        Developing External Shaders
                     =================================


To develope external shaders 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++ and 20% faster that Symantec C++).
Visual C++ (32 bit) and Symantec C++ should be able to be used but both
have some problems.

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

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

Each shader also needs a file with name extension .SFX 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
shaders. Essentially there are number of functions located in the Shader DLL
that are called by the Renderer and Modeler.

                      SetExternalParameters(...)

                      ExternalTextureStartup(...)
                      ExternalTextureMorph(...)
                      ExternalTextureClose(...)

                      ExternalTextureProcedure(...)


SetExternalParameters() is called from the Modeler and/or Shader Previewer
to give user access to parameters that control the Shaders appearance.
This will usually include a Dialog callback function. It also allocates and
writes all the parameters needed by the shader and passed them back to
the Modeler to be saved in the SCE file. It should also read parameters
passed in by the Modeler if they exist.


ExternalTextureStartup(), ExternalTextureMorph() and ExternalTextureClose()
are called by the Renderer once per frame as shaders are called for.
These functions should initialize local variables and read the parameters
from the text string which was built by the SetExternalParameters()
function.

ExternalTextureProcedure() is called every time the Renderer needs a color
value to fill a pixel in the image. It is therefore the most computationally
intensive part of the shader. Parameters passed to this function include
a co-ordinate system relative to the shader axis in use for the visible face
and model model. A special structure X__SHADER contains pointers to
internal subroutines that can be called to perform relevant shader functions.
The details of this structure are in two include files RSTRUCT.H and
DEFINES.H, defines.h sets up symbols that allows conventional type
function names to be used for the callback pointers in the X__SHADER
structure.

The build process can be executed by running the BAT file associated with
the shader. 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 modeler or previewer, the other functions in a shader are
called from the renderer.

If possible a Dialog Editor will make designing the dialog a LOT easier.
Micorsoft's DlgEditor (from the Win32 developers kit) is the best I have
seen and easiest to use but it only works on Windows NT. The Symatec
resource toolkit is quite good and adequate. Visual C++ (16 bit) has probably
got a Dialog Editor that can produce a DLG file which Watcom's resource
compiler can handle. (A bit of text editing may be required eg. remove
LANGUAGE statements).

Additional example is provided for a very simple DOTS shader in the
files V_DOTS.C V_DOTS.RC V_DOTS.H and V_DOTS.BAT and a shader V_BOZO that
shows how to call the Renderer's noise generating functions.

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.


Calling functions from the shader
-----------------------------------
A number of Renderer functions can be called from with in an external
shader. The functions that can be called are "noise" generating functions
useful in creating fractal textures, see the example V_BOZO. These
functions cannot be invoked simply by calling them, instead their addresses
are passed to the Shader through a special structure that stores pointers
to the functions.
These functions are invoked buy what looks like a normal
C function call. These "calls" are macros, the address of the function
is passed to the library as part of the X__SHADER structure.
For ease of use and compatibility with C function calls appropriate
macros are defined in the file DEFINES.H which should be included in
the shader code. The X__SHADER structure is defined in the file
RSTRUCT.H. Look at the V_BOZO.C shader for an example of the use of this
technique.


Compiler Notes
--------------
Symantec 6.1  does not support floating point %f in sprintf() nor atoi()
in DLL's so is effectively useless.

Watcom 9.5 does not allow custom classes in dialog boxes so no spin
buttons (WRC bug ???). Watcom 10.0 has all problems fixed)

Visual C++ does not appear to work well with Watcom C++  it generates a GP
fault when the DLL detaches. No problem is evident if the renderer is
built with Visual C++ but then the speed drops.

So it looks like Watcom C++ is the only practical compiler for the PC.
Microsoft's Win32 SDK is the only compiler available for MIPS development,
no problems with external DLL's have been reported on MIPS system.







