This repository was archived by the owner on Dec 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
187 lines (154 loc) · 4.02 KB
/
main.cpp
File metadata and controls
187 lines (154 loc) · 4.02 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <format>
#include <fstream>
#include <ios>
#include <iostream>
#include <string>
#define UNABLE_TO_OPEN_FILE 1
#define FACTOR 60
#define FRAMES 377
#define MAX_VALUE 255
typedef struct vec4 {
double x, y, z, w;
} vec4;
typedef struct vec2 {
double x, y;
vec2 yx() const { return vec2(y, x); }
vec4 xyyx() const { return vec4(x, y, y, x); }
} vec2;
vec2 operator*(const vec2 &a, double s) {
return {
.x = a.x * s,
.y = a.y * s,
};
}
vec2 operator+(const vec2 &a, double s) {
return {
.x = a.x + s,
.y = a.y + s,
};
}
vec2 operator*(double s, const vec2 &a) { return a * s; }
vec2 operator-(const vec2 &a, const vec2 &b) {
return {
.x = a.x - b.x,
.y = a.y - b.y,
};
}
vec2 operator-(double s, const vec2 &a) { return vec2(s - a.x, s - a.y); }
vec2 operator+(const vec2 &a, const vec2 &b) {
return {
.x = a.x + b.x,
.y = a.y + b.y,
};
}
vec2 operator*(const vec2 &a, const vec2 &b) {
return {
.x = a.x * b.x,
.y = a.y * b.y,
};
}
vec2 operator/(const vec2 &a, double s) {
return {
.x = a.x / s,
.y = a.y / s,
};
}
double dot(const vec2 &a, const vec2 &b) { return a.x * b.x + a.y * b.y; }
vec2 abs(const vec2 &a) {
return {
.x = abs(a.x),
.y = abs(a.y),
};
}
vec2 &operator+=(vec2 &a, const vec2 &b) {
a = a + b;
return a;
}
vec2 &operator+=(vec2 &a, double s) {
a = a + s;
return a;
}
vec2 cos(const vec2 &a) {
return {
.x = cos(a.x),
.y = cos(a.y),
};
}
vec2 sin(const vec2 &a) {
return {
.x = sin(a.x),
.y = sin(a.y),
};
}
vec4 sin(const vec4 &a) {
return vec4(sin(a.x), sin(a.y), sin(a.z), sin(a.w));
}
vec4 exp(const vec4 &a) {
return vec4(exp(a.x), exp(a.y), exp(a.z), exp(a.w));
}
vec4 tanh(const vec4 &a) {
return vec4(tanh(a.x), tanh(a.y), tanh(a.z), tanh(a.w));
}
vec4 operator+(const vec4 &a, double s) {
return vec4(a.x + s, a.y + s, a.z + s, a.w + s);
}
vec4 operator*(const vec4 &a, double s) {
return vec4(a.x * s, a.y * s, a.z * s, a.w * s);
}
vec4 operator*(double s, const vec4 &a) { return a * s; }
vec4 operator+(const vec4 &a, const vec4 &b) {
return vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
vec4 &operator+=(vec4 &a, const vec4 &b) {
a = a + b;
return a;
}
vec4 operator-(double s, const vec4 &a) {
return vec4(s - a.x, s - a.y, s - a.z, s - a.w);
}
vec4 operator/(const vec4 &a, const vec4 &b) {
return vec4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
}
int main(int argc, char *argv[]) {
for (uint16_t i = 0; i < FRAMES; ++i) {
std::string file_path = std::format("frames/out{:03d}.ppm", i);
std::ofstream output(file_path, std::ios::binary);
if (!output.is_open()) {
std::cerr << std::format("Unable to open file {}", file_path);
return UNABLE_TO_OPEN_FILE;
}
const std::uint16_t width{24 * FACTOR};
const std::uint16_t height{15 * FACTOR};
const vec2 r = {
.x = (double)width,
.y = (double)height,
};
output << "P6\n"
<< std::format("{} {}\n", width, height)
<< std::format("{}\n", MAX_VALUE);
double t = (double)i / 60.0;
for (uint64_t y = 0; y < height; ++y) {
for (uint64_t x = 0; x < width; ++x) {
vec4 o = {0, 0, 0, 0};
vec2 FC = {
.x = (double)x,
.y = (double)y,
};
vec2 p = (FC * 2. - r) / r.y, l = {0, 0},
v = p * (1. - (l += abs(.7 - dot(p, p)))) / .2;
for (double idx = 0; idx++ < 8.; o += (sin(v.xyyx()) + 1.) * abs(v.x - v.y) * .2)
v += cos(v.yx() * idx + vec2(0, idx) + t) / idx + .7;
o = tanh(exp(p.y * vec4(1, -1, -2, 0)) * exp(-4. * l.x) / o);
output.put(static_cast<char>(std::clamp(o.x * MAX_VALUE, 0.0, 255.0)));
output.put(static_cast<char>(std::clamp(o.y * MAX_VALUE, 0.0, 255.0)));
output.put(static_cast<char>(std::clamp(o.z * MAX_VALUE, 0.0, 255.0)));
}
}
output.close();
std::cout << std::format("Generated {}\n", file_path);
}
return 0;
}