Beispiel #1
0
        private Colour GetNaturalColor(Thing thing, Vector3 pos, Vector3 norm, Vector3 rd, Scene scene)
        {
            Colour ret = new Colour(0, 0, 0);

            foreach (LightSource light in scene.Lights)
            {
                Vector3 ldis       = light.Pos - pos;
                Vector3 livec      = ldis.Normalized();
                double  neatIsect  = TestRay(new Ray(pos, livec), scene);
                bool    isInShadow = !((neatIsect > ldis.Length()) || (neatIsect == 0));
                if (!isInShadow)
                {
                    double illum    = livec.DotProduct(norm);
                    Colour lcolor   = illum > 0 ? illum * light.Color : new Colour(0, 0, 0);
                    double specular = livec.DotProduct(rd.Normalized());
                    Colour scolor   = specular > 0 ? Math.Pow(specular, thing.Surface.Roughness) * light.Color : new Colour(0, 0, 0);
                    ret = ret + (thing.Surface.Diffuse(pos) * lcolor) + (thing.Surface.Specular(pos) * scolor);
                }
            }
            return(ret);
        }
Beispiel #2
0
        public override Intersection CalculateIntersection(Ray withRay)
        {
            Vector3 eo = Centre - withRay.Start;
            double  v  = eo.DotProduct(withRay.Dir);
            double  dist;

            if (v < 0)
            {
                dist = 0;
            }
            else
            {
                double disc = Math.Pow(Radius, 2) - (eo.DotProduct(eo) - Math.Pow(v, 2));
                dist = disc < 0 ? 0 : v - Math.Sqrt(disc);
            }
            if (dist == 0)
            {
                return(null);
            }
            return(new Intersection(this, withRay, dist));
        }