| 1 | greg | 1.1 | /* Copyright (c) 1989 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | *  t_data.c - routine for stored textures | 
| 9 |  |  | * | 
| 10 |  |  | *     6/4/86 | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include  "ray.h" | 
| 14 |  |  |  | 
| 15 |  |  | #include  "data.h" | 
| 16 |  |  |  | 
| 17 |  |  | /* | 
| 18 |  |  | *      A stored texture is specified as follows: | 
| 19 |  |  | * | 
| 20 |  |  | *      modifier texdata name | 
| 21 |  |  | *      8+ xfunc yfunc zfunc xdfname ydfname zdfname vfname v0 v1 .. xf | 
| 22 |  |  | *      0 | 
| 23 |  |  | *      n A1 A2 .. An | 
| 24 |  |  | * | 
| 25 |  |  | *  Vfname 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 dfnames are the data file | 
| 28 |  |  | *  names.  The dimensions of the data files and the number | 
| 29 |  |  | *  of variables must match.  The funcs take three arguments to produce | 
| 30 |  |  | *  interpolated values from the file.  The xf is a transformation | 
| 31 |  |  | *  to get from the original coordinates to the current coordinates. | 
| 32 |  |  | */ | 
| 33 |  |  |  | 
| 34 |  |  | typedef struct { | 
| 35 |  |  | struct { | 
| 36 |  |  | double  sca;            /* scale factor */ | 
| 37 |  |  | double  xfm[4][4];      /* transformation matrix */ | 
| 38 |  |  | }  fore, back; | 
| 39 |  |  | }  XFORM; | 
| 40 |  |  |  | 
| 41 |  |  |  | 
| 42 |  |  | t_data(m, r)                    /* interpolate texture data */ | 
| 43 |  |  | register OBJREC  *m; | 
| 44 |  |  | RAY  *r; | 
| 45 |  |  | { | 
| 46 |  |  | extern double  varvalue(), funvalue(), datavalue(); | 
| 47 |  |  | extern int  errno; | 
| 48 |  |  | int  nv; | 
| 49 |  |  | FVECT  dval, disp; | 
| 50 |  |  | double  pt[MAXDIM]; | 
| 51 |  |  | DATARRAY  *dp; | 
| 52 |  |  | register XFORM  *mxf; | 
| 53 |  |  | register char  **sa; | 
| 54 |  |  | register int  i; | 
| 55 |  |  |  | 
| 56 |  |  | if (m->oargs.nsargs < 8) | 
| 57 |  |  | objerror(m, USER, "bad # arguments"); | 
| 58 |  |  | sa = m->oargs.sarg; | 
| 59 |  |  |  | 
| 60 | greg | 1.2 | for (i = 7; i < m->oargs.nsargs && sa[i][0] != '-'; i++) | 
| 61 |  |  | ; | 
| 62 |  |  | nv = i-7; | 
| 63 | greg | 1.1 | if ((mxf = (XFORM *)m->os) == NULL) { | 
| 64 |  |  | mxf = (XFORM *)malloc(sizeof(XFORM)); | 
| 65 |  |  | if (mxf == NULL) | 
| 66 |  |  | goto memerr; | 
| 67 |  |  | mxf->fore.sca = 1.0; | 
| 68 |  |  | setident4(mxf->fore.xfm); | 
| 69 |  |  | if (xf(mxf->fore.xfm, &mxf->fore.sca, | 
| 70 |  |  | m->oargs.nsargs-i, sa+i) != m->oargs.nsargs-i) | 
| 71 |  |  | objerror(m, USER, "bad transform"); | 
| 72 |  |  | if (mxf->fore.sca < 0.0) | 
| 73 |  |  | mxf->fore.sca = -mxf->fore.sca; | 
| 74 |  |  | mxf->back.sca = 1.0; | 
| 75 |  |  | setident4(mxf->back.xfm); | 
| 76 |  |  | invxf(mxf->back.xfm, &mxf->back.sca, | 
| 77 |  |  | m->oargs.nsargs-i, sa+i); | 
| 78 |  |  | if (mxf->back.sca < 0.0) | 
| 79 |  |  | mxf->back.sca = -mxf->back.sca; | 
| 80 |  |  | m->os = (char *)mxf; | 
| 81 |  |  | } | 
| 82 |  |  |  | 
| 83 |  |  | setmap(m, r, mxf->back.xfm, mxf->back.sca); | 
| 84 |  |  |  | 
| 85 |  |  | if (nv > MAXDIM) | 
| 86 |  |  | goto dimerr; | 
| 87 |  |  | if (!vardefined(sa[7])) | 
| 88 |  |  | loadfunc(sa[6]); | 
| 89 |  |  | errno = 0; | 
| 90 |  |  | for (i = 0; i < nv; i++) | 
| 91 |  |  | pt[i] = varvalue(sa[i+7]); | 
| 92 |  |  | if (errno) | 
| 93 |  |  | goto computerr; | 
| 94 |  |  | for (i = 0; i < 3; i++) { | 
| 95 |  |  | dp = getdata(sa[i+3]); | 
| 96 |  |  | if (dp->nd != nv) | 
| 97 |  |  | goto dimerr; | 
| 98 |  |  | dval[i] = datavalue(dp, pt); | 
| 99 |  |  | } | 
| 100 |  |  | errno = 0; | 
| 101 |  |  | for (i = 0; i < 3; i++) | 
| 102 |  |  | disp[i] = funvalue(sa[i], 3, dval); | 
| 103 |  |  | if (errno) | 
| 104 |  |  | goto computerr; | 
| 105 |  |  |  | 
| 106 |  |  | for (i = 0; i < 3; i++) | 
| 107 |  |  | r->pert[i] += ( disp[0]*mxf->fore.xfm[0][i] + | 
| 108 |  |  | disp[1]*mxf->fore.xfm[1][i] + | 
| 109 |  |  | disp[2]*mxf->fore.xfm[2][i] ) | 
| 110 |  |  | / mxf->fore.sca; | 
| 111 |  |  | return; | 
| 112 |  |  | dimerr: | 
| 113 |  |  | objerror(m, USER, "dimension error"); | 
| 114 |  |  | memerr: | 
| 115 |  |  | error(SYSTEM, "out of memory in t_data"); | 
| 116 |  |  | computerr: | 
| 117 |  |  | objerror(m, WARNING, "compute error"); | 
| 118 |  |  | return; | 
| 119 |  |  | } |