world.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * World implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <world.h>
  10. #include <light.h>
  11. #include <shape.h>
  12. #define MIN_ALLOC (2)
  13. World::World() : objectCount(0), lightCount(0)
  14. {
  15. this->allocatedLightCount = MIN_ALLOC;
  16. this->lightList = (Light **)calloc(sizeof(Light *), MIN_ALLOC);
  17. this->lightCount = 0;
  18. this->allocatedObjectCount = MIN_ALLOC;
  19. this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  20. this->objectCount = 0;
  21. };
  22. World::~World()
  23. {
  24. /* We need to do some cleanup... */
  25. }
  26. void World::addObject(Shape *s)
  27. {
  28. if ((this->objectCount + 1) > this->allocatedObjectCount)
  29. {
  30. this->allocatedObjectCount *= 2;
  31. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  32. }
  33. this->objectList[this->objectCount++] = s;
  34. }
  35. void World::addLight(Light *l)
  36. {
  37. if ((this->lightCount + 1) > this->allocatedLightCount)
  38. {
  39. this->allocatedLightCount *= 2;
  40. this->lightList = (Light **)realloc(this->lightList, sizeof(Light **) * this->allocatedLightCount);
  41. }
  42. this->lightList[this->lightCount++] = l;
  43. }
  44. bool World::lightIsIn(Light &l)
  45. {
  46. int i;
  47. for(i = 0; i < this->lightCount; i++)
  48. {
  49. if (*this->lightList[i] == l)
  50. {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. bool World::objectIsIn(Shape &s)
  57. {
  58. int i;
  59. for(i = 0; i < this->objectCount; i++)
  60. {
  61. if (*this->objectList[i] == s)
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. Intersect World::intersect(Ray r)
  69. {
  70. Intersect ret;
  71. int i, j;
  72. for(i = 0; i < this->objectCount; i++)
  73. {
  74. Intersect xs = this->objectList[i]->intersect(r);
  75. for(j = 0; xs.count(); j++)
  76. {
  77. ret.add(xs[i]);
  78. }
  79. }
  80. return ret;
  81. }