Ejemplo n.º 1
0
        public override IPixelColor GetPointLightColor(IRayHit hit, ILight light)
        {
            IVector lightRayVec = new Vector(hit.Intersection, light.Position);
            IRay    lightRay    = new Ray(hit.Intersection, lightRayVec, lightRayVec.Length());

            IRGBColor diffuseStrength = hit.IntersectShape.Material.Diffuse;

            IVector   halfway          = lightRay.Direction.Halfway(hit.Ray.Direction.Negate());
            float     specularFactor   = ((float)Math.Pow(Math.Max(0.0f, hit.Normal.Dot(halfway)), hit.IntersectShape.Material.Shiny));
            IRGBColor specularColor    = new RGBColor(specularFactor, specularFactor, specularFactor);
            IRGBColor specularStrength = hit.IntersectShape.Material.Specular.Intensify(specularColor);

            IRGBColor shapeColor = hit.IntersectShape.Material.GetColor(hit.Intersection);

            float r = light.Color.Red * (shapeColor.Red * diffuseStrength.Red + specularStrength.Red);
            float g = light.Color.Green * (shapeColor.Green * diffuseStrength.Green + specularStrength.Green);
            float b = light.Color.Blue * (shapeColor.Blue * diffuseStrength.Blue + specularStrength.Blue);

            IPixelColor pixelLightColor = new PixelColor
            {
                Ambient  = new RGBColor(0.0f, 0.0f, 0.0f),
                Diffuse  = new RGBColor(diffuseStrength.Red, diffuseStrength.Green, diffuseStrength.Blue),
                Specular = new RGBColor(specularStrength.Red, specularStrength.Green, specularStrength.Blue),
                Color    = new RGBColor(r, g, b)
            };

            return(pixelLightColor);
        }
Ejemplo n.º 2
0
        public IPixelColor Trace(IRay ray, int depth)
        {
            IRayHit hit = this.FindHit(ray);

            if (hit == null)
            {
                return(this.BackGroundColor);
            }

            IPixelColor color = new PixelColor
            {
                Ambient  = new RGBColor(0.0f, 0.0f, 0.0f),
                Diffuse  = new RGBColor(0.0f, 0.0f, 0.0f),
                Specular = new RGBColor(0.0f, 0.0f, 0.0f),
                Color    = new RGBColor(0.0f, 0.0f, 0.0f)
            };

            this.World.Lights.ForEach(delegate(ILight light)
            {
                IPixelColor lightColor = this.Shading.GetColor(hit, light);

                if (light.Type == LightType.PointLight)
                {
                    IVector lightRayVec = new Vector(hit.Intersection, light.Position);
                    IRay lightRay       = new Ray(hit.Intersection, lightRayVec, lightRayVec.Length());
                    IRayHit obstruction = this.FindHit(lightRay);

                    if (obstruction != null)
                    {
                        if (obstruction.IntersectShape is Sphere && obstruction.IntersectShape != hit.IntersectShape)
                        {
                            IVector vectorIntersectionToObstruction = new Vector(hit.Intersection, obstruction.Intersection);
                            float distanceToObstruction             = vectorIntersectionToObstruction.Length();
                            float distanceToLight = lightRayVec.Length();
                            float distanceFactor  = distanceToObstruction / distanceToLight;

                            lightColor.Color = lightColor.Color.Intensify(distanceFactor);
                        }
                    }
                }

                color = color.Blend(lightColor);
            });

            if (depth < this.MaxRecursionLevel)
            {
                if (hit.IntersectShape.Material.IsReflective())
                {
                    color = color.Blend(this.Trace(hit.GetReflectionRay(), depth + 1).Intensify(hit.IntersectShape.Material.Reflection));
                }

                if (hit.IntersectShape.Material.IsRefractive())
                {
                    color = color.Blend(this.Trace(hit.GetRefractionRay(), depth + 1).Intensify(hit.IntersectShape.Material.Refraction));
                }
            }

            return(color);
        }
Ejemplo n.º 3
0
 private void EnterCall(IRayHit hit)
 {
     if (mPrev.IsNotNull())
     {
         return;
     }
     mHitStart = Time.time;
     mPrev     = hit;
     hit.OnEnter(mFrom);
     return;
 }
Ejemplo n.º 4
0
 private void ExitToObject()
 {
     if (mPrev.IsNull())
     {
         return;
     }
     mPrev.OnExit(mFrom, Time.time - mHitStart);
     mHitStart = Time.time;
     mPrev     = null;
     Debug.Log("ExitToObject");
 }
Ejemplo n.º 5
0
        public IPixelColor GetColor(IRayHit hit, ILight light)
        {
            switch (light.Type)
            {
            case LightType.AmbientLight:
                return(this.GetAmbientLightColor(hit, light));

            case LightType.PointLight:
                return(this.GetPointLightColor(hit, light));

            default:
                return(null);
            }
        }
Ejemplo n.º 6
0
        public IPixelColor GetAmbientLightColor(IRayHit hit, ILight light)
        {
            IRGBColor ambientColor = hit.IntersectShape.Material.Ambient.Intensify(light.Color);

            IRGBColor shapeColor = hit.IntersectShape.Material.GetColor(hit.Intersection);

            IPixelColor pixelLightColor = new PixelColor
            {
                Ambient  = ambientColor.Intensify(shapeColor),
                Diffuse  = new RGBColor(0.0f, 0.0f, 0.0f),
                Specular = new RGBColor(0.0f, 0.0f, 0.0f),
                Color    = ambientColor.Intensify(shapeColor)
            };

            return(pixelLightColor);
        }
        public IRayHit Intersect(IRay ray)
        {
            IRayHit rayHit = null;
            float   time   = float.MaxValue;

            foreach (IShape shape in this.Shapes)
            {
                IRayHit h = shape.Intersect(ray);
                if (h != null && h.Time < time)
                {
                    rayHit = h;
                    time   = h.Time;
                }
            }

            return(rayHit);
        }
Ejemplo n.º 8
0
        public IRayHit FindHit(IRay ray)
        {
            IRayHit rayHit     = null;
            float   rayTimeHit = float.MaxValue;

            this.World.Shapes.ForEach(delegate(IShape shape)
            {
                IRayHit rayShapeHit = shape.Intersect(ray);
                if (rayShapeHit != null && rayShapeHit.Time < rayTimeHit)
                {
                    rayHit     = rayShapeHit;
                    rayTimeHit = rayShapeHit.Time;
                }
            });

            return(rayHit);
        }
 public override IPixelColor GetPointLightColor(IRayHit hit, ILight light)
 {
     return(null);
 }
Ejemplo n.º 10
0
 private void StayEventCall(IRayHit hit)
 {
     hit.OnStay(mFrom, Time.time - mHitStart);
 }
Ejemplo n.º 11
0
 public abstract IPixelColor GetPointLightColor(IRayHit hit, ILight light);