public bool calculatePhotons(PhotonStore map, string type, int seed, Options options) { if (map == null) return true; if (lights.Length == 0) { UI.printError(UI.Module.LIGHT, "Unable to trace {0} photons, no lights in scene", type); return false; } float[] histogram = new float[lights.Length]; histogram[0] = lights[0].getPower(); for (int i = 1; i < lights.Length; i++) histogram[i] = histogram[i - 1] + lights[i].getPower(); UI.printInfo(UI.Module.LIGHT, "Tracing {0} photons ...", type); map.prepare(options, scene.getBounds()); int numEmittedPhotons = map.numEmit(); if (numEmittedPhotons <= 0 || histogram[histogram.Length - 1] <= 0) { UI.printError(UI.Module.LIGHT, "Photon mapping enabled, but no {0} photons to emit", type); return false; } UI.taskStart("Tracing " + type + " photons", 0, numEmittedPhotons); Thread[] photonThreads = new Thread[scene.getThreads()]; float scale = 1.0f / numEmittedPhotons; int delta = numEmittedPhotons / photonThreads.Length; photonCounter = 0; SunflowSharp.Systems.Timer photonTimer = new SunflowSharp.Systems.Timer(); photonTimer.start(); for (int i = 0; i < photonThreads.Length; i++) { int threadID = i; int start = threadID * delta; int end = (threadID == (photonThreads.Length - 1)) ? numEmittedPhotons : (threadID + 1) * delta; photonThreads[i] = new Thread(new ThreadStart(new CalculatePhotons(start, end, this, seed, histogram, scale, map, lockObj).Run)); photonThreads[i].Priority = scene.getThreadPriority(); photonThreads[i].Start(); } for (int i = 0; i < photonThreads.Length; i++) { try { photonThreads[i].Join(); } catch (Exception e) { UI.printError(UI.Module.LIGHT, "Photon thread {0} of {1} was interrupted", i + 1, photonThreads.Length); return false; } } if (UI.taskCanceled()) { UI.taskStop(); // shut down task cleanly return false; } photonTimer.end(); UI.taskStop(); UI.printInfo(UI.Module.LIGHT, "Tracing time for {0} photons: {1}", type, photonTimer.ToString()); map.init(); return true; }
public bool calculatePhotons(PhotonStore map, string type, int seed) { if (map == null) { return(true); } if (lights.Length == 0) { UI.printError(UI.Module.LIGHT, "Unable to trace {0} photons, no lights in scene", type); return(false); } float[] histogram = new float[lights.Length]; histogram[0] = lights[0].getPower(); for (int i = 1; i < lights.Length; i++) { histogram[i] = histogram[i - 1] + lights[i].getPower(); } UI.printInfo(UI.Module.LIGHT, "Tracing %s photons ...", type); int numEmittedPhotons = map.numEmit(); if (numEmittedPhotons <= 0 || histogram[histogram.Length - 1] <= 0) { UI.printError(UI.Module.LIGHT, "Photon mapping enabled, but no {0} photons to emit", type); return(false); } map.prepare(scene.getBounds()); UI.taskStart("Tracing " + type + " photons", 0, numEmittedPhotons); Thread[] photonThreads = new Thread[scene.getThreads()]; float scale = 1.0f / numEmittedPhotons; int delta = numEmittedPhotons / photonThreads.Length; photonCounter = 0; SunflowSharp.Systems.Timer photonTimer = new SunflowSharp.Systems.Timer(); photonTimer.start(); for (int i = 0; i < photonThreads.Length; i++) { int threadID = i; int start = threadID * delta; int end = (threadID == (photonThreads.Length - 1)) ? numEmittedPhotons : (threadID + 1) * delta; photonThreads[i] = new Thread(new ThreadStart(new CalculatePhotons(start, end, this, seed, histogram, scale, map, lockObj).Run)); photonThreads[i].Priority = scene.getThreadPriority(); photonThreads[i].Start(); } for (int i = 0; i < photonThreads.Length; i++) { try { photonThreads[i].Join(); } catch (Exception e) { UI.printError(UI.Module.LIGHT, "Photon thread {0} of {1} was interrupted", i + 1, photonThreads.Length); return(false); } } if (UI.taskCanceled()) { UI.taskStop(); // shut down task cleanly return(false); } photonTimer.end(); UI.taskStop(); UI.printInfo(UI.Module.LIGHT, "Tracing time for {0} photons: {1}", type, photonTimer.ToString()); map.init(); return(true); }