intersection.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. Shape *s = this->object;
  62. /* For now don't get root group material */
  63. //while(s->parent != nullptr) { s = s->parent; }
  64. return Computation(this->object,
  65. this->t,
  66. hitP,
  67. eyeV,
  68. normalV,
  69. overHitP,
  70. inside,
  71. reflectV,
  72. n1,
  73. n2,
  74. underHitP,
  75. &s->material);
  76. }