Пример #1
0
        public override IntersectionInfo Intersect(Ray ray)
        {
            IntersectionInfo info = new IntersectionInfo();

            info.hit_object = this;

            Vector dst = ray.Position - this.Position;
            double b   = dst.dot(ray.Direction);
            double c   = dst.dot(dst) - (radius * radius);
            double d   = b * b - c;

            if (d > 0) // hit
            {
                info.is_hit = true;
                info.dist   = -b - (double)Math.Sqrt(d);
                info.pos    = ray.Position + ray.Direction * info.dist;
                info.Color  = this.Material.color;
                info.normal = (info.pos - Position).normalize();
            }
            else
            {
                info.is_hit = false;
            }
            return(info);
        }
Пример #2
0
        public Color CalculateColor(Ray ray, Scene scene)
        {
            IntersectionInfo info = TestIntersection(ray, scene, null);

            if (info.is_hit)
            {
                Color c = RayTrace(info, ray, scene, 0).toStandart();
                return(c);
            }

            return(scene.backColor.toStandart());
        }
Пример #3
0
        public IntersectionInfo IntersectRay(Ray ray)
        {
            int hitCount         = 0;
            IntersectionInfo res = new IntersectionInfo();

            res.dist = double.MaxValue;
            foreach (IObject o in objects)
            {
                IntersectionInfo i = o.Intersect(ray);
                if (i.is_hit && i.dist < res.dist && i.dist > 0)
                {
                    res = i;
                    hitCount++;
                }
            }
            return(res);
        }
Пример #4
0
        private Color_dbl RayTrace(IntersectionInfo info, Ray ray, Scene scene, int depth)
        {
            Color_dbl color = info.Color * scene.ambience;

            foreach (Light l in scene.lights)
            {
                Vector v = (l.Position - info.pos).normalize(); // diffuse
                double L = v.dot(info.normal);
                if (L > 0.0f)
                {
                    color += info.Color * l.Color * L;
                }

                if (depth < 5)
                {
                    //shadows
                    IntersectionInfo shadow_info = new IntersectionInfo();
                    Ray toLight = new Ray(info.pos, v);
                    shadow_info = TestIntersection(toLight, scene, info.hit_object);
                    if (shadow_info.is_hit && shadow_info.hit_object != info.hit_object)
                    {
                        color *= 0.5;
                    }

                    //reflections
                    if (info.hit_object.Material.Reflection > 0)
                    {
                        Ray reflectionray         = new Ray(info.pos, ray.Direction + info.normal * 2 * -info.normal.dot(ray.Direction));
                        IntersectionInfo ref_info = TestIntersection(reflectionray, scene, info.hit_object);
                        if (ref_info.is_hit && ref_info.dist > 0)
                        {
                            ref_info.Color = RayTrace(ref_info, reflectionray, scene, depth + 1);
                        }
                        else
                        {
                            ref_info.Color = scene.backColor;
                        }
                        color = color.Blend(ref_info.Color, info.hit_object.Material.Reflection);
                    }
                }
            }
            color.Correct();
            return(color);
        }
Пример #5
0
        private IntersectionInfo TestIntersection(Ray ray, Scene scene, IObject ignore)
        {
            int hitcount          = 0;
            IntersectionInfo best = new IntersectionInfo();

            best.dist = double.MaxValue;

            foreach (IObject o in scene.objects)
            {
                if (o == ignore)
                {
                    continue;
                }
                IntersectionInfo info = o.Intersect(ray);
                if (info.is_hit && info.dist < best.dist && info.dist >= 0)
                {
                    best = info;
                    hitcount++;
                }
            }
            best.hit_count = hitcount;
            return(best);
        }
Пример #6
0
        public override IntersectionInfo Intersect(Ray ray)
        {
            IntersectionInfo info = new IntersectionInfo();
            double           Vd   = Position.dot(ray.Direction);

            if (Vd == 0)
            {
                return(info);         // no intersection
            }
            double t = -(Position.dot(ray.Position) + D) / Vd;

            if (t <= 0)
            {
                return(info);
            }

            info.hit_object = this;
            info.is_hit     = true;
            info.pos        = ray.Position + ray.Direction * t;
            info.dist       = t;
            info.Color      = Material.color;
            info.normal     = Position;
            return(info);
        }