intersect.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Intersect implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <math_helper.h>
  11. #include <intersect.h>
  12. #include <float.h>
  13. #define MIN_ALLOC (2)
  14. Intersect::Intersect()
  15. {
  16. this->allocated = MIN_ALLOC;
  17. this->list = (Intersection *)calloc(sizeof(Intersection *), MIN_ALLOC);
  18. this->num = 0;
  19. }
  20. void Intersect::add(Intersection i)
  21. {
  22. if ((this->num + 1) > this->allocated)
  23. {
  24. this->allocated *= 2;
  25. this->list = (Intersection *)realloc(this->list, sizeof(Intersection *) * this->allocated);
  26. }
  27. this->list[this->num++] = i;
  28. }
  29. Intersection Intersect::hit()
  30. {
  31. int i;
  32. double minHit = DBL_MAX;
  33. uint32_t curHit = -1;
  34. for(i = 0; i < this->num; i++)
  35. {
  36. if ((this->list[i].t >= 0) && (this->list[i].t < minHit))
  37. {
  38. curHit = i;
  39. minHit = this->list[i].t;
  40. }
  41. }
  42. if (curHit == -1)
  43. {
  44. return Intersection(0, nullptr);
  45. }
  46. return this->list[curHit];
  47. }