-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube_example.cpp
More file actions
101 lines (85 loc) · 3.43 KB
/
Copy pathcube_example.cpp
File metadata and controls
101 lines (85 loc) · 3.43 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
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <yadl/canvas.hpp>
#include <yadl/context.hpp>
#include <yadl/io.hpp>
#include <yadl/shape.hpp>
#include <yadl/animation.hpp>
int main(int argc, char const *argv[])
{
using namespace yadl;
(void)argc;
(void)argv;
glm::vec3 vertices[] = {
glm::vec3(-0.5f, -0.5f, -0.5f),
glm::vec3(0.5f, -0.5f, -0.5f),
glm::vec3(0.5f, 0.5f, -0.5f),
glm::vec3(-0.5f, 0.5f, -0.5f),
glm::vec3(-0.5f, -0.5f, 0.5f),
glm::vec3(0.5f, -0.5f, 0.5f),
glm::vec3(0.5f, 0.5f, 0.5f),
glm::vec3(-0.5f, 0.5f, 0.5f)};
glm::ivec2 edges[] = {
glm::ivec2(0, 1),
glm::ivec2(1, 2),
glm::ivec2(2, 3),
glm::ivec2(3, 0),
glm::ivec2(4, 5),
glm::ivec2(5, 6),
glm::ivec2(6, 7),
glm::ivec2(7, 4),
glm::ivec2(0, 4),
glm::ivec2(1, 5),
glm::ivec2(2, 6),
glm::ivec2(3, 7)};
const int width = 300;
const int height = 300;
const int line_width = std::min(width, height) / 300;
const int circle_radius = std::min(width, height) / 100;
const float scale = 130.0f;
const float center_x = width / 2.0f;
const float center_y = height / 2.0f;
const float fov = glm::radians(90.0f);
const int frame_count = 300;
const int frame_delay = 2;
Canvas canvas(width, height);
Animation animation(canvas.GetWidth(), canvas.GetHeight());
canvas.Clear(Color::Dark);
Context ctx(canvas);
for (int i = 0; i < frame_count; i++)
{
float angle = i * 2.0f * glm::pi<float>() / frame_count;
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(1.0f, 0.0f, 0.0f)); // x
rotation = glm::rotate(rotation, angle, glm::vec3(0.0f, 1.0f, 0.0f)); // y
rotation = glm::rotate(rotation, angle, glm::vec3(0.0f, 0.0f, 1.0f)); // z
glm::mat4 translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 3.0f));
glm::mat4 model = translation * rotation;
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projection = glm::perspective(fov, (float)width / (float)height, 0.1f, 100.0f);
glm::mat4 mvp = projection * view * model;
canvas.Clear(Color::Dark);
ctx.SetColor(Color::White);
for (int i = 0; i < 12; ++i)
{
glm::vec4 v1 = mvp * glm::vec4(vertices[edges[i].x], 1.0f);
glm::vec4 v2 = mvp * glm::vec4(vertices[edges[i].y], 1.0f);
ctx.SetPosition(v1.x * scale + center_x, v1.y * scale + center_y).SetThickness(line_width);
shape::DrawLine(ctx, v2.x * scale + center_x, v2.y * scale + center_y);
}
ctx.SetColor(Color::Red);
for (int i = 0; i < 8; ++i)
{
glm::vec4 v = mvp * glm::vec4(vertices[i], 1.0f);
ctx.SetPosition(v.x * scale + center_x, v.y * scale + center_y);
shape::DrawFilledCircle(ctx, circle_radius);
}
std::cout << "Rendering frame " << i + 1 << "/" << frame_count << std::endl;
animation.AddFrame(canvas);
}
std::cout << "Saving animation as GIF" << std::endl;
io::SaveAsGIF("cube.gif", animation, frame_delay);
std::cout << "Saving animation as Y4M" << std::endl;
io::SaveAsY4M("cube.y4m", animation, 100/frame_delay);
return 0;
}