public override bool hit(Ray r, float t_min, float t_max, ref hit_record rec) { Vector3 oc = r.origin() - center(r._time); float a = Vector3.dot(r.direction(), r.direction()); float b = 2.0f * Vector3.dot(oc, r.direction()); float c = Vector3.dot(oc, oc) - radius * radius; float discriminant = b * b - 4 * a * c; if (discriminant > 0) { // update the hit_record to pass the information back to the calling object float temp = (-b - (float)Math.Sqrt(b * b - 4 * a * c)) / (2 * a); // check the roots if (temp < t_max && temp > t_min) { rec.t = temp; rec.p = r.point(rec.t); rec.normal = (rec.p - center(r._time)) / radius; rec.mat = material; return(true); } temp = (-b + (float)Math.Sqrt(b * b - 4 * a * c)) / (2 * a); if (temp < t_max && temp > t_min) { rec.t = temp; rec.p = r.point(rec.t); rec.normal = (rec.p - center(r._time)) / radius; rec.mat = material; return(true); } } return(false); }
public bool hit(Ray r, float tmin, float tmax) { float t0 = Utils.fmin((_min.x() - r.origin().x()) / r.direction().x(), (_max.x() - r.origin().x()) / r.direction().x()); float t1 = Utils.fmax((_min.x() - r.origin().x()) / r.direction().x(), (_max.x() - r.origin().x()) / r.direction().x()); tmin = Utils.fmax(t0, tmin); tmax = Utils.fmin(t1, tmax); if (tmax <= tmin) { return(false); } t0 = Utils.fmin((_min.y() - r.origin().y()) / r.direction().y(), (_max.y() - r.origin().y()) / r.direction().y()); t1 = Utils.fmax((_min.y() - r.origin().y()) / r.direction().y(), (_max.y() - r.origin().y()) / r.direction().y()); tmin = Utils.fmax(t0, tmin); tmax = Utils.fmin(t1, tmax); if (tmax <= tmin) { return(false); } t0 = Utils.fmin((_min.z() - r.origin().z()) / r.direction().z(), (_max.z() - r.origin().z()) / r.direction().z()); t1 = Utils.fmax((_min.y() - r.origin().y()) / r.direction().y(), (_max.z() - r.origin().z()) / r.direction().z()); tmin = Utils.fmax(t0, tmin); tmax = Utils.fmin(t1, tmax); if (tmax <= tmin) { return(false); } return(true); }