Exemple #1
0
    private Color TraceRay(Ray ray, Scene scene, int depth)
    {
        ISect isect = MinIntersection(ray, scene);

        if (ISect.IsNull(isect))
        {
            return(Color.Background);
        }
        return(Shade(isect, scene, depth));
    }
Exemple #2
0
        private static Color TraceRay(Ray ray, Scene scene, int depth)
        {
            ISect isect = IntersectRay(ray, scene);

            if (isect == null)
            {
                return(Color.Background);
            }
            return(Shade(ray, isect, scene, depth));
        }
Exemple #3
0
    private double TestRay(Ray ray, Scene scene)
    {
        ISect isect = MinIntersection(ray, scene);

        if (ISect.IsNull(isect))
        {
            return(0);
        }
        return(isect.Dist);
    }
Exemple #4
0
            private Color TraceRay(Ray ray, Scene scene, int depth)
            {
                var   isects = Intersections(ray, scene);
                ISect isect  = isects.FirstOrDefault();

                if (isect == null)
                {
                    return(Color.Background);
                }
                return(Shade(isect, scene, depth));
            }
Exemple #5
0
            private double TestRay(Ray ray, Scene scene)
            {
                var   isects = Intersections(ray, scene);
                ISect isect  = isects.FirstOrDefault();

                if (isect == null)
                {
                    return(0);
                }
                return(isect.Dist);
            }
Exemple #6
0
    private Color Shade(ISect isect, Scene scene, int depth)
    {
        Vector d          = isect.Ray.Dir;
        Vector pos        = Vector.Plus(Vector.Times(isect.Dist, isect.Ray.Dir), isect.Ray.Start);
        Vector normal     = isect.Thing.Normal(pos);
        Vector reflectDir = Vector.Minus(d, Vector.Times(2 * Vector.Dot(normal, d), normal));
        Color  ret        = Color.DefaultColor;

        ret = Color.Plus(ret, GetNaturalColor(isect.Thing, pos, normal, reflectDir, scene));
        if (depth >= MaxDepth)
        {
            return(Color.Plus(ret, new Color(.5, .5, .5)));
        }
        return(Color.Plus(ret, GetReflectionColor(isect.Thing, Vector.Plus(pos, Vector.Times(.001, reflectDir)), normal, reflectDir, scene, depth)));
    }
Exemple #7
0
        private static Color Shade(Ray ray, ISect isect, Scene scene, int depth)
        {
            var      pos        = isect.Pos;
            var      normal     = isect.Normal;
            var      reflectDir = ray.Dir - 2 * F64Vec3.Dot(normal, ray.Dir) * normal;
            Color    ret        = Color.Black;
            Material material   = isect.Material;

            ret += GetNaturalColor(material, pos, normal, reflectDir, scene);
            if (depth >= MaxDepth)
            {
                return(ret + new Color(F32.Half, F32.Half, F32.Half));
            }
            return(ret + GetReflectionColor(material, pos + F64.Ratio(1, 1000) * reflectDir, normal, reflectDir, scene, depth));
        }
Exemple #8
0
    private ISect MinIntersection(Ray ray, Scene scene)
    {
        ISect min = ISect.Null;

        foreach (SceneObject obj in scene.Things)
        {
            ISect isect = obj.Intersect(ray);
            if (!ISect.IsNull(isect))
            {
                if (ISect.IsNull(min) || min.Dist > isect.Dist)
                {
                    min = isect;
                }
            }
        }
        return(min);
    }
Exemple #9
0
        private static ISect IntersectRay(Ray ray, Scene scene)
        {
            F64   bestDist = F64.MaxValue;
            ISect best     = null;

            foreach (SceneObject obj in scene.Things)
            {
                ISect isect = obj.Intersect(ray);
                if (isect != null)
                {
                    if (isect.Dist < bestDist)
                    {
                        best     = isect;
                        bestDist = isect.Dist;
                    }
                }
            }

            return(best);
        }
Exemple #10
0
        private static Color GetNaturalColor(Material material, F64Vec3 pos, F64Vec3 norm, F64Vec3 rd, Scene scene)
        {
            Color ret = Color.Black;

            foreach (Light light in scene.Lights)
            {
                F64Vec3 ldis       = light.Pos - pos;
                F64Vec3 livec      = F64Vec3.NormalizeFastest(ldis);
                ISect   isect      = IntersectRay(new Ray(pos, livec), scene);
                bool    isInShadow = (isect != null) && (isect.Dist * isect.Dist) < F64Vec3.LengthSqr(ldis);
                if (!isInShadow)
                {
                    F32   illum    = F32.Max(F32.Zero, F64Vec3.Dot(livec, norm).F32);
                    Color lcolor   = illum * light.Color;
                    F32   specular = F64Vec3.Dot(livec, F64Vec3.NormalizeFastest(rd)).F32;
                    Color scolor   = specular > 0 ? F32.PowFastest(specular, material.Roughness) * light.Color : Color.Black;
                    ret += material.Diffuse * lcolor + material.Specular * scolor;
                }
            }
            return(ret);
        }
Exemple #11
0
 public static bool IsNull(ISect sect)
 {
     return sect == null;
 }
Exemple #12
0
 public static bool IsNull(ISect sect)
 {
     return(sect == null);
 }
Exemple #13
0
 private Color Shade(ISect isect, Scene scene, int depth)
 {
     Vector d = isect.Ray.Dir;
     Vector pos = Vector.Plus(Vector.Times(isect.Dist, isect.Ray.Dir), isect.Ray.Start);
     Vector normal = isect.Thing.Normal(pos);
     Vector reflectDir = Vector.Minus(d, Vector.Times(2 * Vector.Dot(normal, d), normal));
     Color ret = Color.DefaultColor;
     ret = Color.Plus(ret, GetNaturalColor(isect.Thing, pos, normal, reflectDir, scene));
     if (depth >= MaxDepth)
     {
         return Color.Plus(ret, new Color(.5, .5, .5));
     }
     return Color.Plus(ret, GetReflectionColor(isect.Thing, Vector.Plus(pos, Vector.Times(.001, reflectDir)), normal, reflectDir, scene, depth));
 }