Example #1
0
        public override bool RayIntersection(Ray r, out float t, out Point3D normal)
        {
            t      = 0;
            normal = null;
            Point3D k = r.start - position;
            float   b = Point3D.Scalar(k, r.dir);
            float   c = Point3D.Scalar(k, k) - rad * rad;
            float   d = b * b - c;

            if (d >= 0)
            {
                float sqrtfd = (float)Math.Sqrt(d);
                float t1     = -b + sqrtfd;
                float t2     = -b - sqrtfd;
                float min_t  = Math.Min(t1, t2);
                float max_t  = Math.Max(t1, t2);
                t = (min_t > Ray.EPSILON) ? min_t : max_t;
                if (t > Ray.EPSILON)
                {
                    normal = Point3D.Norm(r.TPos(t) - position);
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        Point3D RayTrace(Ray r, int rec, float env = 1, float impact = 1)
        {
            bool    BackToAir = false;
            Point3D clr       = new Point3D(0, 0, 0);

            if (rec <= 0 || impact < EPS)
            {
                return(clr);
            }
            Hit h = GenerateHit(r);

            if (!h.success)
            {
                return(clr);
            }
            if (Point3D.Scalar(r.dir, h.normal) > 0)
            {
                h.normal  = h.normal * -1;
                BackToAir = true;
            }

            Point3D hit_pos = r.TPos(h.hit_point);

            foreach (var l in lights)
            {
                clr += Point3D.blend(h.mat.clr, l.amb * h.mat.amb_coef);
                if (IsVisibleLight(l.points[0], hit_pos))
                {
                    clr += h.Shade(l, hit_pos, f);
                }
            }

            if (h.mat.reflection_coef > 0)

            {
                Ray reflRay = h.Reflect(hit_pos);
                clr += h.mat.reflection_coef * RayTrace(reflRay, rec - 1, env, impact * h.mat.reflection_coef);
            }


            if (h.mat.refraction_coef > 0)

            {
                Ray refrRay = h.Refract(hit_pos, BackToAir ? env / 1 : env / h.mat.env_coef);
                if (refrRay != null)
                {
                    clr += h.mat.refraction_coef * RayTrace(refrRay, rec - 1, impact * h.mat.reflection_coef);
                }
            }


            return(clr);
        }