intersection.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Intersection header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_INTERSECTION_H
  10. #define DORAYME_INTERSECTION_H
  11. #include <stdlib.h>
  12. #include <ray.h>
  13. class Shape;
  14. class Intersect;
  15. struct Computation
  16. {
  17. Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, Tuple overHitP,
  18. bool inside, Tuple reflectV = Vector(0, 0, 0), double n1 = 1.0, double n2 = 1.0,
  19. Tuple underHitP = Point(0, 0, 0)) :
  20. object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside),
  21. overHitPoint(overHitP), underHitPoint(underHitP), reflectVector(reflectV), n1(n1), n2(n2) { };
  22. double schlick()
  23. {
  24. /* Find the cos of the angle betzeen the eye and normal vector */
  25. double cos = this->eyeVector.dot(this->normalVector);
  26. double r0;
  27. /* Total internal reflection can only occur when n1 > n2 */
  28. if (this->n1 > this->n2)
  29. {
  30. double n, sin2_t;
  31. n = this->n1 / this->n2;
  32. sin2_t = (n * n) * (1.0 - (cos * cos));
  33. if (sin2_t > 1.0)
  34. {
  35. return 1.0;
  36. }
  37. /* Compute the cos of theta */
  38. cos = sqrt(1.0 - sin2_t);
  39. }
  40. r0 = ((this->n1 - this->n2) / (this->n1 + this->n2));
  41. r0 = r0 * r0;
  42. return r0 + (1 - r0) * ((1 - cos)*(1 - cos)*(1 - cos)*(1 - cos)*(1 - cos));
  43. };
  44. Shape *object;
  45. double t;
  46. Tuple hitPoint;
  47. Tuple overHitPoint;
  48. Tuple underHitPoint;
  49. Tuple eyeVector;
  50. Tuple normalVector;
  51. Tuple reflectVector;
  52. double n1;
  53. double n2;
  54. bool inside;
  55. };
  56. class Intersection
  57. {
  58. public:
  59. double t;
  60. Shape *object;
  61. public:
  62. Intersection(double t, Shape *object) : t(t), object(object) { };
  63. bool nothing() { return (this->object == nullptr); };
  64. Computation prepareComputation(Ray r, Intersect *xs = nullptr);
  65. bool operator==(const Intersection &b) const { return ((this->t == b.t) && (this->object == b.object)); };
  66. };
  67. #endif /* DORAYME_INTERSECTION_H */