Esempio n. 1
0
 public abstract bool Intersect(Ray ray, Intersection isec);
Esempio n. 2
0
        public override bool Intersect(Ray ray, Intersection isec)
        {
            var a = 1;
            var b = 2 * ray.Direction.Dot (ray.Origin - Center);
            var c = Center.Dot (Center) + ray.Origin.Dot (ray.Origin) - 2 * (Center.Dot (ray.Origin)) - Radius * Radius;

            var d = b * b - 4 * a * c;

            if (d < 0)
                return false;

            var sqrtd = (Prec)Math.Sqrt (d);

            var t1 = (-b - sqrtd) / 2;//(2*a);
            var t2 = (-b + sqrtd) / 2;//(2*a);

            var t = t1;
            if (t1 < 0) {
                if (t2 < 0)
                    return false;
                t = t2;
            } else {
                if (t2 > 0 && t2 < t1)
                    t = t2;
            }

            var p = ray.Origin + ray.Direction * t;
            var n = (p - Center);
            n.Normalize ();

            isec.Set (t, n, Material);

            return true;
        }
Esempio n. 3
0
 public override bool Intersect(Ray ray, Intersection isec)
 {
     var d = ray.Direction.Dot (Normal);
     if (d == 0)
         return false;
     var t = (Position - ray.Origin).Dot (Normal) / d;
     isec.Set (t, Normal, Material);
     return true;
 }
Esempio n. 4
0
        Vec Shade(Ray ray, Intersection isec, int depth)
        {
            var mat = isec.Material;
            var color = mat.DiffuseColor * (Prec)0.01;

            foreach (var light in Lights) {
                var sray = new Ray (isec.Point, light.Position);
                var l = isec.Point.NormalTo (light.Position);
                var v = isec.Point.NormalTo (CameraCenter);
                var r = isec.Normal;
                if (isec.Normal.Dot (sray.Direction) > 0) {
                    var s = ShadowCoefficient (isec.Point, light.Position);
                    var fatt = (Prec)1;
                    color += (mat.DiffuseColor * (mat.DiffuseCoefficient * isec.Normal.Dot (l)) +
                        mat.SpecularColor * (mat.SpecularCoefficient * r.Dot (v))) * (s * fatt);
                }
            }

            if (depth < MaxDepth) {

            }

            color *= ((Prec)10) / isec.RayT;

            return color;
        }
Esempio n. 5
0
 public Prec ShadowCoefficient(Vec orig, Vec light)
 {
     //
     // INSERSECT ALL
     //
     var d = (light - orig).Length;
     var ray = new Ray (orig, light);
     var i = new Intersection ();
     foreach (var o in Objects) {
         i.Ray = ray;
         if (o.Intersect (ray, i)) {
             if (i.RayT > 0.0000001 && i.RayT < d) {
                 return 0;
             }
         }
     }
     return 1;
 }
Esempio n. 6
0
        public void Run()
        {
            ints = new Intersection[Objects.Length];
            for (var i = 0; i < ints.Length; i++) {
                ints [i] = new Intersection ();
            }

            var bmp = new PixelBuffer (1920, 1080);

            var ss = 10 / (Prec)bmp.Width;

            var startT = DateTime.Now;

            var ray = new Ray ();

            var ns = 32;

            var rand = new Random ();

            for (var y = 0; y < bmp.Height; y++) {
                for (var x = 0; x < bmp.Width; x++) {

                    var col = new Vec (0,0,0);

                    for (var samp = 0; samp < ns; samp++) {
                        var r0 = CameraCenter;
                        var r1 = new Vec (
                            (Prec)(x + (ns > 1 ? rand.NextDouble () : 0.0) - bmp.Width / 2) * ss,
                            (Prec)(y + (ns > 1 ? rand.NextDouble () : 0.0) - bmp.Height / 2) * ss,
                            6);

                        var v = r1 - r0;
                        v.Normalize ();

                        ray.Origin = r0;
                        ray.Direction = v;

                        col += Trace (ray, 1);
                    }

                    col *= ((Prec)1)/ns;

                    bmp.PutPixel (x, y, col);
                }
            }

            var endT = DateTime.Now;
            var time = (endT - startT).TotalSeconds;

            System.Console.WriteLine ("Rays/sec = {0}, T = {1}", bmp.Width * bmp.Height / time, time);

            bmp.SavePng ();
        }
 public abstract bool Intersect(Ray ray, Intersection isec);