Ejemplo n.º 1
0
    private void Final()
    {
        Vector3D lookFrom    = new Vector3D(278, 278, -800);
        Vector3D lookAt      = new Vector3D(278, 278, 0);
        float    diskToFocus = 10;
        float    aperture    = 0;
        float    vfov        = 40;

        camera = new Camera(lookFrom, lookAt, new Vector3D(0, 1, 0), vfov,
                            (float)width / (float)height, aperture, diskToFocus, 0, 1);

        List <Hitable> list = new List <Hitable>();

        Material red   = new Lambertian(new ConstantTexture(new Vector3D(0.65f, 0.05f, 0.05f)));
        Material white = new Lambertian(new ConstantTexture(new Vector3D(0.73f, 0.73f, 0.73f)));
        Material green = new Lambertian(new ConstantTexture(new Vector3D(0.12f, 0.45f, 0.15f)));
        Material light = new DiffuseLight(new ConstantTexture(new Vector3D(15, 15, 15)));

        list.Add(new FlipNormals(new XZRect(213, 343, 227, 332, 554, light)));
        list.Add(new FlipNormals(new YZRect(0, 555, 0, 555, 555, green)));
        list.Add(new YZRect(0, 555, 0, 555, 0, red));
        list.Add(new FlipNormals(new XZRect(0, 555, 0, 555, 555, white)));
        list.Add(new XZRect(0, 555, 0, 555, 0, white));
        list.Add(new FlipNormals(new XYRect(0, 555, 0, 555, 555, white)));

        list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                   new Vector3D(165, 165, 165), white), -18), new Vector3D(130, 0, 65)));
        list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                   new Vector3D(165, 330, 165), white), 15), new Vector3D(265, 0, 295)));
        BVHNode b = new BVHNode(list, list.Count, 0, 1);

        world.list.Add(b);
    }
Ejemplo n.º 2
0
        public void Update(GameTime gameTime, float heightAbovePlanetSurface, Vector3 cameraPos)
        {
            if (SystemCore.ActiveScene.LightsInScene.Count > 0)
            {
                DiffuseLight light = SystemCore.ActiveScene.LightsInScene[0] as DiffuseLight;

                if (groundScatteringHelper != null)
                {
                    groundScatteringHelper.Update(heightAbovePlanetSurface, light.LightDirection,
                                                  cameraPos - CurrentCenterPosition);

                    if (atmosphere != null)
                    {
                        atmosphere.Update(light.LightDirection, cameraPos, heightAbovePlanetSurface);
                    }
                }

                if (CurrentOrbit != null)
                {
                    Vector3 pointToOrbit = CurrentOrbit.OrbitPoint;
                    if (CurrentOrbit.BodyToOrbit != null)
                    {
                        pointToOrbit = CurrentOrbit.BodyToOrbit.CurrentCenterPosition;
                    }

                    CalculateOrbit(pointToOrbit, CurrentOrbit.Axis, CurrentOrbit.Speed);
                }
                if (CurrentSpin != null)
                {
                    CalculateRotation(CurrentSpin.Axis, CurrentSpin.Speed);
                }
            }
        }
Ejemplo n.º 3
0
        public VolumeScene(double aspect)
        {
            var red   = new Lambertian(ConstantTexture.Create(0.65, 0.05, 0.05));
            var white = new Lambertian(ConstantTexture.Create(0.73, 0.73, 0.73));
            var green = new Lambertian(ConstantTexture.Create(0.12, 0.45, 0.12));
            var light = new DiffuseLight(ConstantTexture.Create(7, 7, 7));
            var box1  = new Translate(new RotateY(new Box(Vec3.Create(0), Vec3.Create(165), white), -18), Vec3.Create(130, 0, 65));
            var box2  = new Translate(new RotateY(new Box(Vec3.Create(0), Vec3.Create(165, 330, 165), white), 15), Vec3.Create(265, 0, 295));

            IHitable[] hitables =
            {
                new FlipNormals(new YZRect(0,  555,                                    0, 555, 555, green)),
                new YZRect(0,                  555,                                    0, 555,   0, red),
                new XZRect(113,                443,                                  127, 432, 554, light),
                new FlipNormals(new XZRect(0,  555,                                    0, 555, 555, white)),
                new XZRect(0,                  555,                                    0, 555,   0, white),
                new FlipNormals(new XYRect(0,  555,                                    0, 555, 555, white)),
                new ConstantMedium(box1,      0.01, new ConstantTexture(Vec3.Create(1))),
                new ConstantMedium(box2,      0.01, new ConstantTexture(Vec3.Create(0))),
            };
            World = new BVHNode(hitables, 0, 1);
            var    lookFrom      = Vec3.Create(278, 278, -800);
            var    lookAt        = Vec3.Create(278, 278, 0);
            double dist_to_focus = 10;
            double aderpture     = 0;
            double vfov          = 40;

            Camera = Camera.CreateLookAt(lookFrom, lookAt, Vec3.Create(0, 1, 0), vfov, aspect, aderpture, dist_to_focus);
        }
Ejemplo n.º 4
0
        public List <IHittable> Generate()
        {
            var tex      = new NoiseTexture(Vec3.One, Vec3.Zero, 4);
            var mat      = new Lambertian(tex);
            var toReturn = new List <IHittable>();

            toReturn.Add(new Sphere
            {
                Center   = new Vec3(0, -1000, 0),
                Radius   = 1000,
                Material = mat
            });

            toReturn.Add(new Sphere
            {
                Center   = new Vec3(0, 2, 0),
                Radius   = 2,
                Material = mat
            });

            var diffLight = new DiffuseLight(new Vec3(4, 4, 4));

            toReturn.Add(new RectXY(3, 5, 1, 3, -2, diffLight));

            toReturn.Add(new Sphere
            {
                Center   = new Vec3(0, 7, 0),
                Radius   = 2,
                Material = diffLight
            });

            return(toReturn);
        }
Ejemplo n.º 5
0
        public void Draw(GameTime gameTime)
        {
            if (!Visible)
            {
                return;
            }

            var transform      = ParentObject.GetComponent <TransformComponent>();
            var renderGeometry = ParentObject.GetComponent <RenderGeometryComponent>();

            effect.World      = Matrix.CreateScale(ParentObject.Transform.Scale * 1.01f) * transform.AbsoluteTransform;
            effect.View       = SystemCore.ActiveCamera.View;
            effect.Projection = SystemCore.ActiveCamera.Projection;


            DiffuseLight sun = SystemCore.ActiveScene.LightsInScene[0] as DiffuseLight;

            effect.DirectionalLight0.Direction    = sun.LightDirection;
            effect.DirectionalLight0.DiffuseColor = sun.LightColor.ToVector3();


            SystemCore.GraphicsDevice.SetVertexBuffer(renderGeometry.VertexBuffer);
            SystemCore.GraphicsDevice.Indices = renderGeometry.IndexBuffer;

            GameObjectManager.verts      += renderGeometry.VertexBuffer.VertexCount;
            GameObjectManager.primitives += renderGeometry.PrimitiveCount;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                SystemCore.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, renderGeometry.VertexBuffer.VertexCount, 0, renderGeometry.PrimitiveCount);
            }
        }
