world.cpp 5.2 KB

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