Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public HitRecord hit(Ray ray, float tmin, floattmax)
- {
- float A = ray.getDirection().lengthSquared();
- Vector3f temp1 = new Vector3f(ray.getOrigin());
- temp1.sub(center);
- temp1.scale(2);
- float B = -1 * temp1.dot(ray.getDirection());
- Vector3f temp2 = new Vector3f(ray.getOrigin());
- temp2.sub(center);
- float C = temp2.lengthSquared() - (radius * radius);
- float t1 = (float)((B + Math.sqrt(((B * B) - (4.0f * A * C)))) / (2.0f * A));
- float t2 = (float)((B - Math.sqrt(((B * B) - (4.0f * A * C)))) / (2.0f * A));
- float t = -1;
- if(t1 > tmin) t = t1;
- if((t2 < t) && (t2 > tmin)) t = t2;
- if((t < tmin) || (t > tmax) || (t == Float.NaN)) return null;
- Vector3f normal = new Vector3f(ray.pointAt(t));
- normal.sub(center);
- HitRecord hit = new HitRecord();
- hit.pos = ray.pointAt(t);
- hit.t = t;
- hit.material = material;
- hit.normal = normal;
- hit.normal.normalize();
- return hit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement