light.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }
  42. }
  43. Tuple Light::pointOnLight(uint32_t u, uint32_t v)
  44. {
  45. if (this->jitter)
  46. {
  47. return this->corner +
  48. this->uVec * (u + this->jitterBy.next()) +
  49. this->vVec * (v + this->jitterBy.next());
  50. }
  51. return this->corner + this->uVec * (u + 0.5) + this->vVec * (v + 0.5);
  52. }