예제 #1
0
        public override (double?, SurfaceInteraction) Intersect(Ray _r)
        {
            Ray ray = WorldToObject.Apply(_r);

            double a = 1;
            double b = 2 * Vector3.Dot(ray.o, ray.d);
            double c = Vector3.Dot(ray.o, ray.o) - Radius * Radius;

            (bool hasSolution, double t0, double t1) = Utils.Quadratic(a, b, c);

            if (!hasSolution || (t0 < Renderer.Epsilon && t1 < Renderer.Epsilon))
            {
                return(null, null);
            }

            double tHit = (t0 < Renderer.Epsilon ? t1 : t0);

            Vector3 pHit   = ray.Point(tHit);
            Vector3 normal = pHit * (1.0 / Radius);
            Vector3 dpdu   = new Vector3(-pHit.y, pHit.x, 0);

            SurfaceInteraction si = new SurfaceInteraction(pHit, normal, -ray.d, dpdu, this);

            return(tHit, ObjectToWorld.Apply(si));
        }
예제 #2
0
        public override (double?, SurfaceInteraction) Intersect(Ray r)
        {
            var ray = WorldToObject.Apply(r);

            // TODO: Compute quadratic sphere coefficients

            // TODO: Initialize _double_ ray coordinate values
            double a = (ray.d.x * ray.d.x) + (ray.d.y * ray.d.y) + (ray.d.z * ray.d.z);
            double b = ((ray.d.x * ray.o.x) + (ray.d.y * ray.o.y) + (ray.d.z * ray.o.z)) * 2;
            double c = (ray.o.x * ray.o.x) + (ray.o.y * ray.o.y) + (ray.o.z * ray.o.z) - (Radius * Radius);

            // TODO: Solve quadratic equation for _t_ values
            (bool ok, double t0, double t1) = Utils.Quadratic(a, b, c);
            // TODO: Check quadric shape _t0_ and _t1_ for nearest intersection
            if (!ok /*|| t0 > ray.max*/ || t1 <= 0)
            {
                return(null, null);
            }
            // TODO: Compute sphere hit position and $\phi$
            double tShapeHit = t0;

            if (tShapeHit <= Renderer.Epsilon)   // skor 0.
            {
                tShapeHit = t1;
                //if (t1 > ray.max)
                //{
                //    return (null, null);
                //}
            }

            Vector3 pHit = ray.Point(tShapeHit);

            //pHit.
            if (pHit.x == 0 && pHit.y == 0)
            {
                pHit.x = 1e-5 * Radius;
            }

            Vector3 dpdu = new Vector3(-pHit.y, pHit.x, 0);

            // TODO: Return shape hit and surface interaction

            return(tShapeHit, ObjectToWorld.Apply(new SurfaceInteraction(pHit, pHit, -ray.d, dpdu, this)));
        }
예제 #3
0
        public override (double?, SurfaceInteraction) Intersect(Ray r)
        {
            Ray ray = WorldToObject.Apply(r);

            var ox = ray.o.x;
            var oy = ray.o.y;
            var oz = ray.o.z;

            var dx = ray.d.x;
            var dy = ray.d.y;
            var dz = ray.d.z;

            var a = dx * dx + dy * dy + dz * dz;
            var b = 2 * (dx * ox + dy * oy + dz * oz);
            var c = ox * ox + oy * oy + oz * oz - Radius * Radius;

            var(solvable, t0, t1) = Utils.Quadratic(a, b, c);
            if (!solvable)
            {
                return(null, null);
            }

            var shapeHit = t0 <= 0 ? t1 : t0;

            var hit = ray.Point(shapeHit);

            hit *= Radius / hit.Length();

            if (Math.Abs(hit.x) < Double.Epsilon && Math.Abs(hit.y) < double.Epsilon)
            {
                hit.x = 1e-5 * Radius;
            }

            var dpdu   = Dpdu(hit);
            var normal = innerOrientation ? -hit : hit;

            var interaction = new SurfaceInteraction(hit, normal, -ray.d, dpdu, this);

            return(shapeHit, ObjectToWorld.Apply(interaction));
        }
예제 #4
0
        public override (double?, SurfaceInteraction) Intersect(Ray ray)
        {
            Ray r = WorldToObject.Apply(ray);

            // Compute quadratic sphere coefficients

            // Initialize _double_ ray coordinate values
            double a = r.d.x * r.d.x + r.d.y * r.d.y + r.d.z * r.d.z;
            double b = 2 * (r.d.x * r.o.x + r.d.y * r.o.y + r.d.z * r.o.z);
            double c = r.o.x * r.o.x + r.o.y * r.o.y + r.o.z * r.o.z - Radius * Radius;

            // Solve quadratic equation for _t_ values
            (bool s, double t0, double t1) = Utils.Quadratic(a, b, c);

            if (!s)
            {
                return(null, null);
            }

            // Check quadric shape _t0_ and _t1_ for nearest intersection
            if (t1 <= 0)
            {
                return(null, null);
            }

            double tShapeHit = t0;

            if (tShapeHit <= Renderer.Epsilon)
            {
                tShapeHit = t1;
            }

            // Compute sphere hit position and $\phi$
            var pHit = r.Point(tShapeHit);
            var dpdu = new Vector3(-pHit.y, pHit.x, 0);
            var si   = new SurfaceInteraction(pHit, pHit.Clone().Normalize(), -r.d, dpdu, this);

            return(tShapeHit, ObjectToWorld.Apply(si));
        }
