Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. public HitRecord hit(Ray ray, float tmin, floattmax)
  2. {
  3.     float A = ray.getDirection().lengthSquared();
  4.     Vector3f temp1 = new Vector3f(ray.getOrigin());
  5.     temp1.sub(center);
  6.     temp1.scale(2);
  7.    
  8.     float B = -1 * temp1.dot(ray.getDirection());
  9.     Vector3f temp2 = new Vector3f(ray.getOrigin());
  10.     temp2.sub(center);
  11.    
  12.     float C = temp2.lengthSquared() - (radius * radius);
  13.  
  14.     float t1 = (float)((B + Math.sqrt(((B * B) - (4.0f * A * C)))) / (2.0f * A));
  15.     float t2 = (float)((B - Math.sqrt(((B * B) - (4.0f * A * C)))) / (2.0f * A));
  16.     float t = -1;
  17.  
  18.     if(t1 > tmin) t = t1;
  19.    
  20.     if((t2 < t) && (t2 > tmin)) t = t2;
  21.    
  22.     if((t < tmin) || (t > tmax) || (t == Float.NaN)) return null;
  23.  
  24.     Vector3f normal = new Vector3f(ray.pointAt(t));
  25.     normal.sub(center);
  26.  
  27.     HitRecord hit = new HitRecord();
  28.     hit.pos = ray.pointAt(t);
  29.     hit.t = t;
  30.     hit.material = material;
  31.     hit.normal = normal;
  32.     hit.normal.normalize();
  33.     return hit;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement