intersection.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Intersection implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <intersection.h>
  10. #include <shape.h>
  11. #include <list.h>
  12. Computation Intersection::prepareComputation(Ray r, Intersect *xs)
  13. {
  14. double n1 = 1.0;
  15. double n2 = 1.0;
  16. Tuple hitP = r.position(this->t);
  17. Tuple normalV = this->object->normalAt(hitP);
  18. Tuple eyeV = -r.direction;
  19. bool inside = false;
  20. if (normalV.dot(eyeV) < 0)
  21. {
  22. inside = true;
  23. normalV = -normalV;
  24. }
  25. Tuple overHitP = hitP + normalV * getEpsilon();
  26. Tuple underHitP = hitP - normalV * getEpsilon();
  27. Tuple reflectV = r.direction.reflect(normalV);
  28. if (xs != nullptr)
  29. {
  30. List containers;
  31. int j, k;
  32. for(j = 0; j < xs->count(); j++)
  33. {
  34. Intersection i = (*xs)[j];
  35. if (*this == i)
  36. {
  37. if (!containers.isEmpty())
  38. {
  39. n1 = containers.last()->material.refractiveIndex;
  40. }
  41. }
  42. if (containers.doesInclude(i.object))
  43. {
  44. containers.remove(i.object);
  45. }
  46. else
  47. {
  48. containers.append(i.object);
  49. }
  50. if (*this == i)
  51. {
  52. if (!containers.isEmpty())
  53. {
  54. n2 = containers.last()->material.refractiveIndex;
  55. }
  56. /* End the loop */
  57. break;
  58. }
  59. }
  60. }
  61. return Computation(this->object,
  62. this->t,
  63. hitP,
  64. eyeV,
  65. normalV,
  66. overHitP,
  67. inside,
  68. reflectV,
  69. n1,
  70. n2,
  71. underHitP);
  72. }