123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #ifndef HIGHMAP_H
- #define HIGHMAP_H
- #include "gaming-core/utils/Random.h"
- template<int W, int H>
- class HighMap {
- public:
- HighMap() {
- Random r(0);
- for(int x = 0; x < W; x++) {
- for(int y = 0; y < H; y++) {
- data[x][y] = r.nextFloat();
- }
- }
- smooth();
- smooth();
- smooth();
- smooth();
- }
- void smooth() {
- float oldNoice[W][H];
- for(int x = 0; x < W; x++) {
- for(int y = 0; y < H; y++) {
- oldNoice[x][y] = data[x][y];
- }
- }
- for(int x = 0; x < W; x++) {
- for(int y = 0; y < H; y++) {
- float sum = 0.0f;
- for(int mx = -1; mx <= 1; mx++) {
- for(int my = -1; my <= 1; my++) {
- sum += oldNoice[(x + mx + W) % W][(y + my + H) % H];
- }
- }
- data[x][y] = sum / 9.0f;
- }
- }
- }
- int getHeight(int x, int y, int max) const {
- return (data[x][y] * max);
- }
- private:
- float data[W][H];
- };
- #endif
|