public Vector3D LeALight(Vector3D pointIlluminated, Vector3D normalIlluminated, out Vector3D wi) { var random = randomFactory.makeRandom(); var indexLight = (int)(lights.Count * random.NextDouble()); var light = lights[indexLight]; var sampleResult = triangleSampler.sample(light); var normalLight = light.getNormal(sampleResult.u, sampleResult.v); var vecR = pointIlluminated - sampleResult.p; var r = vecR.Length; var wo = vecR / r; wi = -wo; double nearest; Triangle hitTarget; Collision collision; var ray = new Ray(pointIlluminated, wi); if (!primitives.findNearest(ray, r, out nearest, out hitTarget, out collision) || !light.Equals(hitTarget)) { return(Constant.BLACK); } var le = light.matter.getLe(new Collision(sampleResult.p, wo, normalLight)); var coswo = Vector3D.DotProduct(normalLight, wo); if (coswo <= Constant.EPSILON) { return(Constant.BLACK); } return(lights.Count * le * coswo * light.area / (r * r)); }
private Vector3D shadeIndirectTerm(ref Collision collision) { var random = randomFactory.makeRandom(); var threthold = rho(); if (random.NextDouble() >= threthold) { return(Constant.BLACK); } return(shadeDividedRho(collision)); }
public SamplePoint sample(Triangle triangle) { var random = randomFactory.makeRandom(); var s = random.NextDouble(); var t = random.NextDouble(); var sqrtT = Math.Sqrt(t); var u = sqrtT * (1 - s); var v = s * sqrtT; var p = (1 - sqrtT) * triangle.a.position + u * triangle.b.position + v * triangle.c.position; return(new SamplePoint(p, u, v)); }
public Vector3D sample(IRandomFactory randomFactory) { double rand1, rand2; var randomer = randomFactory.makeRandom(); rand1 = randomer.NextDouble(); rand2 = randomer.NextDouble(); double cosPhi = Math.Cos(2.0 * Math.PI * rand1); double sinPhi = Math.Sin(2.0 * Math.PI * rand1); double cosTheta2 = 1.0 - rand2; double cosTheta = Math.Sqrt(cosTheta2); double sinTheta = Math.Sqrt(1.0 - cosTheta2); var u = sinTheta * cosPhi; var v = sinTheta * sinPhi; var w = cosTheta; return(new Vector3D(u, v, w)); }