public static Vec3 random_in_unit_sphere() { Vec3 p = new Vec3(); do { //p = (float)2.0 * new Vec3(Rng.f(), Rng.f(), Rng.f()) - new Vec3(1.0f, 1.0f, 1.0f); //p = new Vec3(2.0f * Rng.f() - 1.0f, 2.0f * Rng.f() - 1.0f, 2.0f * Rng.f() - 1.0f); p[0] = 2.0f * Rng.f() - 1.0f; p[1] = 2.0f * Rng.f() - 1.0f; p[2] = 2.0f * Rng.f() - 1.0f; } while (p.squared_length() >= 1.0f); return(p); }
public static Vec3 random_in_unit_disk() { Vec3 p = new Vec3(); p[2] = 0.0f; do { //p = (float)2.0 * new Vec3(Rng.f(), Rng.f(), 0.0f) - new Vec3(1.0f, 1.0f, 0.0f); //p = new Vec3(2.0f * Rng.f() - 1.0f, 2.0f * Rng.f() - 1.0f, 0.0f); p[0] = 2.0f * Rng.f() - 1.0f; p[1] = 2.0f * Rng.f() - 1.0f; } while (dot(p) >= 1.0f); return(p); }
public static Hitable my_random_scene() { int max_items = 500; List <Hitable> list = new List <Hitable>(max_items + 1); list.Add(new Sphere(new Vec3(0.0f, -1000.0f, 0.0f), 1000.0f, new Lambertian(TextureLib.green_white_checker))); if (true) { for (int a = -11; a < 11; a++) { for (int b = -11; b < 11; b++) { float choose_mat = Rng.f(); Vec3 center = new Vec3((float)a + 0.8f * Rng.f(), 0.2f, (float)b + 0.8f * Rng.f()); if ((center - new Vec3(4.0f, 0.2f, 0.0f)).length() > 1.5f && (center - new Vec3(0.0f, 0.2f, 0.0f)).length() > 1.5f && (center - new Vec3(-4.0f, 0.2f, 0.0f)).length() > 1.5f) { if (choose_mat < 0.8f) { list.Add(new Sphere(center, 0.2f, new Lambertian(new ConstantTexture(new Vec3(Rng.f(), Rng.f(), Rng.f()))))); } else if (choose_mat < 0.95f) { list.Add(new Sphere(center, 0.2f, new Metal(new Vec3(0.5f * (1.0f + Rng.f()), 0.5f * (1.0f + Rng.f()), 0.5f * (1.0f + Rng.f())), 0.5f * Rng.f()))); } else { list.Add(new Sphere(center, 0.2f, new Dielectric(1.5f))); } } } } } list.Add(new Sphere(new Vec3(0.0f, 1.0f, 0.0f), 1.0f, new Dielectric(1.5f, new Vec3(1.0f, 1.0f, 1.0f)))); list.Add(new Sphere(new Vec3(-4.0f, 1.0f, 0.0f), 1.0f, new Lambertian(TextureLib.burnt_sienna))); list.Add(new Sphere(new Vec3(4.0f, 1.0f, 0.0f), 1.0f, new Metal(new Vec3(0.7f, 0.6f, 0.5f), 0.0f))); int i = list.Count(); return(new Hitable_List(list, i)); }
public override bool scatter(Ray r_in, Hit_Record rec, ref Vec3 attenuation, ref Ray scattered) { Vec3 outward_normal; Vec3 reflected = Vec3.reflect(r_in.direction(), rec.normal); float ni_over_nt; attenuation = color; Vec3 refracted = new Vec3(1.0f, 0.0f, 0.0f); float reflect_prob; float cosine; if (Vec3.dot(r_in.direction(), rec.normal) > 0.0f) { outward_normal = -rec.normal; ni_over_nt = ref_idx; cosine = ref_idx * Vec3.dot(r_in.direction(), rec.normal) / r_in.direction().length(); } else { outward_normal = rec.normal; ni_over_nt = 1.0f / ref_idx; cosine = -Vec3.dot(r_in.direction(), rec.normal) / r_in.direction().length(); } if (refract(r_in.direction(), outward_normal, ni_over_nt, ref refracted)) { reflect_prob = schlick(cosine, ref_idx); } else { scattered = new Ray(rec.p, reflected); reflect_prob = 1.0f; } if (Rng.f() < reflect_prob) { scattered = new Ray(rec.p, reflected); // reFLEcted } else { scattered = new Ray(rec.p, refracted); // reFRActed } return(true); }
public static void childTracer(object data) { int offset = (int)data; int nx = rc.getNumXPixels(); int ny = rc.getNumYPixels(); int ns = rc.getNumSamples(); int coreNum = offset + 1; Console.WriteLine("starting thread " + coreNum + " of " + numThreads); Vec3 col = new Vec3(0.0f, 0.0f, 0.0f); for (int j = ny - 1; j >= 0; j--) { for (int i = offset; i < nx; i += numThreads) { for (int s = 0; s < ns; s++) { float u = (float)((float)i + (ConfigParams.aliasing_on ? Rng.f() : 0.0f)) / (float)nx; float v = (float)((float)j + (ConfigParams.aliasing_on ? Rng.f() : 0.0f)) / (float)ny; col = color(Camera.get_ray(u, v), 0); rc.addR(i, j, col.r()); rc.addG(i, j, col.g()); rc.addB(i, j, col.b()); } } progressList[offset] += 1; } Interlocked.Increment(ref threadCounter); // signal this thread has finished return; } // end childTracer