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 reflectV = r.direction.reflect(normalV);
  27. if (xs != nullptr)
  28. {
  29. List containers;
  30. int j, k;
  31. Intersection hit = xs->hit();
  32. for(j = 0; j < xs->count(); j++)
  33. {
  34. Intersection i = (*xs)[j];
  35. if (hit == 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 (hit == i)
  51. {
  52. if (!containers.isEmpty())
  53. {
  54. Shape *cur = containers.last();
  55. n2 = cur->material.refractiveIndex;
  56. }
  57. /* End the loop */
  58. break;
  59. }
  60. }
  61. }
  62. return Computation(this->object,
  63. this->t,
  64. hitP,
  65. eyeV,
  66. normalV,
  67. overHitP,
  68. inside,
  69. reflectV,
  70. n1,
  71. n2);
  72. }