intersection.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <material.h>
  14. #include <renderstat.h>
  15. class Shape;
  16. class Intersect;
  17. struct Computation
  18. {
  19. Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, Tuple overHitP,
  20. bool inside, Tuple reflectV = Vector(0, 0, 0), double n1 = 1.0, double n2 = 1.0,
  21. Tuple underHitP = Point(0, 0, 0), Material *objMat = nullptr) :
  22. object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside),
  23. overHitPoint(overHitP), underHitPoint(underHitP), reflectVector(reflectV), n1(n1), n2(n2), material(objMat) { };
  24. double schlick()
  25. {
  26. /* Find the cos of the angle betzeen the eye and normal vector */
  27. double cos = this->eyeVector.dot(this->normalVector);
  28. double r0;
  29. /* Total internal reflection can only occur when n1 > n2 */
  30. if (this->n1 > this->n2)
  31. {
  32. double n, sin2_t;
  33. n = this->n1 / this->n2;
  34. sin2_t = (n * n) * (1.0 - (cos * cos));
  35. if (sin2_t > 1.0)
  36. {
  37. return 1.0;
  38. }
  39. /* Compute the cos of theta */
  40. cos = sqrt(1.0 - sin2_t);
  41. }
  42. r0 = ((this->n1 - this->n2) / (this->n1 + this->n2));
  43. r0 = r0 * r0;
  44. return r0 + (1 - r0) * ((1 - cos)*(1 - cos)*(1 - cos)*(1 - cos)*(1 - cos));
  45. };
  46. Shape *object;
  47. double t;
  48. Tuple hitPoint;
  49. Tuple overHitPoint;
  50. Tuple underHitPoint;
  51. Tuple eyeVector;
  52. Tuple normalVector;
  53. Tuple reflectVector;
  54. Material *material;
  55. double n1;
  56. double n2;
  57. bool inside;
  58. };
  59. class Intersection
  60. {
  61. public:
  62. double t;
  63. Shape *object;
  64. double u, v;
  65. public:
  66. Intersection(double t, Shape *object, double u = NAN, double v = NAN) : t(t), object(object), u(u), v(v) { stats.addIntersection(); };
  67. bool nothing() { return (this->object == nullptr); };
  68. Computation prepareComputation(Ray r, Intersect *xs = nullptr);
  69. bool operator==(const Intersection &b) const { return ((this->t == b.t) && (this->object == b.object)); };
  70. };
  71. #endif /* DORAYME_INTERSECTION_H */