예제 #5
0
파일: Sphere.cs 프로젝트: ZBabnik/nrg_hw2
        public override (double?, SurfaceInteraction) Intersect(Ray ray)
        {
            Ray r = WorldToObject.Apply(ray);

            // TODO: Compute quadratic sphere coefficients

            // TODO: Initialize _double_ ray coordinate values
            (double dx, double dy, double dz) = (r.d.x, r.d.y, r.d.z);
            (double ox, double oy, double oz) = (r.o.x, r.o.y, r.o.z);
            double a = dx * dx + dy * dy + dz * dz;
            double b = 2 * (dx * ox + dy * oy + dz * oz);
            double c = ox * ox + oy * oy + oz * oz - this.Radius * this.Radius;

            // TODO: Solve quadratic equation for _t_ values
            (bool zzz, double t0, double t1) = Utils.Quadratic(a, b, c);
            if (!zzz)
            {
                return(null, null);
            }

            // TODO: Check quadric shape _t0_ and _t1_ for nearest intersection
            //double tShapeHit;
            //if (t1 < Renderer.Epsilon)
            //    return (null, null);
            //if (t0 < Renderer.Epsilon)
            //    tShapeHit = t1;
            //else
            //{
            //    Ray temp = new Ray(r.o, r.d);
            //    double d1 = Vector3.Dot(temp.Point(t0), temp.o);
            //    double d2 = Vector3.Dot(temp.Point(t1), temp.o);
            //    if (d1 == d2)
            //        return (null, null);
            //    else if (d1 > d2)
            //    {
            //        if (Outside)
            //            tShapeHit = t0;
            //        else
            //            tShapeHit = t1;
            //    }
            //    else
            //    {
            //        if (Outside)
            //            tShapeHit = t1;
            //        else
            //            tShapeHit = t0;
            //    }
            //}

            //if (t1 - t0 <= Renderer.Epsilon)
            //    return (null, null);

            //Ray temp = new Ray(r.o, r.d);
            //double d0 = Vector3.Dot(temp.o, temp.Point(t0));
            //double d1 = Vector3.Dot(temp.o, temp.Point(t1));
            //double tShapeHit;

            //if (d0 >= d1)
            //    tShapeHit = t0;
            //else
            //    tShapeHit = t1;

            if (Outside)
            {
                Console.WriteLine("HERE");

                //if (t1 < Renderer.Epsilon)
                //    return (null, null);
                //double tShapeHit = t0;
                //if (tShapeHit < Renderer.Epsilon)
                //{
                //    tShapeHit = t1;
                //}

                double  tShapeHit;
                Ray     temp = new Ray(r.o, r.d);
                Vector3 p_t0 = temp.Point(t0);
                Vector3 p_t1 = temp.Point(t1);
                double  d0   = Math.Sqrt(Math.Pow(temp.o.x - p_t0.x, 2) + Math.Pow(temp.o.y - p_t0.y, 2) + Math.Pow(temp.o.z - p_t0.z, 2));
                double  d1   = Math.Sqrt(Math.Pow(temp.o.x - p_t1.x, 2) + Math.Pow(temp.o.y - p_t1.y, 2) + Math.Pow(temp.o.z - p_t1.z, 2));

                if (d0 > d1)
                {
                    tShapeHit = t1;
                }
                else
                {
                    tShapeHit = t0;
                }

                //// TODO: Compute sphere hit position and $\phi$
                Vector3 pHit = r.Point(tShapeHit);
                //pHit *= Radius / Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y + pHit.z * pHit.z);

                //if (Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y + pHit.z * pHit.z) >= this.Radius + Renderer.Epsilon)
                //    return (null, null);
                //if (Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y + pHit.z * pHit.z) <= this.Radius - Renderer.Epsilon)
                //    return (null, null);

                //double phi = Math.Atan2(pHit.y, pHit.x);
                //if (phi < 0)
                //    phi += 2 * Math.PI;
                //double theta = Math.Acos(Utils.Clamp(pHit.z / this.Radius, -1, 1));

                //double invR = 1 / Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y);
                //Vector3 n = new Vector3(pHit.z * pHit.x * invR, pHit.z * pHit.y * invR, -this.Radius * Math.Sin(theta));

                Vector3 n = (ObjectToWorld.ApplyPoint(pHit) - ObjectToWorld.ApplyPoint(Vector3.ZeroVector)).Clone().Normalize();
                //Vector3 n = new Vector3(0, 0, 1);
                //Vector3 n = pHit.Clone() * this.Radius;

                // TODO: Return shape hit and surface interaction
                Vector3 dpdu = new Vector3(-pHit.y, pHit.x, 0);

                //Console.WriteLine("dpdu1 " + dpdu.x + " " + dpdu.y + " " + dpdu.z);

                n.Faceforward(dpdu);

                //Console.WriteLine("normal " + n.x + " " + n.y + " " + n.z);

                double abDot = Vector3.Dot(n, dpdu);
                double bbDot = Vector3.Dot(n, n);

                Vector3 proj = abDot / bbDot * n;

                dpdu -= proj;

                //Console.WriteLine("proj " + proj.x + " " + proj.y + " " + proj.z);

                //Console.WriteLine("dot " + Vector3.Dot(n, dpdu));

                var si = new SurfaceInteraction(pHit, n, -ray.d, dpdu, this);
                //var si = new SurfaceInteraction(pHit, new Vector3(0, 0, 1), -ray.d, dpdu, this);


                return(tShapeHit, ObjectToWorld.Apply(si));
            }
            else
            {
                //Console.WriteLine(r.o.x + " " + r.o.y + " " + r.o.z);
                //Console.WriteLine(t0);
                //Console.WriteLine(t1);
                //System.Environment.Exit(1);

                double  tShapeHit;
                Ray     temp = new Ray(r.o, r.d);
                Vector3 p_t0 = temp.Point(t0);
                Vector3 p_t1 = temp.Point(t1);

                Vector3 x = p_t0 - temp.o;
                Vector3 y = p_t1 - temp.o;

                double x_x = Vector3.Dot(x, temp.d);
                double y_x = Vector3.Dot(y, temp.d);

                //Console.WriteLine(temp.d.x + " " + temp.d.y + " " + temp.d.z);
                //Console.WriteLine(x_x);
                //Console.WriteLine(y_x);
                //System.Environment.Exit(1);

                if (x_x > 0)
                {
                    tShapeHit = t0;
                }
                else if (y_x > 0)
                {
                    tShapeHit = t1;
                }
                else
                {
                    return(null, null);
                }

                //// TODO: Compute sphere hit position and $\phi$
                Vector3 pHit = r.Point(tShapeHit);
                //pHit *= Radius / Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y + pHit.z * pHit.z);

                //if (Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y + pHit.z * pHit.z) >= this.Radius + Renderer.Epsilon)
                //    return (null, null);
                //if (Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y + pHit.z * pHit.z) <= this.Radius - Renderer.Epsilon)
                //    return (null, null);

                //double phi = Math.Atan2(pHit.y, pHit.x);
                //if (phi < 0)
                //    phi += 2 * Math.PI;
                //double theta = Math.Acos(Utils.Clamp(pHit.z / this.Radius, -1, 1));

                //double invR = 1 / Math.Sqrt(pHit.x * pHit.x + pHit.y * pHit.y);
                //Vector3 n = new Vector3(pHit.z * pHit.x * invR, pHit.z * pHit.y * invR, -this.Radius * Math.Sin(theta));

                Vector3 n = (ObjectToWorld.ApplyPoint(Vector3.ZeroVector) - ObjectToWorld.ApplyPoint(pHit)).Clone().Normalize();
                //Vector3 n = new Vector3(0, 0, 1);
                //Vector3 n = pHit.Clone() * this.Radius;

                // TODO: Return shape hit and surface interaction
                Vector3 dpdu = new Vector3(-pHit.y, pHit.x, 0);

                //Console.WriteLine("dpdu1 " + dpdu.x + " " + dpdu.y + " " + dpdu.z);

                dpdu.Faceforward(n);

                //Console.WriteLine("normal " + n.x + " " + n.y + " " + n.z);

                double abDot = Vector3.Dot(n, dpdu);
                double bbDot = Vector3.Dot(n, n);

                Vector3 proj = abDot / bbDot * n;

                dpdu -= proj;

                //Console.WriteLine("proj " + proj.x + " " + proj.y + " " + proj.z);

                //Console.WriteLine("dot " + Vector3.Dot(n, dpdu));

                var si = new SurfaceInteraction(pHit, n, -ray.d, dpdu, this);
                //var si = new SurfaceInteraction(pHit, new Vector3(0, 0, 1), -ray.d, dpdu, this);


                return(tShapeHit, ObjectToWorld.Apply(si));
            }

            // A dummy return example
            //double dummyHit = 0.0;
            //Vector3 dummyVector = new Vector3(0, 0, 0);
            //SurfaceInteraction dummySurfaceInteraction = new SurfaceInteraction(dummyVector, dummyVector, dummyVector, dummyVector, this);
            //return (dummyHit, dummySurfaceInteraction);
        }