Ejemplo n.º 6
0
        public override Hitable GetObjects()
        {
            var list = new List <Hitable>();

            Material red   = new Lambertian(new ConstantTexture(new Vec3(0.65, 0.05, 0.05)));
            Material white = new Lambertian(new ConstantTexture(new Vec3(0.73, 0.73, 0.73)));
            Material green = new Lambertian(new ConstantTexture(new Vec3(0.12, 0.45, 0.15)));
            Material light = new DiffuseLight(new ConstantTexture(new Vec3(15.0, 15.0, 15.0)));

            list.Add(new FlipNormals(new YZRect(0, 555, 0, 555, 555, green)));
            list.Add(new YZRect(0, 555, 0, 555, 0, red));
            list.Add(new XZRect(213, 343, 227, 332, 554, light));
            list.Add(new FlipNormals(new XZRect(0, 555, 0, 555, 555, white)));
            list.Add(new XZRect(0, 555, 0, 555, 0, white));
            list.Add(new FlipNormals(new XYRect(0, 555, 0, 555, 555, white)));


            var box1 = new Box(new Vec3(0, 0, 0), new Vec3(165, 165, 165), white);
            var box2 = new Box(new Vec3(0, 0, 0), new Vec3(165, 330, 165), white);

            list.Add(new Translate(new RotateY(box1, -18), new Vec3(130, 0, 65)));
            list.Add(new Translate(new RotateY(box2, 15), new Vec3(265, 0, 295)));

            return((Hitable) new HitableList(list));
        }
Ejemplo n.º 7
0
    private void CornellSmoke()
    {
        Vector3D lookFrom    = new Vector3D(278, 278, -800);
        Vector3D lookAt      = new Vector3D(278, 278, 0);
        double   diskToFocus = 10;
        double   aperture    = 0;
        double   vfov        = 40;

        camera = new Camera(lookFrom, lookAt, new Vector3D(0, 1, 0), vfov,
                            (double)width / (double)height, aperture, diskToFocus, 0, 1);

        List <Hitable> list = new List <Hitable>();

        Material red   = new Lambertian(new ConstantTexture(new Vector3D(0.65, 0.05, 0.05)));
        Material white = new Lambertian(new ConstantTexture(new Vector3D(0.73, 0.73, 0.73)));
        Material green = new Lambertian(new ConstantTexture(new Vector3D(0.12, 0.45, 0.15)));
        Material light = new DiffuseLight(new ConstantTexture(new Vector3D(7, 7, 7)));

        list.Add(new XZRect(113, 443, 127, 432, 554, light));
        list.Add(new FlipNormals(new YZRect(0, 555, 0, 555, 555, green)));
        list.Add(new YZRect(0, 555, 0, 555, 0, red));
        list.Add(new FlipNormals(new XZRect(0, 555, 0, 555, 555, white)));
        list.Add(new XZRect(0, 555, 0, 555, 0, white));
        list.Add(new FlipNormals(new XYRect(0, 555, 0, 555, 555, white)));
        Hitable b1 = new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                       new Vector3D(165, 165, 165), white), -18), new Vector3D(130, 0, 65));
        Hitable b2 = new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                       new Vector3D(165, 330, 165), white), 15), new Vector3D(265, 0, 295));

        list.Add(new ConstantMedium(b1, 0.01, new ConstantTexture(new Vector3D(1, 1, 1))));
        list.Add(new ConstantMedium(b2, 0.01, new ConstantTexture(new Vector3D(0, 0, 0))));
        BVHNode b = new BVHNode(list, list.Count, 0, 1);

        world.list.Add(b);
    }
Ejemplo n.º 8
0
        public CornellBoxScene()
        {
            var light = new DiffuseLight(new ColorTexture(15.0f, 15.0f, 15.0f));
            var glass = new DialectricMaterial(1.5f);

            _light       = new XzRect(213.0f, 343.0f, 227.0f, 332.0f, 554.0f, light);
            _glassSphere = new Sphere(new Vector3(190.0f, 90.0f, 190.0f), 90.0f, glass);
        }
Ejemplo n.º 9
0
        public HitableList GetSceneWorld()
        {
            var perlinTexture = new NoiseTexture(1f);
            var lightTexture  = new DiffuseLight(new ConstantTexture(new Vector3(4, 4, 4)));

            return(new HitableList()
            {
                new Sphere(new Vector3(0, -1000, 0), 1000, new Lambertian(perlinTexture)),
                new Sphere(new Vector3(0, 2, 0), 2, new Lambertian(perlinTexture)),
                new XYRectangle(3, 1, -2, 2, 2, lightTexture),
                new Sphere(new Vector3(0, 8, 0), 3, lightTexture)
            });
        }
Ejemplo n.º 10
0
 private void AddDiffuseLight(DiffuseLight diffuseLight)
 {
     if (diffuseLight.DiffuseType == DiffuseLightType.Key)
     {
         if (ParameterExists("DiffuseLightColor"))
         {
             effect.Parameters["DiffuseLightColor"].SetValue(diffuseLight.LightColor.ToVector4());
         }
         if (ParameterExists("DiffuseLightDirection"))
         {
             effect.Parameters["DiffuseLightDirection"].SetValue(Vector3.Normalize(diffuseLight.LightDirection));
         }
         if (ParameterExists("DiffuseLightIntensity"))
         {
             effect.Parameters["DiffuseLightIntensity"].SetValue(diffuseLight.LightIntensity);
         }
     }
     if (diffuseLight.DiffuseType == DiffuseLightType.Fill)
     {
         if (ParameterExists("Diffuse2LightColor"))
         {
             effect.Parameters["Diffuse2LightColor"].SetValue(diffuseLight.LightColor.ToVector4());
         }
         if (ParameterExists("Diffuse2LightDirection"))
         {
             effect.Parameters["Diffuse2LightDirection"].SetValue(Vector3.Normalize(diffuseLight.LightDirection));
         }
         if (ParameterExists("Diffuse2LightIntensity"))
         {
             effect.Parameters["Diffuse2LightIntensity"].SetValue(diffuseLight.LightIntensity);
         }
     }
     if (diffuseLight.DiffuseType == DiffuseLightType.Back)
     {
         if (ParameterExists("Diffuse3LightColor"))
         {
             effect.Parameters["Diffuse3LightColor"].SetValue(diffuseLight.LightColor.ToVector4());
         }
         if (ParameterExists("Diffuse3LightDirection"))
         {
             effect.Parameters["Diffuse3LightDirection"].SetValue(Matrix.Invert(SystemCore.ActiveCamera.View).Forward);
         }
         if (ParameterExists("Diffuse3LightIntensity"))
         {
             effect.Parameters["Diffuse3LightIntensity"].SetValue(diffuseLight.LightIntensity);
         }
     }
 }
