light.c 717 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. * 3D Engine
  3. * light.c:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 06/03/2021.
  8. */
  9. #include <display.h>
  10. #include <vector.h>
  11. #include <light.h>
  12. void createLight(light_t *light, vec3_t direction)
  13. {
  14. light->direction = direction;
  15. vec3Normalize(&light->direction);
  16. }
  17. colour_t applyLight(colour_t c, double lightLevel)
  18. {
  19. if (lightLevel < 0) { lightLevel = 0; }
  20. if (lightLevel > 1) { lightLevel = 1; }
  21. uint8_t r = round(((c >> 16) & 0xFF) * lightLevel);
  22. uint8_t g = round(((c >> 8) & 0xFF) * lightLevel);
  23. uint8_t b = round(((c ) & 0xFF) * lightLevel);
  24. return MAKE_RGB(r, g, b);
  25. }