Exemple #1
0
        private void AntiAliasXSpan(RectangleInt viewport, Scene scene, ColorF[][] imageBufferAsDoubles, int maxSamples, int y)
        {
            for (int x = 1; x < viewport.Width - 1; x++)
            {
                ColorF avg = (imageBufferAsDoubles[x - 1][y - 1] + imageBufferAsDoubles[x][y - 1] + imageBufferAsDoubles[x + 1][y - 1] +
                              imageBufferAsDoubles[x - 1][y] + imageBufferAsDoubles[x][y] + imageBufferAsDoubles[x + 1][y] +
                              imageBufferAsDoubles[x - 1][y + 1] + imageBufferAsDoubles[x][y + 1] + imageBufferAsDoubles[x + 1][y + 1]) / 9;

                // use a more accurate anti-aliasing method (MonteCarlo implementation)
                // this will fire multiple rays per pixel
                double sumOfDifferencesThreshold = .05;                 // TODO: figure out a good way to determine this.
                if (avg.SumOfDistances(imageBufferAsDoubles[x][y]) > sumOfDifferencesThreshold)
                {
                    ColorF accumulatedColor = imageBufferAsDoubles[x][y];
                    for (int i = 0; i < maxSamples; i++)
                    {
                        // get some 'random' samples
                        double rx = Math.Sign(i % 4 - 1.5) * (IntNoise(x + y * viewport.Width * maxSamples * 2 + i) + 1) / 4;
                        double ry = Math.Sign(i % 2 - 0.5) * (IntNoise(x + y * viewport.Width * maxSamples * 2 + 1 + i) + 1) / 4;

                        double xp = x + rx;
                        double yp = y + ry;

                        Ray ray = scene.camera.GetRay(xp, yp);
                        accumulatedColor += FullyTraceRay(ray, scene, out _);
                    }

                    imageBufferAsDoubles[x][y] = accumulatedColor / (maxSamples + 1);
                }
            }
        }