Пример #1
0
        public Ray GetRay(float s, float t)
        {
            var temp           = MathR.RandomPointInDisc() * LensRadius;
            var apertureOffset = (U * temp.X) + (V * temp.Y);

            return(new Ray(Origin + apertureOffset, LowerLeftCorner + (U * s * 2 * HalfWidth) + (V * t * 2 * HalfHeight) - (Origin + apertureOffset)));
        }
Пример #2
0
        public static Vec3 Refract(this Vec3 uv, Vec3 n, float etaiOverEtat)
        {
            var cosTheta          = Math.Min(-MathR.Dot(uv, n), 1);
            var rOutParallel      = (uv + (n * cosTheta)) * etaiOverEtat;
            var rOutPerpendicular = n * -MathF.Sqrt(1 - Math.Min(rOutParallel.SquareLenght(), 1));

            return(rOutPerpendicular + rOutParallel);
        }
Пример #3
0
        public static byte FinalizeColor(float pixelColor, int samplesPerPixel)
        {
            var c = pixelColor;

            // Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.
            if (float.IsNaN(c))
            {
                c = 0.0f;
            }

            // Divide the color by the number of samples and gamma-correct for gamma=2.0.
            var scale = 1.0f / samplesPerPixel;

            c = MathF.Sqrt(scale * c);

            // return the translated [0,255] value of each color component.
            return(Convert.ToByte(255 * MathR.Clamp(c, 0.0f, 0.999f)));
        }
Пример #4
0
        public Camera(Vec3 origin, Vec3 lookAt, Vec3 vUp, float fovY, float aspectRatio, float aperture, float distToFocus) : this()
        {
            Origin      = origin;
            LookAt      = lookAt;
            VecUp       = vUp;
            DistToFocus = distToFocus;
            AspectRatio = aspectRatio;
            FovY        = MathR.ConvertDegreesToRadians(fovY);

            LensRadius = aperture / 2;
            HalfHeight = MathF.Tan(FovY / 2) * DistToFocus;
            HalfWidth  = HalfHeight * AspectRatio;

            W = MathR.Normalize(LookAt - Origin);
            U = MathR.Normalize(MathR.Cross(W, vUp));

            V = MathR.Cross(U, W);
            LowerLeftCorner = Origin + (W * DistToFocus) - (V * HalfHeight) - (U * HalfWidth);
        }
Пример #5
0
        public Vec3 GetColor(Ray ray, HitableList scene, int depth)
        {
            if (depth == 0)
            {
                return(new Vec3(0, 0, 0));
            }

            var hit = scene.GetNearestHit(ray, 0.0001F);

            if (hit != null)
            {
                var result = hit.Material.Scatter(ray, hit);
                if (result != default)
                {
                    return(GetColor(result.ray, scene, depth - 1) * result.color);
                }
            }
            var t = (MathR.Normalize(ray.Direction).Y + 1) * 0.5F;

            return((new Vec3(1, 1, 1) * (1.0F - t)) + (new Vec3(0.5F, 0.7F, 1) * t));
        }
Пример #6
0
        public static byte[] FinalizeColor(Vec3 pixelColor, int samplesPerPixel)
        {
            (var r, var g, var b) = pixelColor;

            // Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life.
            //if (r != r) r = 0.0f;
            //if (g != g) g = 0.0f;
            //if (b != b)  b = 0.0f;

            // Divide the color by the number of samples and gamma-correct for gamma=2.0.
            var scale = 1.0f / samplesPerPixel;

            r = MathF.Sqrt(scale * r);
            g = MathF.Sqrt(scale * g);
            b = MathF.Sqrt(scale * b);

            // return the translated [0,255] value of each color component.
            return(new[] {
                Convert.ToByte(255 * MathR.Clamp(r, 0.0f, 0.999f)),
                Convert.ToByte(255 * MathR.Clamp(g, 0.0f, 0.999f)),
                Convert.ToByte(255 * MathR.Clamp(b, 0.0f, 0.999f))
            });
        }