Beispiel #1
0
        public bool Intersects(Ray ray, out double distance)
        {
            distance = 0.0;
            if (Contains(ray.Start))
                return true;

            double t1 = (Min.X - ray.Start.X) * ray.DirFrac.X;
            double t2 = (Max.X - ray.Start.X) * ray.DirFrac.X;
            double t3 = (Min.Y - ray.Start.Y) * ray.DirFrac.Y;
            double t4 = (Max.Y - ray.Start.Y) * ray.DirFrac.Y;
            double t5 = (Min.Z - ray.Start.Z) * ray.DirFrac.Z;
            double t6 = (Max.Z - ray.Start.Z) * ray.DirFrac.Z;

            double tmin = Math.Max(Math.Max(Math.Min(t1, t2), Math.Min(t3, t4)), Math.Min(t5, t6));
            double tmax = Math.Min(Math.Min(Math.Max(t1, t2), Math.Max(t3, t4)), Math.Max(t5, t6));

            // if tmax < 0, ray (line) is intersecting AABB, but whole AABB is behind us
            if (tmax < 0.0)
                return false;

            // if tmin > tmax, ray doesn't intersect AABB
            if (tmin > tmax)
                return false;

            distance = tmin;
            return true;
        }
Beispiel #2
0
 public override Intersection Intersects(Ray ray)
 {
     double denom = Vector3.DotProduct(normal, ray.Direction);
     if (Math.Abs(denom) > 0.0001)
     {
         double t = Vector3.DotProduct(position - ray.Start, normal) / denom;
         if (t > 0.0005)
             return new Intersection(t, ray.PointAt(t), this, normal, null);
     }
     return Intersection.False;
 }
Beispiel #3
0
        public override Intersection Intersects(Ray ray)
        {
            Intersection closestIntersec = Intersection.False;
            closestIntersec.Distance = double.MaxValue;

            foreach (Triangle tri in Triangles)
            {
                Intersection res = tri.Intersects(ray);

                if (res.Intersects && res.Distance > 0.0005 && res.Distance < closestIntersec.Distance)
                    closestIntersec = res;
            }

            return closestIntersec;
        }
Beispiel #4
0
        public override Intersection Intersects(Ray ray)
        {
            Vector3 T = ray.Start - Vertex0.Position;
            Vector3 P = Vector3.CrossProduct(ray.Direction, E2);
            Vector3 Q = Vector3.CrossProduct(T, E1);

            double denom = Vector3.DotProduct(P, E1);
            if (Math.Abs(denom) > 0.0001)
            {
                double u = Vector3.DotProduct(P, T) / denom;
                double v = Vector3.DotProduct(Q, ray.Direction) / denom;
                double t = Vector3.DotProduct(Q, E2) / denom;
                bool kl = denom < 0.0;
                if ((u >= 0.0 && u <= 1.0) && (v >= 0.0 && u + v <= 1.0))
                    return new Intersection(t, ray.PointAt(t), this, kl ? Normal : Normal.Negated, new Vector2(u, kl ? v : 1 - v)); // (kl ? v : 1-v) automatic texture flipping
            }
            return Intersection.False;
        }
Beispiel #5
0
        private void DrawRay(Color color, Ray ray)
        {
            Vector2 start = new Vector2(ray.Start.X, ray.Start.Z);
            Vector2 center = VectorToPixel(start);

            double ellipseRadius = 50.0 / areaSize;
            g.FillEllipse(new SolidBrush(color), (int)(center.X - ellipseRadius), (int)(center.Y - ellipseRadius), (int)(ellipseRadius * 2), (int)(ellipseRadius * 2));

            Pen p = new Pen(color, 2f);
            Vector2 dir = new Vector2(ray.Direction.X, ray.Direction.Z).Normalized;
            DrawLine(p, start, start + dir * 2);
        }
