You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0KB

  1. #pragma once
  2. #include "common.hpp"
  3. namespace rack {
  4. namespace engine {
  5. struct Light {
  6. /** The square of the brightness.
  7. Unstable API. Use set/getBrightness().
  8. */
  9. float value = 0.f;
  10. /** Sets the brightness immediately with no light decay. */
  11. void setBrightness(float brightness) {
  12. value = (brightness > 0.f) ? std::pow(brightness, 2) : 0.f;
  13. }
  14. float getBrightness() {
  15. return std::sqrt(value);
  16. }
  17. /** Emulates light decay with slow fall but immediate rise. */
  18. void setSmoothBrightness(float brightness, float deltaTime) {
  19. float v = (brightness > 0.f) ? std::pow(brightness, 2) : 0.f;
  20. if (v < value) {
  21. // Fade out light
  22. const float lambda = 30.f;
  23. value += (v - value) * lambda * deltaTime;
  24. }
  25. else {
  26. // Immediately illuminate light
  27. value = v;
  28. }
  29. }
  30. /** Use `setSmoothBrightness(brightness, APP->engine->getSampleTime())` instead. */
  31. DEPRECATED void setBrightnessSmooth(float brightness, float frames = 1.f) {
  32. setSmoothBrightness(brightness, frames / 44100.f);
  33. }
  34. };
  35. } // namespace engine
  36. } // namespace rack