コード例 #1
0
ファイル: Mesh.cs プロジェクト: xiaozuizui/littleRender
        public override bool Intersect(Ray ray, ref DifferentialGeometry dg)
        {
            Point3 p1 = m.P[v[0]];
            Point3 p2 = m.P[v[1]];
            Point3 p3 = m.P[v[2]];

            Vector e1 = p2 - p1;
            Vector e2 = p3 - p1;
            Vector s1 = LR.Cross(ray.d, e2);

            float divisor = LR.Dot(s1, e1);

            if (divisor == 0.0f)
            {
                return(false);
            }
            float invDivisor = 1.0f / divisor;

            // Compute first barycentric coordinate
            Vector s  = ray.o - p1;
            float  b1 = LR.Dot(s, s1) * invDivisor;

            if (b1 < 0.0f || b1 > 1.0f)
            {
                return(false);
            }

            // Compute second barycentric coordinate
            Vector s2 = LR.Cross(s, e1);
            float  b2 = LR.Dot(ray.d, s2) * invDivisor;

            if (b2 < 0.0f || b1 + b2 > 1.0f)
            {
                return(false);
            }

            // Compute _t_ to intersection point
            float t = LR.Dot(e2, s2) * invDivisor;

            if (t < ray.mint || t > ray.maxt)
            {
                return(false);
            }


            return(false);
        }