public void getSamples(ShadingState state) { Vector3 d = Point3.sub(lightPoint, state.getPoint(), new Vector3()); if (Vector3.dot(d, state.getNormal()) > 0 && Vector3.dot(d, state.getGeoNormal()) > 0) { LightSample dest = new LightSample(); // prepare shadow ray dest.setShadowRay(new Ray(state.getPoint(), lightPoint)); float scale = 1.0f / (float)(4 * Math.PI * lightPoint.distanceToSquared(state.getPoint())); dest.setRadiance(power, power); dest.getDiffuseRadiance().mul(scale); dest.getSpecularRadiance().mul(scale); dest.traceShadow(state); state.addSample(dest); } }
public void getSamples(ShadingState state) { if (storedPhotons == 0) return; NearestPhotons np = new NearestPhotons(state.getPoint(), gatherNum, gatherRadius * gatherRadius); locatePhotons(np); if (np.found < 8) return; Point3 ppos = new Point3(); Vector3 pdir = new Vector3(); Vector3 pvec = new Vector3(); float invArea = 1.0f / ((float)Math.PI * np.dist2[0]); float maxNDist = np.dist2[0] * 0.05f; float f2r2 = 1.0f / (filterValue * filterValue * np.dist2[0]); float fInv = 1.0f / (1.0f - 2.0f / (3.0f * filterValue)); for (int i = 1; i <= np.found; i++) { Photon phot = np.index[i]; Vector3.decode(phot.dir, pdir); float cos = -Vector3.dot(pdir, state.getNormal()); if (cos > 0.001) { ppos.set(phot.x, phot.y, phot.z); Point3.sub(ppos, state.getPoint(), pvec); float pcos = Vector3.dot(pvec, state.getNormal()); if ((pcos < maxNDist) && (pcos > -maxNDist)) { LightSample sample = new LightSample(); sample.setShadowRay(new Ray(state.getPoint(), pdir.negate())); sample.setRadiance(new Color().setRGBE(np.index[i].power).mul(invArea / cos), Color.BLACK); sample.getDiffuseRadiance().mul((1.0f - (float)Math.Sqrt(np.dist2[i] * f2r2)) * fInv); state.addSample(sample); } } } }
public void getSamples(ShadingState state) { if (samples == null) { int n = state.getDiffuseDepth() > 0 ? 1 : numSamples; for (int i = 0; i < n; i++) { // random offset on unit square, we use the infinite version of // getRandom because the light sampling is adaptive double randX = state.getRandom(i, 0, n); double randY = state.getRandom(i, 1, n); int x = 0; while (randX >= colHistogram[x] && x < colHistogram.Length - 1) x++; float[] rowHistogram = imageHistogram[x]; int y = 0; while (randY >= rowHistogram[y] && y < rowHistogram.Length - 1) y++; // sample from (x, y) float u = (float)((x == 0) ? (randX / colHistogram[0]) : ((randX - colHistogram[x - 1]) / (colHistogram[x] - colHistogram[x - 1]))); float v = (float)((y == 0) ? (randY / rowHistogram[0]) : ((randY - rowHistogram[y - 1]) / (rowHistogram[y] - rowHistogram[y - 1]))); float px = ((x == 0) ? colHistogram[0] : (colHistogram[x] - colHistogram[x - 1])); float py = ((y == 0) ? rowHistogram[0] : (rowHistogram[y] - rowHistogram[y - 1])); float su = (x + u) / colHistogram.Length; float sv = (y + v) / rowHistogram.Length; float invP = (float)Math.Sin(sv * Math.PI) * jacobian / (n * px * py); Vector3 dir = getDirection(su, sv); basis.transform(dir); if (Vector3.dot(dir, state.getGeoNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), dir)); dest.getShadowRay().setMax(float.MaxValue); Color radiance = texture.getPixel(su, sv); dest.setRadiance(radiance, radiance); dest.getDiffuseRadiance().mul(invP); dest.getSpecularRadiance().mul(invP); dest.traceShadow(state); state.addSample(dest); } } } else { for (int i = 0; i < numSamples; i++) { if (Vector3.dot(samples[i], state.getGeoNormal()) > 0 && Vector3.dot(samples[i], state.getNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), samples[i])); dest.getShadowRay().setMax(float.MaxValue); dest.setRadiance(colors[i], colors[i]); dest.traceShadow(state); state.addSample(dest); } } } }
public void getSamples(ShadingState state) { if (lightBounds.contains(state.getPoint()) && state.getPoint().z < maxZ) { int n = state.getDiffuseDepth() > 0 ? 1 : samples; float a = area / n; for (int i = 0; i < n; i++) { // random offset on unit square double randX = state.getRandom(i, 0, n); double randY = state.getRandom(i, 1, n); Point3 p = new Point3(); p.x = (float)(lxmin * (1 - randX) + lxmax * randX); p.y = (float)(lymin * (1 - randY) + lymax * randY); p.z = maxZ - 0.001f; LightSample dest = new LightSample(); // prepare shadow ray to sampled point dest.setShadowRay(new Ray(state.getPoint(), p)); // check that the direction of the sample is the same as the // normal float cosNx = dest.dot(state.getNormal()); if (cosNx <= 0) return; // light source facing point ? // (need to check with light source's normal) float cosNy = dest.getShadowRay().dz; if (cosNy > 0) { // compute geometric attenuation and probability scale // factor float r = dest.getShadowRay().getMax(); float g = cosNy / (r * r); float scale = g * a; // set sample radiance dest.setRadiance(radiance, radiance); dest.getDiffuseRadiance().mul(scale); dest.getSpecularRadiance().mul(scale); dest.traceShadow(state); state.addSample(dest); } } } }