示例#1
0
        public void Render(ICamera camera, World world, int nbThreads = 4, int maxRecursion = 10, bool shuffle = true)
        {
            var pixels = new List <Tuple <int, int> >();

            for (int y = 0; y < camera.VSize; y++)
            {
                for (int x = 0; x < camera.HSize; x++)
                {
                    pixels.Add(new Tuple <int, int>(x, y));
                }
            }

            if (shuffle)
            {
                Random r = new Random(0);
                pixels = pixels.OrderBy(job => r.Next()).ToList();
            }

            const int BatchSize = 128;

            for (int i = 0; i < pixels.Count; i += BatchSize)
            {
                PixelJob job = new PixelJob(Image, world, maxRecursion, RenderStatistics, camera);
                for (int j = 0; j < BatchSize; j++)
                {
                    if (i + j >= pixels.Count)
                    {
                        continue;
                    }
                    var pixel = pixels[i + j];
                    job.X.Add(pixel.Item1);
                    job.Y.Add(pixel.Item2);
                }
                PixelJobs.Enqueue(job);
            }

            threads = new Thread[nbThreads];

            if (nbThreads == 0)
            {
                Run();
                return;
            }

            for (int i = 0; i < nbThreads; i++)
            {
                Thread t = new Thread(Run)
                {
                    Name = $"RayTracerWorker_{i}"
                };
                t.Start();
                threads[i] = t;
            }
        }
示例#2
0
 public void Stop()
 {
     stopRequested = true;
     PixelJobs.Clear();
 }