Ejemplo n.º 11
0
        private HitableList CornellBox()
        {
            List <IHitable> list = new List <IHitable>();

            Material red   = new Lambertian(new ConstantTexture(new Vector3D(0.65, 0.05, 0.05)));
            Material white = new Lambertian(new ConstantTexture(new Vector3D(0.73, 0.73, 0.73)));
            Material green = new Lambertian(new ConstantTexture(new Vector3D(0.12, 0.45, 0.15)));
            Material light = new DiffuseLight(new ConstantTexture(new Vector3D(15, 15, 15)));

            list.Add(new YZRect(0, 555, 0, 555, 555, green));
            list.Add(new YZRect(0, 555, 0, 555, 0, red));
            list.Add(new XZRect(213, 343, 227, 332, 554, light));
            list.Add(new XZRect(0, 555, 0, 555, 0, white));
            list.Add(new XYRect(0, 555, 0, 555, 555, white));
            return(new HitableList(list, list.Count));
        }
Ejemplo n.º 12
0
        private void InitScene()
        {
            int      width       = 512;
            int      height      = 512;
            bool     isSky       = false;
            Vector3D lookFrom    = new Vector3D(278, 278, -800);
            Vector3D lookAt      = new Vector3D(278, 278, 0);
            float    diskToFocus = 10;
            float    aperture    = 0;
            float    vfov        = 40;
            Camera   camera      = new Camera(lookFrom, lookAt, new Vector3D(0, 1, 0), vfov,
                                              (float)width / (float)height, aperture, diskToFocus, 0, 1);

            List <Hitable> list = new List <Hitable>();

            Material red   = new Lambertian(new ConstantTexture(new Vector3D(0.65f, 0.05f, 0.05f)));
            Material white = new Lambertian(new ConstantTexture(new Vector3D(0.73f, 0.73f, 0.73f)));
            Material green = new Lambertian(new ConstantTexture(new Vector3D(0.12f, 0.45f, 0.15f)));
            Material light = new DiffuseLight(new ConstantTexture(new Vector3D(7, 7, 7)));

            list.Add(new FlipNormals(new XZTriangle(113, 443, 278, 127, 127, 432, 554, light)));
            list.Add(new FlipNormals(new YZRect(0, 555, 0, 555, 555, green)));
            list.Add(new YZRect(0, 555, 0, 555, 0, red));
            list.Add(new FlipNormals(new XZRect(0, 555, 0, 555, 555, white)));
            list.Add(new XZRect(0, 555, 0, 555, 0, white));
            list.Add(new FlipNormals(new XYRect(0, 555, 0, 555, 555, white)));
            Hitable b1 = new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                           new Vector3D(165, 165, 165), white), -18), new Vector3D(130, 0, 65));
            Hitable b2 = new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                           new Vector3D(165, 330, 165), white), 15), new Vector3D(265, 0, 295));

            list.Add(new ConstantMedium(b1, 0.01f, new ConstantTexture(new Vector3D(1, 1, 1))));
            list.Add(new ConstantMedium(b2, 0.01f, new ConstantTexture(new Vector3D(0, 0, 0))));
            BVHNode     b     = new BVHNode(list, list.Count, 0, 1);
            HitableList world = new HitableList();

            world.list.Add(b);


            HitableList lightShapeList = new HitableList();
            Hitable     lightShape     = new XZRect(113, 443, 127, 432, 554, null);

            lightShapeList.list.Add(lightShape);
            scene = new Scene(width, height, world, isSky, camera, 0.5f, true, lightShapeList);
        }
Ejemplo n.º 13
0
    private void CornellBox()
    {
        this.width  = 512;
        this.height = 512;
        isSky       = false;

        Vector3D lookFrom    = new Vector3D(278, 278, -800);
        Vector3D lookAt      = new Vector3D(278, 278, 0);
        float    diskToFocus = 10;
        float    aperture    = 0;
        float    vfov        = 40;

        camera = new Camera(lookFrom, lookAt, new Vector3D(0, 1, 0), vfov,
                            (float)width / (float)height, aperture, diskToFocus, 0, 1);

        List <Hitable> list = new List <Hitable>();

        Material red      = new Lambertian(new ConstantTexture(new Vector3D(0.65f, 0.05f, 0.05f)));
        Material white    = new Lambertian(new ConstantTexture(new Vector3D(0.73f, 0.73f, 0.73f)));
        Material green    = new Lambertian(new ConstantTexture(new Vector3D(0.2f, 0.45f, 0.15f)));
        Material light    = new DiffuseLight(new ConstantTexture(new Vector3D(50, 50, 50)));
        Material aluminum = new Metal(new Vector3D(0.8f, 0.85f, 0.88f), 0);
        Material glass    = new Dielectric(1.5f);

        //list.Add(new FlipNormals(new XZRect(213, 343, 227, 332, 554, light)));
        list.Add(new FlipNormals(new XZTriangle(213, 343, 278, 227, 227, 332, 554, light)));
        list.Add(new FlipNormals(new YZRect(0, 555, 0, 555, 555, green)));
        list.Add(new YZRect(0, 555, 0, 555, 0, red));
        //list.Add(new YZTriangle(0, 555, 555, 0, 0, 555, 0, red));
        list.Add(new FlipNormals(new XZRect(0, 555, 0, 555, 555, white)));
        list.Add(new XZRect(0, 555, 0, 555, 0, white));
        list.Add(new FlipNormals(new XYRect(0, 555, 0, 555, 555, white)));

        //list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
        //  new Vector3D(165, 165, 165), white), -18), new Vector3D(130, 0, 65)));
        list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                   new Vector3D(165, 330, 165), white), 15), new Vector3D(265, 0, 295)));

        // list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
        //    new Vector3D(165, 330, 165), aluminum), 15), new Vector3D(265, 0, 295)));
        list.Add(new Sphere(new Vector3D(190, 90, 190), 90, glass));
        BVHNode b = new BVHNode(list, list.Count, 0, 1);

        world.list.Add(b);
    }
Ejemplo n.º 14
0
        public List <IHittable> Generate()
        {
            var toReturn = new List <IHittable>();

            var red   = new Lambertian(new Vec3(0.65, 0.05, 0.05));
            var white = new Lambertian(new Vec3(0.73, 0.73, 0.73));
            var green = new Lambertian(new Vec3(0.12, 0.45, 0.15));
            var light = new DiffuseLight(new Vec3(15, 15, 15));

            toReturn.Add(new RectYZ(0, 555, 0, 555, 555, green));
            toReturn.Add(new RectYZ(0, 555, 0, 555, 0, red));

            toReturn.Add(new RectXZ(213, 343, 227, 332, 554, light));
            toReturn.Add(new RectXZ(0, 555, 0, 555, 0, white));
            toReturn.Add(new RectXZ(0, 555, 0, 555, 555, white));

            toReturn.Add(new RectXY(0, 555, 0, 555, 555, white));

            IHittable box1 = new Box(
                new Vec3(0, 0, 0),
                new Vec3(165, 330, 165),
                white);

            box1 = new RotateY(box1, 15);
            box1 = new Translate(box1, new Vec3(265, 0, 295));
            toReturn.Add(box1);

            IHittable box2 = new Box(
                new Vec3(0, 0, 0),
                new Vec3(165, 165, 165),
                white);

            box2 = new RotateY(box2, -18);
            box2 = new Translate(box2, new Vec3(130, 0, 65));
            toReturn.Add(box2);

            return(toReturn);
        }
