-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (85 loc) · 3.06 KB
/
main.cpp
File metadata and controls
114 lines (85 loc) · 3.06 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
#include "data_v1/array.hpp"
#include "data_v1/lazy.hpp"
#include "emscripten_v1/html5.hpp"
#include "fabrik_v1/fabrik.hpp"
#include "gl_v1/gl.hpp"
#include "math3d_v1/transform.hpp"
#include "math3d_v1/trig.hpp"
#include <cstdio>
#include "mesh.hpp"
namespace basic {
using namespace emscripten;
static const auto viewport = vec(800, 800);
static const auto prepared = lazy([]() {
return prepare_mesh(strided<vertex_t>({
{{0, 0, 0}, {-1, 0, 0}},
{{0.2, 0, -1}, {0, 0, -1}},
{{0.2, 1, 0}, {0, 1, 0}},
{{0.2, 0, 1}, {0, 0, 1}},
{{0.2, -1, 0}, {0, -1, 0}},
{{1, 0, 0}, {1, 0, 0}},
}),
strided<triangle_t>({
{0, 2, 1},
{0, 3, 2},
{0, 4, 3},
{0, 1, 4},
{5, 1, 2},
{5, 2, 3},
{5, 3, 4},
{5, 4, 1},
}));
});
static const auto projection = perspective(from_angle(45.0f), 1.0f, 9.0f);
static const auto camera = translation(vec(0.0f, 0.0f, -8.0f));
static const auto projection_camera = projection * camera;
static const auto center_z = homogenize(projection_camera * vec(0, 0, 0, 1))[2];
static const auto projection_camera_inv = inverse(projection_camera);
static vec_t<float, 3> target = {1, -2, 0};
static vec_t<float, 3> positions[] = {
{0, 0, 0}, {0, 1, 0}, {0, 2.5, 0}, {0, 3, 0}, {0, 4, 0}, {0, 4.5, 0}};
static const float distances[] = {1, 1.5, 0.5, 1, 0.5};
void main() {
gl::Init("canvas");
gl::Viewport(viewport);
set_mouse_callback(
"canvas", strided({MOUSEDOWN, MOUSEMOVE}), [](auto, auto &event) {
if (event.buttons & 1) {
auto mouse_pos =
(vec(event.targetX, event.targetY) * 2.0f - viewport) / viewport *
vec(1, -1);
auto r = projection_camera_inv * vec(mouse_pos, center_z, 1.0f);
target = sub<3>(homogenize(r));
}
return true;
});
request_animation_frame_loop([](auto) {
gl::ClearColor(0, 0, 0, 1);
gl::Clear(gl::COLOR_BUFFER_BIT);
fabrik::move_to(target, 0.001, positions, distances, 1);
auto currentPos = begin(positions);
auto lastPos = end(positions) - 1;
do {
auto &previous = *currentPos;
auto ¤t = *++currentPos;
auto delta = current - previous;
auto distance = mag(delta);
auto x = delta * (1 / distance);
auto y = normalize(cross(vec(0, 0, 1), x));
auto z = cross(x, y);
auto world =
translation(previous) *
from_columns(vec(x, 0), vec(y, 0), vec(z, 0), vec(0, 0, 0, 1)) *
scaling(vec(distance, 0.1f, 0.1f));
render(prepared, world, projection_camera);
} while (currentPos != lastPos);
return true;
});
}
} // namespace basic
int main() {
printf("main()\n");
basic::main();
printf("main() -> 0\n");
return 0;
}