private void generateFixedSamples(Vector3[] samples, Color[] colors) { for (int i = 0; i < samples.Length; i++) { double randX = (double)i / (double)samples.Length; double randY = QMC.halton(0, i); 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 / (numSamples * px * py); samples[i] = getDirection(su, sv); basis.transform(samples[i]); colors[i] = texture.getPixel(su, sv).mul(invP); } }
/** * Ambient occlusion routine, returns a value between bright and dark * depending on the amount of geometric occlusion in the scene. * * @param samples number of sample rays * @param maxDist maximum Length of the rays * @param bright color when nothing is occluded * @param dark color when fully occluded * @return occlusion color */ public Color occlusion(int samples, float maxDist, Color bright, Color dark) { if (n == null) { // in case we got called on a geometry without orientation return(bright); } // make sure we are on the right side of the material faceforward(); OrthoNormalBasis onb = getBasis(); Vector3 w = new Vector3(); Color result = Color.black(); for (int i = 0; i < samples; i++) { float xi = (float)getRandom(i, 0, samples); float xj = (float)getRandom(i, 1, samples); float phi = (float)(2 * Math.PI * xi); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; onb.transform(w); Ray r = new Ray(p, w); r.setMax(maxDist); result.add(Color.blend(bright, dark, traceShadow(r))); } return(result.mul(1.0f / samples)); }
public Color getIrradiance(ShadingState state, Color diffuseReflectance) { OrthoNormalBasis onb = state.getBasis(); Vector3 w = new Vector3(); Color result = Color.black(); for (int i = 0; i < samples; i++) { float xi = (float)state.getRandom(i, 0, samples); float xj = (float)state.getRandom(i, 1, samples); float phi = (float)(2 * Math.PI * xi); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; onb.transform(w); Ray r = new Ray(state.getPoint(), w); r.setMax(maxDist); result.add(Color.blend(bright, dark, state.traceShadow(r))); } return(result.mul((float)Math.PI / samples)); }
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 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 if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0.0) { state.getNormal().negate(); state.getGeoNormal().negate(); } diffuse = Color.GRAY; 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 Vector3 getBump(float x, float y, OrthoNormalBasis basis, float scale) { Bitmap bitmap = getBitmap(); if (bitmap == null) { return(basis.transform(new Vector3(0, 0, 1))); } float dx = 1.0f / (bitmap.Width - 1); float dy = 1.0f / (bitmap.Height - 1); float b0 = getPixel(x, y).getLuminance(); float bx = getPixel(x + dx, y).getLuminance(); float by = getPixel(x, y + dy).getLuminance(); return(basis.transform(new Vector3(scale * (bx - b0) / dx, scale * (by - b0) / dy, 1)).normalize()); }
public void getSamples(ShadingState state) { if (Vector3.dot(sunDirWorld, state.getGeoNormal()) > 0 && Vector3.dot(sunDirWorld, state.getNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), sunDirWorld)); dest.getShadowRay().setMax(float.MaxValue); dest.setRadiance(sunColor, sunColor); dest.traceShadow(state); state.addSample(dest); } int n = state.getDiffuseDepth() > 0 ? 1 : numSkySamples; 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 localDir = getDirection(su, sv); Vector3 dir = basis.transform(localDir, new Vector3()); if (Vector3.dot(dir, state.getGeoNormal()) > 0 && Vector3.dot(dir, state.getNormal()) > 0) { LightSample dest = new LightSample(); dest.setShadowRay(new Ray(state.getPoint(), dir)); dest.getShadowRay().setMax(float.MaxValue); Color radiance = getSkyRGB(localDir); dest.setRadiance(radiance, radiance); dest.getDiffuseRadiance().mul(invP); dest.getSpecularRadiance().mul(invP); dest.traceShadow(state); state.addSample(dest); } } }
public Vector3 getBump(float x, float y, OrthoNormalBasis basis, float scale) { Bitmap bitmap = getBitmap(); float dx = 1.0f / bitmap.getWidth(); float dy = 1.0f / bitmap.getHeight(); float b0 = getPixel(x, y).getLuminance(); float bx = getPixel(x + dx, y).getLuminance(); float by = getPixel(x, y + dy).getLuminance(); return(basis.transform(new Vector3(scale * (b0 - bx), scale * (b0 - by), 1)).normalize()); }
/** * Computes a phong specular response to the current light samples and * global illumination. * * @param spec specular color * @param power phong exponent * @param numRays number of glossy rays to trace * @return shaded color */ public Color specularPhong(Color spec, float power, int numRays) { // integrate a phong specular function Color lr = Color.black(); if (!includeSpecular || spec.isBlack()) { return(lr); } // reflected direction float dn = 2 * cosND; Vector3 refDir = new Vector3(); refDir.x = (dn * n.x) + r.dx; refDir.y = (dn * n.y) + r.dy; refDir.z = (dn * n.z) + r.dz; // direct lighting foreach (LightSample sample in this) { float cosNL = sample.dot(n); float cosLR = sample.dot(refDir); if (cosLR > 0) { lr.madd(cosNL * (float)Math.Pow(cosLR, power), sample.getSpecularRadiance()); } } // indirect lighting if (numRays > 0) { int numSamples = getDepth() == 0 ? numRays : 1; OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(refDir); float mul = (2.0f * (float)Math.PI / (power + 1)) / numSamples; for (int i = 0; i < numSamples; i++) { // specular indirect lighting double r1 = getRandom(i, 0, numSamples); double r2 = getRandom(i, 1, numSamples); double u = 2 * Math.PI * r1; double s = (float)Math.Pow(r2, 1 / (power + 1)); double s1 = (float)Math.Sqrt(1 - s * s); Vector3 w = new Vector3((float)(Math.Cos(u) * s1), (float)(Math.Sin(u) * s1), (float)s); w = onb.transform(w, new Vector3()); float wn = Vector3.dot(w, n); if (wn > 0) { lr.madd(wn * mul, traceGlossy(new Ray(p, w), i)); } } } lr.mul(spec).mul((power + 2) / (2.0f * (float)Math.PI)); return(lr); }
public void getPhoton(double randX1, double randY1, double randX2, double randY2, Point3 p, Vector3 dir, Color power) { float phi = (float)(2 * Math.PI * randX1); float s = (float)Math.Sqrt(1.0f - randY1); dir.x = r * (float)Math.Cos(phi) * s; dir.y = r * (float)Math.Sin(phi) * s; dir.z = 0; basis.transform(dir); Point3.add(src, dir, p); dir.set(this.dir); power.set(radiance).mul((float)Math.PI * r2); }
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 getPhoton(double randX1, double randY1, double randX2, double randY2, Point3 p, Vector3 dir, Color power) { double rnd = randX1 * totalArea; int j = areas.Length - 1; for (int i = 0; i < areas.Length; i++) { if (rnd < areas[i]) { j = i; break; } rnd -= areas[i]; // try next triangle } rnd /= areas[j]; randX1 = rnd; double s = Math.Sqrt(1 - randX2); float u = (float)(randY2 * s); float v = (float)(1 - s); float w = 1 - u - v; int tri3 = j * 3; int index0 = 3 * triangles[tri3 + 0]; int index1 = 3 * triangles[tri3 + 1]; int index2 = 3 * triangles[tri3 + 2]; p.x = w * points[index0 + 0] + u * points[index1 + 0] + v * points[index2 + 0]; p.y = w * points[index0 + 1] + u * points[index1 + 1] + v * points[index2 + 1]; p.z = w * points[index0 + 2] + u * points[index1 + 2] + v * points[index2 + 2]; p.x += 0.001f * ngs[j].x; p.y += 0.001f * ngs[j].y; p.z += 0.001f * ngs[j].z; OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(ngs[j]); u = (float)(2 * Math.PI * randX1); s = Math.Sqrt(randY1); onb.transform(new Vector3((float)(Math.Cos(u) * s), (float)(Math.Sin(u) * s), (float)(Math.Sqrt(1 - randY1))), dir); Color.mul((float)Math.PI * areas[j], radiance, power); }
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 Color getIrradiance(ShadingState state, Color diffuseReflectance) { if (samples <= 0) { return(Color.BLACK); } // compute new sample Color irr = Color.black(); OrthoNormalBasis onb = state.getBasis(); Vector3 w = new Vector3(); int n = state.getDiffuseDepth() == 0 ? samples : 1; for (int i = 0; i < n; i++) { float xi = (float)state.getRandom(i, 0, n); float xj = (float)state.getRandom(i, 1, n); float phi = (float)(xi * 2 * Math.PI); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; onb.transform(w); ShadingState temp = state.traceFinalGather(new Ray(state.getPoint(), w), i); if (temp != null) { temp.getInstance().prepareShadingState(temp); if (temp.getShader() != null) { irr.add(temp.getShader().getRadiance(temp)); } } } irr.mul((float)Math.PI / n); return(irr); }
public void getPhoton(double randX1, double randY1, double randX2, double randY2, Point3 p, Vector3 dir, Color power) { double s = Math.Sqrt(1 - randX2); float u = (float)(randY2 * s); float v = (float)(1 - s); float w = 1 - u - v; int index0 = 3 * meshlight.triangles[tri3 + 0]; int index1 = 3 * meshlight.triangles[tri3 + 1]; int index2 = 3 * meshlight.triangles[tri3 + 2]; p.x = w * meshlight.points[index0 + 0] + u * meshlight.points[index1 + 0] + v * meshlight.points[index2 + 0]; p.y = w * meshlight.points[index0 + 1] + u * meshlight.points[index1 + 1] + v * meshlight.points[index2 + 1]; p.z = w * meshlight.points[index0 + 2] + u * meshlight.points[index1 + 2] + v * meshlight.points[index2 + 2]; p.x += 0.001f * ng.x; p.y += 0.001f * ng.y; p.z += 0.001f * ng.z; OrthoNormalBasis onb = OrthoNormalBasis.makeFromW(ng); u = (float)(2 * Math.PI * randX1); s = Math.Sqrt(randY1); onb.transform(new Vector3((float)(Math.Cos(u) * s), (float)(Math.Sin(u) * s), (float)(Math.Sqrt(1 - randY1))), dir); Color.mul((float)Math.PI * area, meshlight.radiance, 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); } }
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 Color GetRadiance(ShadingState state) { // make sure we are on the right side of the material state.faceforward(); OrthoNormalBasis onb = state.getBasis(); // direct lighting and caustics state.initLightSamples(); state.initCausticSamples(); Color lr = Color.black(); // compute specular contribution if (state.includeSpecular) { Vector3 inv = state.getRay().getDirection().negate(new Vector3()); foreach (LightSample sample in state) { float cosNL = sample.dot(state.getNormal()); float fr = brdf(inv, sample.getShadowRay().getDirection(), onb); lr.madd(cosNL * fr, sample.getSpecularRadiance()); } // indirect lighting - specular if (numRays > 0) { int n = state.getDepth() == 0 ? numRays : 1; for (int i = 0; i < n; i++) { // specular indirect lighting double r1 = state.getRandom(i, 0, n); double r2 = state.getRandom(i, 1, n); 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; onb.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; float no = onb.untransformZ(o); float ni = onb.untransformZ(inv); float w = ih * cosTheta * cosTheta * cosTheta * (float)Math.Sqrt(Math.Abs(no / ni)); Ray r = new Ray(state.getPoint(), o); lr.madd(w / n, state.traceGlossy(r, i)); } } lr.mul(rhoS); } // add diffuse contribution lr.add(state.diffuse(getDiffuse(state))); return(lr); }
public void getSamples(ShadingState state) { if (getNumSamples() <= 0) { return; } Vector3 wc = Point3.sub(center, state.getPoint(), new Vector3()); float l2 = wc.LengthSquared(); if (l2 <= r2) { return; // inside the sphere? } // top of the sphere as viewed from the current shading point float topX = wc.x + state.getNormal().x *radius; float topY = wc.y + state.getNormal().y *radius; float topZ = wc.z + state.getNormal().z *radius; if (state.getNormal().dot(topX, topY, topZ) <= 0) { return; // top of the sphere is below the horizon } float cosThetaMax = (float)Math.Sqrt(Math.Max(0, 1 - r2 / Vector3.dot(wc, wc))); OrthoNormalBasis basis = OrthoNormalBasis.makeFromW(wc); int samples = state.getDiffuseDepth() > 0 ? 1 : getNumSamples(); float scale = (float)(2 * Math.PI * (1 - cosThetaMax)); Color c = Color.mul(scale / samples, radiance); for (int i = 0; i < samples; i++) { // random offset on unit square double randX = state.getRandom(i, 0, samples); double randY = state.getRandom(i, 1, samples); // cone sampling double cosTheta = (1 - randX) * cosThetaMax + randX; double sinTheta = Math.Sqrt(1 - cosTheta * cosTheta); double phi = randY * 2 * Math.PI; Vector3 dir = new Vector3((float)(Math.Cos(phi) * sinTheta), (float)(Math.Sin(phi) * sinTheta), (float)cosTheta); basis.transform(dir); // check that the direction of the sample is the same as the // normal float cosNx = Vector3.dot(dir, state.getNormal()); if (cosNx <= 0) { continue; } float ocx = state.getPoint().x - center.x; float ocy = state.getPoint().y - center.y; float ocz = state.getPoint().z - center.z; float qa = Vector3.dot(dir, dir); float qb = 2 * ((dir.x * ocx) + (dir.y * ocy) + (dir.z * ocz)); float qc = ((ocx * ocx) + (ocy * ocy) + (ocz * ocz)) - r2; double[] t = Solvers.solveQuadric(qa, qb, qc); if (t == null) { continue; } LightSample dest = new LightSample(); // compute shadow ray to the sampled point dest.setShadowRay(new Ray(state.getPoint(), dir)); // FIXME: arbitrary bias, should handle as in other places dest.getShadowRay().setMax((float)t[0] - 1e-3f); // prepare sample dest.setRadiance(c, c); dest.traceShadow(state); state.addSample(dest); } }
public bool update(ParameterList pl, SunflowAPI api) { updateBasis(pl.getVector("center", null), pl.getVector("up", null)); numSamples = pl.getInt("samples", numSamples); string filename = pl.getstring("texture", null); if (filename != null) { texture = TextureCache.getTexture(api.resolveTextureFilename(filename), true); } // no texture provided if (texture == null) { return(false); } Bitmap b = texture.getBitmap(); if (b == null) { return(false); } // rebuild histograms if this is a new texture if (filename != null) { imageHistogram = new float[b.Width][]; for (int i = 0; i < imageHistogram.Length; i++) { imageHistogram[i] = new float[b.Height]; } colHistogram = new float[b.Width]; float du = 1.0f / b.Width; float dv = 1.0f / b.Height; for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { float u = (x + 0.5f) * du; float v = (y + 0.5f) * dv; Color c = texture.getPixel(u, v); // box filter the image // c.add(texture.getPixel(u + du, v)); // c.add(texture.getPixel(u + du, v+ dv)); // c.add(texture.getPixel(u, v + dv)); // c.mul(0.25f); imageHistogram[x][y] = c.getLuminance() * (float)Math.Sin(Math.PI * v); if (y > 0) { imageHistogram[x][y] += imageHistogram[x][y - 1]; } } colHistogram[x] = imageHistogram[x][b.Height - 1]; if (x > 0) { colHistogram[x] += colHistogram[x - 1]; } for (int y = 0; y < b.Height; y++) { imageHistogram[x][y] /= imageHistogram[x][b.Height - 1]; } } for (int x = 0; x < b.Width; x++) { colHistogram[x] /= colHistogram[b.Width - 1]; } jacobian = (float)(2 * Math.PI * Math.PI) / (b.Width * b.Height); } // take fixed samples if (pl.getbool("fixed", samples != null)) { // Bitmap loc = new Bitmap(filename); samples = new Vector3[numSamples]; colors = new Color[numSamples]; for (int i = 0; i < numSamples; i++) { double randX = (double)i / (double)numSamples; double randY = QMC.halton(0, i); 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 / (numSamples * px * py); samples[i] = getDirection(su, sv); basis.transform(samples[i]); colors[i] = texture.getPixel(su, sv).mul(invP); // loc.setPixel(x, y, Color.YELLOW.copy().mul(1e6f)); } // loc.save("samples.hdr"); } else { // turn off samples = null; colors = null; } return(true); }
public Color getIrradiance(ShadingState state, Color diffuseReflectance) { if (samples <= 0) { return(Color.BLACK); } if (state.getDiffuseDepth() > 0) { // do simple path tracing for additional bounces (single ray) float xi = (float)state.getRandom(0, 0, 1); float xj = (float)state.getRandom(0, 1, 1); float phi = (float)(xi * 2 * Math.PI); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); Vector3 w = new Vector3(); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; OrthoNormalBasis onb = state.getBasis(); onb.transform(w); Ray r = new Ray(state.getPoint(), w); ShadingState temp = state.traceFinalGather(r, 0); return(temp != null?getGlobalRadiance(temp).copy().mul((float)Math.PI) : Color.BLACK); } //rwl.readLock().lockwoot();//fixme Color irr; lock (lockObj) irr = getIrradiance(state.getPoint(), state.getNormal()); //rwl.readLock().unlock(); if (irr == null) { // compute new sample irr = Color.black(); OrthoNormalBasis onb = state.getBasis(); float invR = 0; float minR = float.PositiveInfinity; Vector3 w = new Vector3(); for (int i = 0; i < samples; i++) { float xi = (float)state.getRandom(i, 0, samples); float xj = (float)state.getRandom(i, 1, samples); float phi = (float)(xi * 2 * Math.PI); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; onb.transform(w); Ray r = new Ray(state.getPoint(), w); ShadingState temp = state.traceFinalGather(r, i); if (temp != null) { minR = Math.Min(r.getMax(), minR); invR += 1.0f / r.getMax(); temp.getInstance().prepareShadingState(temp); irr.add(getGlobalRadiance(temp)); } } irr.mul((float)Math.PI / samples); invR = samples / invR; //rwl.writeLock().lockwoot();//fixme lock (lockObj) insert(state.getPoint(), state.getNormal(), invR, irr); //rwl.writeLock().unlock(); // view irr-cache points // irr = Color.YELLOW.copy().mul(1e6f); } return(irr); }
public Vector3 getNormal(float x, float y, OrthoNormalBasis basis) { float[] rgb = getPixel(x, y).getRGB(); return(basis.transform(new Vector3(2 * rgb[0] - 1, 2 * rgb[1] - 1, 2 * rgb[2] - 1)).normalize()); }
public Color getIrradiance(ShadingState state, Color diffuseReflectance) { float b = (float)Math.PI * c / diffuseReflectance.getMax(); Color irr = Color.black(); Point3 p = state.getPoint(); Vector3 n = state.getNormal(); int set = (int)(state.getRandom(0, 1, 1) * numSets); foreach (PointLight vpl in virtualLights[set]) { Ray r = new Ray(p, vpl.p); float dotNlD = -(r.dx * vpl.n.x + r.dy * vpl.n.y + r.dz * vpl.n.z); float dotND = r.dx * n.x + r.dy * n.y + r.dz * n.z; if (dotNlD > 0 && dotND > 0) { float r2 = r.getMax() * r.getMax(); Color opacity = state.traceShadow(r); Color power = Color.blend(vpl.power, Color.BLACK, opacity); float g = (dotND * dotNlD) / r2; irr.madd(0.25f * Math.Min(g, b), power); } } // bias compensation int nb = (state.getDiffuseDepth() == 0 || numBias <= 0) ? numBias : 1; if (nb <= 0) { return(irr); } OrthoNormalBasis onb = state.getBasis(); Vector3 w = new Vector3(); float scale = (float)Math.PI / nb; for (int i = 0; i < nb; i++) { float xi = (float)state.getRandom(i, 0, nb); float xj = (float)state.getRandom(i, 1, nb); float phi = (float)(xi * 2 * Math.PI); float cosPhi = (float)Math.Cos(phi); float sinPhi = (float)Math.Sin(phi); float sinTheta = (float)Math.Sqrt(xj); float cosTheta = (float)Math.Sqrt(1.0f - xj); w.x = cosPhi * sinTheta; w.y = sinPhi * sinTheta; w.z = cosTheta; onb.transform(w); Ray r = new Ray(state.getPoint(), w); r.setMax((float)Math.Sqrt(cosTheta / b)); ShadingState temp = state.traceFinalGather(r, i); if (temp != null) { temp.getInstance().prepareShadingState(temp); if (temp.getShader() != null) { float dist = temp.getRay().getMax(); float r2 = dist * dist; float cosThetaY = -Vector3.dot(w, temp.getNormal()); if (cosThetaY > 0) { float g = (cosTheta * cosThetaY) / r2; // was this path accounted for yet? if (g > b) { irr.madd(scale * (g - b) / g, temp.getShader().GetRadiance(temp)); } } } } } return(irr); }