コード例 #1
0
 public void Render()
 {
     if (sampler != null)
     {
         for (int i = 0; i < camera.ScreenWidth; i++)
         {
             for (int j = threadId; j < camera.ScreenHeight; j += Constants.NumberOfThreads)
             {
                 Color pixelColor = new Color(0, 0, 0);
                 List <List <Sample> > subPathSamples = new List <List <Sample> >();
                 List <Sample>         samples        = sampler.CreateSamples();
                 List <LightSample>    lightSamples   = new List <LightSample>();
                 if (integrator is PathTraceIntegrator)
                 {
                     lightSamples = sampler.GetLightSamples(Constants.MaximalPathLength);
                     for (int s = 0; s < Constants.MaximalPathLength; s++)
                     {
                         subPathSamples.Add(sampler.CreateSamples());
                     }
                     foreach (Sample sample in samples)
                     {
                         Ray ray = camera.CreateRay(i + sample.X, j + sample.Y);
                         pixelColor.Append(integrator.Integrate(ray, objects, lights, sampler, subPathSamples, Randomizer.PickRandomLightSample(lightSamples)));
                     }
                 }
                 else
                 {
                     lightSamples = sampler.GetLightSamples();
                     foreach (Sample sample in samples)
                     {
                         Ray ray = camera.CreateRay(i + sample.X, j + sample.Y);
                         pixelColor.Append(integrator.Integrate(ray, objects, lights, sampler, subPathSamples, Randomizer.PickRandomLightSample(lightSamples)));
                     }
                 }
                 pixelColor.VoidDiv(samples.Count);
                 film.SetPixel(i, j, pixelColor);
             }
         }
     }
     else
     {
         for (int i = 0; i < camera.ScreenWidth; i++)
         {
             for (int j = threadId; j < camera.ScreenHeight; j += Constants.NumberOfThreads)
             {
                 Ray   ray        = camera.CreateRay(i, j);
                 Color pixelColor = integrator.Integrate(ray, objects, lights, null, null, null);
                 film.SetPixel(i, j, pixelColor);
             }
         }
     }
     ThreadDone(this, new EventArgs());
 }
コード例 #2
0
ファイル: BasicRayTracer.cs プロジェクト: lunactic/RayTracer
        public void Render()
        {
            stopwatch = new Stopwatch();
            stopwatch.Reset();
            if (Constants.NumberOfThreads == 1) //One Thread
            {
                stopwatch.Start();
                ISampler sampler = (ISampler)Activator.CreateInstance(Constants.Sampler);
                if (Constants.IsSamplingOn && Constants.NumberOfSamples > 0)
                {
                    Console.WriteLine("Using Supersampling with: " + Constants.NumberOfSamples + " samples!");

                    for (int i = 0; i < scene.Film.Width; i++)
                    {
                        for (int j = 0; j < scene.Film.Height; j++)
                        {
                            Color pixelColor = new Color(0, 0, 0);
                            List <List <Sample> > subPathSamples = new List <List <Sample> >();
                            List <LightSample>    lightSamples   = new List <LightSample>();
                            List <Sample>         samples        = sampler.CreateSamples();
                            if (scene.Integrator is PathTraceIntegrator)
                            {
                                lightSamples = sampler.GetLightSamples(Constants.MaximalPathLength);
                                for (int s = 0; s < Constants.MaximalPathLength; s++)
                                {
                                    subPathSamples.Add(sampler.CreateSamples());
                                }
                                foreach (Sample sample in samples)
                                {
                                    Ray ray = scene.Camera.CreateRay(i + sample.X, j + sample.Y);
                                    pixelColor.Append(scene.Integrator.Integrate(ray, scene.Objects, scene.Lights, sampler, subPathSamples, Randomizer.PickRandomLightSample(lightSamples)));
                                }
                            }
                            else
                            {
                                lightSamples = sampler.GetLightSamples();
                                foreach (Sample sample in samples)
                                {
                                    Ray ray = scene.Camera.CreateRay(i + sample.X, j + sample.Y);
                                    pixelColor.Append(scene.Integrator.Integrate(ray, scene.Objects, scene.Lights, sampler, subPathSamples, Randomizer.PickRandomLightSample(lightSamples)));
                                }
                            }

                            pixelColor.VoidDiv(samples.Count);
                            scene.Film.SetPixel(i, j, pixelColor);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < scene.Film.Width; i++)
                    {
                        for (int j = 0; j < scene.Film.Height; j++)
                        {
                            Ray   ray   = scene.Camera.CreateRay(i, j);
                            Color color = scene.Integrator.Integrate(ray, scene.Objects, scene.Lights, null, null, null);
                            scene.Film.SetPixel(i, j, color);
                        }
                    }
                }
                stopwatch.Stop();
                Debug.WriteLine("Finished rendering in: " + stopwatch.ElapsedMilliseconds + " ms.");
                Tonemapper.SaveImage("C:\\Test\\" + scene.FileName, scene.Film);
            }
            else //More than 1 thread
            {
                if (Constants.IsSamplingOn && Constants.NumberOfSamples > 0)
                {
                    Debug.WriteLine("Start rendering with: " + Constants.NumberOfThreads + " Threads");
                    stopwatch.Start();
                    finishedThreads = 0;
                    for (int i = 0; i < Constants.NumberOfThreads; i++)
                    {
                        ISampler sampler = new StratifiedSampler();
                        MultiThreadingRenderer renderer = new MultiThreadingRenderer(i, scene.Objects, scene.Lights,
                                                                                     scene.Camera,
                                                                                     scene.Film);
                        renderer.ThreadDone += HandleThreadDone;
                        Thread t = new Thread(renderer.Render);
                        t.Start();
                    }
                }
                else
                {
                    stopwatch.Start();
                    finishedThreads = 0;
                    for (int i = 0; i < Constants.NumberOfThreads; i++)
                    {
                        MultiThreadingRenderer renderer = new MultiThreadingRenderer(i, scene.Objects, scene.Lights,
                                                                                     scene.Camera,
                                                                                     scene.Film);
                        renderer.ThreadDone += HandleThreadDone;
                        Thread t = new Thread(renderer.Render);
                        t.Start();
                    }
                }
            }
        }