P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
Visualizer.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <functional>
14#include <fstream>
15#include <string>
16#include <cmath>
17#include <cstdint>
18#include <algorithm>
19
20inline void visualizer2D(
21 const std::string& filename,
22 int width,
23 int height,
24 float scale,
25 const std::function<float(float, float)>& noiseFunc)
26{
27 std::ofstream out(filename, std::ios::binary);
28 if (!out) return;
29
30 out << "P6\n" << width << " " << height << "\n255\n";
31
32 for (int y = 0; y < height; ++y) {
33 for (int x = 0; x < width; ++x) {
34 float nx = (x - width / 2.0f) / scale;
35 float ny = (y - height / 2.0f) / scale;
36 float n = noiseFunc(nx, ny);
37
38 // Normalize from [-1, 1] → [0, 255]
39 unsigned char v = static_cast<unsigned char>(
40 std::clamp((n * 0.5f + 0.5f) * 255.0f, 0.0f, 255.0f)
41 );
42
43 out.put(v).put(v).put(v); // grayscale RGB
44 }
45 }
46
47 out.close();
48}
49
50inline void visualizer2D(const std::string& filename, const std::vector<std::vector<float>>& map) {
51 int H = map.size(), W = map[0].size();
52 std::ofstream out(filename, std::ios::binary);
53 out << "P6\n" << W << " " << H << "\n255\n";
54 for (int y = 0; y < H; ++y)
55 for (int x = 0; x < W; ++x) {
56 unsigned char c = static_cast<unsigned char>(
57 std::clamp(map[y][x] * 255.0f, 0.0f, 255.0f)
58 );
59
60 out.put(c).put(c).put(c);
61 }
62 out.close();
63}
64
void visualizer2D(const std::string &filename, int width, int height, float scale, const std::function< float(float, float)> &noiseFunc)
Definition Visualizer.hpp:20