-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSavageCubeMatrix.h
123 lines (94 loc) · 2.42 KB
/
SavageCubeMatrix.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
#pragma once
#include "BH3D_Drawable.hpp"
enum CubeStatus
{
CUBESTATUS_EMPTY,
CUBESTATUS_FULL,
CUBESTATUS_MAGIC,
CUBESTATUS_UNDESTRUCTIBLE,
CUBESTATUS_COUNT
};
struct CubeLogic
{
//Constant variable in time
int m_id = 0;
int m_status = 0;
glm::ivec3 m_position;
//variable in time
glm::mat4 m_translate;
};
struct Animation
{
float m_durations = 15.0f; //! Duration in sec of the animation
};
struct RotationAnimation : public Animation
{
float m_angle = 0;
float m_speed = glm::half_pi<float>() / m_durations;
glm::vec3 m_axe = { 1.0, 0.0, 0.0 }; //! Rotation axes
inline auto compute(float elapse_time) {
float elapse = m_speed * elapse_time;
m_angle += elapse;
return glm::rotate(glm::mat4(1.0f), m_angle, m_axe);
}
void reset() {
float m_angle = 0;
m_speed = glm::half_pi<float>() / m_durations;
}
};
struct TranslationAnimation : public Animation
{
glm::vec3 m_position = { 0.0, 0.0, 0.0 };
float m_speed = 1.0f / m_durations;
glm::vec3 m_direction = { 0.0, 0.0, 1.0 };
inline auto compute(float elapse_time) {
float elapse = m_speed * elapse_time;
m_position += (elapse * m_direction);
return glm::translate(glm::mat4(1.0f), m_position);
}
void reset() {
m_position = { 0.0, 0.0, 0.0 };
float m_speed = 1.0f / m_durations;
}
};
class SavageCubeMatrix : public bh3d::Drawable
{
glm::vec3 m_cube_size = { 0.99f, 0.25f, 0.99f };
int m_cols = 8;
int m_rows = 32;
bh3d::Texture m_texture;
std::vector<CubeLogic> m_vCubeLogics;
std::vector<bh3d::Texture> m_vTextures;
RotationAnimation m_rotationAnimation;
TranslationAnimation m_translationAnimation;
public:
SavageCubeMatrix() {}
SavageCubeMatrix(const glm::vec3 & cube_size) :
m_cube_size(cube_size)
{}
void Init(int rows, int cols);
void Draw(const glm::mat4& mvp) override
{
m_mesh.BindMaterial(0);
m_mesh.BindVBO();
for (auto& cube : m_vCubeLogics)
{
m_shader(mvp * cube.m_translate);
m_mesh.DrawSubMeshElements(0);
}
}
void DrawAnimation(const glm::mat4& mvp, float elapse_time = 1.0f / 60.0f)
{
auto rotation_mat = m_rotationAnimation.compute(elapse_time);
auto translation_mat = m_translationAnimation.compute(elapse_time);
auto anim_mat = translation_mat * rotation_mat;
m_mesh.BindVBO();
for (auto& cube : m_vCubeLogics)
{
assert(cube.m_status < m_vTextures.size());
m_vTextures[cube.m_status].Bind();
m_shader(mvp * cube.m_translate * anim_mat);
m_mesh.DrawSubMeshElements(0);
}
}
};