Exemplo n.º 1
0
        private IntersectionObject intersectSphere(Ray ray)
        {
            if (!parameters.ContainsKey("radius"))
            {
                throw new Exception("Key radius is absent for sphere object");
            }

            IntersectionObject interObject = new IntersectionObject();
            var radius = parameters["radius"];
            var a      = 1;
            var b      = 2 * Vector3.dot(ray.direction, ray.origin - position);
            var c      = (ray.origin - position).getNormSquare() - (radius * radius);

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

            if (delta < 0)
            {
                return(interObject);
            }
            var t1 = (-b - Math.Sqrt(delta)) / (2 * a);
            var t2 = (-b + Math.Sqrt(delta)) / (2 * a);

            if (t2 < 0)
            {
                return(interObject);
            }

            interObject.ray               = ray;
            interObject.obj               = this;
            interObject.hasIntersection   = true;
            interObject.distance          = t1 > 0 ? t1 : t2;
            interObject.intersectionPoint = ray.origin + ray.direction * interObject.distance;
            interObject.iPointNormal      = (interObject.intersectionPoint - position).normalized();
            return(interObject);
        }
Exemplo n.º 2
0
        private IntersectionObject intersectPlane(Ray ray)
        {
            if (!parameters.ContainsKey("normaleX"))
            {
                throw new Exception("Key normaleX is absent for plane object");
            }
            if (!parameters.ContainsKey("normaleY"))
            {
                throw new Exception("Key normaleY is absent for plane object");
            }
            if (!parameters.ContainsKey("normaleZ"))
            {
                throw new Exception("Key normaleZ is absent for plane object");
            }

            IntersectionObject interObject = new IntersectionObject();
            Vector3            planNormal  = new Vector3(parameters["normaleX"], parameters["normaleY"], parameters["normaleZ"]);

            var    denom = planNormal.dot(ray.direction);
            double t;

            if (denom == 0)
            {
                return(interObject);
            }

            t = (-(planNormal.dot(ray.origin - position))) / denom;

            if (t < 0)
            {
                return(interObject);
            }

            interObject.hasIntersection   = true;
            interObject.intersectionPoint = ray.origin + ray.direction * t;

            if (denom < 0)
            {
                interObject.iPointNormal = planNormal;
            }

            else
            {
                interObject.iPointNormal = -planNormal;
            }

            interObject.obj = this;
            interObject.ray = ray;
            return(interObject);
        }
Exemplo n.º 3
0
        public MyColorClass getLighAt(IntersectionObject intersectionObject, double _distance, bool shadow)
        {
            var lightVector = intersectionObject.intersectionPoint - position;

            lightVector.normalize();

            var angle = intersectionObject.iPointNormal.dot(-lightVector);

            if (angle <= 0)
            {
                return(new MyColorClass());
            }

            var newDistance = Math.Round(_distance / distance, 4);

            var finalColor = intersectionObject.obj.material.diffuseColor * angle * intensity * color - newDistance;

            if (shadow)
            {
                return(intersectionObject.obj.material.diffuseColor * angle * color * angle);
            }
            return(finalColor);
        }