world.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #include <stdio.h>
  13. #include <string.h>
  14. #define MIN_ALLOC (2)
  15. World::World() : objectCount(0), lightCount(0)
  16. {
  17. this->allocatedLightCount = MIN_ALLOC;
  18. this->lightList = (Light **)calloc(sizeof(Light *), MIN_ALLOC);
  19. this->lightCount = 0;
  20. this->allocatedObjectCount = MIN_ALLOC;
  21. this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  22. this->objectCount = 0;
  23. };
  24. World::~World()
  25. {
  26. /* We need to do some cleanup... */
  27. }
  28. void World::addObject(Shape *s)
  29. {
  30. if ((this->objectCount + 1) > this->allocatedObjectCount)
  31. {
  32. this->allocatedObjectCount *= 2;
  33. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  34. }
  35. this->objectList[this->objectCount++] = s;
  36. }
  37. void World::addLight(Light *l)
  38. {
  39. if ((this->lightCount + 1) > this->allocatedLightCount)
  40. {
  41. this->allocatedLightCount *= 2;
  42. this->lightList = (Light **)realloc(this->lightList, sizeof(Light **) * this->allocatedLightCount);
  43. }
  44. this->lightList[this->lightCount++] = l;
  45. }
  46. bool World::lightIsIn(Light &l)
  47. {
  48. int i;
  49. for(i = 0; i < this->lightCount; i++)
  50. {
  51. if (*this->lightList[i] == l)
  52. {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. bool World::objectIsIn(Shape &s)
  59. {
  60. int i;
  61. for(i = 0; i < this->objectCount; i++)
  62. {
  63. if (*this->objectList[i] == s)
  64. {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. Intersect World::intersect(Ray r)
  71. {
  72. Intersect ret;
  73. int i, j;
  74. for(i = 0; i < this->objectCount; i++)
  75. {
  76. Intersect xs = this->objectList[i]->intersect(r);
  77. for(j = 0; j < xs.count(); j++)
  78. {
  79. ret.add(xs[j]);
  80. }
  81. }
  82. return ret;
  83. }
  84. Tuple World::shadeHit(Computation comps, uint32_t depthCount)
  85. {
  86. uint32_t lightIndex;
  87. Tuple surface = Colour(0, 0, 0);
  88. for(lightIndex = 0; lightIndex < this->lightCount; lightIndex++)
  89. {
  90. double lightLevel = this->lightList[lightIndex]->intensityAt(*this, comps.overHitPoint);
  91. surface = surface + comps.material->lighting(*this->lightList[lightIndex], comps.overHitPoint, comps.eyeVector,
  92. comps.normalVector, comps.object, lightLevel);
  93. }
  94. Tuple reflected = this->reflectColour(comps, depthCount);
  95. Tuple refracted = this->refractedColour(comps, depthCount);
  96. if ((comps.material->reflective > 0) && (comps.material->transparency > 0))
  97. {
  98. double reflectance = comps.schlick();
  99. return surface + reflected * reflectance + refracted * (1 - reflectance);
  100. }
  101. return surface + reflected + refracted;
  102. }
  103. Tuple World::colourAt(Ray r, uint32_t depthCount)
  104. {
  105. Intersect allHits = this->intersect(r);
  106. Intersection hit = allHits.hit();
  107. stats.setMaxDepth(depthCount);
  108. if (hit.nothing())
  109. {
  110. return Colour(0, 0, 0);
  111. }
  112. else
  113. {
  114. return this->shadeHit(hit.prepareComputation(r, &allHits), depthCount);
  115. }
  116. }
  117. bool World::isShadowed(Tuple point, Tuple lightPosition)
  118. {
  119. Tuple v = lightPosition - point;
  120. double distance = v.magnitude();
  121. Tuple direction = v.normalise();
  122. Ray r = Ray(point, direction);
  123. stats.addLightRay();
  124. Intersect xs = this->intersect(r);
  125. int i;
  126. for(i = 0; i < xs.count(); i++)
  127. {
  128. Intersection h = xs[i];
  129. if (h.t < 0) continue;
  130. if ((h.object->dropShadow == true) && (h.t < distance))
  131. {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. Colour World::reflectColour(Computation comps, uint32_t depthCount)
  138. {
  139. if ((depthCount == 0) || (comps.material->reflective == 0))
  140. {
  141. return Colour(0, 0, 0);
  142. }
  143. /* So it is reflective, even just a bit. Let'sr reflect the ray! */
  144. Ray reflectedRay = Ray(comps.overHitPoint, comps.reflectVector);
  145. stats.addReflectRay();
  146. Tuple hitColour = this->colourAt(reflectedRay, depthCount - 1);
  147. hitColour = hitColour * comps.material->reflective;
  148. return Colour(hitColour.x, hitColour.y, hitColour.z);
  149. }
  150. Colour World::refractedColour(Computation comps, uint32_t depthCount)
  151. {
  152. double nRatio = comps.n1 / comps.n2;
  153. double cos_i = comps.eyeVector.dot(comps.normalVector);
  154. double sin2_t = (nRatio*nRatio) * (1 - cos_i * cos_i);
  155. if ((sin2_t > 1 ) || (depthCount == 0) || (comps.material->transparency == 0))
  156. {
  157. return Colour(0, 0, 0);
  158. }
  159. double cos_t = sqrt(1.0 - sin2_t);
  160. Tuple direction = comps.normalVector * (nRatio * cos_i - cos_t) - comps.eyeVector * nRatio;
  161. Ray refractedRay = Ray(comps.underHitPoint, direction);
  162. stats.addRefractRay();
  163. Tuple hitColour = this->colourAt(refractedRay, depthCount - 1) * comps.material->transparency;
  164. return Colour(hitColour.x, hitColour.y, hitColour.z);
  165. }
  166. void World::dumpMe(FILE *fp)
  167. {
  168. int i;
  169. /* JSON Opening */
  170. fprintf(fp, "{\n");
  171. fprintf(fp, "\"Lights\": {\n");
  172. for(i = 0; i < this->lightCount; i++)
  173. {
  174. fprintf(fp, "\"%d\": {\n", i);
  175. this->lightList[i]->dumpMe(fp);
  176. fprintf(fp, "},\n");
  177. }
  178. fprintf(fp, "},\n");
  179. fprintf(fp, "\"Objects\": {\n");
  180. for(i = 0; i < this->objectCount; i++)
  181. {
  182. fprintf(fp, "\"%d\": {\n", i);
  183. this->objectList[i]->dumpMe(fp);
  184. fprintf(fp, "},\n");
  185. }
  186. fprintf(fp, "},\n");
  187. /* JSON Closing */
  188. fprintf(fp, "}\n");
  189. }