diff options
Diffstat (limited to 'src/procedural')
| -rw-r--r-- | src/procedural/generator.cc | 36 |
1 files changed, 8 insertions, 28 deletions
diff --git a/src/procedural/generator.cc b/src/procedural/generator.cc index 0eb8b03..f6d4e02 100644 --- a/src/procedural/generator.cc +++ b/src/procedural/generator.cc @@ -33,7 +33,8 @@ bool gen_perlin(uint8_t* buffer, int w, int h, const float* params, // Pre-allocate temporary float buffer for accumulating noise float* accum = (float*)calloc((size_t)w * h, sizeof(float)); - if (!accum) return false; + if (!accum) + return false; float current_freq = base_freq; float current_amp = base_amp; @@ -42,7 +43,8 @@ bool gen_perlin(uint8_t* buffer, int w, int h, const float* params, for (int o = 0; o < octaves; ++o) { const int lattice_w = (int)ceil(current_freq) + 1; const int lattice_h = (int)ceil(current_freq) + 1; - float* lattice = (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float)); + float* lattice = + (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float)); if (!lattice) { free(accum); return false; @@ -110,43 +112,33 @@ bool gen_perlin(uint8_t* buffer, int w, int h, const float* params, bool gen_noise(uint8_t* buffer, int w, int h, const float* params, int num_params) { - float freq = (num_params > 1) ? params[1] : 4.0f; if (num_params > 0 && params[0] != 0) { - srand((unsigned int)params[0]); - } - - // Create a small lattice of random values const int lattice_w = (int)ceil(freq); const int lattice_h = (int)ceil(freq); - float* lattice = (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float)); - - if (!lattice) return false; - + float* lattice = + (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float)); + if (!lattice) + return false; for (int i = 0; i < lattice_w * lattice_h; ++i) { - lattice[i] = (float)rand() / RAND_MAX; - } const float scale_u = 1.f * (lattice_w - 1) / w; const float scale_v = 1.f * (lattice_h - 1) / h; - - for (int y = 0; y < h; ++y) { - const float v = scale_v * y; const int ly = (int)floor(v); @@ -162,7 +154,6 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params, uint8_t* const dst = &buffer[y * w * 4]; for (int x = 0; x < w; ++x) { - float u = scale_u * x; const int lx = (int)floor(u); @@ -171,8 +162,6 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params, float fu = smooth(u - lx); - - float n00 = n0[lx]; float n10 = n0[lx_next]; @@ -181,12 +170,8 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params, float n11 = n1[lx_next]; - - const float noise = mix(mix(n00, n10, fu), mix(n01, n11, fu), fv); - - const uint8_t val = (uint8_t)(noise * 255.0f); dst[4 * x + 0] = val; // R @@ -196,19 +181,14 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params, dst[4 * x + 2] = val; // B dst[4 * x + 3] = 255; // A - } - } free(lattice); return true; - } - - // Simple grid generator // Params[0]: Grid Size (pixels) // Params[1]: Line Thickness (pixels) |
