Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. // Triangle class
  2. // defines a Triangle shape
  3.  
  4. import javax.vecmath.*;
  5.  
  6. public class Triangle extends Shape {
  7.     private Vector3f p0, p1, p2;    // three vertices make a triangle
  8.     private Vector3f n0, n1, n2;    // normal at each vertex
  9.  
  10.     public Triangle() {
  11.     }
  12.     public Triangle(Vector3f _p0, Vector3f _p1, Vector3f _p2, Material mat) {
  13.         p0 = new Vector3f(_p0);
  14.         p1 = new Vector3f(_p1);
  15.         p2 = new Vector3f(_p2);
  16.         material = mat;
  17.         Vector3f normal = new Vector3f();
  18.         Vector3f v1 = new Vector3f();
  19.         Vector3f v2 = new Vector3f();
  20.         v1.sub(p1, p0);
  21.         v2.sub(p2, p0);
  22.         normal.cross(v1, v2);
  23.         normal.normalize();             // compute default normal:
  24.         n0 = new Vector3f(normal);      // the normal of the plane defined by the triangle
  25.         n1 = new Vector3f(normal);
  26.         n2 = new Vector3f(normal);
  27.     }
  28.     public Triangle(Vector3f _p0, Vector3f _p1, Vector3f _p2,
  29.                     Vector3f _n0, Vector3f _n1, Vector3f _n2,
  30.                     Material mat) {
  31.         p0 = new Vector3f(_p0);
  32.         p1 = new Vector3f(_p1);
  33.         p2 = new Vector3f(_p2);
  34.         material = mat;
  35.         n0 = new Vector3f(_n0);     // the normal of the plane defined by the triangle
  36.         n1 = new Vector3f(_n1);
  37.         n2 = new Vector3f(_n2);
  38.     }
  39.     public HitRecord hit(Ray ray, float tmin, float tmax) {
  40.  
  41.         /* YOUR WORK HERE: complete the triangle's intersection routine
  42.          * Normal should be computed by a bilinear interpolation from n0, n1 and n2
  43.          * using the barycentric coordinates: alpha, beta, (1.0 - alpha - beta) */
  44.         Vector3f o = ray.getOrigin();
  45.         Vector3f d = ray.getDirection();
  46.  
  47.         Matrix3f matrix = new Matrix3f(-d.x, p1.x - p0.x, p2.x - p0.x, -d.y, p1.y - p0.y, p2.y - p0.y, -d.z, p1.z - p0.z, p2.z - p0.z);
  48.         Vector3f finalcol = new Vector3f(o.x - p0.x, o.y - p0.y, o.z - p0.z);
  49.         Matrix3f matrixT = new Matrix3f(matrix);
  50.         matrixT.setColumn(0, finalcol);
  51.         Matrix3f matrixB = new Matrix3f(matrix);
  52.         matrixB.setColumn(1, finalcol);
  53.         Matrix3f matrixG = new Matrix3f(matrix);
  54.         matrixG.setColumn(2, finalcol);
  55.  
  56.         float det = matrix.determinant();
  57.         if (det == 0) {
  58.             return null;
  59.         }
  60.         float t = matrixT.determinant() / det;
  61.         float beta = matrixB.determinant() / det;
  62.         float gamma = matrixG.determinant() / det;
  63.         float alpha = 1.0f - beta - gamma;
  64.         if (t < tmin || t > tmax) {
  65.             return null;
  66.         }
  67.         if (alpha < 0 || alpha > 1 || beta < 0 || beta > 1 || gamma < 0 || gamma > 1) {
  68.             return null;
  69.         }
  70.  
  71.         Vector3f p = ray.pointAt(t);
  72.         HitRecord rec = new HitRecord();
  73.         rec.pos = p;
  74.         rec.t = t;
  75.         rec.material = material;
  76.  
  77.         Vector3f scaledAlpha = new Vector3f(n0);
  78.         scaledAlpha.scale(alpha);
  79.         Vector3f scaledBeta = new Vector3f(n1);
  80.         scaledBeta.scale(beta);
  81.         Vector3f scaledGamma = new Vector3f(n2);
  82.         scaledGamma.scale(gamma);
  83.  
  84.         rec.normal = new Vector3f();
  85.         rec.normal.add(scaledAlpha);
  86.         rec.normal.add(scaledBeta);
  87.         rec.normal.add(scaledGamma);
  88.         rec.normal.normalize();
  89.  
  90.         return rec;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement