world.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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; j < xs.count(); j++)
  76. {
  77. ret.add(xs[j]);
  78. }
  79. }
  80. return ret;
  81. }
  82. Tuple World::shadeHit(Computation comps, uint32_t depthCount)
  83. {
  84. uint32_t lightIndex;
  85. Tuple surface = Colour(0, 0, 0);
  86. for(lightIndex = 0; lightIndex < this->lightCount; lightIndex++)
  87. {
  88. bool isThereAnObstacle = this->isShadowed(comps.overHitPoint, lightIndex);
  89. surface = surface + comps.object->material.lighting(*this->lightList[lightIndex], comps.overHitPoint, comps.eyeVector,
  90. comps.normalVector, comps.object, isThereAnObstacle);
  91. }
  92. Tuple reflected = this->reflectColour(comps, depthCount);
  93. Tuple refracted = this->refractedColour(comps, depthCount);
  94. if ((comps.object->material.reflective > 0) && (comps.object->material.transparency > 0))
  95. {
  96. double reflectance = comps.schlick();
  97. return surface + reflected * reflectance + refracted * (1 - reflectance);
  98. }
  99. return surface + reflected + refracted;
  100. }
  101. Tuple World::colourAt(Ray r, uint32_t depthCount)
  102. {
  103. Intersect allHits = this->intersect(r);
  104. Intersection hit = allHits.hit();
  105. if (hit.nothing())
  106. {
  107. return Colour(0, 0, 0);
  108. }
  109. else
  110. {
  111. return this->shadeHit(hit.prepareComputation(r, &allHits), depthCount);
  112. }
  113. }
  114. bool World::isShadowed(Tuple point, uint32_t light)
  115. {
  116. Tuple v = this->lightList[light]->position - point;
  117. double distance = v.magnitude();
  118. Tuple direction = v.normalise();
  119. Ray r = Ray(point, direction);
  120. Intersect xs = this->intersect(r);
  121. int i;
  122. for(i = 0; i < xs.count(); i++)
  123. {
  124. Intersection h = xs[i];
  125. if (h.t < 0) continue;
  126. if ((h.object->dropShadow == true) && (h.t < distance))
  127. {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. Colour World::reflectColour(Computation comps, uint32_t depthCount)
  134. {
  135. if ((depthCount == 0) || (comps.object->material.reflective == 0))
  136. {
  137. return Colour(0, 0, 0);
  138. }
  139. /* So it is reflective, even just a bit. Let'sr reflect the ray! */
  140. Ray reflectedRay = Ray(comps.overHitPoint, comps.reflectVector);
  141. Tuple hitColour = this->colourAt(reflectedRay, depthCount - 1);
  142. hitColour = hitColour * comps.object->material.reflective;
  143. return Colour(hitColour.x, hitColour.y, hitColour.z);
  144. }
  145. Colour World::refractedColour(Computation comps, uint32_t depthCount)
  146. {
  147. double nRatio = comps.n1 / comps.n2;
  148. double cos_i = comps.eyeVector.dot(comps.normalVector);
  149. double sin2_t = (nRatio*nRatio) * (1 - cos_i * cos_i);
  150. if ((sin2_t > 1 ) || (depthCount == 0) || (comps.object->material.transparency == 0))
  151. {
  152. return Colour(0, 0, 0);
  153. }
  154. double cos_t = sqrt(1.0 - sin2_t);
  155. Tuple direction = comps.normalVector * (nRatio * cos_i - cos_t) - comps.eyeVector * nRatio;
  156. Ray refractedRay = Ray(comps.underHitPoint, direction);
  157. Tuple hitColour = this->colourAt(refractedRay, depthCount - 1) * comps.object->material.transparency;
  158. return Colour(hitColour.x, hitColour.y, hitColour.z);
  159. }