| 1 |
greg |
1.1 |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* Routines for mirror material supporting virtual light sources |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include "ray.h" |
| 12 |
|
|
|
| 13 |
|
|
#include "otypes.h" |
| 14 |
|
|
|
| 15 |
|
|
#include "source.h" |
| 16 |
|
|
|
| 17 |
|
|
/* |
| 18 |
|
|
* The real arguments for MAT_MIRROR are simply: |
| 19 |
|
|
* |
| 20 |
|
|
* 3 rrefl grefl brefl |
| 21 |
|
|
* |
| 22 |
|
|
* Additionally, the user may specify a single string argument |
| 23 |
|
|
* which is interpreted as the name of the material to use |
| 24 |
|
|
* instead of the mirror if the ray being considered is not |
| 25 |
|
|
* part of the direct calculation. |
| 26 |
|
|
*/ |
| 27 |
|
|
|
| 28 |
|
|
|
| 29 |
|
|
int mir_proj(); |
| 30 |
|
|
VSMATERIAL mirror_vs = {mir_proj, 1}; |
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
|
|
m_mirror(m, r) /* shade mirrored ray */ |
| 34 |
|
|
register OBJREC *m; |
| 35 |
|
|
register RAY *r; |
| 36 |
|
|
{ |
| 37 |
|
|
COLOR mcolor; |
| 38 |
|
|
RAY nr; |
| 39 |
|
|
register int i; |
| 40 |
|
|
/* check arguments */ |
| 41 |
|
|
if (m->oargs.nfargs != 3 || m->oargs.nsargs > 1) |
| 42 |
|
|
objerror(m, USER, "bad number of arguments"); |
| 43 |
|
|
/* check if source ray */ |
| 44 |
|
|
if (r->rsrc >= 0) { /* aiming for somebody */ |
| 45 |
|
|
if (source[r->rsrc].so != r->ro) |
| 46 |
|
|
return; /* but not us */ |
| 47 |
|
|
} else if (m->oargs.nsargs > 0) { /* else call substitute? */ |
| 48 |
|
|
rayshade(r, modifier(m->oargs.sarg[0])); |
| 49 |
|
|
return; |
| 50 |
|
|
} |
| 51 |
|
|
if (r->rod < 0.) /* back is black */ |
| 52 |
|
|
return; |
| 53 |
|
|
/* get modifiers */ |
| 54 |
|
|
raytexture(r, m->omod); |
| 55 |
|
|
/* assign material color */ |
| 56 |
|
|
setcolor(mcolor, m->oargs.farg[0], |
| 57 |
|
|
m->oargs.farg[1], |
| 58 |
|
|
m->oargs.farg[2]); |
| 59 |
|
|
multcolor(mcolor, r->pcol); |
| 60 |
|
|
/* compute reflected ray */ |
| 61 |
|
|
if (r->rsrc >= 0) { /* relayed light source */ |
| 62 |
|
|
rayorigin(&nr, r, REFLECTED, 1.); |
| 63 |
|
|
/* ignore textures */ |
| 64 |
|
|
for (i = 0; i < 3; i++) |
| 65 |
|
|
nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 66 |
|
|
/* source we're aiming for next */ |
| 67 |
greg |
1.2 |
nr.rsrc = source[r->rsrc].sa.sv.sn; |
| 68 |
greg |
1.1 |
} else { /* ordinary reflection */ |
| 69 |
|
|
FVECT pnorm; |
| 70 |
|
|
double pdot; |
| 71 |
|
|
|
| 72 |
|
|
if (rayorigin(&nr, r, REFLECTED, bright(mcolor)) < 0) |
| 73 |
|
|
return; |
| 74 |
|
|
pdot = raynormal(pnorm, r); /* use textures */ |
| 75 |
|
|
for (i = 0; i < 3; i++) |
| 76 |
|
|
nr.rdir[i] = r->rdir[i] + 2.*pdot*pnorm[i]; |
| 77 |
|
|
} |
| 78 |
|
|
rayvalue(&nr); |
| 79 |
|
|
multcolor(nr.rcol, mcolor); |
| 80 |
|
|
addcolor(r->rcol, nr.rcol); |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
|
| 84 |
|
|
mir_proj(pm, o, s, n) /* compute a mirror's projection */ |
| 85 |
|
|
MAT4 pm; |
| 86 |
|
|
register OBJREC *o; |
| 87 |
greg |
1.2 |
SRCREC *s; |
| 88 |
greg |
1.1 |
int n; |
| 89 |
|
|
{ |
| 90 |
|
|
FVECT nv; |
| 91 |
|
|
double od; |
| 92 |
|
|
/* get surface normal and offset */ |
| 93 |
greg |
1.2 |
od = getplaneq(nv, o); |
| 94 |
greg |
1.1 |
/* check for behind */ |
| 95 |
|
|
if (DOT(s->sloc, nv) <= (s->sflags & SDISTANT ? FTINY : od+FTINY)) |
| 96 |
|
|
return(0); |
| 97 |
|
|
/* everything OK -- compute projection */ |
| 98 |
|
|
mirrorproj(pm, nv, od); |
| 99 |
|
|
return(1); |
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
|
|
|
| 103 |
|
|
mirrorproj(m, nv, offs) /* get mirror projection for surface */ |
| 104 |
|
|
register MAT4 m; |
| 105 |
|
|
FVECT nv; |
| 106 |
|
|
double offs; |
| 107 |
|
|
{ |
| 108 |
|
|
register int i, j; |
| 109 |
|
|
/* assign matrix */ |
| 110 |
|
|
setident4(m); |
| 111 |
greg |
1.2 |
for (j = 0; j < 3; j++) { |
| 112 |
|
|
for (i = 0; i < 3; i++) |
| 113 |
greg |
1.1 |
m[i][j] -= 2.*nv[i]*nv[j]; |
| 114 |
|
|
m[3][j] = 2.*offs*nv[j]; |
| 115 |
greg |
1.2 |
} |
| 116 |
greg |
1.1 |
} |