WritePPM() public static method

public static WritePPM ( Vector3 data ) : void
data Vector3
return void
Esempio n. 1
0
    public static void Main(String[] args)
    {
        spheres[0] = (new Sphere(
                          new Vector3(0, -10002, 0),
                          9999,
                          new Vector3(1, 1, 1),
                          false));

        spheres[1] = (new Sphere(
                          new Vector3(-10012, 0, 0),
                          9999,
                          new Vector3(1, 0, 0),
                          false));

        spheres[2] = (new Sphere(
                          new Vector3(10012, 0, 0),
                          9999,
                          new Vector3(0, 1, 0),
                          false));

        spheres[3] = (new Sphere(
                          new Vector3(0, 0, -10012),
                          9999,
                          new Vector3(1, 1, 1),
                          false));

        spheres[4] = (new Sphere(
                          new Vector3(0, 10012, 0),
                          9999,
                          new Vector3(1, 1, 1),
                          true));

        spheres[5] = (new Sphere(
                          new Vector3(-5, 0, 2),
                          2,
                          new Vector3(1, 1, 0),
                          false));

        spheres[6] = (new Sphere(
                          new Vector3(0, 5, -1),
                          4,
                          new Vector3(1, 0, 0),
                          false));

        spheres[7] = (new Sphere(
                          new Vector3(8, 5, -1),
                          2,
                          new Vector3(0, 0, 1),
                          false));

        var data = new Vector3[RayBench.HEIGHT][];
        var cam  = new Camera {
            eye = new Vector3(0.0f, 4.5f, 75.0f),
            lt  = new Vector3(-8, 9, 50),
            rt  = new Vector3(8, 9, 50),
            lb  = new Vector3(-8, 0, 50),
        };
        var vdu = cam.rt.Sub(cam.lt).Div(RayBench.WIDTH);
        var vdv = cam.lb.Sub(cam.lt).Div(RayBench.HEIGHT);

        var options = new ParallelOptions();

        options.MaxDegreeOfParallelism = Environment.ProcessorCount;

        Parallel.For(0, RayBench.HEIGHT, options, y => {
            var random = rnd.Value;
            data[y]    = new Vector3[RayBench.WIDTH];
            for (int x = 0; x < RayBench.WIDTH; ++x)
            {
                var color = new Vector3();
                var ray   = new Ray();

                ray.origin = cam.eye;

                for (int i = 0; i < RayBench.SAMPLES; ++i)
                {
                    ray.direction = cam.lt.Add(
                        vdu.Mul(x + random.NextFloat()).Add(
                            vdv.Mul(y + random.NextFloat())));

                    ray.direction = ray.direction.Sub(ray.origin);
                    ray.direction = ray.direction.Unit();
                    color         = color.Add(RayBench.trace(ray, 0, random));
                }

                color = color.Div(RayBench.SAMPLES);

                data[y][x] = color;
            }
        });

        RayBench.WritePPM(data);
    }
Esempio n. 2
0
    public static void Main(String[] args)
    {
        spheres[0] = (new Sphere(
                          new Vector3(0, -10002, 0),
                          9999,
                          new Vector3(1, 1, 1),
                          false));

        spheres[1] = (new Sphere(
                          new Vector3(-10012, 0, 0),
                          9999,
                          new Vector3(1, 0, 0),
                          false));

        spheres[2] = (new Sphere(
                          new Vector3(10012, 0, 0),
                          9999,
                          new Vector3(0, 1, 0),
                          false));

        spheres[3] = (new Sphere(
                          new Vector3(0, 0, -10012),
                          9999,
                          new Vector3(1, 1, 1),
                          false));

        spheres[4] = (new Sphere(
                          new Vector3(0, 10012, 0),
                          9999,
                          new Vector3(1, 1, 1),
                          true));

        spheres[5] = (new Sphere(
                          new Vector3(-5, 0, 2),
                          2,
                          new Vector3(1, 1, 0),
                          false));

        spheres[6] = (new Sphere(
                          new Vector3(0, 5, -1),
                          4,
                          new Vector3(1, 0, 0),
                          false));

        spheres[7] = (new Sphere(
                          new Vector3(8, 5, -1),
                          2,
                          new Vector3(0, 0, 1),
                          false));

        var data = new Vector3[RayBench.HEIGHT][];
        var cam  = new Camera();
        var vdu  = cam.rt.Sub(cam.lt).Div(RayBench.WIDTH);
        var vdv  = cam.lb.Sub(cam.lt).Div(RayBench.HEIGHT);

        var options = new ParallelOptions();

        options.MaxDegreeOfParallelism = 4;

        //for(int y = 0; y < RayBench.HEIGHT; ++y) {
        Parallel.For(0, RayBench.HEIGHT, options, y => {
            data[y] = new Vector3[RayBench.WIDTH];
            for (int x = 0; x < RayBench.WIDTH; ++x)
            {
                var color = new Vector3();
                var ray   = new Ray();

                ray.origin = cam.eye;

                for (int i = 0; i < RayBench.SAMPLES; ++i)
                {
                    ray.direction = cam.lt.Add(
                        vdu.Mul((float)(x + rnd.NextDouble())).Add(
                            vdv.Mul((float)(y + rnd.NextDouble()))));

                    ray.direction = ray.direction.Sub(ray.origin);
                    ray.direction = ray.direction.Unit();
                    color         = color.Add(RayBench.trace(ray, 0));
                }

                color = color.Div(RayBench.SAMPLES);

                data[y][x] = color;
            }
        });

        RayBench.WritePPM(data);
    }