public bool hit(ray r, float t_min, float t_max, hit_record rec) { vec3 oc = r.Origin - center; float a = vec3.dot(r.Direction, r.Direction); float b = vec3.dot(oc, r.Direction); float c = vec3.dot(oc, oc) - radius * radius; float discriminant = b * b - a * c; if (discriminant > 0) { float temp = (-b - (float)Math.Sqrt(discriminant)) / a; if (temp < t_max && temp > t_min) { rec.t = temp; rec.p = r.point_at_parameter(rec.t); rec.normal = (rec.p - center) / radius; return(true); } temp = (-b + (float)Math.Sqrt(discriminant)) / a; if (temp < t_max && temp > t_min) { rec.t = temp; rec.p = r.point_at_parameter(rec.t); rec.normal = (rec.p - center) / radius; return(true); } } return(false); }
public bool hit(ray r, float t_min, float t_max, hit_record rec) { hit_record temp_rec = new hit_record(); bool hit_anything = false; double closest_so_far = t_max; for (int i = 0; i < list_size; i++) { if (list[i].hit(r, t_min, (float)closest_so_far, temp_rec)) { hit_anything = true; closest_so_far = temp_rec.t; rec = temp_rec; } } return(hit_anything); }
static vec3 GetColor(ray r, hitable world) { hit_record rec = new hit_record(); //float value = -1; //bool hit = HitSphere(new vec3(0, 0, -1), 0.5f, r, out value); if (world.hit(r, 0.0f, float.MaxValue, rec)) { return(new vec3(1 + rec.normal.x, 1 + rec.normal.y, 1 + rec.normal.z) * 0.5f); //[-1,1]-->[0,2]-->[0,1] } else { vec3 Normal = (r.point_at_parameter(value) - new vec3(0, 0, -1)).Normalize(); //normal:[-1,1] return(new vec3(1 + Normal.x, 1 + Normal.y, 1 + Normal.z) * 0.5f); //[-1,1]-->[0,2]-->[0,1] } vec3 normalizedDir = r.Direction.Normalize(); float y = normalizedDir.y + 1; //[-1,1]-->[0,2] float t = 0.5f * y; //[0,2]-->[0,1] vec3 color_start = new vec3(1, 1, 1); //white vec3 color_end = new vec3(0.5f, 0.7f, 1); //light blue return(color_start * (1 - t) + color_end * t); //linear interpolate }