Пример #1
0
  private static Intersection getIntersection(Ray ray, SphereObject obj, Model model) {
    RealMatrix transform = obj.getTransform();
    final RealMatrix transformInverse = obj.getTransformInverse();
    ray = ray.transform(transformInverse);
    Vector3D c = VectorUtils.toVector3D(obj.getCenter());
    Vector3D p0 = VectorUtils.toVector3D(ray.getP0());
    Vector3D p1 = VectorUtils.toVector3D(ray.getP1());
    float a = (float) p1.dotProduct(p1);
    Vector3D p0c = p0.subtract(c);
    float b = (float) p1.dotProduct(p0c) * 2.0f;

    float cc = (float) (p0c.dotProduct(p0c)) - obj.getSize() * obj.getSize();
    Double t = quadraticEquationRoot1(a, b, cc);
    if (t == null || t < EPSILON) {
      return new Intersection(false);
    }
    Intersection result = new Intersection(true);
    result.setObject(obj);
    final Vector3D p = p0.add(p1.scalarMultiply(t));
    RealVector pv = VectorUtils.toRealVector(p);
    pv.setEntry(3, 1.0);
    RealVector pt = transform.preMultiply(pv);
    result.setP(VectorUtils.toVector3D(pt));
    RealVector nv = pv.subtract(obj.getCenter());
    final RealVector nvt = transformInverse.transpose().preMultiply(nv);
    result.setN(VectorUtils.toVector3D(nvt).normalize());
    result.setDistance(t);
    return result;
  }
Пример #2
0
  private static Ray rayThruPixel(Camera cam, int xi, int yi) {
    Ray result = new Ray();
    float x = xi + .5f;
    float y = yi + .5f;
    final int halfW = cam.getHalfW();
    float alpha = (float) (cam.getTanHalfFovX()) * ((x - halfW) / halfW);
    final int halfH = cam.getHalfH();
    float beta = (float) (cam.getTanHalfFovY() * (halfH - y) / halfH);
    Vector3D p13 =
        cam.getU().scalarMultiply(alpha).add(cam.getV().scalarMultiply(beta)).subtract(cam.getW());

    result.setP0(cam.getFrom());
    result.setP1(VectorUtils.toRealVector(p13));

    return result;
  }