-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pattern.h
483 lines (447 loc) · 18.3 KB
/
Pattern.h
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#pragma once
#include "pch.h"
#include "Tuple.h"
#include "Color.h"
#include "Matrix.h"
//// uv stuff
struct AlignCheck final{
Color main;
Color upper_left;
Color upper_right;
Color bottom_left;
Color bottom_right;
};
constexpr AlignCheck uv_align_check(Color main, Color upper_left, Color upper_right,
Color bottom_left, Color bottom_right) noexcept{
return AlignCheck{main, upper_left, upper_right, bottom_left, bottom_right};
}
constexpr Color uv_pattern_at(const AlignCheck& pattern, const UVCoords& uv) noexcept{
if(uv.v > 0.8f){
if(uv.u < 0.2f){ return pattern.upper_left; }
if(uv.u > 0.8f){ return pattern.upper_right; }
} else if(uv.v < 0.2f){
if(uv.u < 0.2f){ return pattern.bottom_left; }
if(uv.u > 0.8f){ return pattern.bottom_right; }
}
return pattern.main;
}
enum class CubeFace{
left = 0,
front = 1,
right = 2,
back = 3,
up = 4,
down = 5,
count = 6
};
constexpr CubeFace face_from_point(const Point& point) noexcept{
float abs_x = math::abs(point.x);
float abs_y = math::abs(point.y);
float abs_z = math::abs(point.z);
float coord = math::max(abs_x, abs_y, abs_z);
if(coord == point.x){ return CubeFace::right; }
if(coord == -point.x){ return CubeFace::left; }
if(coord == point.y){ return CubeFace::up; }
if(coord == -point.y){ return CubeFace::down; }
if(coord == point.z){ return CubeFace::front; }
return CubeFace::back;
}
constexpr UVCoords cube_uv_front(const Point& p) noexcept{
return uv((p.x + 1.0f) * 0.5f, (p.y + 1.0f) * 0.5f);
/*const auto u = ((p.x + 1) % 2) / 2.0f;
const auto v = ((p.y + 1) % 2) / 2.0f;
return uv(u, v);*/
}
constexpr UVCoords cube_uv_back(const Point& p) noexcept{
return uv((1.0f - p.x) * 0.5f, (p.y + 1.0f) * 0.5f);
/*const auto u = ((1.0f - p.x) % 2) / 2.0f;
const auto v = ((p.y + 1) % 2) / 2.0f;
return uv(u, v);*/
}
constexpr UVCoords cube_uv_left(const Point& p) noexcept{
return uv((p.z + 1.0f) * 0.5f, (p.y + 1.0f) * 0.5f);
}
constexpr UVCoords cube_uv_right(const Point& p) noexcept{
return uv((1.0f - p.z) * 0.5f, (p.y + 1.0f) * 0.5f);
}
constexpr UVCoords cube_uv_up(const Point& p) noexcept{
return uv((p.x + 1.0f) * 0.5f, (1.0f - p.z) * 0.5f);
}
constexpr UVCoords cube_uv_down(const Point& p) noexcept{
return uv((p.x + 1.0f) * 0.5f, (p.z + 1.0f) * 0.5f);
}
//// uv stuff
struct NullPattern final{
constexpr Color at([[maybe_unused]] const Point& p) const noexcept{ return MAGENTA; }
explicit constexpr operator bool() const noexcept{ return false; }
constexpr bool operator==(const NullPattern& that) const noexcept = default;
constexpr const Matrix4& get_transform() const noexcept{ return _transform; }
constexpr const Matrix4& inv_transform() const noexcept{ return _invTransform; }
constexpr void set_transform([[maybe_unused]] Matrix4 mat) noexcept{}
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
};
struct TestPattern final{
constexpr Color at([[maybe_unused]] const Point& p) const noexcept{ return color(p.x, p.y, p.z); }
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==(const TestPattern& that) const noexcept = default;
constexpr const Matrix4& get_transform() const noexcept{ return _transform; }
constexpr const Matrix4& inv_transform() const noexcept{ return _invTransform; }
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
};
struct StripePattern final{
constexpr StripePattern(Matrix4 mat, Color a_, Color b_) noexcept : a{a_}, b{b_}{
set_transform(std::move(mat));
}
constexpr Color at(const Point& p) const noexcept{
return (math::int_floor(p.x) % 2 == 0) ? a : b;
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==(const StripePattern& that) const noexcept = default;
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Color a{};
Color b{};
};
struct GradientPattern final{
constexpr GradientPattern(Matrix4 mat, Color a_, Color b_) noexcept : a{a_}, b{b_}{
set_transform(std::move(mat));
}
constexpr Color at(const Point& p) const noexcept{
const auto dx = p.x - math::floor(p.x);
return lerp(a, b, dx);
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==(const GradientPattern& that) const noexcept = default;
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Color a{};
Color b{};
};
struct RadialGradientPattern final{
constexpr RadialGradientPattern(Matrix4 mat, Color a_, Color b_) noexcept : a{a_}, b{b_}{
set_transform(std::move(mat));
}
constexpr Color at(const Point& p) const noexcept{
const auto dist_from_center = magnitude(vector(p.x, p.y, p.z));
const auto fraction = dist_from_center - math::floor(dist_from_center);
return lerp(a, b, fraction);
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==(const RadialGradientPattern& that) const noexcept = default;
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Color a{};
Color b{};
};
struct RingPattern final{
constexpr RingPattern(Matrix4 mat, Color a_, Color b_) noexcept : a{a_}, b{b_}{
set_transform(std::move(mat));
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr Color at(const Point& p) const noexcept{
const auto distance_from_center = math::sqrt((p.x * p.x) + (p.z * p.z));
const auto mod = math::int_floor(distance_from_center) % 2;
return mod == 0 ? a : b;
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==(const RingPattern& that) const noexcept = default;
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Color a{};
Color b{};
};
struct CheckersPattern final{
constexpr CheckersPattern(Matrix4 mat, Color a_, Color b_) noexcept : a{a_}, b{b_}{
set_transform(std::move(mat));
}
constexpr Color at(const Point& p) const noexcept{
const auto val = static_cast<int>(math::floor(p.x) + math::floor(p.y) + math::floor(p.z));
return (val % 2 == 0) ? a : b;
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==(const CheckersPattern& that) const noexcept = default;
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Color a{};
Color b{};
};
struct UVCheckers final{
constexpr UVCheckers(unsigned width_, unsigned height_, Color a_, Color b_) noexcept
: width{width_}, height{height_}, a{a_}, b{b_}{}
constexpr Color at(const UVCoords& uv) const noexcept{
const auto u2 = math::int_floor(uv.u * width);
const auto v2 = math::int_floor(uv.v * height);
const auto sum = u2 + v2;
return (sum % 2 == 0) ? a : b;
}
private:
Color a{};
Color b{};
unsigned width = 0;
unsigned height = 0;
};
struct TextureMap final{
using Texture = UVCheckers;
using Callable = std::function<UVCoords(const Point&)>;
/*constexpr*/ TextureMap(Texture uv_pattern, Callable uv_map) noexcept
: uv_pattern{std::move(uv_pattern)}, uv_map{std::move(uv_map)}{}
/*constexpr*/ Color at(const Point& p) const noexcept{
const auto texCoords = uv_map(p);
return uv_pattern.at(texCoords);
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==([[maybe_unused]] const TextureMap& that) const noexcept{
return true; /* TODO: the implementation is necessary for the build to succeed,
as = default won't do. std::function isn't (easily) comparable anyway so some
thought is needed to decide on how this comparison should operate.
Right now all TextureMaps are functionally identical so returning true is mostly correct?
The proper solution might be to move texture and uv mapping functionality away from the
"Pattern"-structure */
}
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Texture uv_pattern;
Callable uv_map;
};
using Faces = std::array<AlignCheck, std::to_underlying(CubeFace::count)>; //std::span<const AlignCheck*>
struct CubeMap final{
constexpr explicit CubeMap(Faces patterns) noexcept
: faces{std::move(patterns)}{
assert(faces.size() == static_cast<size_t>(CubeFace::count) && "A CubeMap must have 6 patterns, one for each face.");
}
constexpr CubeMap() noexcept{
//default constructor = test cube pattern from Bonus chapter 1, test: "Finding the colors on a mapped cube"
const auto left = uv_align_check(YELLOW, CYAN, RED, BLUE, BROWN);
const auto front = uv_align_check(CYAN, RED, YELLOW, BROWN, GREEN);
const auto right = uv_align_check(RED, YELLOW, PURPLE, GREEN, WHITE);
const auto back = uv_align_check(GREEN, PURPLE, CYAN, WHITE, BLUE);
const auto up = uv_align_check(BROWN, CYAN, PURPLE, RED, YELLOW);
const auto down = uv_align_check(PURPLE, BROWN, GREEN, BLUE, WHITE);
faces = Faces({left, front, right, back, up, down});
}
constexpr Color at(const Point& p) const noexcept{
const auto face = face_from_point(p);
auto uvCoords = uv(0, 0);
if(face == CubeFace::left){
uvCoords = cube_uv_left(p);
} else if(face == CubeFace::right){
uvCoords = cube_uv_right(p);
} else if(face == CubeFace::front){
uvCoords = cube_uv_front(p);
} else if(face == CubeFace::back){
uvCoords = cube_uv_back(p);
} else if(face == CubeFace::up){
uvCoords = cube_uv_up(p);
} else{ //CubeFace::down
uvCoords = cube_uv_down(p);
}
const auto i = std::to_underlying(face);
assert(i >= 0 && i < static_cast<int>(CubeFace::count));
return uv_pattern_at(faces[i], uvCoords);
}
constexpr void set_transform(Matrix4 mat) noexcept{
_transform = std::move(mat);
_invTransform = inverse(_transform);
}
constexpr const Matrix4& get_transform() const noexcept{
return _transform;
}
constexpr const Matrix4& inv_transform() const noexcept{
return _invTransform;
}
explicit constexpr operator bool() const noexcept{ return true; }
constexpr bool operator==([[maybe_unused]] const CubeMap& that) const noexcept{
return true;
}
private:
Matrix4 _transform{Matrix4Identity};
Matrix4 _invTransform{Matrix4Identity};
Faces faces;
};
/*constexpr*/ auto cube_map(Faces patterns) noexcept{
assert(patterns.size() == static_cast<size_t>(CubeFace::count) && "A CubeMap must have 6 patterns, one for each face.");
return CubeMap(std::move(patterns));
}
/*constexpr*/ auto texture_map(TextureMap::Texture uv_pattern, TextureMap::Callable uv_map) noexcept{
return TextureMap(std::move(uv_pattern), std::move(uv_map));
}
constexpr auto null_pattern() noexcept{
return NullPattern{};
};
constexpr auto test_pattern() noexcept{
return TestPattern();
};
constexpr auto stripe_pattern(Color a, Color b, Matrix4 m = Matrix4Identity) noexcept{
return StripePattern(std::move(m), a, b);
};
constexpr auto gradient_pattern(Color a, Color b, Matrix4 m = Matrix4Identity) noexcept{
return GradientPattern(std::move(m), a, b);
};
constexpr auto radial_gradient_pattern(Color a, Color b, Matrix4 m = Matrix4Identity) noexcept{
return RadialGradientPattern(std::move(m), a, b);
};
constexpr auto ring_pattern(Color a, Color b, Matrix4 m = Matrix4Identity) noexcept{
return RingPattern(std::move(m), a, b);
};
constexpr auto checkers_pattern(Color a, Color b, Matrix4 m = Matrix4Identity) noexcept{
return CheckersPattern(std::move(m), a, b);
};
constexpr auto uv_checkers(unsigned width, unsigned height, Color a, Color b) noexcept{
return UVCheckers(width, height, a, b);
};
using Patterns = std::variant<NullPattern, TestPattern, StripePattern, GradientPattern, RingPattern, CheckersPattern, RadialGradientPattern, TextureMap, CubeMap>;
template<typename T>
concept is_pattern = std::is_same_v<NullPattern, T> || std::is_same_v<TestPattern, T> ||
std::is_same_v<StripePattern, T> || std::is_same_v<GradientPattern, T> ||
std::is_same_v<RingPattern, T> || std::is_same_v<CheckersPattern, T> ||
std::is_same_v<RadialGradientPattern, T> || std::is_same_v<TextureMap, T> ||
std::is_same_v<CubeMap, T>;
template<typename T>
requires is_pattern<T>
inline bool operator==(const T& t, const Patterns& v){
const T* c = std::get_if<T>(&v);
return c && *c == t; // true if v contains a pattern that compares equal to v
}
inline bool operator==(const Patterns& v, const is_pattern auto& t){
return t == v;
};
constexpr void set_transform(Patterns& variant, const Matrix4& newTransform) noexcept{
return std::visit([&newTransform](auto& pattern) noexcept -> void{
return pattern.set_transform(newTransform);
}, variant);
};
constexpr const Matrix4& get_transform(const Patterns& variant) noexcept{
return std::visit([](const auto& pattern) noexcept -> const Matrix4&{
return pattern.get_transform();
}, variant);
};
constexpr const Matrix4& get_inverse_transform(const Patterns& variant) noexcept{
return std::visit([](const auto& pattern) noexcept -> const Matrix4&{
return pattern.inv_transform();
}, variant);
};
Color pattern_at(const Patterns& variant, const Point& p) noexcept{
assert(!std::holds_alternative<NullPattern>(variant) && "pattern_at: called on NullPattern.");
return std::visit([&p](const auto& pattern) noexcept -> Color{ return pattern.at(p); }, variant);
};
//#include "Shapes.h"
//template<typename T>
//requires is_shape<T>
Color pattern_at(const Patterns& pattern, const /*must be is_shapes but I can't name that here. got some circular dependency going on.*/ auto& obj, const Point& world_point) noexcept{
assert(!std::holds_alternative<NullPattern>(pattern) && "pattern_at: called on NullPattern.");
const auto object_point = get_inverse_transform(obj) * world_point;
const auto pattern_point = get_inverse_transform(pattern) * object_point;
return pattern_at(pattern, pattern_point);
};
/*constexpr*/ UVCoords spherical_map(const Point& p) noexcept{
// Compute the azimuthal angle
// -π < theta <= π
// Angle increases clockwise as viewed from above,
// which is opposite of what we want, but we'll fix it later.
const auto theta = std::atan2(p.x, p.z);
// Compute the polar angle
// 0 <= phi <= π
//we are treating the point as a vector pointing from the sphere's origin (the world origin)
//to the point, and calculating the magnitude of that get the sphere's radius.
const auto phi = std::acos(p.y / math::sqrt(math::square(p.x) + math::square(p.y) + math::square(p.z)));
// -0.5 < raw_u <= 0.5
const auto raw_u = theta / math::TWO_PI;
// 0 <= u < 1
// Here's also where we fix the direction of u. Subtract it from 1,
// so that it increases counterclockwise as viewed from above.
const auto u = 1.0f - (raw_u + 0.5f);
// We want v to be 0 at the south pole of the sphere,
// and 1 at the north pole, so we have to "flip it over"
// by subtracting it from 1.
const auto v = 1 - phi / math::PI;
return {u, v};
}
/*planar mapping tiles every unit square on the plane, and ignores the y coordinate.*/
/*constexpr*/ UVCoords planar_map(const Point& p) noexcept{
const auto u = p.x - std::floor(p.x); // treat the fractional portion of the x coordinate as u
const auto v = p.z - std::floor(p.z); //and the fractional portion of z as v.
return uv(u, v);
}
/*planar mapping tiles every unit square on the plane, and ignores the y coordinate.*/
/*constexpr*/ UVCoords cylindrical_map(const Point& p) noexcept{
//compute the azimuthal angle, same as with spherical_map()
const auto theta = std::atan2(p.x, p.z);
const auto raw_u = theta / math::TWO_PI;
const auto u = 1 - (raw_u + 0.5f);
// let v go from 0 to 1 between whole units of y
const auto v = p.y - std::floor(p.y);
return uv(u, v);
}