Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. @Override
  2.     public void intersect( Ray ray, IntersectResult result ) {
  3.  
  4.         // TODO: Objective 2: finish this class
  5.        
  6.         // at^2 + bt + c = 0
  7.        
  8.         double A = ray.viewDirection.dot(ray.viewDirection);
  9.        
  10.         Vector3d centerToEye = new Vector3d();
  11.         center.scale(-1);
  12.         centerToEye.add(center, ray.eyePoint);
  13.         center.scale(-1);
  14.        
  15.         double B = 2 * ray.viewDirection.dot(centerToEye);
  16.        
  17.         double C = centerToEye.dot(centerToEye) - Math.pow(radius, 2);
  18.        
  19.         double discriminant = Math.sqrt( Math.pow(B, 2) - 4 * A * C);
  20.        
  21.         if (discriminant < 0) {
  22.             return;
  23.         }
  24.        
  25.         double t0 = (-B + discriminant) / (2 * A);
  26.         double t1 = (-B - discriminant) / (2 * A);
  27.         if (!Double.isNaN(t0) || !Double.isNaN(t1)) {
  28.            
  29.             double firstT = Math.min(t0, t1);
  30.            
  31.             if (!Double.isInfinite(result.t) && result.t < firstT) {
  32.                 return;
  33.             }
  34.            
  35.             ray.getPoint(firstT, result.p);
  36.            
  37.             Vector3d normal = new Vector3d();
  38.             normal.x = result.p.x - center.x;
  39.             normal.y = result.p.y - center.y;
  40.             normal.z = result.p.z - center.z;
  41.             normal.normalize();
  42.             result.n = normal;
  43.             result.t = firstT;
  44.             result.material = material;
  45.            
  46.         }
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement