public void intersectPrimitive(Ray r, int primID, IntersectionState state) { // intersect in local space float qa = r.dx * r.dx + r.dy * r.dy + r.dz * r.dz; float qb = 2 * ((r.dx * r.ox) + (r.dy * r.oy) + (r.dz * r.oz)); float qc = ((r.ox * r.ox) + (r.oy * r.oy) + (r.oz * r.oz)) - 1; double[] t = Solvers.solveQuadric(qa, qb, qc); if (t != null) { // early rejection if (t[0] >= r.getMax() || t[1] <= r.getMin()) { return; } if (t[0] > r.getMin()) { r.setMax((float)t[0]); } else { r.setMax((float)t[1]); } state.setIntersection(0, 0, 0); } }
public void intersectPrimitive(Ray r, int primID, IntersectionState state) { int i3 = primID * 3; float ocx = r.ox - particles[i3 + 0]; float ocy = r.oy - particles[i3 + 1]; float ocz = r.oz - particles[i3 + 2]; float qa = r.dx * r.dx + r.dy * r.dy + r.dz * r.dz; float qb = 2 * ((r.dx * ocx) + (r.dy * ocy) + (r.dz * ocz)); float qc = ((ocx * ocx) + (ocy * ocy) + (ocz * ocz)) - r2; double[] t = Solvers.solveQuadric(qa, qb, qc); if (t != null) { // early rejection if (t[0] >= r.getMax() || t[1] <= r.getMin()) { return; } if (t[0] > r.getMin()) { r.setMax((float)t[0]); } else { r.setMax((float)t[1]); } state.setIntersection(primID); } }
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 void intersectPrimitive(Ray r, int primID, IntersectionState state) { // intersect with bounding sphere float qc = ((r.ox * r.ox) + (r.oy * r.oy) + (r.oz * r.oz)) - BOUNDING_RADIUS2; float qt = r.getMin(); if (qc > 0) { // we are starting outside the sphere, find intersection on the // sphere float qa = r.dx * r.dx + r.dy * r.dy + r.dz * r.dz; float qb = 2 * ((r.dx * r.ox) + (r.dy * r.oy) + (r.dz * r.oz)); double[] t = Solvers.solveQuadric(qa, qb, qc); // early rejection if (t == null || t[0] >= r.getMax() || t[1] <= r.getMin()) { return; } qt = (float)t[0]; } float dist = float.PositiveInfinity; float rox = r.ox + qt * r.dx; float roy = r.oy + qt * r.dy; float roz = r.oz + qt * r.dz; float invRayLength = (float)(1 / Math.Sqrt(r.dx * r.dx + r.dy * r.dy + r.dz * r.dz)); // now we can start intersection while (true) { float zw = rox; float zx = roy; float zy = roz; float zz = 0; float zpw = 1; float zpx = 0; float zpy = 0; float zpz = 0; // run several iterations float dotz = 0; for (int i = 0; i < maxIterations; i++) { { // zp = 2 * (z * zp) float nw = zw * zpw - zx * zpx - zy * zpy - zz * zpz; float nx = zw * zpx + zx * zpw + zy * zpz - zz * zpy; float ny = zw * zpy + zy * zpw + zz * zpx - zx * zpz; zpz = 2 * (zw * zpz + zz * zpw + zx * zpy - zy * zpx); zpw = 2 * nw; zpx = 2 * nx; zpy = 2 * ny; } { // z = z*z + c float nw = zw * zw - zx * zx - zy * zy - zz * zz + cw; zx = 2 * zw * zx + cx; zy = 2 * zw * zy + cy; zz = 2 * zw * zz + cz; zw = nw; } dotz = zw * zw + zx * zx + zy * zy + zz * zz; if (dotz > ESCAPE_THRESHOLD) { break; } } float normZ = (float)Math.Sqrt(dotz); dist = 0.5f * normZ * (float)Math.Log(normZ) / Length(zpw, zpx, zpy, zpz); rox += dist * r.dx; roy += dist * r.dy; roz += dist * r.dz; qt += dist; if (dist * invRayLength < epsilon) { break; } if (rox * rox + roy * roy + roz * roz > BOUNDING_RADIUS2) { return; } } // now test t value again if (!r.isInside(qt)) { return; } if (dist * invRayLength < epsilon) { // valid hit r.setMax(qt); state.setIntersection(0); } }