private void RenderLoop(int iterations) { // Create a ray tracer, and create a reference to "sphere2" that we are going to bounce var rayTracer = new RayTracer(_width, _height); var scene = rayTracer.DefaultScene; var sphere2 = (Sphere)scene.Things[0]; // The first item is assumed to be our sphere var baseY = sphere2.Radius; sphere2.Center.Y = sphere2.Radius; // Timing determines how fast the ball bounces as well as diagnostics frames/second info var renderingTime = new Stopwatch(); var totalTime = Stopwatch.StartNew(); // Keep rendering until the iteration count is hit for (_frames = 0; _frames < iterations; _frames++) { // Or the rendering task has been canceled if (_cancellation.IsCancellationRequested) { break; } // Get the next buffer var rgb = _freeBuffers.GetObject(); // Determine the new position of the sphere based on the current time elapsed double dy2 = 0.8 * Math.Abs(Math.Sin(totalTime.ElapsedMilliseconds * Math.PI / 3000)); sphere2.Center.Y = (float)(baseY + dy2); // Render the scene renderingTime.Reset(); renderingTime.Start(); ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = _degreeOfParallelism, CancellationToken = _cancellation.Token }; if (!_parallel) { rayTracer.RenderSequential(scene, rgb); } else if (_showThreads) { rayTracer.RenderParallelShowingThreads(scene, rgb, options); } else { rayTracer.RenderParallel(scene, rgb, options); } renderingTime.Stop(); _framesPerSecond = (1000.0 / renderingTime.ElapsedMilliseconds); _freeBuffers.PutObject(rgb); } }
private void RenderOneTo(string fileName, bool wirteToFile) { // Create a ray tracer, and create a reference to "sphere2" that we are going to bounce var rayTracer = new RayTracer(_width, _height); var scene = rayTracer.DefaultScene; var sphere2 = (Sphere)scene.Things[0]; // The first item is assumed to be our sphere var baseY = sphere2.Radius; sphere2.Center.Y = sphere2.Radius; var rgb = new Color[_width * _height]; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); rayTracer.RenderSequential(scene, rgb); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); if (wirteToFile) { var file = new System.IO.StreamWriter(fileName); file.WriteLine("P3"); file.WriteLine(_width + " " + _height); file.WriteLine("255"); for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { var point = rgb[i * _height + j]; file.Write(String.Format("{0,-3}", Color.ToInt32(point.R)) + " " + String.Format("{0,-3}", Color.ToInt32(point.G)) + " " + String.Format("{0,-3}", Color.ToInt32(point.B)) + " "); } file.Write("\n"); } } }
private void RenderLoop(int iterations) { // Create a ray tracer, and create a reference to "sphere2" that we are going to bounce var rayTracer = new RayTracer(_width, _height); var scene = rayTracer.DefaultScene; var sphere2 = (Sphere)scene.Things[0]; // The first item is assumed to be our sphere var baseY = sphere2.Radius; sphere2.Center.Y = sphere2.Radius; // Timing determines how fast the ball bounces as well as diagnostics frames/second info var renderingTime = new Stopwatch(); var totalTime = Stopwatch.StartNew(); // Keep rendering until the iteration count is hit for (_frames = 0; _frames < iterations; _frames++) { // Or the rendering task has been canceled if (_cancellation.IsCancellationRequested) { break; } // Get the next buffer var rgb = _freeBuffers.GetObject(); // Determine the new position of the sphere based on the current time elapsed double dy2 = 0.8 * Math.Abs(Math.Sin(totalTime.ElapsedMilliseconds * Math.PI / 3000)); sphere2.Center.Y = (float)(baseY + dy2); // Render the scene renderingTime.Reset(); renderingTime.Start(); ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = _degreeOfParallelism, CancellationToken = _cancellation.Token }; if (!_parallel) rayTracer.RenderSequential(scene, rgb); else if (_showThreads) rayTracer.RenderParallelShowingThreads(scene, rgb, options); else rayTracer.RenderParallel(scene, rgb, options); renderingTime.Stop(); _framesPerSecond = (1000.0 / renderingTime.ElapsedMilliseconds); _freeBuffers.PutObject(rgb); } }