light.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Light implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <light.h>
  11. #include <world.h>
  12. void Light::dumpMe(FILE *fp)
  13. {
  14. fprintf(fp, "\"Colour\": {\"red\": %f, \"green\": %f, \"blue\": %f},\n",
  15. this->intensity.x, this->intensity.y, this->intensity.z);
  16. fprintf(fp, "\"Position\": {\"x\": %f, \"y\": %f, \"z\":%f},\n",
  17. this->position.x, this->position.y, this->position.z);
  18. fprintf(fp, "\"Type\": \"PointLight\",\n");
  19. }
  20. double Light::intensityAt(World &w, Tuple point)
  21. {
  22. switch(this->type)
  23. {
  24. case POINT_LIGHT:
  25. default:
  26. return (w.isShadowed(point, this->position))?0.0:1.0;
  27. case AREA_LIGHT:
  28. double total = 0.0;
  29. uint32_t v, u;
  30. for(v = 0; v < this->vSteps; v++)
  31. {
  32. for(u = 0; u < this->uSteps; u++)
  33. {
  34. if (!w.isShadowed(point, this->pointOnLight(u, v)))
  35. {
  36. total = total + 1.0;
  37. }
  38. }
  39. }
  40. return total / this->samples;
  41. break;
  42. }
  43. }
  44. Tuple Light::pointOnLight(uint32_t u, uint32_t v)
  45. {
  46. if (this->jitter)
  47. {
  48. /* For some reason, for the test to pass, I need to get the sequence for V first, then U contrary to what
  49. * the bonus chapter says
  50. */
  51. return this->corner +
  52. this->vVec * (v + this->jitterBy.next()) +
  53. this->uVec * (u + this->jitterBy.next());
  54. }
  55. return this->corner + this->uVec * (u + 0.5) + this->vVec * (v + 0.5);
  56. }