Beispiel #1
0
        /// <summary>
        /// Creates a camera.
        /// </summary>
        /// <param name="lookFrom">The point where the camera is located</param>
        /// <param name="lookAt">The point that the camera is looking at</param>
        /// <param name="up">The up direction</param>
        /// <param name="verticalFov">Vertical field of view in radians</param>
        /// <param name="aspectRatio">width over height</param>
        public Camera(
            UnitCircleUniformSampler rng,
            Vector3 lookFrom,
            Vector3 lookAt,
            Vector3 up,
            double verticalFov,
            double aspectRatio,
            double aperature,
            double focusDist)
        {
            this.rng = rng;

            this.lensRadius = aperature / 2.0;
            double halfHeight = Math.Atan(verticalFov / 2.0);
            double halfWidth  = aspectRatio * halfHeight;

            this.origin = lookFrom;
            this.w      = (lookFrom - lookAt).ToUnitVector();
            this.u      = up.Cross(w).ToUnitVector();
            this.v      = w.Cross(u);

            this.lowerLeftCorner = this.origin - halfWidth * focusDist * u - halfHeight * focusDist * v - focusDist * w;
            this.horizontal      = 2.0 * halfWidth * focusDist * u;
            this.vertical        = 2.0 * halfHeight * focusDist * v;
        }
Beispiel #2
0
        public void RenderScene()
        {
            int nx = 800;
            int ny = 400;
            int ns = 50;

            Random rng = new Random();
            UnitCircleUniformSampler circleSampler = new UnitCircleUniformSampler(rng);

            HitableList world = this.GenerateWorld(rng);

            Console.WriteLine("Generated the world");

            Vector3 lookFrom = new Vector3(8, 2, 1.5);
            Vector3 lookAt   = new Vector3(0, 0.0, 0);
            Camera  camera   = new Camera(
                circleSampler,
                lookFrom,
                lookAt,
                new Vector3(0, 1, 0),
                45.0 * Math.PI / 180.0,
                (double)nx / (double)ny,
                0.005,
                (lookAt - lookFrom).Length());

            Random random = new Random();

            // Create folder to output frames
            if (!Directory.Exists("RandomMarbles"))
            {
                Directory.CreateDirectory("RandomMarbles");
            }

            Sensor sensor   = new Sensor(nx, ny, new SqrtColorSpace(), RGBColor.Black);
            int    frameNum = 0;

            for (int s = 0; s < ns; ++s)
            {
                for (int j = ny - 1; j >= 0; --j)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        double u = (i + random.NextDouble()) / nx;
                        double v = (j + random.NextDouble()) / ny;

                        Ray3    r     = new Ray3(camera.GetRay(u, v));
                        Vector3 color = Program.Color(r, world, 0);
                        sensor.AddSample(i, j, new RGBColor(color[0], color[1], color[2]));
                    }
                }

                if (s % (ns / 10) == 0)
                {
                    // Output 1 frame with current number of samples
                    sensor.WritePNGFile($"RandomMarbles\\frame_{frameNum}.png").Wait();
                    frameNum++;
                }

                Console.Write("\r{0}", s);
            }

            sensor.WritePNGFile("RandomMarbles.png").Wait();
        }
Beispiel #3
0
        public void RenderScene()
        {
            int nx = 800;
            int ny = 400;
            int ns = 100;

            var lowerLeftCorner = new Vector3(-2.0, -1.0, -1.0);
            var horizontal      = new Vector3(4.0, 0.0, 0.0);
            var vertical        = new Vector3(0.0, 2.0, 0.0);
            var origin          = new Vector3(0.0, 0.0, 0.0);

            Random rng = new Random();
            UnitSphereUniformSampler sphereSampler = new UnitSphereUniformSampler(rng);
            UnitCircleUniformSampler circleSampler = new UnitCircleUniformSampler(rng);

            IMaterial mat1 = new Lambertian(new Vector3(0.1, 0.2, 0.5), sphereSampler);
            IMaterial mat2 = new Lambertian(new Vector3(0.8, 0.8, 0.0), sphereSampler);
            IMaterial mat3 = new Metal(new Vector3(0.8, 0.6, 0.2), 0.3, sphereSampler);
            IMaterial mat4 = new Dielectric(1.5, rng);

            List <IHitable> objects = new List <IHitable>(4);

            objects.Add(new Sphere(new Vector3(0, 0, -1), 0.5, mat1));
            objects.Add(new Sphere(new Vector3(0, -100.5, -1), 100, mat2));
            objects.Add(new Sphere(new Vector3(1, 0, -1), 0.5, mat3));
            objects.Add(new Sphere(new Vector3(-1, 0, -1), 0.5, mat4));
            objects.Add(new Sphere(new Vector3(-1, 0, -1), -0.45, mat4));

            HitableList world    = new HitableList(objects);
            Vector3     lookFrom = new Vector3(-2, 2, 1);
            Vector3     lookAt   = new Vector3(0, 0, -1);
            Camera      camera   = new Camera(
                circleSampler,
                lookFrom,
                lookAt,
                new Vector3(0, 1, 0),
                90.0 * Math.PI / 180.0,
                (double)nx / (double)ny,
                0.2,
                (lookAt - lookFrom).Length());

            Random random = new Random();

            // Create folder to output frames
            if (!Directory.Exists("ThreeSpheres"))
            {
                Directory.CreateDirectory("ThreeSpheres");
            }

            Sensor sensor   = new Sensor(nx, ny, new SqrtColorSpace(), RGBColor.Black);
            int    frameNum = 0;

            for (int s = 0; s < ns; ++s)
            {
                for (int j = ny - 1; j >= 0; --j)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        double u = (i + random.NextDouble()) / nx;
                        double v = (j + random.NextDouble()) / ny;

                        Ray3    r     = new Ray3(camera.GetRay(u, v));
                        Vector3 color = Program.Color(r, world, 0);
                        sensor.AddSample(i, j, new RGBColor(color[0], color[1], color[2]));
                    }
                }

                if (s % (ns / 10) == 0)
                {
                    // Output 1 frame with current number of samples
                    sensor.WritePPMFile($"ThreeSpheres\\frame_{frameNum}.ppm").Wait();
                    frameNum++;
                }

                Console.Write("\r{0}", s);
            }

            sensor.WritePPMFile("finalOutput.ppm").Wait();
        }