예제 #1
0
파일: csrb-mt.cs 프로젝트: lostmsu/raybench
    public static Vector3 trace(Ray ray, int depth, Random rnd)
    {
        var  color   = new Vector3();
        bool did_hit = false;
        var  hit     = new Hit();
        var  sp      = new Sphere();

        hit.dist = 1e15f;

        foreach (var s in spheres)
        {
            var lh = s.Hit(ray);

            if (lh != null && lh.Value.dist > 0.0001f && lh.Value.dist < hit.dist)
            {
                sp      = s;
                did_hit = true;
                color   = s.color;
                hit     = lh.Value;
            }
        }

        if (did_hit && depth < MAX_DEPTH)
        {
            if (sp.is_light != true)
            {
                var nray = new Ray(
                    hit.point,
                    RayBench.rnd_dome(hit.normal, rnd)
                    );
                var ncolor = RayBench.trace(nray, depth + 1, rnd);
                var at     = nray.direction.Dot(hit.normal);

                color = color.Mul(ncolor.Mul(at));
            }
        }

        if (did_hit == false || depth >= MAX_DEPTH)
        {
            return(new Vector3());
        }

        return(color);
    }
예제 #2
0
파일: csrb-mt.cs 프로젝트: lostmsu/raybench
    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);
    }
예제 #3
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);
    }