示例#1
0
        public Tuple ShadeHit(World w, Computation comps, int remaining)
        {
            Tuple surface = Tuple.Color(0, 0, 0);

            foreach (var light in w.Lights)
            {
                bool shadowed = w.IsShadowed(light, comps.OverPoint);
                surface += Lighting(comps.Material,
                                    comps.Object,
                                    light,
                                    comps.Point,
                                    comps.Eyev,
                                    comps.Normalv,
                                    shadowed);
            }

            Tuple reflected = ReflectedColor(w, comps, remaining);
            Tuple refracted = RefractedColor(w, comps, remaining);

            Material material = comps.Material;

            if (material.Reflective > 0 && material.Transparency > 0)
            {
                float reflectance = Fresnel.Schlick(comps);
                return(surface + reflected * reflectance + refracted * (1 - reflectance));
            }

            return(surface + reflected + refracted);
        }
示例#2
0
        public override Tuple Lighting(Material m, Shape shape, Light l, Tuple position, Tuple toEye, Tuple normal, bool inShadow)
        {
            Tuple color = (m.Pattern == null)? m.Color : m.Pattern.ColorAtShape(shape, position);

            Tuple effectiveColor = color * l.Intensity;

            Tuple toLight = (l.Position - position).Normalize();

            Tuple ambient = effectiveColor * m.Ambient;

            float ndotl = normal.Dot(toLight);

            Tuple diffuse  = Tuple.Color(0, 0, 0);
            Tuple specular = Tuple.Color(0, 0, 0);

            if ((inShadow == false) && (ndotl >= 0))
            {
                diffuse = effectiveColor * m.Diffuse * ndotl;

                Tuple reflect = (-toLight).Reflect(normal);
                float rdote   = reflect.Dot(toEye);

                if (rdote > 0)
                {
                    specular = l.Intensity * m.Specular * Pow(rdote, m.Shininess);
                }
            }

            return(ambient + diffuse + specular);
        }
示例#3
0
        public Tuple RefractedColor(World w, Computation comps, int remaining)
        {
            if ((remaining <= 0) ||
                (Abs(comps.Material.Transparency) < Constants.floatEps))
            {
                return(Tuple.Color(0, 0, 0));
            }



            float nRatio      = comps.N1 / comps.N2;
            float cosTheta_i  = comps.Eyev.Dot(comps.Normalv);
            float sin2Theta_t = nRatio * nRatio * (1 - cosTheta_i * cosTheta_i);

            if (sin2Theta_t >= 1)
            {
                return(Tuple.Color(0, 0, 0));
            }

            float cosTheta_t = Sqrt(1 - sin2Theta_t);

            Tuple direction = comps.Normalv * (nRatio * cosTheta_i - cosTheta_t) - comps.Eyev * nRatio;

            Ray   refractRay = new Ray(comps.UnderPoint, direction);
            Tuple color      = ColorAt(w, refractRay, remaining - 1);

            return(color * comps.Material.Transparency);
        }
示例#4
0
 public Material()
 {
     Color           = Tuple.Color(1, 1, 1);
     Ambient         = 0.1f;
     Diffuse         = 0.9f;
     Specular        = 0.9f;
     Shininess       = 200.0f;
     RefractiveIndex = 1;
 }
示例#5
0
        public Tuple ReflectedColor(World w, Computation comps, int remaining)
        {
            if ((remaining <= 0) ||
                (Abs(comps.Material.Reflective) < Constants.floatEps))
            {
                return(Tuple.Color(0, 0, 0));
            }

            Ray   reflectRay = new Ray(comps.OverPoint, comps.Reflectv);
            Tuple color      = ColorAt(w, reflectRay, remaining - 1);

            return(color * comps.Material.Reflective);
        }
示例#6
0
        public Tuple ColorAt(World w, Ray r, int remaining)
        {
            List <Intersection> intersections = w.Intersection(r);
            Intersection        intersection  = Intersection.Hit(intersections);

            if (intersection == null)
            {
                return(Tuple.Color(0, 0, 0));
            }
            Computation comps = new Computation(intersection, r, intersections);

            return(ShadeHit(w, comps, remaining));
        }
示例#7
0
        public Canvas(int width, int height)
        {
            Width   = width;
            Height  = height;
            _pixels = new Tuple[Height, Width];

            for (int y = 0; y < Height; ++y)
            {
                for (int x = 0; x < Width; ++x)
                {
                    _pixels[y, x] = Tuple.Color(0, 0, 0);
                }
            }
        }
示例#8
0
        public DefaultWorld()
        {
            Lights.Add(new PointLight(Tuple.Point(-10, 10, -10), Tuple.Color(1, 1, 1)));

            Shape s1 = new Sphere();

            s1.Material.Color    = Tuple.Color(0.8f, 1, 0.6f);
            s1.Material.Diffuse  = 0.7f;
            s1.Material.Specular = 0.2f;

            Shapes.Add(s1);

            Shape s2 = new Sphere();

            s2.Transform = Transformation.Scaling(0.5f, 0.5f, 0.5f);

            Shapes.Add(s2);
        }
示例#9
0
 private static Tuple ReadColor(JToken jToken)
 {
     return(Tuple.Color((float)jToken[0], (float)jToken[1], (float)jToken[2]));
 }