示例#1
0
        private Ray GetReflectedRay(Ray r, SceneObject hitObject)
        {
            Vector3D n = hitObject.Geometry.GetNormalAtPoint(hitObject.Material.Hitpoint);
            float k = (-1f) * r.Direction * n;

            return new Ray(hitObject.Material.Hitpoint, r.Direction + (2 * k * n), hitObject);
        }
示例#2
0
 public SceneObject GetIntersectedObject(Ray r)
 {
     var hitObjects = from so in sceneObjects
                      where so.Hit(r).Item1
                      orderby so.Hit(r).Item2
                      select so;
     return hitObjects.FirstOrDefault();
 }
示例#3
0
        public Tuple<bool, float> Hit(Ray ray)
        {
            var ht = geometry.Hit(ray);

            if (ht.Item1)
                material.Hitpoint = ray.Origin + ray.Direction * ht.Item2;
            return ht;
        }
示例#4
0
        public override Tuple<bool, float> Hit(Ray r)
        {
            Tuple<bool, float> hit = plane.Hit(r);
            Vector3D hitpoint = r.Origin + hit.Item2 * r.Direction;
            Vector3D v = hitpoint - A;
            float dot = plane.GetNormalAtPoint(hitpoint) * v;

            if (v.Length <= radius && dot < Geometry.Epsilon)
                return hit;
            return Geometry.NoIntersection;
        }
示例#5
0
        public override Tuple<bool, float> Hit(Ray r)
        {
            Vector3D v = p - r.Origin;

            float a = n * v;
            float b = n * r.Direction;

            if (b < Geometry.Epsilon) return Geometry.NoIntersection;

            return new Tuple<bool, float>(true, a / b);
        }
示例#6
0
        public Color Shade(Ray ray)
        {
            Color L = Color.Black;
            Vector3D normal = parentObject.Geometry.GetNormalAtPoint(hitpoint);

            foreach (var light in world.Lights)
            {
                Vector3D toLight = (light.Position - hitpoint).Unit;
                float s = toLight * normal;

                if (s < 0f) s = 0f;
                L += props.Color * (props.Ambient + props.Diffuse * s);
            }

            return L;
        }
示例#7
0
        public override Tuple<bool, float> Hit(Ray r)
        {
            Vector3D v = r.Origin - center;
            float a = r.Direction * r.Direction;
            float b = 2f * v * r.Direction;
            float c = v * v - radius * radius;
            float delta = b * b - 4f * a * c;

            if (delta < 0f) return Geometry.NoIntersection;
            if (delta > 0f)
            {
                float t = (-b - (float)Math.Sqrt(delta)) / (2f * a);
                if (t > Geometry.Epsilon)
                    return new Tuple<bool, float>(true, t);
            }
            return Geometry.NoIntersection;
        }
示例#8
0
        private Ray GetRefractedRay(Ray r, SceneObject hitObject)
        {
            Vector3D n = hitObject.Geometry.GetNormalAtPoint(hitObject.Material.Hitpoint);
            float n1 = r.ObjectAtEmission.Material.RefractiveIndex;
            float n2 = hitObject.Material.RefractiveIndex;
            float ri = n1 / n2;
            float c1 = -(n * r.Direction);
            float c2 = (float)Math.Sqrt(1f - ri * ri * (1f - c1 * c1));
            Vector3D dir = (ri * r.Direction) + (ri * c1 - c2) * n;

            return new Ray(hitObject.Material.Hitpoint, dir, hitObject);
        }
示例#9
0
        private Color TraceRay(Ray r, int d)
        {
            Color L = Color.Black;
            if (d > maxDepth) return L;

            var hitObject = GetIntersectedObject(r);

            if (hitObject == null) return L;
            L = hitObject.Material.Shade(r);

            if (hitObject.Material.IsReflexive)
                L += TraceRay(GetReflectedRay(r, hitObject), d + 1);
            if (hitObject.Material.IsRefractive)
                L += TraceRay(GetRefractedRay(r, hitObject), d + 1);

            return L;
        }
示例#10
0
 public virtual Tuple<bool, float> Hit(Ray r)
 {
     throw new NotImplementedException();
 }
示例#11
0
 public override Tuple<bool, float> Hit(Ray r)
 {
     return bt;
 }