Example #1
0
        public void DrawRay(VectorMath.Ray ray, Screen screen, float distance, Vector3 color)
        {
            Point p1 = screen.ConvertToScreenCoords(ray.origin);
            Point p2 = screen.ConvertToScreenCoords(ray.origin + ray.direction * distance);

            Line(p1.X, p1.Y, p2.X, p2.Y, VectorMath.GetColorInt(color));
        }
Example #2
0
        private Vector3 ComputeColor(VectorMath.Ray ray, Intersection intersection, int reflectionNum)
        {
            //DIFFUSE
            //calculate the color at the intersection
            Vector3 diffuseColor = _scene.GetIntersectionColor(intersection);

            Vector3 reflectionColor = Vector3.Zero;
            float   reflectionIndex = intersection.primitive.Material.ReflectionIndex;

            //REFLECTION
            //if reflectionIndex > 0 calculate, compute the color
            if (_renderHigh)
            {
                if (reflectionIndex > 0)
                {
                    //calculate the reflectionRay
                    Vector3 reflection = VectorMath.Reflect(ray.direction, intersection.Normal).Normalized();

                    //Trace the reflectionRay and increase reflectionNum, so we do not get stuck in an infinite recursive loop
                    //Also at a little offset to the intersectionPoint to prevent shadow acne
                    reflectionColor += TraceRay(new VectorMath.Ray(intersection.IntersectionPoint + 0.01f * reflection, reflection), ++reflectionNum);
                }
            }

            //FINAL COLOR
            return(diffuseColor * (1 - reflectionIndex) + reflectionColor * reflectionIndex);
        }
Example #3
0
 public Intersection(Primitive primitive, Vector3 intersectionPoint, Vector3 normal, VectorMath.Ray ray, float distance)
 {
     _primitive         = primitive;
     _intersectionPoint = intersectionPoint;
     _normal            = normal;
     _ray      = ray;
     _distance = distance;
 }
Example #4
0
 private void DrawShadowRays(Intersection intersection)
 {
     VectorMath.Ray shadowRay;
     foreach (Light l in _scene.Lights)
     {
         //draw a white shadowray to the intersection if there is a shadow, else draw a pink ray with a large magnitude
         shadowRay = new VectorMath.Ray(intersection.IntersectionPoint, l.Position - intersection.IntersectionPoint);
         _surface.DrawRay(shadowRay, _camera.Screen, shadowRay.magnitude, l.InShadow(intersection, l.Position, _scene) ? new Vector3(1, 1, 1) : new Vector3(0, 1, 1));
     }
 }
Example #5
0
 /*
  * DEBUG
  */
 private void DrawDebugRays(Intersection intersection, VectorMath.Ray ray, int reflectionNum)
 {
     if ((intersection != null && intersection.IntersectionPoint.Y == 0) || reflectionNum > 0)
     {
         DrawTracedRay(intersection, ray);
         if (intersection != null)
         {
             DrawShadowRays(intersection);
         }
     }
 }
Example #6
0
        public override Intersection GetIntersection(VectorMath.Ray ray)
        {
            //if the ray is not parralel to the plane, calculate the intersection point
            float dotN_R = Vector3.Dot(_normal, ray.direction);

            if (dotN_R < 0)
            {
                Vector3 p = ray.origin + (-(Vector3.Dot(ray.origin, _normal) + Position.Length) / dotN_R) * ray.direction;
                return(new Intersection(this, p, _normal, ray, (p - ray.origin).Length));
            }
            else
            {
                return(null);
            }
        }
Example #7
0
        public Intersection GetClosestIntersection(VectorMath.Ray ray)
        {
            float        minD         = float.MaxValue;
            Intersection intersection = null;
            Intersection closest      = null;

            foreach (Primitive p in _primitives)
            {
                intersection = p.GetIntersection(ray);
                if (intersection != null && intersection.Distance > 0 && intersection.Distance < minD)
                {
                    minD    = intersection.Distance;
                    closest = intersection;
                }
            }
            return(closest);
        }
Example #8
0
        private Vector3 TraceRay(VectorMath.Ray ray, int reflectionNum)
        {
            //if this ray isn't recursively called more than maxReflection times trace this ray,
            //else increase rayCount, as the reflections have all been drawn and a new primary ray is going to be shot, instead of a reflective ray
            if (reflectionNum < REFLECTIONCAP)
            {
                //Cast a ray from the camera through a point on the 2D screen and find the primitive in the world it hits first
                Intersection intersection = _scene.GetClosestIntersection(ray);

                //if there was an intersection, compute the color to return
                if (intersection != null)
                {
                    DrawDebugRays(intersection, ray, reflectionNum);
                    return(ComputeColor(ray, intersection, reflectionNum));
                }
            }
            //increase rayCount, which is used for debugging, when a new PRIMARY ray is casted
            _rayCount++;
            return(Vector3.Zero);
        }
Example #9
0
        public override Intersection GetIntersection(VectorMath.Ray ray)
        {
            Vector3 centerToOrigin = ray.origin - Position;
            float   a = ray.direction.LengthSquared;
            float   b = 2 * Vector3.Dot(ray.direction, centerToOrigin);
            float   c = centerToOrigin.LengthSquared - _radius * _radius;

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

            if (d < 0)
            {
                return(null);
            }
            else
            {
                float   sqrtD             = (float)Math.Sqrt(d);
                float   t0                = (-b + sqrtD) / (2 * a);
                float   t1                = (-b - sqrtD) / (2 * a);
                float   distance          = Math.Min(t0, t1);
                Vector3 intersectionPoint = ray.origin + distance * ray.direction;
                return(new Intersection(this, intersectionPoint, (intersectionPoint - Position).Normalized(), ray, distance));
            }
        }
Example #10
0
 private void DrawTracedRay(Intersection intersection, VectorMath.Ray ray)
 {
     _surface.DrawRay(ray, _camera.Screen, intersection == null ? ray.magnitude * 10 : intersection.Distance, new Vector3(1, 0, 0));
 }
Example #11
0
 public virtual Intersection GetIntersection(VectorMath.Ray ray)
 {
     return(null);
 }