lightingGenerator.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright (C) 2015 Marien Raat
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <vector>
  16. struct Vec3f {
  17. float x;
  18. float y;
  19. float z;
  20. };
  21. struct Light {
  22. // The color of the light. The colors are saved in (x, y, z) ->
  23. // (r, g, b). The basic strength of the light is implicited in the
  24. // lightness of this color.
  25. Vec3f color;
  26. // The falloff of the strength of the light that occurs when a
  27. // point is farther from this light. The total light strenth of a
  28. // light is calculated by multiplying the total falloff with the
  29. // color of the light.
  30. // The total falloff is calculated with:
  31. // total_falloff = falloff.x * pow(distance, 1) +
  32. // falloff.y * pow(distance, 2) +
  33. // falloff.z * pow(distance, 3);
  34. Vec3f falloff;
  35. // The position of the light relative to the top left corner of
  36. // the map.
  37. Vec3f position;
  38. };
  39. // LigthingGenerator can generate a normal map based on a given
  40. // heightmap. It can then generate a lighting map based on this
  41. // normal map and a vector of Lights.
  42. class LightingGenerator {
  43. public:
  44. LightingGenerator();
  45. // Generates a normal map from a heightmap and puts it in
  46. // this->normalmap
  47. void generateNormalMap(
  48. // The heightmap to generate a normal map from
  49. double *heightmap,
  50. // The width and the height of the heightmap (and thus the
  51. // generated normal map)
  52. int width,
  53. int height,
  54. // Whether to overwrite the old normal map
  55. bool overwriteOldMap = true
  56. );
  57. // Generates a lighting map based on this->normalmap and a vector
  58. // of lights.
  59. // This function doesn't take the actual height of the points on
  60. // the map in regard, since lights are usually to far away for
  61. // this to have any impact.
  62. // This function will not generate a light if this->normalmap is
  63. // not initialized (when this->generateNormalmap has not been
  64. // called).
  65. void generateLightMap(
  66. // A vector of the lights used to light the heightmap
  67. std::vector<Light> lights,
  68. // The diffuse light value. All pixels will get at least this
  69. // light
  70. Vec3f diffuse,
  71. // Whether to overwrite the old light map
  72. bool overwriteOldMap = true
  73. );
  74. Vec3f *normalmap;
  75. Vec3f *lightmap;
  76. private:
  77. int currentMapWidth, currentMapHeight;
  78. int previousLightmapWidth, previousLightmapHeight;
  79. bool firstNormalMap, firstLightMap;
  80. Vec3f getNormal(int x, int y, double *heightmap);
  81. float getDotProduct(int x, int y, Light light);
  82. };