intersection.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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;
  18. if (xs != nullptr)
  19. {
  20. Intersection hit = xs->hit();
  21. normalV = this->object->normalAt(hitP, &hit);
  22. }
  23. else
  24. {
  25. normalV = this->object->normalAt(hitP, nullptr);
  26. }
  27. Tuple eyeV = -r.direction;
  28. bool inside = false;
  29. if (normalV.dot(eyeV) < 0)
  30. {
  31. inside = true;
  32. normalV = -normalV;
  33. }
  34. Tuple overHitP = hitP + normalV * getEpsilon();
  35. Tuple underHitP = hitP - normalV * getEpsilon();
  36. Tuple reflectV = r.direction.reflect(normalV);
  37. /* If the hit object is not transparent, there is no need to do that. I think .*/
  38. if ((xs != nullptr) && (xs->hit().object->material.transparency > 0))
  39. {
  40. List containers;
  41. int j, k;
  42. for (j = 0 ; j < xs->count() ; j++)
  43. {
  44. Intersection i = ( *xs )[j];
  45. if (*this == i)
  46. {
  47. if (!containers.isEmpty())
  48. {
  49. n1 = containers.last()->material.refractiveIndex;
  50. }
  51. }
  52. if (containers.doesInclude(i.object))
  53. {
  54. containers.remove(i.object);
  55. }
  56. else
  57. {
  58. containers.append(i.object);
  59. }
  60. if (*this == i)
  61. {
  62. if (!containers.isEmpty())
  63. {
  64. n2 = containers.last()->material.refractiveIndex;
  65. }
  66. /* End the loop */
  67. break;
  68. }
  69. }
  70. }
  71. Shape *s = this->object;
  72. /* For now don't get root group material */
  73. while((!s->materialSet) && (s->parent != nullptr)) { s = s->parent; }
  74. return Computation(this->object,
  75. this->t,
  76. hitP,
  77. eyeV,
  78. normalV,
  79. overHitP,
  80. inside,
  81. reflectV,
  82. n1,
  83. n2,
  84. underHitP,
  85. &s->material);
  86. }