public void scatterPhoton(ShadingState state, Color power) { Color diffuse; // make sure we are on the right side of the material if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0.0) { state.getNormal().negate(); state.getGeoNormal().negate(); } diffuse = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float avg = diffuse.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avg) { // photon is scattered power.mul(diffuse).mul(1.0f / avg); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avg; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } }
public FakeGIEngine(Options options) { up = options.getVector("gi.fake.up", new Vector3(0, 1, 0)).normalize(); sky = options.getColor("gi.fake.sky", Color.WHITE).copy(); ground = options.getColor("gi.fake.ground", Color.BLACK).copy(); sky.mul((float)Math.PI); ground.mul((float)Math.PI); }
public bool init(Options options, Scene scene) { up = options.getVector("gi.fake.up", new Vector3(0, 1, 0)).normalize(); sky = options.getColor("gi.fake.sky", Color.WHITE).copy(); ground = options.getColor("gi.fake.ground", Color.BLACK).copy(); sky.mul((float) Math.PI); ground.mul((float) Math.PI); return true; }
public void scatterPhoton(ShadingState state, Color power) { // make sure we are on the right side of the material state.faceforward(); Color d = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, d); float avgD = d.getAverage(); float avgS = spec.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avgD) { // photon is scattered diffusely power.mul(d).mul(1.0f / avgD); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avgD; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0f - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } else if (rnd < avgD + avgS) { // photon is scattered specularly float dn = 2.0f * state.getCosND(); // reflected direction Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().dx; refDir.y = (dn * state.getNormal().y) + state.getRay().dy; refDir.z = (dn * state.getNormal().z) + state.getRay().dz; power.mul(spec).mul(1.0f / avgS); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * (rnd - avgD) / avgS; double v = state.getRandom(0, 1, 1); float s = (float)Math.Pow(v, 1 / (this.power + 1)); float s1 = (float)Math.Sqrt(1 - s * s); Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s); w = onb.transform(w, new Vector3()); state.traceReflectionPhoton(new Ray(state.getPoint(), w), power); } }
public void ScatterPhoton(ShadingState state, Color power) { float avg = color.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd >= avg) return; state.faceforward(); float cos = state.getCosND(); power.mul(color).mul(1.0f / avg); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); }
public void getPhoton(double randX1, double randY1, double randX2, double randY2, Point3 p, Vector3 dir, Color power) { float z = (float)(1 - 2 * randX2); float r = (float)Math.Sqrt(Math.Max(0, 1 - z * z)); float phi = (float)(2 * Math.PI * randY2); float x = r * (float)Math.Cos(phi); float y = r * (float)Math.Sin(phi); p.x = center.x + x * radius; p.y = center.y + y * radius; p.z = center.z + z * radius; OrthoNormalBasis basis = OrthoNormalBasis.makeFromW(new Vector3(x, y, z)); phi = (float)(2 * Math.PI * randX1); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(randY1); float cosTheta = (float)Math.Sqrt(1 - randY1); dir.x = cosPhi * sinTheta; dir.y = sinPhi * sinTheta; dir.z = cosTheta; basis.transform(dir); power.set(radiance); power.mul((float)(Math.PI * Math.PI * 4 * r2)); }
public void ScatterPhoton(ShadingState state, Color power) { Color diffuse; // make sure we are on the right side of the material state.faceforward(); diffuse = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float d = diffuse.getAverage(); float r = d * refl; double rnd = state.getRandom(0, 0, 1); if (rnd < d) { // photon is scattered power.mul(diffuse).mul(1.0f / d); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / d; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } else if (rnd < d + r) { float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection()); power.mul(diffuse).mul(1.0f / d); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } }
public void Run() { ByteUtil.InitByteUtil(); IntersectionState istate = new IntersectionState(); for (int i = start; i < end; i++) { lock (lockObj) { UI.taskUpdate(server.photonCounter); server.photonCounter++; if (UI.taskCanceled()) return; } int qmcI = i + seed; double rand = QMC.halton(0, qmcI) * histogram[histogram.Length - 1]; int j = 0; while (rand >= histogram[j] && j < histogram.Length) j++; // make sure we didn't pick a zero-probability light if (j == histogram.Length) continue; double randX1 = (j == 0) ? rand / histogram[0] : (rand - histogram[j]) / (histogram[j] - histogram[j - 1]); double randY1 = QMC.halton(1, qmcI); double randX2 = QMC.halton(2, qmcI); double randY2 = QMC.halton(3, qmcI); Point3 pt = new Point3(); Vector3 dir = new Vector3(); Color power = new Color(); server.lights[j].getPhoton(randX1, randY1, randX2, randY2, pt, dir, power); power.mul(scale); Ray r = new Ray(pt, dir); server.scene.trace(r, istate); if (istate.hit()) server.shadePhoton(ShadingState.createPhotonState(r, istate, qmcI, map, server), power); } }
public void precomputeRadiance() { if (storedPhotons == 0) return; // precompute the radiance for all photons that are neither // leaves nor parents of leaves in the tree. int quadStoredPhotons = halfStoredPhotons / 2; Point3 p = new Point3(); Vector3 n = new Vector3(); Point3 ppos = new Point3(); Vector3 pdir = new Vector3(); Vector3 pvec = new Vector3(); Color irr = new Color(); Color pow = new Color(); float maxDist2 = gatherRadius * gatherRadius; NearestPhotons np = new NearestPhotons(p, numGather, maxDist2); Photon[] temp = new Photon[quadStoredPhotons + 1]; UI.taskStart("Precomputing radiance", 1, quadStoredPhotons); for (int i = 1; i <= quadStoredPhotons; i++) { UI.taskUpdate(i); Photon curr = photons[i]; p.set(curr.x, curr.y, curr.z); Vector3.decode(curr.normal, n); irr.set(Color.BLACK); np.reset(p, maxDist2); locatePhotons(np); if (np.found < 8) { curr.data = 0; temp[i] = curr; continue; } float invArea = 1.0f / ((float)Math.PI * np.dist2[0]); float maxNDist = np.dist2[0] * 0.05f; for (int j = 1; j <= np.found; j++) { Photon phot = np.index[j]; Vector3.decode(phot.dir, pdir); float cos = -Vector3.dot(pdir, n); if (cos > 0.01f) { ppos.set(phot.x, phot.y, phot.z); Point3.sub(ppos, p, pvec); float pcos = Vector3.dot(pvec, n); if ((pcos < maxNDist) && (pcos > -maxNDist)) irr.add(pow.setRGBE(phot.power)); } } irr.mul(invArea); // compute radiance irr.mul(new Color(curr.data)).mul(1.0f / (float)Math.PI); curr.data = irr.toRGBE(); temp[i] = curr; } UI.taskStop(); // resize photon map to only include irradiance photons numGather /= 4; maxRadius = 1.4f * (float)Math.Sqrt(maxPower * numGather); if (gatherRadius > maxRadius) gatherRadius = maxRadius; storedPhotons = quadStoredPhotons; halfStoredPhotons = storedPhotons / 2; log2n = (int)Math.Ceiling(Math.Log(storedPhotons) / Math.Log(2.0)); photons = temp; hasRadiance = true; }
public void ScatterPhoton(ShadingState state, Color power) { Color diffuse, specular; // make sure we are on the right side of the material state.faceforward(); diffuse = getDiffuse(state); specular = getSpecular(state); state.storePhoton(state.getRay().getDirection(), power, diffuse); float d = diffuse.getAverage(); float r = specular.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < d) { // photon is scattered power.mul(diffuse).mul(1.0f / d); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / d; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } else if (rnd < d + r) { if (glossyness == 0) { float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection()); power.mul(diffuse).mul(1.0f / d); // photon is reflected float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else { float dn = 2.0f * state.getCosND(); // reflected direction Vector3 refDir = new Vector3(); refDir.x = (dn * state.getNormal().x) + state.getRay().dx; refDir.y = (dn * state.getNormal().y) + state.getRay().dy; refDir.z = (dn * state.getNormal().z) + state.getRay().dz; power.mul(spec).mul(1.0f / r); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * (rnd - r) / r; double v = state.getRandom(0, 1, 1); float s = (float)Math.Pow(v, 1 / ((1.0f / glossyness) + 1)); float s1 = (float)Math.Sqrt(1 - s * s); Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s); w = onb.transform(w, new Vector3()); state.traceReflectionPhoton(new Ray(state.getPoint(), w), power); } } }
public void scatterPhoton(ShadingState state, Color power) { // make sure we are on the right side of the material state.faceforward(); Color d = getDiffuse(state); state.storePhoton(state.getRay().getDirection(), power, d); float avgD = d.getAverage(); float avgS = rhoS.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avgD) { // photon is scattered diffusely power.mul(d).mul(1.0f / avgD); OrthoNormalBasis onb = state.getBasis(); double u = 2 * Math.PI * rnd / avgD; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0f - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } else if (rnd < avgD + avgS) { // photon is scattered specularly power.mul(rhoS).mul(1 / avgS); OrthoNormalBasis basis = state.getBasis(); Vector3 inv = state.getRay().getDirection().negate(new Vector3()); double r1 = rnd / avgS; double r2 = state.getRandom(0, 1, 1); float alphaRatio = alphaY / alphaX; float phi = 0; if (r1 < 0.25) { double val = 4 * r1; phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); } else if (r1 < 0.5) { double val = 1 - 4 * (0.5 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = (float)Math.PI - phi; } else if (r1 < 0.75) { double val = 4 * (r1 - 0.5); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi += (float)Math.PI; } else { double val = 1 - 4 * (1 - r1); phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val)); phi = 2 * (float)Math.PI - phi; } float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float denom = (cosPhi * cosPhi) / (alphaX * alphaX) + (sinPhi * sinPhi) / (alphaY * alphaY); float theta = (float)Math.Atan(Math.Sqrt(-Math.Log(1 - r2) / denom)); float sinTheta = (float)Math.Sin(theta); float cosTheta = (float)Math.Cos(theta); Vector3 h = new Vector3(); h.x = sinTheta * cosPhi; h.y = sinTheta * sinPhi; h.z = cosTheta; basis.transform(h); Vector3 o = new Vector3(); float ih = Vector3.dot(h, inv); o.x = 2 * ih * h.x - inv.x; o.y = 2 * ih * h.y - inv.y; o.z = 2 * ih * h.z - inv.z; Ray r = new Ray(state.getPoint(), o); state.traceReflectionPhoton(r, power); } }
public static Color mul(float s, Color c) { return(Color.mul(s, c, new Color())); }
public static Color mul(Color c1, Color c2) { return(Color.mul(c1, c2, new Color())); }
public void ScatterPhoton(ShadingState state, Color power) { Color refr = Color.mul(1 - f0, color); Color refl = Color.mul(f0, color); float avgR = refl.getAverage(); float avgT = refr.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avgR) { state.faceforward(); // don't reflect internally if (state.isBehind()) return; // photon is reflected float cos = state.getCosND(); power.mul(refl).mul(1.0f / avgR); float dn = 2 * cos; Vector3 dir = new Vector3(); dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else if (rnd < avgR + avgT) { state.faceforward(); // photon is refracted float cos = state.getCosND(); float neta = state.isBehind() ? eta : 1.0f / eta; power.mul(refr).mul(1.0f / avgT); float wK = -neta; float arg = 1 - (neta * neta * (1 - (cos * cos))); Vector3 dir = new Vector3(); if (state.isBehind() && absorptionDistance > 0) { // this ray is inside the object and leaving it // compute attenuation that occured along the ray power.mul(Color.mul(-state.getRay().getMax() / absorptionDistance, absorptionColor.copy().opposite()).exp()); } if (arg < 0) { // TIR float dn = 2 * cos; dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x; dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y; dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z; state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power); } else { float nK = (neta * cos) - (float)Math.Sqrt(arg); dir.x = (-wK * state.getRay().dx) + (nK * state.getNormal().x); dir.y = (-wK * state.getRay().dy) + (nK * state.getNormal().y); dir.z = (-wK * state.getRay().dz) + (nK * state.getNormal().z); state.traceRefractionPhoton(new Ray(state.getPoint(), dir), power); } } }
public void ScatterPhoton(ShadingState state, Color power) { int side = state.getPrimitiveID(); Color kd = null; switch (side) { case 0: kd = left; break; case 1: kd = right; break; case 3: kd = back; break; case 4: kd = bottom; break; case 5: float lx = state.getPoint().x; float ly = state.getPoint().y; if (lx >= lxmin && lx < lxmax && ly >= lymin && ly < lymax && state.getRay().dz > 0) return; kd = top; break; default: Debug.Assert(false); break; } // make sure we are on the right side of the material if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0) { state.getNormal().negate(); state.getGeoNormal().negate(); } state.storePhoton(state.getRay().getDirection(), power, kd); double avg = kd.getAverage(); double rnd = state.getRandom(0, 0, 1); if (rnd < avg) { // photon is scattered power.mul(kd).mul(1 / (float)avg); OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(state.getNormal()); double u = 2 * Math.PI * rnd / avg; double v = state.getRandom(0, 1, 1); float s = (float)Math.Sqrt(v); float s1 = (float)Math.Sqrt(1.0 - v); Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1); w = onb.transform(w, new Vector3()); state.traceDiffusePhoton(new Ray(state.getPoint(), w), power); } }