Ejemplo n.º 15
0
        private HitableList CornellBox()
        {
            HitableList    world = new HitableList();
            List <Hitable> list  = new List <Hitable>();

            Material red   = new Lambertian(new ConstantTexture(new Vector3D(0.65, 0.05, 0.05)));
            Material white = new Lambertian(new ConstantTexture(new Vector3D(0.73, 0.73, 0.73)));
            Material green = new Lambertian(new ConstantTexture(new Vector3D(0.12, 0.45, 0.15)));
            Material light = new DiffuseLight(new ConstantTexture(new Vector3D(55, 55, 55)));

            list.Add(new FlipNormals(new YZRect(0, 555, 0, 555, 555, green)));
            list.Add(new YZRect(0, 555, 0, 555, 0, red));
            list.Add(new XZRect(213, 343, 227, 332, 554, light));
            list.Add(new FlipNormals(new XZRect(0, 555, 0, 555, 555, white)));
            list.Add(new XZRect(0, 555, 0, 555, 0, white));
            list.Add(new FlipNormals(new XYRect(0, 555, 0, 555, 555, white)));
            list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                       new Vector3D(165, 165, 165), white), -18), new Vector3D(130, 0, 65)));
            list.Add(new Translate(new RotateY(new Box(new Vector3D(0, 0, 0),
                                                       new Vector3D(165, 330, 165), white), 15), new Vector3D(265, 0, 295)));
            world.list.Add(new BVHNode(list, list.Count, 0, 1));
            return(world);
        }
Ejemplo n.º 16
0
        private void InitScene()
        {
            int      width       = 512;
            int      height      = 512;
            bool     isSky       = false;
            Vector3D lookFrom    = new Vector3D(500, 300, -600);
            Vector3D lookAt      = new Vector3D(278, 278, 0);
            float    diskToFocus = 10;
            float    aperture    = 0;
            float    vfov        = 40;
            Camera   camera      = new Camera(lookFrom, lookAt, new Vector3D(0, 1, 0), vfov,
                                              (float)width / (float)height, aperture, diskToFocus, 0, 1);

            List <Hitable> list     = new List <Hitable>();
            List <Hitable> boxList  = new List <Hitable>();
            List <Hitable> boxList2 = new List <Hitable>();

            int      nb     = 20;
            Material white  = new Lambertian(new ConstantTexture(new Vector3D(0.73f, 0.73f, 0.73f)));
            Material ground = new Lambertian(new ConstantTexture(new Vector3D(0.48f, 0.83f, 0.53f)));
            Material light  = new DiffuseLight(new ConstantTexture(new Vector3D(7, 7, 7)));

            for (int i = 0; i < nb; i++)
            {
                for (int j = 0; j < nb; j++)
                {
                    float w  = 100;
                    float x0 = -1000 + i * w;
                    float z0 = -1000 + j * w;
                    float y0 = 0;
                    float x1 = x0 + w;
                    float y1 = 100 * (Mathf.Randomfloat() + 0.01f);
                    float z1 = z0 + w;
                    boxList.Add(new Box(new Vector3D(x0, y0, z0), new Vector3D(x1, y1, z1), ground));
                }
            }
            list.Add(new BVHNode(boxList, boxList.Count, 0, 1));
            list.Add(new FlipNormals(new XZRect(123, 423, 147, 412, 554, light)));
            Vector3D center = new Vector3D(400, 400, 200);

            list.Add(new MovingSphere(center, center + new Vector3D(30, 0, 0), 0, 1, 50,
                                      new Lambertian(new ConstantTexture(new Vector3D(0.7f, 0.3f, 0.1f)))));
            list.Add(new Sphere(new Vector3D(260, 150, 45), 50, new Dielectric(1.5f)));
            list.Add(new Sphere(new Vector3D(0, 150, 145), 50, new Metal(
                                    new Vector3D(0.8f, 0.8f, 0.9f), 10)));
            Hitable boundary = new Sphere(new Vector3D(360, 150, 145), 70, new Dielectric(1.5f));

            list.Add(boundary);
            list.Add(new ConstantMedium(boundary, 0.2f, new ConstantTexture(new Vector3D(0.2f, 0.4f, 0.9f))));
            boundary = new Sphere(new Vector3D(0, 0, 0), 5000, new Dielectric(1.5f));
            list.Add(new ConstantMedium(boundary, 0.0001f, new ConstantTexture(new Vector3D(1, 1, 1))));

            Material emat = new Lambertian(new Imagetexture("Earth.jpg"));

            list.Add(new Sphere(new Vector3D(400, 200, 400), 100, emat));
            Texture pertext = new NoiseTexture(1f);

            list.Add(new Sphere(new Vector3D(220, 280, 300), 80, new Lambertian(pertext)));
            int ns = 1000;

            for (int j = 0; j < ns; j++)
            {
                boxList2.Add(new Sphere(new Vector3D(165 * Mathf.Randomfloat(), 165 * Mathf.Randomfloat(), 165 * Mathf.Randomfloat()), 10, white));
            }
            list.Add(new Translate(new RotateY(new BVHNode(boxList2, ns, 0, 1), 15), new Vector3D(-100, 270, 395)));


            BVHNode     b     = new BVHNode(list, list.Count, 0, 1);
            HitableList world = new HitableList();

            world.list.Add(b);


            HitableList lightShapeList = new HitableList();
            Hitable     lightShape     = new XZRect(123, 423, 147, 412, 554, null);

            lightShapeList.list.Add(lightShape);
            scene = new Scene(width, height, world, isSky, camera, 0.5f, false, lightShapeList);
        }
Ejemplo n.º 17
0
        public CornellBoxWithSmokeScene()
        {
            var light = new DiffuseLight(new ColorTexture(7.0f, 7.0f, 7.0f));

            _light = new XzRect(113.0f, 443.0f, 127.0f, 432.0f, 554.0f, light);
        }
