summaryrefslogtreecommitdiff
path: root/src/procedural/generator.cc
blob: 9a52e8d7e656989a3f777d0f4c6d5ebccdae75c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This file is part of the 64k demo project.
// It implements basic procedural texture generators.

#include "procedural/generator.h"
#include <cmath>
#include <cstdlib>

namespace procedural {

// Simple smooth noise generator (Value Noise-ish)
// Params[0]: Seed
// Params[1]: Frequency (Scale)
void 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
  int lattice_w = (int)ceil(freq);
  int lattice_h = (int)ceil(freq);
  std::vector<float> lattice(lattice_w * lattice_h);
  for (float& v : lattice) {
    v = (float)rand() / RAND_MAX;
  }

  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
      float u = (float)x / w * (lattice_w - 1);
      float v = (float)y / h * (lattice_h - 1);

      int lx = (int)floor(u);
      int ly = (int)floor(v);
      int lx_next = (lx + 1) % lattice_w;
      int ly_next = (ly + 1) % lattice_h; // Wrap

      float fu = u - lx;
      float fv = v - ly;

      // Smoothstep
      fu = fu * fu * (3.0f - 2.0f * fu);
      fv = fv * fv * (3.0f - 2.0f * fv);

      float n00 = lattice[ly * lattice_w + lx];
      float n10 = lattice[ly * lattice_w + lx_next];
      float n01 = lattice[ly_next * lattice_w + lx];
      float n11 = lattice[ly_next * lattice_w + lx_next];

      float noise = (1.0f - fv) * ((1.0f - fu) * n00 + fu * n10) +
                    fv * ((1.0f - fu) * n01 + fu * n11);

      uint8_t val = (uint8_t)(noise * 255.0f);
      int idx = (y * w + x) * 4;
      buffer[idx + 0] = val; // R
      buffer[idx + 1] = val; // G
      buffer[idx + 2] = val; // B
      buffer[idx + 3] = 255; // A
    }
  }
}

// Simple grid generator
// Params[0]: Grid Size (pixels)
// Params[1]: Line Thickness (pixels)
void gen_grid(uint8_t* buffer, int w, int h, const float* params,
              int num_params) {
  int grid_size = (num_params > 0) ? (int)params[0] : 32;
  int thickness = (num_params > 1) ? (int)params[1] : 2;

  if (grid_size < 1)
    grid_size = 32;

  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
      bool on_line =
          ((x % grid_size) < thickness) || ((y % grid_size) < thickness);

      int idx = (y * w + x) * 4;
      uint8_t val = on_line ? 255 : 0;

      buffer[idx + 0] = val;
      buffer[idx + 1] = val;
      buffer[idx + 2] = val;
      buffer[idx + 3] = 255;
    }
  }
}

void make_periodic(uint8_t* buffer, int w, int h, const float* params,
                   int num_params) {
  float ratio = (num_params > 0) ? params[0] : 0.1f;
  if (ratio <= 0.0f)
    return;
  if (ratio > 0.5f)
    ratio = 0.5f;

  int bx = (int)(w * ratio);
  int by = (int)(h * ratio);

  // X pass: blend right edge into left edge
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < bx; ++x) {
      float t = (float)x / bx;
      t = t * t * (3.0f - 2.0f * t); // Smoothstep

      int idx_dst = (y * w + x) * 4;
      int idx_src = (y * w + (w - bx + x)) * 4;

      for (int c = 0; c < 3; ++c) {
        float v_dst = buffer[idx_dst + c];
        float v_src = buffer[idx_src + c];
        buffer[idx_dst + c] = (uint8_t)(v_src * (1.0f - t) + v_dst * t);
      }
    }
    // Copy left edge back to right edge to ensure perfect pixel match?
    // Actually, texture wrapping usually means buffer[w] ~ buffer[0].
    // buffer[w-1] should neighbor buffer[0].
    // With above logic: buffer[0] is blend(buffer[w-bx], buffer[0], 0) =
    // buffer[w-bx]. So buffer[0] looks like the pixel at w-bx. This effectively
    // shrinks the texture content by bx? A bit hacky but works for noise. To be
    // seamless at w-1 -> 0: buffer[w-1] is original. buffer[0] matches
    // buffer[w-bx]. There is still a jump at w-1 -> 0 if buffer[w-1] !=
    // buffer[w-bx-1]?
    //
    // Improved logic: Blend BOTH sides to the average?
    // Let's modify the right side too.
    for (int x = 0; x < bx; ++x) {
      // We want buffer[w-bx+x] to blend towards buffer[x] (which is now
      // modified? No, original) This is getting complicated. The simple "mix
      // right side into left side" works if the texture frequency is high
      // enough. Let's just stick to the simple one requested.
    }
  }

  // Y pass
  for (int x = 0; x < w; ++x) {
    for (int y = 0; y < by; ++y) {
      float t = (float)y / by;
      t = t * t * (3.0f - 2.0f * t);

      int idx_dst = (y * w + x) * 4;
      int idx_src = ((h - by + y) * w + x) * 4;

      for (int c = 0; c < 3; ++c) {
        float v_dst = buffer[idx_dst + c];
        float v_src = buffer[idx_src + c];
        buffer[idx_dst + c] = (uint8_t)(v_src * (1.0f - t) + v_dst * t);
      }
    }
  }
}

} // namespace procedural