[SESI logo]

Prisms Development Toolkit Documentation

Side Effects Software Inc. 1997

Custom SOPs

Example 2: A Wave SOP

In which we take a single geometry input, in this case a grid in the XZ plane, and modify the y values depending on a sin wave pattern. You could use this to do simple water by cascading several of these SOPs in a row, each taking as input the modified output of the previous Wave SOP.

Note that we don't actually check the input to see if it is a grid, or what plane it lies in. Also note that the offset value is initialized to $F/10, which means that it automatically animates.


#include <custom.h>

static char	*sopHelp[] =
{
    "This SOP takes a grid in the XZ plane and modulates\
    the Y values according to a sin wave",
    " ",
    "Options:",
    " center 	- the source of the waves",
    " height 	- the max height of the waves",
    " frequency	- wave frequency",
    " offset 	- wave offset (use for animating waves)",
	0
};

#define		NSOURCES	1	/* Takes One Source */

typedef struct sop_data 
{
    SOP_EXPR	center[3];
    SOP_EXPR	amp;			/* amplitude (height) */
    SOP_EXPR	freq;			/* frequency */
    SOP_EXPR	off;			/* offset */
} SOP_DATA;

static SOP_PARMS	sopParms[] =
{
    { SOPPARM_TRIPLE,	"Center",	NULL , {NULL} },
    { SOPPARM_TRIPLE,	"Center",	NULL , {NULL} },
    { SOPPARM_TRIPLE,	"Center",	NULL , {NULL} },
    { SOPPARM_EXPR,	"Height",	NULL , {NULL} },
    { SOPPARM_EXPR,	"Frequency",	NULL , {NULL} },
    { SOPPARM_EXPR,	"Offset",	NULL , {NULL} },
    { NULL },
};

static void
sopDefaults(SOP *sop)
{
    SOP_DATA	*sb;

    if (sop->special) free(sop->special);
    sb = (SOP_DATA *)Calloc(sizeof(SOP_DATA), "Project sopDefaults");
    sop->special = (char *)sb;

    sop->nsources = NSOURCES;
    sop->sopin[0].type = SOURCE_PREV;

    InitSopExpr(&sb->center[0], "0");
    InitSopExpr(&sb->center[1], "0");
    InitSopExpr(&sb->center[2], "0");
    InitSopExpr(&sb->amp, "1");
    InitSopExpr(&sb->freq, "1");
    InitSopExpr(&sb->off, "$F/10");	/* animate with frame */
}

static void
sopScan(SOP *sop, int argc, char *argv[])
{
    SOP_DATA	*sb;
    sb = (SOP_DATA *) sop->special;	

    SopScanTriple('c', sb->center, sop);
    SopScanExpr  ('h', &sb->amp,   sop);
    SopScanExpr  ('F', &sb->freq,  sop);
    SopScanExpr  ('O', &sb->off,   sop);
}

static void
sopPrint(FILE *fp, SOP *sop)
{
    SOP_DATA	*sb;
    sb = (SOP_DATA *)sop->special;

    SopPrintTriple(fp, sb->center, "0", "0", "0", "-c");
    SopPrintExpr(fp, &sb->amp, "1", "-h");
    SopPrintExpr(fp, &sb->freq, "1", "-F");
    SopPrintExpr(fp, &sb->off, "$F/10", "-O");
}

static void
sopCook(SOP *sop)
{
    SOP_DATA	*sb;
    PolyDetail	*sourcePdp;
    PolyPointPointer	points, tmppt;
    int		npts;
    float	amp, freq, off, d;
    Vector	dist, cent;

    sb = (SOP_DATA *)sop->special;

    if (sop->pdp) FreePolyDetail(sop->pdp);
    sop->pdp = 0;

    /* read in the input geometry */
    GETSOURCE(sourcePdp, sop, 0, "Invalid source SOP");

    /* Make a copy of the source pdp, don't modify the source */
    sop->pdp = PolyCopyDetail(sourcePdp);

    /* evaluate the slider values */
    EVALUATE(sop, sb->center[0]);
    EVALUATE(sop, sb->center[1]);
    EVALUATE(sop, sb->center[2]);
    EVALUATE(sop, sb->amp);
    EVALUATE(sop, sb->freq);
    EVALUATE(sop, sb->off);

    /* and collect those values into local variables
	(just for simplicity) */
    amp = sb->amp.value.fval;
    freq = sb->freq.value.fval;
    off = sb->off.value.fval;

    cent.x = sb->center[0].value.fval;
    cent.y = sb->center[1].value.fval;
    cent.z = sb->center[2].value.fval;

    FOR_ALL_POINTS(sop->pdp->ppt, tmppt, npts)
    {
	/* first compute 3D distance from point to center,
	    using Prisms vector macro */
	VSUB(tmppt->pos, cent, dist);
	d = sqrt(dist.x*dist.x + dist.y*dist.y + dist.z*dist.z);

	/* computer height as function of distance */
	tmppt->pos.y += sin(d*freq+off)*amp;
    }

    CLEARSOPNML(sop)
}

static void
sopClean(SOP *sop)		
{
    FreeSopExpr((SOP_EXPR *)sop->special, sopParms);
    free(sop->special);
    sop->special = NULL;
}

/*
 * Here are the labels that are to be used for the construction of the
 * source SOP gadgets.
 */
static char	*srclabels[] =
{
    "Source"
};

GETSOPPARMS

static void
sopGPMake(UI_DBOX *dbp, SOP *sop)
{
    InitSopParms(sop);
    BuildCommonSopGadgets(sop, NSOURCES, sopParms, 
			sopHelp, dbp, srclabels);
}

#define SOPUSAGE	\
"swave [-c x y z] [-h height] [-F frequency] [-O offset] \
\n"

SOP_FUNC	WaveSopRef = {
	"wave",
	"c:::h:F:O:",
	sopScan, sopPrint, sopCook, sopDefaults, sopClean, sopGPMake,
	sopGetParms,
	SOPUSAGE,
};


Tookit Overview
Expression Functions | User Functions | Custom SOPs
Prisms Internal Format | Programmer's Reference
Compiling SOPs | Using sopbuild | SOP Routine Reference
Dynamic SOPs | Prisms Types | Macros
Math Routines | Data Routines | Polygon Routines | Winged-edge Routines

Copyright © 1997 Side Effects Software Inc.
477 Richmond Street West, Toronto, Ontario, Canada M5V 3E7