Ejemplo n.º 18
0
    private void Final()
    {
        Vector3D lookFrom    = new Vector3D(578, 278, -800);
        Vector3D lookAt      = new Vector3D(278, 278, 0);
        double   diskToFocus = 10;
        double   aperture    = 0;
        double   vfov        = 40;

        camera = new Camera(lookFrom, lookAt, new Vector3D(0, 1, 0), vfov,
                            (double)width / (double)height, aperture, diskToFocus, 0, 1);

        List <Hitable> list     = new List <Hitable>();
        List <Hitable> boxList  = new List <Hitable>();
        List <Hitable> boxList2 = new List <Hitable>();

        int      nb     = 20;
        Material white  = new Lambertian(new ConstantTexture(new Vector3D(0.73, 0.73, 0.73)));
        Material ground = new Lambertian(new ConstantTexture(new Vector3D(0.48, 0.83, 0.53)));
        Material light  = new DiffuseLight(new ConstantTexture(new Vector3D(7, 7, 7)));

        for (int i = 0; i < nb; i++)
        {
            for (int j = 0; j < nb; j++)
            {
                double w  = 100;
                double x0 = -1000 + i * w;
                double z0 = -1000 + j * w;
                double y0 = 0;
                double x1 = x0 + w;
                double y1 = 100 * (Tools.RandomDouble() + 0.01);
                double z1 = z0 + w;
                boxList.Add(new Box(new Vector3D(x0, y0, z0), new Vector3D(x1, y1, z1), ground));
            }
        }
        list.Add(new BVHNode(boxList, boxList.Count, 0, 1));
        list.Add(new XZRect(123, 423, 147, 412, 554, light));
        Vector3D center = new Vector3D(400, 400, 200);

        list.Add(new MovingSphere(center, center + new Vector3D(30, 0, 0), 0, 1, 50,
                                  new Lambertian(new ConstantTexture(new Vector3D(0.7, 0.3, 0.1)))));
        list.Add(new Sphere(new Vector3D(260, 150, 45), 50, new Dielectric(1.5)));
        list.Add(new Sphere(new Vector3D(0, 150, 145), 50, new Metal(
                                new ConstantTexture(new Vector3D(0.8, 0.8, 0.9)), 10)));
        Hitable boundary = new Sphere(new Vector3D(360, 150, 145), 70, new Dielectric(1.5));

        list.Add(boundary);
        list.Add(new ConstantMedium(boundary, 0.2, new ConstantTexture(new Vector3D(0.2, 0.4, 0.9))));
        boundary = new Sphere(new Vector3D(0, 0, 0), 5000, new Dielectric(1.5));
        list.Add(new ConstantMedium(boundary, 0.0001, new ConstantTexture(new Vector3D(1, 1, 1))));

        Material emat = new Lambertian(new Imagetexture("Earth.jpg"));

        list.Add(new Sphere(new Vector3D(400, 200, 400), 100, emat));
        Texture pertext = new NoiseTexture(0.1);

        list.Add(new Sphere(new Vector3D(220, 280, 300), 80, new Lambertian(pertext)));
        int ns = 1000;

        for (int j = 0; j < ns; j++)
        {
            boxList2.Add(new Sphere(new Vector3D(165 * Tools.RandomDouble(), 165 * Tools.RandomDouble(), 165 * Tools.RandomDouble()), 10, white));
        }
        list.Add(new Translate(new RotateY(new BVHNode(boxList2, ns, 0, 1), 15), new Vector3D(-100, 270, 395)));



        world.list = list;
    }
Ejemplo n.º 19
0
        public CoverSceneRT2(double aspect)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            //string[] names = assembly.GetManifestResourceNames();
            var resource_stream  = assembly.GetManifestResourceStream("rt_2_the_next_week.raytrace.Resource.worldmap1.png");
            var worldmap_texture = Texture.Create(resource_stream);
            var perlin_texture   = NoiseTexture.Create(0.05, NoiseTexture.Type.SIN_Z);
            var hitables         = new List <IHitable>();
            var sampler          = new Random();
            var white            = new Lambertian(ConstantTexture.Create(0.73, 0.73, 0.73));
            var ground           = new Lambertian(ConstantTexture.Create(0.48, 0.83, 0.53));
            var light            = new DiffuseLight(ConstantTexture.Create(7, 7, 7));
            var emat             = new Lambertian(worldmap_texture);
            var boxList1         = new List <IHitable>();
            var nb = 20;

            for (int i = 0; i < nb; i++)
            {
                for (int j = 0; j < nb; j++)
                {
                    double w     = 100;
                    var    v_min = Vec3.Create(-1000 + i * w, 0, -1000 + j * w);
                    var    v_max = Vec3.Create(v_min.X + w, 100 * (sampler.NextDouble() + 0.01), v_min.Z + w);
                    boxList1.Add(new Box(v_min, v_max, ground));
                }
            }
            var boxList2 = new List <IHitable>();
            int ns       = 1000;

            for (int i = 0; i < ns; ++i)
            {
                boxList2.Add(new Sphere(Vec3.Create(165 * sampler.NextDouble(), 165 * sampler.NextDouble(), 165 * sampler.NextDouble()), 10, white));
            }
            hitables.Add(new BVHNode(boxList1.ToArray(), 0, 1));
            hitables.Add(new XZRect(123, 423, 147, 412, 554, light));
            var center = Vec3.Create(400, 400, 200);

            hitables.Add(new MovingSphere(center, center + Vec3.Create(30, 0, 0), 0, 1, 50,
                                          new Lambertian(new ConstantTexture(Vec3.Create(0.7, 0.3, 0.1)))));
            hitables.Add(new Sphere(Vec3.Create(260, 150, 45), 50, new Dielectric(1.5)));
            hitables.Add(new Sphere(Vec3.Create(0, 150, 145), 50,
                                    new Metal(new ConstantTexture(Vec3.Create(0.8, 0.8, 0.9)), 10)));
            var boundary1 = new Sphere(Vec3.Create(360, 150, 145), 70, new Dielectric(1.5));

            hitables.Add(boundary1);
            hitables.Add(new ConstantMedium(boundary1, 0.2, new ConstantTexture(Vec3.Create(0.2, 0.4, 0.5))));
            var boundary2 = new Sphere(Vec3.Create(0), 5000, new Dielectric(1.5));

            hitables.Add(new ConstantMedium(boundary2, 0.0001, new ConstantTexture(Vec3.Create(1.0, 1.0, 1.0))));
            hitables.Add(new Sphere(Vec3.Create(400, 200, 400), 100, emat));
            hitables.Add(new Sphere(Vec3.Create(220, 280, 300), 80, new Lambertian(perlin_texture)));
            hitables.Add(new Translate(new RotateY(new BVHNode(boxList2.ToArray(), 0, 1), 15), Vec3.Create(-100, 270, 395)));
            World = new BVHNode(hitables.ToArray(), 0, 1);
            var    lookFrom      = Vec3.Create(478, 278, -600);
            var    lookAt        = Vec3.Create(278, 278, 0);
            double dist_to_focus = 10;
            double aderpture     = 0;
            double vfov          = 40;

            Camera = Camera.CreateLookAt(lookFrom, lookAt, Vec3.Create(0, 1, 0), vfov, aspect, aderpture, dist_to_focus);
        }
