示例#1
0
        public Bitmap CreateBitmap()
        {
            Bitmap image = new Bitmap(this.width, this.height);

            Random rand = new Random(0);

            Photonmap photonmap = new Photonmap(this.intersectionFinder, this.data.LightSource, 100000);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int    sampleCount = 10;
                    Vector sum         = new Vector(0, 0, 0);
                    for (int i = 0; i < sampleCount; i++)
                    {
                        sum += EstimateColorWithPhotonmapping(x, y, rand, photonmap);
                    }
                    sum /= sampleCount;
                    image.SetPixel(x, y, VectorToColor(sum));
                }
            }

            return(image);
        }
示例#2
0
        private Vector EstimateColorWithPhotonmapping(int x, int y, Random rand, Photonmap photonmap)
        {
            Ray    primaryRay  = data.Camera.GetPrimaryRay(x, y);
            float  cosAtCamera = Vector.Dot(primaryRay.Direction, this.data.Camera.Forward);
            Vector pathWeight  = new Vector(1, 1, 1) * cosAtCamera;

            var eyePoint = this.intersectionFinder.GetIntersectionPoint(primaryRay);

            Vector pixelColor = new Vector(0, 0, 0);

            if (eyePoint != null)
            {
                if (eyePoint.IsLocatedOnLightSource)
                {
                    return(eyePoint.Emission);
                }

                pathWeight = Vector.Mult(pathWeight, DiffuseBrdf.Evaluate(eyePoint));

                //Direct Lighting
                var lightPoint = data.LightSource.GetRandomPointOnLightSource(rand);
                if (IsVisible(eyePoint.Position, lightPoint.Position))
                {
                    pixelColor += Vector.Mult(pathWeight, lightPoint.Color * GeometryTerm(eyePoint, lightPoint) / lightPoint.PdfA);
                }

                //Final Gathering
                Vector newDirection = DiffuseBrdf.SampleDirection(eyePoint.Normal, rand);
                pathWeight = pathWeight * Vector.Dot(eyePoint.Normal, newDirection) / DiffuseBrdf.PdfW(eyePoint.Normal, newDirection);
                var finalGatherPoint = this.intersectionFinder.GetIntersectionPoint(new Ray(eyePoint.Position, newDirection));

                if (finalGatherPoint != null)
                {
                    //100000 3000 1000
                    float searchRadius   = 0.00634257728f * 2;//0.0383904837f;//0.065260686f;
                    var   photons        = photonmap.SearchPhotons(finalGatherPoint, searchRadius);
                    float kernelFunction = 1.0f / (searchRadius * searchRadius * (float)Math.PI);
                    foreach (var photon in photons)
                    {
                        pixelColor += Vector.Mult(pathWeight, Vector.Mult(DiffuseBrdf.Evaluate(finalGatherPoint), photon.PathWeight) * kernelFunction);
                    }
                }
            }

            return(pixelColor);
        }
示例#3
0
        public Bitmap CreateBitmap()
        {
            Bitmap image = new Bitmap(this.width, this.height);

            Random rand = new Random(0);

            Photonmap photonmap = new Photonmap(this.intersectionFinder, this.data.LightSource, 100000);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Vector pixelColor = GetPixelColorFromPhotonmap(x, y, photonmap);
                    image.SetPixel(x, y, VectorToColor(pixelColor));
                }
            }

            return(image);
        }
示例#4
0
        private Vector GetPixelColorFromPhotonmap(int x, int y, Photonmap photonmap)
        {
            Ray    primaryRay  = data.Camera.GetPrimaryRay(x, y);
            float  cosAtCamera = Vector.Dot(primaryRay.Direction, this.data.Camera.Forward);
            Vector pathWeight  = new Vector(1, 1, 1) * cosAtCamera;

            var eyePoint = this.intersectionFinder.GetIntersectionPoint(primaryRay);

            Vector pixelColor = new Vector(0, 0, 0);

            if (eyePoint != null)
            {
                //100000 3000 1000
                float searchRadius   = 0.00634257728f * 2;//0.0383904837f;//0.065260686f;
                var   photons        = photonmap.SearchPhotons(eyePoint, searchRadius);
                float kernelFunction = 1.0f / (searchRadius * searchRadius * (float)Math.PI);
                foreach (var photon in photons)
                {
                    pixelColor += Vector.Mult(pathWeight, Vector.Mult(DiffuseBrdf.Evaluate(eyePoint), photon.PathWeight) * kernelFunction);
                }
            }

            return(pixelColor);
        }