Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void intersect( Ray ray, IntersectResult result ) {
- // TODO: Objective 2: finish this class
- // at^2 + bt + c = 0
- double A = ray.viewDirection.dot(ray.viewDirection);
- Vector3d centerToEye = new Vector3d();
- center.scale(-1);
- centerToEye.add(center, ray.eyePoint);
- center.scale(-1);
- double B = 2 * ray.viewDirection.dot(centerToEye);
- double C = centerToEye.dot(centerToEye) - Math.pow(radius, 2);
- double discriminant = Math.sqrt( Math.pow(B, 2) - 4 * A * C);
- if (discriminant < 0) {
- return;
- }
- double t0 = (-B + discriminant) / (2 * A);
- double t1 = (-B - discriminant) / (2 * A);
- if (!Double.isNaN(t0) || !Double.isNaN(t1)) {
- double firstT = Math.min(t0, t1);
- if (!Double.isInfinite(result.t) && result.t < firstT) {
- return;
- }
- ray.getPoint(firstT, result.p);
- Vector3d normal = new Vector3d();
- normal.x = result.p.x - center.x;
- normal.y = result.p.y - center.y;
- normal.z = result.p.z - center.z;
- normal.normalize();
- result.n = normal;
- result.t = firstT;
- result.material = material;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement