public void Render(Scene scene) { double nx = buffer.width; double ny = buffer.height; var hdw = ny / nx; var lowerLeft = new Vector3(-fov / 2f, -fov / 2f * hdw, 1f); var horizon = Vector3.right * fov; var vertical = Vector3.up * fov * hdw; var origin = Vector3.zero; //todo: tranform calculation here. for (int k = 0; k < spp; k++) { for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { var u = (i + CoreRandom.Sample()) / nx; var v = (j + CoreRandom.Sample()) / ny; Ray r = new Ray(origin, lowerLeft + u * horizon + v * vertical - origin, depth); scene.ColorRay(r); buffer.pixels[i, j] = (buffer.pixels[i, j] * k + r.color) / (k + 1); } } } }
public override bool Scatter(Vector3 normal, Ray ray) { Vector3 outwardNor; double cos, niByNt, reflectProb; var rDn = Vector3.Dot(ray.direction, normal); if (rDn > 0) { outwardNor = normal * (-1f); niByNt = reflectiveIdx; cos = reflectiveIdx * rDn; } else { outwardNor = normal; niByNt = 1 / reflectiveIdx; cos = -rDn; } Vector3 refractedDir; if (Vector3.Refract(ray.direction, outwardNor, niByNt, out refractedDir)) { reflectProb = Schlik(cos); ray.origin = ray.GetHitPiont(); ray.direction = CoreRandom.Sample() < reflectProb?Vector3.Reflect(ray.direction, normal) : refractedDir; } else { ray.origin = ray.GetHitPiont(); ray.direction = Vector3.Reflect(ray.direction, normal); } return(true); }