HighMap.h 1.1 KB

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