| 1 |
gwlarson |
2.7 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
greg |
1.1 |
|
| 3 |
|
|
#ifndef lint |
| 4 |
gwlarson |
2.7 |
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
greg |
1.1 |
#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 |
greg |
2.6 |
int rpure = 1; |
| 40 |
greg |
1.1 |
register int i; |
| 41 |
|
|
/* check arguments */ |
| 42 |
|
|
if (m->oargs.nfargs != 3 || m->oargs.nsargs > 1) |
| 43 |
|
|
objerror(m, USER, "bad number of arguments"); |
| 44 |
greg |
1.3 |
/* check for substitute material */ |
| 45 |
|
|
if (m->oargs.nsargs > 0 && |
| 46 |
|
|
(r->rsrc < 0 || source[r->rsrc].so != r->ro)) { |
| 47 |
greg |
2.4 |
if (!strcmp(m->oargs.sarg[0], VOIDID)) { |
| 48 |
|
|
raytrans(r); |
| 49 |
|
|
return(1); |
| 50 |
|
|
} |
| 51 |
gwlarson |
2.7 |
return(rayshade(r, lastmod(objndx(m), m->oargs.sarg[0]))); |
| 52 |
greg |
1.1 |
} |
| 53 |
greg |
1.3 |
/* check for bad source ray */ |
| 54 |
|
|
if (r->rsrc >= 0 && source[r->rsrc].so != r->ro) |
| 55 |
greg |
2.3 |
return(1); |
| 56 |
greg |
1.3 |
|
| 57 |
greg |
1.1 |
if (r->rod < 0.) /* back is black */ |
| 58 |
greg |
2.3 |
return(1); |
| 59 |
greg |
1.1 |
/* get modifiers */ |
| 60 |
|
|
raytexture(r, m->omod); |
| 61 |
|
|
/* assign material color */ |
| 62 |
|
|
setcolor(mcolor, m->oargs.farg[0], |
| 63 |
|
|
m->oargs.farg[1], |
| 64 |
|
|
m->oargs.farg[2]); |
| 65 |
|
|
multcolor(mcolor, r->pcol); |
| 66 |
|
|
/* compute reflected ray */ |
| 67 |
|
|
if (r->rsrc >= 0) { /* relayed light source */ |
| 68 |
|
|
rayorigin(&nr, r, REFLECTED, 1.); |
| 69 |
|
|
/* ignore textures */ |
| 70 |
|
|
for (i = 0; i < 3; i++) |
| 71 |
|
|
nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 72 |
|
|
/* source we're aiming for next */ |
| 73 |
greg |
1.2 |
nr.rsrc = source[r->rsrc].sa.sv.sn; |
| 74 |
greg |
1.1 |
} else { /* ordinary reflection */ |
| 75 |
|
|
FVECT pnorm; |
| 76 |
|
|
double pdot; |
| 77 |
|
|
|
| 78 |
|
|
if (rayorigin(&nr, r, REFLECTED, bright(mcolor)) < 0) |
| 79 |
greg |
2.3 |
return(1); |
| 80 |
greg |
2.6 |
if (DOT(r->pert,r->pert) > FTINY*FTINY) { |
| 81 |
|
|
pdot = raynormal(pnorm, r); /* use textures */ |
| 82 |
|
|
for (i = 0; i < 3; i++) |
| 83 |
|
|
nr.rdir[i] = r->rdir[i] + 2.*pdot*pnorm[i]; |
| 84 |
|
|
rpure = 0; |
| 85 |
|
|
} |
| 86 |
greg |
2.2 |
/* check for penetration */ |
| 87 |
greg |
2.6 |
if (rpure || DOT(nr.rdir, r->ron) <= FTINY) |
| 88 |
greg |
2.2 |
for (i = 0; i < 3; i++) |
| 89 |
|
|
nr.rdir[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 90 |
greg |
1.1 |
} |
| 91 |
|
|
rayvalue(&nr); |
| 92 |
|
|
multcolor(nr.rcol, mcolor); |
| 93 |
|
|
addcolor(r->rcol, nr.rcol); |
| 94 |
greg |
2.6 |
if (rpure && r->ro != NULL && isflat(r->ro->otype)) |
| 95 |
|
|
r->rt = r->rot + nr.rt; |
| 96 |
greg |
2.3 |
return(1); |
| 97 |
greg |
1.1 |
} |
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
mir_proj(pm, o, s, n) /* compute a mirror's projection */ |
| 101 |
|
|
MAT4 pm; |
| 102 |
|
|
register OBJREC *o; |
| 103 |
greg |
1.2 |
SRCREC *s; |
| 104 |
greg |
1.1 |
int n; |
| 105 |
|
|
{ |
| 106 |
greg |
2.5 |
FVECT nv, sc; |
| 107 |
greg |
1.1 |
double od; |
| 108 |
greg |
2.5 |
register int i, j; |
| 109 |
greg |
1.1 |
/* get surface normal and offset */ |
| 110 |
greg |
1.2 |
od = getplaneq(nv, o); |
| 111 |
greg |
2.5 |
/* check for extreme point for behind */ |
| 112 |
|
|
VCOPY(sc, s->sloc); |
| 113 |
|
|
for (i = s->sflags & SFLAT ? SV : SW; i >= 0; i--) |
| 114 |
|
|
if (DOT(nv, s->ss[i]) > 0.) |
| 115 |
|
|
for (j = 0; j < 3; j++) |
| 116 |
|
|
sc[j] += s->ss[i][j]; |
| 117 |
|
|
else |
| 118 |
|
|
for (j = 0; j < 3; j++) |
| 119 |
|
|
sc[j] -= s->ss[i][j]; |
| 120 |
|
|
if (DOT(sc, nv) <= (s->sflags & SDISTANT ? FTINY : od+FTINY)) |
| 121 |
greg |
1.1 |
return(0); |
| 122 |
|
|
/* everything OK -- compute projection */ |
| 123 |
|
|
mirrorproj(pm, nv, od); |
| 124 |
|
|
return(1); |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
|
| 128 |
|
|
mirrorproj(m, nv, offs) /* get mirror projection for surface */ |
| 129 |
|
|
register MAT4 m; |
| 130 |
|
|
FVECT nv; |
| 131 |
|
|
double offs; |
| 132 |
|
|
{ |
| 133 |
|
|
register int i, j; |
| 134 |
|
|
/* assign matrix */ |
| 135 |
|
|
setident4(m); |
| 136 |
greg |
1.2 |
for (j = 0; j < 3; j++) { |
| 137 |
|
|
for (i = 0; i < 3; i++) |
| 138 |
greg |
1.1 |
m[i][j] -= 2.*nv[i]*nv[j]; |
| 139 |
|
|
m[3][j] = 2.*offs*nv[j]; |
| 140 |
greg |
1.2 |
} |
| 141 |
greg |
1.1 |
} |