////////////// // // tri.hpp // // basic triangle polygon object // #ifndef _TRI_ #define _TRI_ #include "object.hpp" #include class tri : public object { public: Vec3f verta, vertb, vertc, norm; //three vertices plus normal tri() {} void set(Vec3f a, Vec3f b, Vec3f c, Vec3f ia, Vec3f d, Vec3f s, Vec3f t, float m) { verta=a; vertb=b; vertc=c; Ia = ia; kd = d; ks = s; kt = t; M = m; // norm = (vertb-verta) / (vertc-verta); norm = vertb; //faster? norm -= verta; norm = norm / (vertc - verta); norm.Normalize(); } tri(Vec3f a, Vec3f b, Vec3f c, Vec3f ia, Vec3f d, Vec3f s, Vec3f t, float m) { set(a,b,c,ia,d,s,t,m); } // (note: was having trouble with tri intersect routine; modifications // based on Kenny's.) int getIntersect(Vec3f &start, Vec3f &dir, float *time) { float dot = dir*norm; if (dot==0) return(0); //ray parallel to tri plane // cout << "dot " << dot << endl; *time = ( norm * (verta-start) ) / dot; //intersect w/ plane if (*time < 0.01) return(0); //discard if behind ray // cout << "hittime " << *time << endl; Vec3f hitPt = dir; hitPt *= (*time); // get potential intersect point hitPt += start; // finally, discard if hit is outside tri if ( ((vertb-verta)/(hitPt-verta))*norm < 0) return(0); else if ( ((vertc-vertb)/(hitPt-vertb))*norm < 0) return(0); else if ( ((verta-vertc)/(hitPt-vertc))*norm < 0) return(0); return(1); } Vec3f getNormal(Vec3f hitPt) { return norm; } }; #endif