--- ray/src/rt/o_mesh.c 2003/03/12 04:59:05 2.3 +++ ray/src/rt/o_mesh.c 2006/02/25 19:49:16 2.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: o_mesh.c,v 2.3 2003/03/12 04:59:05 greg Exp $"; +static const char RCSid[] = "$Id: o_mesh.c,v 2.11 2006/02/25 19:49:16 greg Exp $"; #endif /* * Routines for computing ray intersections with meshes. @@ -21,11 +21,12 @@ static const char RCSid[] = "$Id: o_mesh.c,v 2.3 2003/ #include "copyright.h" -#include "ray.h" +#include +#include "ray.h" #include "mesh.h" - #include "tmesh.h" +#include "rtotypes.h" #define EDGE_CACHE_SIZ 251 /* length of mesh edge cache */ @@ -39,7 +40,7 @@ struct EdgeCache { OBJREC *o; /* mesh object */ MESHINST *mi; /* current mesh instance */ struct EdgeSide { - int4 v1i, v2i; /* vertex indices (lowest first) */ + int32 v1i, v2i; /* vertex indices (lowest first) */ short signum; /* signed volume */ } cache[EDGE_CACHE_SIZ]; } edge_cache; @@ -52,45 +53,40 @@ OBJREC *o; /* get mesh instance */ edge_cache.mi = getmeshinst(edge_cache.o = o, IO_ALL); /* clear edge cache */ - bzero((void *)edge_cache.cache, sizeof(edge_cache.cache)); + memset((void *)edge_cache.cache, '\0', sizeof(edge_cache.cache)); } static int -signed_volume(r, v1, v2) /* get signed volume for ray and edge */ +volume_sign(r, v1, v2) /* get signed volume for ray and edge */ register RAY *r; -int4 v1, v2; +int32 v1, v2; { int reversed = 0; register struct EdgeSide *ecp; if (v1 > v2) { - int4 t = v2; v2 = v1; v1 = t; + int32 t = v2; v2 = v1; v1 = t; reversed = 1; } ecp = &edge_cache.cache[((v2<<11 ^ v1) & 0x7fffffff) % EDGE_CACHE_SIZ]; - if (ecp->v1i != v1 || ecp->v2i != v2) { + if ((ecp->v1i != v1) | (ecp->v2i != v2)) { MESHVERT tv1, tv2; /* compute signed volume */ + FVECT v2d; double vol; - if (!getmeshvert(&tv1, edge_cache.mi->msh, v1, MT_V) || + if (!getmeshvert(&tv1, edge_cache.mi->msh, v1, MT_V) | !getmeshvert(&tv2, edge_cache.mi->msh, v2, MT_V)) objerror(edge_cache.o, INTERNAL, - "missing mesh vertex in signed_volume"); + "missing mesh vertex in volume_sign"); + VSUB(v2d, tv2.v, r->rorg); vol = (tv1.v[0] - r->rorg[0]) * - ( (tv2.v[1] - r->rorg[1])*r->rdir[2] - - (tv2.v[2] - r->rorg[2])*r->rdir[1] ); + (v2d[1]*r->rdir[2] - v2d[2]*r->rdir[1]); vol += (tv1.v[1] - r->rorg[1]) * - ( (tv2.v[2] - r->rorg[2])*r->rdir[0] - - (tv2.v[0] - r->rorg[0])*r->rdir[2] ); + (v2d[2]*r->rdir[0] - v2d[0]*r->rdir[2]); vol += (tv1.v[2] - r->rorg[2]) * - ( (tv2.v[0] - r->rorg[0])*r->rdir[1] - - (tv2.v[1] - r->rorg[1])*r->rdir[0] ); - if (vol > .0) - ecp->signum = 1; - else if (vol < .0) - ecp->signum = -1; - else - ecp->signum = 0; + (v2d[0]*r->rdir[1] - v2d[1]*r->rdir[0]); + /* don't generate 0 */ + ecp->signum = vol > .0 ? 1 : -1; ecp->v1i = v1; ecp->v2i = v2; } @@ -103,23 +99,24 @@ mesh_hit(oset, r) /* intersect ray with mesh triangle OBJECT *oset; RAY *r; { - int4 tvi[3]; + int32 tvi[3]; int sv1, sv2, sv3; MESHVERT tv[3]; + OBJECT tmod; FVECT va, vb, nrm; double d; int i; /* check each triangle */ for (i = oset[0]; i > 0; i--) { - if (!getmeshtrivid(tvi, curmsh, oset[i])) + if (!getmeshtrivid(tvi, &tmod, curmsh, oset[i])) objerror(edge_cache.o, INTERNAL, "missing triangle vertices in mesh_hit"); - sv1 = signed_volume(r, tvi[0], tvi[1]); - sv2 = signed_volume(r, tvi[1], tvi[2]); - sv3 = signed_volume(r, tvi[2], tvi[0]); - if (sv1 != sv2 || sv2 != sv3) /* compare volume signs */ - if (sv1 && sv2 && sv3) - continue; + sv1 = volume_sign(r, tvi[0], tvi[1]); + sv2 = volume_sign(r, tvi[1], tvi[2]); + sv3 = volume_sign(r, tvi[2], tvi[0]); + /* compare volume signs */ + if ((sv1 != sv2) | (sv2 != sv3)) + continue; /* compute intersection */ getmeshvert(&tv[0], curmsh, tvi[0], MT_V); getmeshvert(&tv[1], curmsh, tvi[1], MT_V); @@ -144,20 +141,22 @@ RAY *r; } -int -o_mesh(o, r) /* compute ray intersection with a mesh */ -OBJREC *o; -register RAY *r; +extern int +o_mesh( /* compute ray intersection with a mesh */ + OBJREC *o, + register RAY *r +) { RAY rcont; int flags; MESHVERT tv[3]; - FLOAT wt[3]; + OBJECT tmod; + RREAL wt[3]; int i; /* get the mesh instance */ prep_edge_cache(o); /* copy and transform ray */ - copystruct(&rcont, r); + rcont = *r; multp3(rcont.rorg, r->rorg, curmi->x.b.xfm); multv3(rcont.rdir, r->rdir, curmi->x.b.xfm); for (i = 0; i < 3; i++) @@ -170,19 +169,23 @@ register RAY *r; return(0); /* missed */ if (rcont.rot * curmi->x.f.sca >= r->rot) return(0); /* not close enough */ - - r->robj = objndx(o); /* record new hit */ - r->ro = o; /* transform ray back */ r->rot = rcont.rot * curmi->x.f.sca; multp3(r->rop, rcont.rop, curmi->x.f.xfm); multv3(r->ron, rcont.ron, curmi->x.f.xfm); normalize(r->ron); r->rod = -DOT(r->rdir, r->ron); - /* compute barycentric weights */ - flags = getmeshtri(tv, curmsh, rcont.robj, MT_ALL); + /* get triangle */ + flags = getmeshtri(tv, &tmod, curmsh, rcont.robj, MT_ALL); if (!(flags & MT_V)) objerror(o, INTERNAL, "missing mesh vertices in o_mesh"); + r->robj = objndx(o); /* set object and material */ + if (o->omod == OVOID && tmod != OVOID) { + r->ro = getmeshpseudo(curmsh, tmod); + r->rox = &curmi->x; + } else + r->ro = o; + /* compute barycentric weights */ if (flags & (MT_N|MT_UV)) if (get_baryc(wt, rcont.rop, tv[0].v, tv[1].v, tv[2].v) < 0) { objerror(o, WARNING, "bad triangle in o_mesh");