intersection.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 the hit object is not transparent, there is no need to do that. I think .*/
  29. if ((xs != nullptr) && (xs->hit().object->material.transparency > 0))
  30. {
  31. List containers;
  32. int j, k;
  33. for (j = 0 ; j < xs->count() ; j++)
  34. {
  35. Intersection i = ( *xs )[j];
  36. if (*this == i)
  37. {
  38. if (!containers.isEmpty())
  39. {
  40. n1 = containers.last()->material.refractiveIndex;
  41. }
  42. }
  43. if (containers.doesInclude(i.object))
  44. {
  45. containers.remove(i.object);
  46. }
  47. else
  48. {
  49. containers.append(i.object);
  50. }
  51. if (*this == i)
  52. {
  53. if (!containers.isEmpty())
  54. {
  55. n2 = containers.last()->material.refractiveIndex;
  56. }
  57. /* End the loop */
  58. break;
  59. }
  60. }
  61. }
  62. Shape *s = this->object;
  63. /* For now don't get root group material */
  64. //while(s->parent != nullptr) { s = s->parent; }
  65. return Computation(this->object,
  66. this->t,
  67. hitP,
  68. eyeV,
  69. normalV,
  70. overHitP,
  71. inside,
  72. reflectV,
  73. n1,
  74. n2,
  75. underHitP,
  76. &s->material);
  77. }