/// <summary> /// Renders the given scene, providing a callback for each individual line rendered. Uses a single background thread. /// </summary> /// <param name="width">The width of the rendered image</param> /// <param name="height">The height of the rendered image</param> /// <param name="scene">The scene to render</param> /// <param name="callback">The delegate invoked when a thread completes rendering a line</param> public void RenderSceneLines(Scene scene, int width, int height, LineFinishedHandler callback) { if (width == -1 || height == -1) { width = renderSize.Width; height = renderSize.Height; } else { renderSize = new Size(width, height); } var before = DateTime.UtcNow; Task.Run(() => // Even single-threaded method should run in the background to avoid freezing UI. { for (int y = height - 1; y >= 0; y--) { var colors = new Color[width]; for (int x = 0; x < width; x++) { var viewPortX = ((2 * x) / (float)width) - 1; var viewPortY = ((2 * y) / (float)height) - 1; var color = TraceRayAgainstScene(GetRay(viewPortX, viewPortY), scene); colors[x] = color; } callback(y, colors); } }); var after = DateTime.UtcNow; System.Diagnostics.Debug.WriteLine("Total render time: " + (after - before).TotalMilliseconds + " ms"); }
/// <summary> /// Renders the given scene, providing a callback for each individual line rendered. Each line is rendered on a separate thread. /// </summary> /// <param name="width">The width of the rendered image</param> /// <param name="width">The height of the rendered image</param> /// <param name="scene">The scene to render</param> /// <param name="callback">The delegate invoked when a thread completes rendering a line</param> public void RenderSceneLinesThreaded(Scene scene, int width, int height, LineFinishedHandler callback) { if (width == -1 || height == -1) { width = renderSize.Width; height = renderSize.Height; } else { renderSize = new Size(width, height); } var before = DateTime.UtcNow; List <Task> tasks = new List <Task>(); for (int yCounter = height - 1; yCounter >= 0; yCounter--) { var y = yCounter; Color[] colors = new Color[width]; Task.Run(() => { for (int xCounter = 0; xCounter < width; xCounter++) { var x = xCounter; var viewPortX = ((2 * x) / (float)width) - 1; var viewPortY = ((2 * y) / (float)height) - 1; var color = TraceRayAgainstScene(GetRay(viewPortX, viewPortY), scene); colors[x] = color; } callback(y, colors); }); } var after = DateTime.UtcNow; System.Diagnostics.Debug.WriteLine("Total render time: " + (after - before).TotalMilliseconds + " ms"); }
/// <summary> /// Renders the given scene, providing a callback for each individual line rendered. Each line is rendered on a separate thread. /// </summary> /// <param name="width">The width of the rendered image</param> /// <param name="width">The height of the rendered image</param> /// <param name="scene">The scene to render</param> /// <param name="callback">The delegate invoked when a thread completes rendering a line</param> public void RenderSceneLinesThreaded(Scene scene, int width, int height, LineFinishedHandler callback) { if (width == -1 || height == -1) { width = renderSize.Width; height = renderSize.Height; } else { renderSize = new Size(width, height); } var before = DateTime.UtcNow; List<Task> tasks = new List<Task>(); for (int yCounter = height - 1; yCounter >= 0; yCounter--) { var y = yCounter; Color[] colors = new Color[width]; Task.Run(() => { for (int xCounter = 0; xCounter < width; xCounter++) { var x = xCounter; var viewPortX = ((2 * x) / (float)width) - 1; var viewPortY = ((2 * y) / (float)height) - 1; var color = TraceRayAgainstScene(GetRay(viewPortX, viewPortY), scene); colors[x] = color; } callback(y, colors); }); } var after = DateTime.UtcNow; System.Diagnostics.Debug.WriteLine("Total render time: " + (after - before).TotalMilliseconds + " ms"); }