Beispiel #6
0
        public override Intersection Intersects(Ray ray)
        {
            Intersection interT1 = T1.Intersects(ray);
            Intersection interT2 = T2.Intersects(ray);
            Intersection res = Intersection.False;

            if (!interT1.Intersects && !interT2.Intersects)
                return res;
            if (interT1.Intersects && !interT2.Intersects)
                res = interT1;
            if (!interT1.Intersects && interT2.Intersects)
                res = interT2;
            res = interT1.Distance < interT2.Distance ? interT1 : interT2;

            if (res.Object == T2)
            {
                res.BaryCoords.X = 1.0 - res.BaryCoords.X;
                res.BaryCoords.Y = 1.0 - res.BaryCoords.Y;
            }

            res.Object = this;
            return res;
        }
Beispiel #7
0
        public override Intersection Intersects(Ray ray)
        {
            double a = Vector3.DotProduct(ray.Direction, ray.Direction);
            double b = 2 * Vector3.DotProduct(ray.Direction, ray.Start - Position);
            Vector3 rayToCenter = ray.Start - Position;
            double c = Vector3.DotProduct(rayToCenter, rayToCenter) - radius2;

            double dis = b * b - 4 * a * c;

            if (dis >= 0.0)
            {
                double t = (-Math.Sqrt(dis) - b) / (2.0 * a);
                Vector3 point = ray.PointAt(t);
                return new Intersection(t, point, this, (point - Position).Normalized, null);
            }
            return Intersection.False;
        }
Beispiel #8
0
 public abstract Intersection Intersects(Ray ray);
Beispiel #9
0
        private Vector3 Trace(Ray ray, int depth)
        {
            if (depth > 3)
                return Vector3.Zero;

            Intersection res = IntersectScene(ray);
            if (!res.Intersects)
                return backgroundColor;

            // lights!
            double lambertAmount = EvalLights(res);

            IObject obj = res.Object;
            Material mat = materials[obj.Material];
            Vector3 objColor = obj.EvalMaterial(res, mat);

            Vector3 resultColor = objColor * mat.Ambient + objColor * lambertAmount * mat.Lambert;

            //if (specular != 0.0)
            //{
            //    Ray reflected = ray.Reflect(res.Point, res.Normal);
            //    reflected.Start += reflected.Direction * 0.001;

            //    Color? reflectColor = Trace(reflected, depth + 1);
            //    if (reflectColor.HasValue)
            //        resultColor += new Vector(reflectColor.Value) * specular;
            //}
            return resultColor;
        }
Beispiel #10
0
        private Intersection IntersectSceneExcept(Ray ray, params IObject[] objs)
        {
            Intersection closestIntersec = Intersection.False;
            closestIntersec.Distance = double.MaxValue;

            foreach (var o in objects.Except(objs))
            {
                Intersection res = o.Intersects(ray);

                if (res.Intersects && res.Distance > 0.0005 && res.Distance < closestIntersec.Distance)
                    closestIntersec = res;
            }

            return closestIntersec;
        }
Beispiel #11
0
        private double GetAmbientOcclusion(Intersection res, int rays)
        {
            int notblocked = 0;
            for (int i = 0; i < rays; i++)
            {
                //Vector2 sample = pdisk_64[i];
                double theta = 2 * Math.PI * MwcRng.GetUniform();
                double phi = Math.Acos(MwcRng.GetUniform() * 2 - 1);

                Vector3 rand = new Vector3(
                    Math.Cos(theta) * Math.Sin(phi),
                    Math.Sin(theta) * Math.Sin(phi),
                    Math.Cos(phi)
                    );

                if (Vector3.DotProduct(rand, res.Normal) < 0.0)
                    rand.Negate();

                Ray newray = new Ray(res.Point, rand);
                Intersection res2 = IntersectSceneExcept(newray, res.Object);
                if (!res2.Intersects)
                    notblocked++;
            }

            return notblocked * 1.0 / rays;
        }