Ejemplo n.º 20
0
        public List <IHittable> Generate()
        {
            var toReturn = new List <IHittable>();

            var       ground       = new Lambertian(new Vec3(0.48, 0.83, 0.53));
            const int boxesPerSide = 20;
            var       boxes1       = new List <IHittable>();

            for (var i = 0; i < boxesPerSide; i++)
            {
                for (var j = 0; j < boxesPerSide; j++)
                {
                    var w  = 100.0;
                    var x0 = -1000 + i * w;
                    var z0 = -1000 + j * w;
                    var y0 = 0;
                    var x1 = x0 + w;
                    var y1 = MathUtils.RandDouble(1, 101);
                    var z1 = z0 + w;
                    boxes1.Add(new Box(
                                   new Vec3(x0, y0, z0),
                                   new Vec3(x1, y1, z1),
                                   ground));
                }
            }
            toReturn.Add(new BvhNode(boxes1, 0, boxes1.Count, 0, 1));

            var light = new DiffuseLight(new Vec3(7, 7, 7));

            toReturn.Add(new RectXZ(123, 423, 147, 412, 554, light));

            var c1 = new Vec3(400, 400, 200);
            var c2 = c1 + new Vec3(30, 0, 0);
            var movingSphereMat = new Lambertian(new Vec3(0.7, 0.3, 0.1));

            toReturn.Add(new MovingSphere {
                Center0 = c1, Center1 = c2, Radius = 50, Time0 = 0, Time1 = 1, Material = movingSphereMat
            });

            toReturn.Add(new Sphere {
                Center = new Vec3(260, 150, 45), Radius = 50, Material = new Dielectric {
                    RefractionIndex = 1.5
                }
            });
            toReturn.Add(new Sphere
            {
                Center   = new Vec3(0, 150, 145),
                Radius   = 50,
                Material = new Metal {
                    Albedo = new Vec3(0.8, 0.8, 0.9), Fuzz = 1.0
                }
            });

            var boundary = new Sphere
            {
                Center   = new Vec3(360, 150, 145), Radius = 70,
                Material = new Dielectric {
                    RefractionIndex = 1.5
                }
            };

            toReturn.Add(boundary);
            toReturn.Add(new ConstantMedium(boundary, 0.2, new Vec3(0.2, 0.4, 0.9)));

            boundary = new Sphere
            {
                Center   = Vec3.Zero,
                Radius   = 5000,
                Material = new Dielectric {
                    RefractionIndex = 1.5
                }
            };
            toReturn.Add(new ConstantMedium(boundary, 0.0001, Vec3.One));

            var emat = new Lambertian(new ImageTexture("../../Data/earthmap.jpg"));

            toReturn.Add(new Sphere
            {
                Center   = new Vec3(400, 200, 400),
                Radius   = 100,
                Material = emat
            });

            var pertex = new NoiseTexture(0.1);

            toReturn.Add(new Sphere
            {
                Center   = new Vec3(220, 280, 300),
                Radius   = 80,
                Material = new Lambertian(pertex)
            });

            var boxes2 = new List <IHittable>();
            var white  = new Lambertian(new Vec3(0.73, 0.73, 0.73));
            var ns     = 1000;

            for (int j = 0; j < ns; j++)
            {
                boxes2.Add(new Sphere
                {
                    Center   = Vec3.Random(0, 165),
                    Radius   = 10,
                    Material = white
                });
            }

            toReturn.Add(new Translate(
                             new RotateY(
                                 new BvhNode(boxes2, 0, boxes2.Count, 0, 1),
                                 15
                                 ),
                             new Vec3(-100, 270, 395)
                             ));


            return(toReturn);
        }
Ejemplo n.º 21
0
 public void DiffuseLight_Constructor_Stores_Location()
 {
     var location = new Point(150.0f, 200.0f, -100.0f);
       var dl = new DiffuseLight(location);
       Assert.AreEqual(location, dl.Location);
 }
Ejemplo n.º 22
0
 public void DiffuseLight_Constructor_Creates_White_Light()
 {
     var location = new Point(150.0f, 200.0f, -100.0f);
       var dl = new DiffuseLight(location);
       Assert.AreEqual(System.Drawing.Color.White, dl.Colour);
 }
Ejemplo n.º 23
0
 public void DiffuseLight_Constructor_Creates_New_Object()
 {
     var location = new Point(150.0f, 200.0f, -100.0f);
       var dl = new DiffuseLight(location);
       Assert.IsNotNull(dl);
 }
Ejemplo n.º 24
0
        public List <IHittable> Generate()
        {
            var world = new List <IHittable>();

            var checker   = new CheckerTexture(new Vec3(0.2, 0.3, 0.1), new Vec3(0.9, 0.9, 0.9));
            var groundMat = new Lambertian(checker);

            world.Add(new Sphere {
                Center = new Vec3(0, -1000, 0), Radius = 1000, Material = groundMat
            });

            var earthTex = new ImageTexture("../../Data/earthmap.jpg");
            var earthMat = new Lambertian(earthTex);

            for (var a = -11; a < 11; a++)
            {
                for (var b = -11; b < 11; b++)
                {
                    var chooseMat = MathUtils.RandDouble();
                    var radius    = MathUtils.RandDouble(0.2, 0.3);
                    var offset    = 1.0 - (radius / 2);
                    var center    = new Vec3(
                        a + offset * MathUtils.RandDouble(),
                        radius,
                        b + offset * MathUtils.RandDouble());

                    if ((center - new Vec3(4, radius, 0)).Length <= 0.9)
                    {
                        continue;
                    }

                    if (chooseMat < 0.05)
                    {
                        var albedo = Vec3.Random() * Vec3.Random() * 10;
                        var mat    = new DiffuseLight(albedo);
                        world.Add(new Sphere
                        {
                            Center   = center,
                            Material = mat,
                            Radius   = radius
                        });
                    }
                    if (chooseMat < 0.2)
                    {
                        var albedo = Vec3.Random() * Vec3.Random();
                        var mat    = new Lambertian(albedo);
                        var c2     = center + new Vec3(0, MathUtils.RandDouble(0, 0.2), 0);
                        world.Add(new MovingSphere {
                            Center0 = center, Center1 = c2, Radius = radius, Time0 = 0, Time1 = 1, Material = mat
                        });
                    }
                    else if (chooseMat < 0.4)
                    {
                        world.Add(new Sphere {
                            Center = center, Radius = radius, Material = earthMat
                        });
                    }
                    else if (chooseMat < 0.6)
                    {
                        var color1 = Vec3.Random() * Vec3.Random();
                        var color2 = Vec3.One - color1;
                        var scale  = MathUtils.RandomInt(3, 10);
                        var mat    = new Lambertian(new NoiseTexture(color1, color2, scale));
                        world.Add(new Sphere {
                            Center = center, Radius = radius, Material = mat
                        });
                    }
                    else if (chooseMat < 0.8)
                    {
                        var albedo = Vec3.Random(0.5, 1);
                        var fuzz   = MathUtils.RandDouble(0, 0.5);
                        var mat    = new Metal {
                            Albedo = albedo, Fuzz = fuzz
                        };
                        world.Add(new Sphere {
                            Center = center, Radius = radius, Material = mat
                        });
                    }
                    else
                    {
                        var mat = new Dielectric {
                            RefractionIndex = 1.5
                        };
                        world.Add(new Sphere {
                            Center = center, Radius = radius, Material = mat
                        });
                    }
                }
            }

            var mat1 = new Dielectric {
                RefractionIndex = 1.5
            };
            var mat2 = new Lambertian(new Vec3(0.4, 0.2, 0.1));
            var mat3 = new Metal {
                Albedo = new Vec3(0.7, 0.6, 0.5), Fuzz = 0.0
            };

            world.Add(new Sphere {
                Center = new Vec3(0, 1, 0), Radius = 1.0, Material = mat1
            });
            world.Add(new Sphere {
                Center = new Vec3(-4, 1, 0), Radius = 1, Material = mat2
            });
            world.Add(new Sphere {
                Center = new Vec3(4, 1, 0), Radius = 1.0, Material = mat3
            });

            return(world);
        }
