-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireframe.cpp
More file actions
153 lines (136 loc) · 4.61 KB
/
Copy pathwireframe.cpp
File metadata and controls
153 lines (136 loc) · 4.61 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
#include <cstdlib>
#include <elementary_visualizer/elementary_visualizer.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace ev = elementary_visualizer;
float z_function(const float x, const float y, const float t)
{
return 0.1f * (sinf(x * 10 - t) + sinf(y * 10 - t));
}
void update_lines_data(
const int width,
const int j,
std::shared_ptr<ev::LinesVisual> lines,
const float t,
bool horizontal
)
{
std::vector<ev::Vertex> lines_data(width);
const int width_half = (width - 1) / 2;
for (int i = -width_half; i <= width_half; ++i)
{
float fx = static_cast<float>(i) / width_half;
float fy = static_cast<float>(j) / width_half;
if (horizontal)
std::swap(fx, fy);
float fz = z_function(fx, fy, t);
lines_data[i + width_half] = ev::Vertex(
glm::vec3(fx, fy, fz), glm::vec4(0.5f, 0.5f, 0.5f, 1.0f)
);
}
lines->set_lines_data(lines_data);
}
int main(int, char **)
{
const glm::ivec2 scene_size(500, 500);
auto scene =
ev::Scene::create(scene_size, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), 4, 4);
if (!scene)
return EXIT_FAILURE;
auto video = ev::Video::create("wireframe.gif", scene_size, 15, 2500000);
if (!video)
return EXIT_FAILURE;
auto window = ev::Window::create("Window", scene_size, false);
if (!window)
return EXIT_FAILURE;
window.value()->on_keyboard_event(
[&](const ev::EventAction action,
const ev::Key key,
const ev::ModifierKey)
{
if (action == ev::EventAction::press && key == ev::Key::q)
window.value()->destroy();
}
);
const float mouse_to_rotation = 0.01f;
std::optional<glm::vec2> rotation_delta;
glm::vec2 mouse_position;
glm::vec2 rotation(0.0f, 0.0f);
window.value()->on_mouse_button_event(
[&](const ev::EventAction action,
const ev::MouseButton button,
const ev::ModifierKey)
{
if (button == ev::MouseButton::left)
{
if (action == ev::EventAction::press)
rotation_delta =
rotation - mouse_to_rotation * mouse_position;
else if (action == ev::EventAction::release)
rotation_delta = std::nullopt;
}
}
);
window.value()->on_mouse_move_event(
[&](const glm::vec2 mouse_position_in)
{
mouse_position = mouse_position_in;
if (rotation_delta)
rotation =
mouse_to_rotation * mouse_position + rotation_delta.value();
}
);
const glm::vec3 eye = glm::vec3(0.0f, -2.0f, 3.0f);
const glm::vec3 center = glm::vec3(0.0f, 0.0f, 0.0f);
const glm::vec3 up(0.0f, 0.0f, 1.0f);
const glm::mat4 view = glm::lookAt(eye, center, up);
const float fov = 45.0f;
const float near = 0.01f;
const float far = 200.0f;
glm::mat4 projection = glm::perspective(fov, 1.0f, near, far);
const int width_half = 30;
const int width = (width_half * 2 + 1);
// Creating all the horizontal and vertical lines.
std::vector<std::array<std::shared_ptr<ev::LinesVisual>, 2>> all_lines(
width
);
for (auto &lines_pair : all_lines)
{
for (auto &lines : lines_pair)
{
lines = ev::LinesVisual::create(std::vector<ev::Vertex>())
.value_or(nullptr);
if (!lines)
return EXIT_FAILURE;
scene.value()->add_visual(lines);
lines->set_view(view);
lines->set_projection(projection);
}
}
float t = 0.0f;
glm::vec3 z_axis(0.0f, 0.0f, 1.0f);
glm::vec3 x_axis(1.0f, 0.0f, 0.0f);
while (!window.value()->should_close_or_invalid())
{
if (!rotation_delta)
rotation.x += 0.02f;
glm::mat4 model = glm::rotate(
glm::rotate(glm::identity<glm::mat4>(), rotation.y, x_axis),
rotation.x,
z_axis
);
for (auto &lines_pair : all_lines)
for (auto &lines : lines_pair)
lines->set_model(model);
for (int i = 0; i < static_cast<int>(all_lines.size()); ++i)
{
update_lines_data(width, i - width_half, all_lines[i][0], t, true);
update_lines_data(width, i - width_half, all_lines[i][1], t, false);
}
t += 0.1f;
auto rendered_scene = scene.value()->render();
video.value()->render(rendered_scene);
window.value()->render(rendered_scene);
ev::poll_window_events();
}
return EXIT_SUCCESS;
}