Antkeeper  0.0.1
cocoon-silk-sdf.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_cocoon_silk_sdf(std::filesystem::path path)
29 {
30  /*
31  debug::log_info("Generating cocoon silk SDF image...");
32 
33  image img;
34  img.format(4, 8);
35  img.resize(2048, 2048);
36 
37  auto width = img.width();
38  auto height = img.height();
39  unsigned char* pixels = (unsigned char*)img.data();
40 
41  const float frequency = 100.0f;
42  float scale_x = 1.0f / static_cast<float>(width - 1) * frequency;
43  float scale_y = 1.0f / static_cast<float>(height - 1) * frequency;
44 
45  std::for_each
46  (
47  std::execution::par_unseq,
48  img.begin<math::vec4<unsigned char>>(),
49  img.end<math::vec4<unsigned char>>(),
50  [pixels, width, height, scale_x, scale_y, frequency](auto& pixel)
51  {
52  const std::size_t i = &pixel - (math::vec4<unsigned char>*)pixels;
53  const std::size_t y = i / width;
54  const std::size_t x = i % width;
55 
56  const math::fvec2 position =
57  {
58  static_cast<float>(x) * scale_x,
59  static_cast<float>(y) * scale_y
60  };
61 
62  const auto
63  [
64  f1_sqr_distance,
65  f1_displacement,
66  f1_id,
67  f1_edge_sqr_distance
68  ] = math::noise::voronoi::f1_edge<float, 2>(position, 1.0f, {frequency, frequency});
69 
70  const float f1_edge_distance = std::sqrt(f1_edge_sqr_distance);
71 
72  const float scale = 255.0f * (255.0f / 204.0f);
73  pixel =
74  {
75  static_cast<unsigned char>(std::min(255.0f, f1_edge_distance * scale)),
76  static_cast<unsigned char>(std::min(255.0f, f1_edge_distance * scale)),
77  static_cast<unsigned char>(std::min(255.0f, f1_edge_distance * scale)),
78  255
79  };
80 
81  // const float f1_distance = std::sqrt(f1_sqr_distance);
82  // const math::fvec2 uv = (position + f1_displacement) / frequency;
83 
84  // pixel =
85  // {
86  // static_cast<unsigned char>(std::min(255.0f, f1_distance * 255.0f)),
87  // static_cast<unsigned char>(std::min(255.0f, uv[0] * 255.0f)),
88  // static_cast<unsigned char>(std::min(255.0f, uv[1] * 255.0f)),
89  // static_cast<unsigned char>(f1_id % 256)
90  // };
91  }
92  );
93  debug::log_info("Generated cocoon silk SDF image");
94 
95  debug::log_info("Saving cocoon silk SDF image to \"{}\"...", path.string());
96 
97  stbi_flip_vertically_on_write(1);
98  stbi_write_png(path.string().c_str(), img.width(), img.height(), img.channel_count(), img.data(), img.width() * img.channel_count());
99 
100  debug::log_info("Saved cocoon silk SDF image to \"{}\"", path.string());
101  */
102 }
void generate_cocoon_silk_sdf(std::filesystem::path path)