world.cpp 5.8 KB

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