| 1 |
greg |
1.1 |
/* Copyright (c) 1986 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* t_func.c - routine for procedural textures. |
| 9 |
|
|
* |
| 10 |
|
|
* 4/8/86 |
| 11 |
|
|
*/ |
| 12 |
|
|
|
| 13 |
|
|
#include "ray.h" |
| 14 |
|
|
|
| 15 |
|
|
/* |
| 16 |
|
|
* A procedural texture perturbs the surface normal |
| 17 |
|
|
* at the point of intersection with an object. It has |
| 18 |
|
|
* the form: |
| 19 |
|
|
* |
| 20 |
|
|
* modifier texfunc name |
| 21 |
|
|
* 4+ xvarname yvarname zvarname filename xf |
| 22 |
|
|
* 0 |
| 23 |
|
|
* n A1 A2 .. |
| 24 |
|
|
* |
| 25 |
|
|
* Filename is the name of the file where the variable definitions |
| 26 |
|
|
* can be found. The list of real arguments can be accessed by |
| 27 |
|
|
* definitions in the file. The xf is a transformation to get |
| 28 |
|
|
* from the original coordinates to the current coordinates. |
| 29 |
|
|
*/ |
| 30 |
|
|
|
| 31 |
|
|
typedef struct { |
| 32 |
|
|
struct { |
| 33 |
|
|
double sca; /* scale factor */ |
| 34 |
|
|
double xfm[4][4]; /* transformation matrix */ |
| 35 |
|
|
} fore, back; |
| 36 |
|
|
} XFORM; |
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
t_func(m, r) /* compute texture for ray */ |
| 40 |
|
|
register OBJREC *m; |
| 41 |
|
|
register RAY *r; |
| 42 |
|
|
{ |
| 43 |
|
|
extern double varvalue(); |
| 44 |
|
|
extern int errno; |
| 45 |
|
|
FVECT disp; |
| 46 |
greg |
1.2 |
register XFORM *mxf; |
| 47 |
greg |
1.1 |
register int i; |
| 48 |
|
|
register char **sa; |
| 49 |
|
|
|
| 50 |
|
|
if (m->oargs.nsargs < 4) |
| 51 |
|
|
objerror(m, USER, "bad # arguments"); |
| 52 |
|
|
sa = m->oargs.sarg; |
| 53 |
|
|
|
| 54 |
greg |
1.2 |
if ((mxf = (XFORM *)m->os) == NULL) { |
| 55 |
greg |
1.1 |
mxf = (XFORM *)malloc(sizeof(XFORM)); |
| 56 |
|
|
if (mxf == NULL) |
| 57 |
|
|
goto memerr; |
| 58 |
|
|
mxf->fore.sca = 1.0; |
| 59 |
|
|
setident4(mxf->fore.xfm); |
| 60 |
|
|
if (xf(mxf->fore.xfm, &mxf->fore.sca, |
| 61 |
|
|
m->oargs.nsargs-4, sa+4) != m->oargs.nsargs-4) |
| 62 |
|
|
objerror(m, USER, "bad transform"); |
| 63 |
|
|
if (mxf->fore.sca < 0.0) |
| 64 |
|
|
mxf->fore.sca = -mxf->fore.sca; |
| 65 |
|
|
mxf->back.sca = 1.0; |
| 66 |
|
|
setident4(mxf->back.xfm); |
| 67 |
|
|
invxf(mxf->back.xfm, &mxf->back.sca, |
| 68 |
|
|
m->oargs.nsargs-4, sa+4); |
| 69 |
|
|
if (mxf->back.sca < 0.0) |
| 70 |
|
|
mxf->back.sca = -mxf->back.sca; |
| 71 |
greg |
1.2 |
m->os = (char *)mxf; |
| 72 |
greg |
1.1 |
} |
| 73 |
|
|
|
| 74 |
|
|
setmap(m, r, mxf->back.sca, mxf->back.xfm); |
| 75 |
|
|
|
| 76 |
|
|
if (!vardefined(sa[0])) |
| 77 |
|
|
loadfunc(sa[3]); |
| 78 |
|
|
errno = 0; |
| 79 |
|
|
for (i = 0; i < 3; i++) |
| 80 |
|
|
disp[i] = varvalue(sa[i]); |
| 81 |
|
|
if (errno) { |
| 82 |
|
|
objerror(m, WARNING, "compute error"); |
| 83 |
|
|
return; |
| 84 |
|
|
} |
| 85 |
|
|
if (mxf->fore.xfm == NULL) |
| 86 |
|
|
for (i = 0; i < 3; i++) |
| 87 |
|
|
r->pert[i] += disp[i]; |
| 88 |
|
|
else |
| 89 |
|
|
for (i = 0; i < 3; i++) |
| 90 |
|
|
r->pert[i] += ( disp[0]*mxf->fore.xfm[0][i] + |
| 91 |
|
|
disp[1]*mxf->fore.xfm[1][i] + |
| 92 |
|
|
disp[2]*mxf->fore.xfm[2][i] ) |
| 93 |
|
|
/ mxf->fore.sca; |
| 94 |
|
|
return; |
| 95 |
|
|
memerr: |
| 96 |
|
|
error(SYSTEM, "out of memory in t_func"); |
| 97 |
|
|
} |