HighMap.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef HIGHMAP_H
  2. #define HIGHMAP_H
  3. #include "common/utils/Types.h"
  4. #include "common/utils/Random.h"
  5. template<uint W, uint H>
  6. class HighMap {
  7. public:
  8. HighMap() {
  9. Random r(0);
  10. for(uint x = 0; x < W; x++) {
  11. for(uint y = 0; y < H; y++) {
  12. data[x][y] = r.nextFloat();
  13. }
  14. }
  15. smooth();
  16. smooth();
  17. smooth();
  18. smooth();
  19. }
  20. void smooth() {
  21. float oldNoice[W][H];
  22. for(uint x = 0; x < W; x++) {
  23. for(uint y = 0; y < H; y++) {
  24. oldNoice[x][y] = data[x][y];
  25. }
  26. }
  27. for(uint x = 0; x < W; x++) {
  28. for(uint y = 0; y < H; y++) {
  29. float sum = 0.0f;
  30. for(uint mx = 0; mx <= 2; mx++) {
  31. for(uint my = 0; my <= 2; my++) {
  32. sum += oldNoice[(x + mx - 1) % W][(y + my - 1) % H];
  33. }
  34. }
  35. data[x][y] = sum / 9.0f;
  36. }
  37. }
  38. }
  39. uint getHeight(uint x, uint y, uint max) const {
  40. return (uint) (data[x][y] * max);
  41. }
  42. private:
  43. float data[W][H];
  44. };
  45. #endif