Ejemplo n.º 25
0
        public void PreDraw(GameTime gameTime)
        {
            if (SystemCore.ActiveScene.LightsInScene.Count == 0)
            {
                return;
            }


            SystemCore.GraphicsDevice.BlendState        = BlendState.Opaque;
            SystemCore.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            ShadowPass = true;

            DiffuseLight light = SystemCore.ActiveScene.LightsInScene[0] as DiffuseLight;

            if (light == null)
            {
                return;
            }

            Matrix lightRotation = Matrix.CreateLookAt(Vector3.Zero, Vector3.Normalize(-light.LightDirection), Vector3.Up);


            // Get the corners of the frustum
            Vector3[] frustumCorners = new BoundingFrustum(SystemCore.ActiveCamera.View
                                                           * SystemCore.ActiveCamera.Projection).GetCorners();

            // Transform the positions of the corners into the direction of the light
            for (int i = 0; i < frustumCorners.Length; i++)
            {
                frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightRotation);
            }

            // Find the smallest box around the points
            BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners);

            Vector3 boxSize     = lightBox.Max - lightBox.Min;
            Vector3 halfBoxSize = boxSize * 0.5f;

            // The position of the light should be in the center of the back
            // pannel of the box.
            Vector3 lightPosition = lightBox.Min + halfBoxSize;

            lightPosition.Z = lightBox.Min.Z;

            lightPosition = Vector3.Transform(lightPosition,
                                              Matrix.Invert(lightRotation));

            // Create the view matrix for the light
            var lightView = Matrix.CreateLookAt(lightPosition,
                                                lightPosition - light.LightDirection,
                                                Vector3.Up);

            // Create the projection matrix for the light
            // The projection is orthographic since we are using a directional light
            Matrix lightProjection = Matrix.CreateOrthographic(boxSize.X, boxSize.Y,
                                                               -boxSize.Z, boxSize.Z);

            LightViewProj = lightView * lightProjection;



            SystemCore.GraphicsDevice.SetRenderTarget(ShadowMapTarget);

            // Clear the render target to white or all 1's
            // We set the clear to white since that represents the
            // furthest the object could be away
            SystemCore.GraphicsDevice.Clear(Color.White);
        }
