class Triangle extends Object { vec vtx0; vec edge1, edge2; vec planenormal; vec nrm0, dnrm1, dnrm2;
Triangle(vec vtx0_, vec vtx1_, vec vtx2_, Material material_, vec nrm0_, vec nrm1_, vec nrm2_) { material = material_; vtx0 = vtx0_;
edge1 = vtx1_.sub(vtx0); edge2 = vtx2_.sub(vtx0); planenormal = edge1.cross(edge2).normal();
if (nrm0_ == null) nrm0_ = planenormal; if (nrm1_ == null) nrm1_ = planenormal; if (nrm2_ == null) nrm2_ = planenormal;
nrm0 = nrm0_; dnrm1 = nrm1_.sub(nrm0_); dnrm2 = nrm2_.sub(nrm0_); }
float intersect_line(vec normal, vec orig, vec dir) { vec pvec = dir.cross(edge2);
float det = edge1.dot(pvec); if (abs(det) < 0.000001) return -1; float inv_det = 1.0 / det;
vec tvec = orig.sub(vtx0);
float u = tvec.dot(pvec) * inv_det; if (u < 0.0 || u > 1.0) return -1;
vec qvec = tvec.cross(edge1);
float v = dir.dot(qvec) * inv_det; if (v < 0.0 || u + v > 1.0) return -1;
float t = edge2.dot(qvec) * inv_det;
normal.set(nrm0.add(dnrm1.scale(u)).add(dnrm2.scale(v)).normal());
return t; } }
|