public bool Hit(Ray r, float t0, float t1, ref Hit_Record rec) { var t = (k - r.Origin.Y) / r.Direction.Y; if (t < t0 || t > t1) { return(false); } var x = r.Origin.X + t * r.Direction.X; var z = r.Origin.Z + t * r.Direction.Z; if (x < x0 || x > x1 || z < z0 || z > z1) { return(false); } rec.U = (x - x0) / (x1 - x0); rec.V = (z - z0) / (z1 - z0); rec.T = t; var outward_normal = new Vector3(0, 1, 0); rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = mp; rec.P = r.At(t); return(true); }
public bool Hit(Ray r, float t0, float t1, ref Hit_Record rec) { var t = (k - r.Origin.X) / r.Direction.X; if (t < t0 || t > t1) { return(false); } var y = r.Origin.Y + t * r.Direction.Y; var z = r.Origin.Z + t * r.Direction.Z; if (y < y0 || y > y1 || z < z0 || z > z1) { return(false); } rec.U = (y - y0) / (y1 - y0); rec.V = (z - z0) / (z1 - z0); rec.T = t; var outward_normal = new Vector3(1, 0, 0); rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = mp; rec.P = r.At(t); return(true); }
public bool Hit(Ray r, float t_min, float t_max, ref Hit_Record rec) { Vector3 oc = r.Origin - this.Center(r.Time); var a = r.Direction.LengthSquared(); var half_b = Vector3.Dot(oc, r.Direction); var c = oc.LengthSquared() - radius * radius; var discriminant = half_b * half_b - a * c; if (discriminant > 0) { var root = (float)Math.Sqrt(discriminant); var temp = (-half_b - root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); var outward_normal = (rec.P - this.Center(r.Time)) / radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = mat_ptr; return(true); } temp = (-half_b + root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); var outward_normal = (rec.P - this.Center(r.Time)) / radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = mat_ptr; return(true); } } return(false); }
public bool Hit(Ray r, float t_min, float t_max, ref Hit_Record rec) { Vector3 oc = r.Origin - this.Center; float a = r.Direction.LengthSquared(); float half_b = Vector3.Dot(oc, r.Direction); float c = oc.LengthSquared() - this.Radius * this.Radius; float discriminant = half_b * half_b - a * c; if (discriminant > 0) { float root = (float)Math.Sqrt(discriminant); float temp = (-half_b - root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); Vector3 outward_normal = (rec.P - this.Center) / this.Radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = Material; this.Get_sphere_uv((rec.P - this.Center) / this.Radius, out rec.U, out rec.V); return(true); } temp = (-half_b + root) / a; if (temp < t_max && temp > t_min) { rec.T = temp; rec.P = r.At(rec.T); Vector3 outward_normal = (rec.P - this.Center) / this.Radius; rec.Set_Face_Normal(r, outward_normal); rec.Mat_ptr = Material; this.Get_sphere_uv((rec.P - this.Center) / this.Radius, out rec.U, out rec.V); return(true); } } return(false); }
public bool Hit(Ray r, float t_min, float t_max, ref Hit_Record rec) { // Print occasional samples when debugging. To enable, set enableDebug true. const bool enableDebug = false; bool debugging = enableDebug && ((float)Helpers.random.NextDouble() < 0.00001f); Hit_Record rec1, rec2; rec1 = rec2 = default; if (!boundary.Hit(r, -Helpers.Infinity, Helpers.Infinity, ref rec1)) { return(false); } if (!boundary.Hit(r, rec1.T + 0.0001f, Helpers.Infinity, ref rec2)) { return(false); } if (debugging) { Console.WriteLine($"t0={rec1.T}, t1={rec2.T}"); } if (rec1.T < t_min) { rec1.T = t_min; } if (rec2.T > t_max) { rec2.T = t_max; } if (rec1.T >= rec2.T) { return(false); } if (rec1.T < 0) { rec1.T = 0; } float ray_length = r.Direction.Length(); float distance_inside_boundary = (rec2.T - rec1.T) * ray_length; float hit_distance = neg_inv_density * (float)Math.Log(Helpers.random.NextDouble()); if (hit_distance > distance_inside_boundary) { return(false); } rec.T = rec1.T + hit_distance / ray_length; rec.P = r.At(rec.T); if (debugging) { Console.WriteLine($"hit_distance = {hit_distance} \n " + $"rec.t = {rec.T} \n" + $"rec.p = {rec.P}"); } rec.Normal = new Vector3(1, 0, 0); // arbitrary rec.Front_face = true; // also arbitrary rec.Mat_ptr = phase_function; return(true); }