Ejemplo n.º 26
0
        public static (List <IHitable>, Camera) CornellScene(string objPath, ImSoRandom rnd, int nx, int ny)
        {
            var world = new List <IHitable>();

            var grad  = new Lambertian(new ColourTexture());
            var blue  = new Lambertian(new ConstantTexture(0.05f, 0.05f, 0.65f));
            var red   = new Lambertian(new ConstantTexture(0.65f, 0.05f, 0.05f));
            var white = new Lambertian(new ConstantTexture(0.73f, 0.73f, 0.73f));
            var green = new Lambertian(new ConstantTexture(0.12f, 0.45f, 0.15f));
            var light = new DiffuseLight(new ConstantTexture(15f, 15f, 15f));
            var glass = new Dialectric(1.5f);

            world.Add(new RectXZ(213, 343, 227, 332, 554, light));

            world.Add(new FlipNormals(new RectYZ(0, 555, 0, 555, 555, green)));
            world.Add(new RectYZ(0, 555, 0, 555, 0, red));
            world.Add(new FlipNormals(new RectXZ(0, 555, 0, 555, 555, white)));
            world.Add(new RectXZ(0, 555, 0, 555, 0, white));
            world.Add(new FlipNormals(new RectXY(0, 555, 0, 555, 555, white)));

            //world.Add( new Translate( new Box(Vector3.Zero, new Vector3(165, 165, 165), white), new Vector3(130, 0, 65)));
            //world.Add( new Translate( new Box(Vector3.Zero, new Vector3(165, 330, 165), white), new Vector3(265, 0, 295)));

            //world.Add(new Sphere(new Vector3(0, 0, 0), 3000.0f, new DiffuseLight(new ConstantTexture(new Vector3(1, 1, 1)))));

            /*
             * for (int i = 0; i < 50; i++)
             * {
             *  var a = rnd.RandomVector();
             *  var b = rnd.RandomVector();
             *  var c = rnd.RandomVector();
             *
             *  //world.Add(new Translate(new Triangle(new Vector3(rnd.NextFloat() * 555f, rnd.NextFloat() * 555f, rnd.NextFloat() * 555f), new Vector3(rnd.NextFloat() * 555f, rnd.NextFloat() * 555f, rnd.NextFloat() * 555f), new Vector3(rnd.NextFloat() * 555f, rnd.NextFloat() * 555f, rnd.NextFloat() * 555f), red),new Vector3(10 + rnd.NextFloat() * 100f, 10 + rnd.NextFloat() * 100f, 10 + rnd.NextFloat() * 100f)));
             *  world.Add(new Triangle(a, b, c, blue));
             * }
             *
             */
            //world.Add(new Triangle( new Vector3(340, 90, 62),new Vector3(100, 320, 190), new Vector3(262, 331, 400), grad));

            var objLoaderFactory = new ObjLoaderFactory();
            var objLoader        = objLoaderFactory.Create();
            var fileStream       = new FileStream(objPath, FileMode.Open);
            var obj          = objLoader.Load(fileStream);
            var objBox       = obj.GetBoundingBox();
            var centerOffset = objBox.Center();
            var height       = objBox.Max.Y - objBox.Min.Y;

            var scaleFactor      = new Vector3(50, 50, 50);       // scale obj to wold space
            var displacement     = new Vector3(555f / 2, height / 2f * scaleFactor.Y, 555f / 3 * 2);
            var glossScaleFactor = new Vector3(0.5f, 0.5f, 0.5f); // scale the unit vector normal

            foreach (var g in obj.Groups)
            {
                foreach (var f in g.Faces)
                {
                    // Verticies have their origin set to the center of the bounding box
                    var v0 = obj.Vertices[f[0].VertexIndex - 1].ToVector3() - centerOffset;
                    var v1 = obj.Vertices[f[1].VertexIndex - 1].ToVector3() - centerOffset;
                    var v2 = obj.Vertices[f[2].VertexIndex - 1].ToVector3() - centerOffset;

                    var t = new Triangle(v0 * scaleFactor, v1 * scaleFactor, v2 * scaleFactor, glass);

                    // add Dialectric
                    world.Add(new Translate(t, displacement));

                    // add Lambertian. translated toward obj center by glossScaleFactor of a unit vector
                    world.Add(new Translate(new Triangle(v0 * scaleFactor, v1 * scaleFactor, v2 * scaleFactor, white), displacement - Vector3.Normalize(t.Normal)));
                }
            }

            var lookFrom    = new Vector3(278, 278, -800);
            var lookAt      = new Vector3(278, 278, 0);
            var distToFocus = (lookFrom - lookAt).Length() - scaleFactor.Length() / 2;
            var aperture    = 0.1f;
            var vFov        = 40;

            var cam = new Camera(lookFrom, lookAt, new Vector3(0, 1, 0), vFov, (float)nx / (float)ny, aperture, distToFocus, 0.0f, 1.0f);

            return(world, cam);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Main photon processing algorithm.
        /// </summary>
        private void ProcessCollisions()
        {
            ChangeStage(RadiosityStage.PhotonBouncing, m_maxPhotons);

            photonsComplete = 0;

            // Loop through all the lights.
            for (int _currentLightIndex = 0; _currentLightIndex < this.m_lights.Count; _currentLightIndex++)
            {
                lightIndex = _currentLightIndex;
                if (m_lights[_currentLightIndex] is DiffuseLight)
                {
                    DiffuseLight light = m_lights[_currentLightIndex] as DiffuseLight;
                    //AddDebugRay(light.Center, light.Normal);
                    AddDebugRay(light.Center, light.Normal, RadiosityDebugLineListID.LightDirections);
                }

                if (m_lights[_currentLightIndex] is RadiosityLight)
                {
                    (m_lights[_currentLightIndex] as RadiosityLight).pointCount = m_maxPhotons;
                }

                int retryCount = 20;

                photonMap.BeginLight();

                // Loop through the number of photons per light.
                Parallel.For(0, m_lights[lightIndex].PhotonCount, delegate(int _currentPhotonIndex)
                {
                    if (stopRequested)
                    {
                        return;
                    }


                    if (!updateInProgress)
                    {
                        SetOperation("Bouncing photons...");
                    }

                    photonIndex = _currentPhotonIndex;

                    int _collisionCount = 0;

                    IPhoton _photon;
                    lock (this.m_lights[_currentLightIndex])
                    {
                        // Generate a photon from the current light.
                        _photon = this.m_lights[_currentLightIndex].GeneratePhoton();
                        AddDebugPoint(_photon.Position, RadiosityDebugPointListID.PhotonSpawn);
                        AddDebugRay(_photon.Position, _photon.Direction, RadiosityDebugLineListID.DirectOnly);
                    }

                    //_photon.Color.Multiply(m_lights[_currentLightIndex].Power * m_lights.Count);

                    // Bounce the photon around for the maximum permitted bounces.
                    while (true)//_collisionCount < this.m_maxBounces)
                    {
                        RadiosityIntersection closest = RadiosityIntersection.None;
                        float distSq = float.MaxValue;

                        if (collideModels)
                        {
                            for (int x = 0; x < m_models.Count; x++)
                            {
                                RadiosityIntersection intersect = m_models[x].Intersection(_photon.Direction, _photon.Position);
                                if (intersect != RadiosityIntersection.None)
                                {
                                    //AddDebugPoint(intersect.Position);
                                    float dist = (intersect.Position - _photon.Position).LengthSq();
                                    if (dist < distSq)
                                    {
                                        distSq  = dist;
                                        closest = intersect;
                                    }
                                }
                            }
                        }

                        for (int x = 0; x < m_bsp.Count; x++)
                        {
                            // TODO: Test radiosity model to see if changes fixed the issues.

                            RadiosityIntersection intersect = m_bsp[x].RadiosityIntersect(_photon.Position, _photon.Direction);
                            if ((intersect.NoIntersection) || (intersect.WrongSide))
                            {
                                continue;
                            }

                            float dist = float.MaxValue;
                            if ((dist = (intersect.Position - _photon.Position).LengthSq()) < distSq)
                            {
                                closest = intersect;
                                distSq  = dist;
                            }
                        }

                        // If the diffuse light never collided, it will randomly generate another.
                        if ((closest == RadiosityIntersection.None))
                        {
                            if (m_lights[_currentLightIndex] is DiffuseLight)
                            {
                                if (_collisionCount < 1)
                                {
                                    if (retryCount > 0)
                                    {
                                        retryCount--;
                                        _currentPhotonIndex--;
                                        break;
                                    }
                                }
                            }

                            retryCount = 5;
                            break;
                        }
                        retryCount = 5;

                        if (_collisionCount == 0)
                        {
                            AddDebugPoint(closest.Position, RadiosityDebugPointListID.DirectOnly);
                        }
                        else
                        {
                            AddDebugPoint(closest.Position, RadiosityDebugPointListID.IndirectOnly);
                        }

                        Photon phot = new Photon
                        {
                            LightmapIndex = (short)closest.LightmapIndex,
                            MaterialIndex = (short)closest.MaterialIndex,
                            Direction     = (PhotonVector3)_photon.Direction,
                            position      = (PhotonVector3)closest.Position,
                            Power         = _photon.Color.Copy()
                        };

                        AddDebugRay(closest.Position, closest.NewDirection, RadiosityDebugLineListID.IndirectOnly);

                        _photon.Position  = closest.Position;
                        _photon.Direction = closest.NewDirection;

                        lock (photonMap)
                        {
                            photonMap.Store(phot);
                        }

                        _photon.Color.Tint(closest.Material.ReflectionTint);

                        _collisionCount++;

                        if ((closest.Absorbed) || (_collisionCount >= m_maxBounces))
                        {
                            break;
                        }
                    }
                    photonsComplete++;
                    IncrementProgress();
                });
                //photonMap.EndLight();
            }

            Interfaces.Output.Write(Interfaces.OutputTypes.Information, "Finished processing photons...");
            ChangeStage(RadiosityStage.TreeBalancing, 100);
            SetOperation("Balancing the photon map...");
            photonMap.Balance();

            RadiosityRenderer.SavePhotonMap();
            Interfaces.Output.Write(Interfaces.OutputTypes.Information, "Saved the photon map!");

            SetOperation("Updating viewport...");
            try
            {
                //UpdateBspLightmaps();
                CreateLightmaps();
            }
            catch (Exception a)
            {
                SetOperation("Error!.. " + a.Message);
                Interfaces.Output.Write(Interfaces.OutputTypes.Error, "Error!.. " + a.Message, a.StackTrace);
            }
            try
            {
                UpdateViewportLightmaps();
            }
            catch (System.Threading.AggregateException a)
            {
                Interfaces.Output.Write(Interfaces.OutputTypes.Error, "Error of type AggregateException thrown.");
                foreach (Exception exc in a.InnerExceptions)
                {
                    Interfaces.Output.Write(Interfaces.OutputTypes.Error, "Error: " + exc.Message, exc.StackTrace);
                }
            }

            #region Debug
#if DEBUG
            Console.WriteLine("Done!");
#endif
            #endregion
            stopRequested = false;
        }