public override bool Scatter(Ray r, HitRecord rec, out Vec3f attenuation, out Ray Scattered, Random drand) { Vec3f reflected = Vec3f.Reflect(r.Direction.GetNormal(), rec.Normal); Scattered = new Ray(rec.Point, reflected + Fuzz * RayTrace1.RandomInUnitSphere(drand)); attenuation = Albedo; return(Vec3f.Dot(Scattered.Direction, rec.Normal) > 0); }
public Vec3f Random_in_unit_disk(Random drand) { Vec3f p; do { p = 2.0f * new Vec3f((float)drand.NextDouble(), (float)drand.NextDouble(), 0) - new Vec3f(1f, 1f, 0); } while (Vec3f.Dot(p, p) >= 1f); return(p); }
public override bool Scatter(Ray r, HitRecord rec, out Vec3f attenuation, out Ray Scattered, Random drand) { Vec3f outward_normal; Vec3f reflected = Vec3f.Reflect(r.Direction, rec.Normal); float ni_over_nt; // try 1,1,0 to see color shift, then change to 1,1,1 and see differance. attenuation = new Vec3f(1.0f, 1.0f, 1.0f); Vec3f refracted; float reflect_prob; float cosine; if (Vec3f.Dot(r.Direction, rec.Normal) > 0) { outward_normal = -rec.Normal; ni_over_nt = RefIndex; cosine = RefIndex * Vec3f.Dot(r.Direction, rec.Normal) / r.Direction.GetLength(); } else { outward_normal = rec.Normal; ni_over_nt = 1.0f / RefIndex; cosine = -Vec3f.Dot(r.Direction, rec.Normal) / r.Direction.GetLength(); } if (Vec3f.Refract(r.Direction, outward_normal, ni_over_nt, out refracted)) { reflect_prob = Schlick(cosine, RefIndex); //Scattered = new Ray(rec.Point, refracted); } else { Scattered = new Ray(rec.Point, reflected); // comment out this line? reflect_prob = 1.0f; //return false; } if (drand.NextDouble() < reflect_prob) { Scattered = new Ray(rec.Point, reflected); } else { Scattered = new Ray(rec.Point, refracted); } return(true); }
public override bool Hit(Ray r, float tMin, float tMax, ref HitRecord record) { Vec3f oc = r.Origin - Center; float a = Vec3f.Dot(r.Direction, r.Direction); float b = Vec3f.Dot(oc, r.Direction); float c = Vec3f.Dot(oc, oc) - Radius * Radius; float discriminant = b * b - a * c; if (discriminant > 0) { float tempSqrt = MathF.Sqrt(b * b - a * c); float temp = (-b - tempSqrt) / a; if (temp < tMax && temp > tMin) { record.t = temp; record.Point = r.PointAtParameter(record.t); record.Normal = (record.Point - Center) / Radius; record.Material = this.Material; return(true); } temp = (-b + tempSqrt) / a; if (temp < tMax && temp > tMin) { record.t = temp; record.Point = r.PointAtParameter(record.t); record.Normal = (record.Point - Center) / Radius; record.Material = this.Material; return(true); } } return(false); }