Antkeeper  0.0.1
rgb-voronoi-noise.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
22 #include <engine/debug/log.hpp>
23 #include <algorithm>
24 #include <execution>
25 #include <fstream>
26 #include <stb/stb_image_write.h>
27 
28 void generate_rgb_voronoi_noise(std::filesystem::path path)
29 {
30  /*
31  image img;
32  img.format(4, 8);
33  img.resize(1024, 1024);
34 
35  auto width = img.width();
36  auto height = img.height();
37  unsigned char* pixels = (unsigned char*)img.data();
38 
39  const float frequency = 512.0f;
40  float scale_x = 1.0f / static_cast<float>(width - 1) * frequency;
41  float scale_y = 1.0f / static_cast<float>(height - 1) * frequency;
42 
43  std::for_each
44  (
45  std::execution::par_unseq,
46  img.begin<math::vec4<unsigned char>>(),
47  img.end<math::vec4<unsigned char>>(),
48  [pixels, width, height, scale_x, scale_y, frequency](auto& pixel)
49  {
50  const std::size_t i = &pixel - (math::vec4<unsigned char>*)pixels;
51  const std::size_t y = i / width;
52  const std::size_t x = i % width;
53 
54  const math::fvec2 position =
55  {
56  static_cast<float>(x) * scale_x,
57  static_cast<float>(y) * scale_y
58  };
59 
60  const auto
61  [
62  f1_sqr_distance,
63  f1_displacement,
64  f1_id,
65  f1_edge_sqr_distance
66  ] = math::noise::voronoi::f1_edge<float, 2>(position, 1.0f, {frequency, frequency});
67 
68  const float f1_edge_distance = std::sqrt(f1_edge_sqr_distance);
69 
70  const float scale = 255.0f * (255.0f / 204.0f);
71  pixel =
72  {
73  static_cast<unsigned char>(f1_id & 255),
74  static_cast<unsigned char>((f1_id >> 8) & 255),
75  static_cast<unsigned char>((f1_id >> 16) & 255),
76  static_cast<unsigned char>((f1_id >> 24) & 255)
77  };
78 
79  // const float f1_distance = std::sqrt(f1_sqr_distance);
80  // const math::fvec2 uv = (position + f1_displacement) / frequency;
81 
82  // pixel =
83  // {
84  // static_cast<unsigned char>(std::min(255.0f, f1_distance * 255.0f)),
85  // static_cast<unsigned char>(std::min(255.0f, uv[0] * 255.0f)),
86  // static_cast<unsigned char>(std::min(255.0f, uv[1] * 255.0f)),
87  // static_cast<unsigned char>(f1_id % 256)
88  // };
89  }
90  );
91 
92  stbi_flip_vertically_on_write(1);
93  stbi_write_png(path.string().c_str(), img.width(), img.height(), img.channel_count(), img.data(), img.width() * img.channel_count());
94  */
95 }
void generate_rgb_voronoi_noise(std::filesystem::path path)