VOX
A little voxel engine
Loading...
Searching...
No Matches
Model.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4#include "logger.hpp"
5#include "Transform.hpp"
6
7#include <glm/glm.hpp>
8
9#include <vulkan/vulkan.h>
10
11#include <vector>
12
14{
15
16public:
17
18 struct Vertex
19 {
20 glm::vec3 pos;
21 glm::vec3 normal;
22 glm::vec2 texCoord;
23
24 static VkVertexInputBindingDescription getBindingDescription()
25 {
26 VkVertexInputBindingDescription bindingDescription{};
27 bindingDescription.binding = 0;
28 bindingDescription.stride = sizeof(Vertex);
29 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
30
31 return bindingDescription;
32 }
33
34 static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions()
35 {
36 std::vector<VkVertexInputAttributeDescription> attributeDescriptions(3);
37
38 attributeDescriptions[0].binding = 0;
39 attributeDescriptions[0].location = 0;
40 attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
41 attributeDescriptions[0].offset = offsetof(Vertex, pos);
42
43 attributeDescriptions[1].binding = 0;
44 attributeDescriptions[1].location = 1;
45 attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
46 attributeDescriptions[1].offset = offsetof(Vertex, normal);
47
48 attributeDescriptions[2].binding = 0;
49 attributeDescriptions[2].location = 2;
50 attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
51 attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
52
53 return attributeDescriptions;
54 }
55 };
56
59
60 PlayerModel(PlayerModel & other) = delete;
61 PlayerModel(PlayerModel && other) = delete;
62 PlayerModel & operator=(PlayerModel & other) = delete;
63 PlayerModel & operator=(PlayerModel && other) = delete;
64
65 constexpr static inline double player_height = 1.8;
66 constexpr static inline double unit = player_height / 32.0;
67
68 constexpr static inline glm::dvec3 chest_size = glm::dvec3(8.0, 12.0, 4.0) * unit;
69 constexpr static inline glm::dvec3 head_size = glm::dvec3(8.0, 8.0, 8.0) * unit;
70 constexpr static inline glm::dvec3 leg_size = glm::dvec3(4.0, 12.0, 4.0) * unit;
71 constexpr static inline glm::dvec3 arm_size = glm::dvec3(4.0, 12.0, 4.0) * unit;
72
73 // static inline glm::dvec3 chest_size = glm::dvec3(0.45, 0.675, 0.225);
74 // static inline glm::dvec3 head_size = glm::dvec3(0.45, 0.45, 0.45);
75 // static inline glm::dvec3 leg_size = glm::dvec3(0.225, 0.675, 0.225);
76 // static inline glm::dvec3 arm_size = glm::dvec3(0.225, 0.675, 0.225);
77
78 constexpr static inline glm::vec3 chest_pos = glm::vec3{0.0, leg_size.y, 0.0};
79 constexpr static inline glm::vec3 head_pos = glm::vec3{0.0, chest_size.y, 0.0};
80 constexpr static inline glm::vec3 left_leg_pos = glm::vec3{-leg_size.x / 2, 0.0, 0.0};
81 constexpr static inline glm::vec3 right_leg_pos = glm::vec3{leg_size.x / 2, 0.0, 0.0};
82 constexpr static inline glm::vec3 left_arm_pos = glm::vec3{-(arm_size.x / 2 + chest_size.x / 2), 0.0, 0.0};
83 constexpr static inline glm::vec3 right_arm_pos = glm::vec3{(arm_size.x / 2 + chest_size.x / 2), 0.0, 0.0};
84
86 {
87
88 public:
89
90 WalkAnimation() = default;
91 ~WalkAnimation() = default;
92
93 void start()
94 {
95 m_is_active = true;
96 m_start_time = std::chrono::steady_clock::now().time_since_epoch();
97 m_should_stop = false;
98 m_direction = 1.0;
99 }
100
101 void stop()
102 {
103 m_should_stop = true;
104 }
105
106 double time_since_start() const
107 {
108 return static_cast<double>((std::chrono::steady_clock::now().time_since_epoch() - m_start_time).count()) / 1e9;
109 }
110
111 bool isActive() const
112 {
113 return m_is_active;
114 }
115
116 double angle() const
117 {
118 return m_direction * amplitude_x * glm::sin(frequency * time_since_start());
119 }
120
121 void update()
122 {
123 if (time_since_start() >= duration_s)
124 {
125 if (m_should_stop)
126 {
127 m_is_active = false;
128 m_should_stop = false;
129 }
130 else
131 {
132 m_start_time = std::chrono::steady_clock::now().time_since_epoch();
133 m_direction = -m_direction;
134 }
135 }
136 }
137
138 private:
139
140 bool m_is_active = false;
141 bool m_should_stop = false;
142 int m_direction = 1;
143 std::chrono::nanoseconds m_start_time = std::chrono::nanoseconds(0);
144
145 constexpr static inline double amplitude_x = 0.5;
146
147 constexpr static inline double duration_s = 0.35;
148
149 constexpr static inline double frequency = glm::pi<double>() / duration_s;
150
151 };
152
154 {
155
156 public:
157
158 AttackAnimation() = default;
159 ~AttackAnimation() = default;
160
161 void start()
162 {
163 m_is_active = true;
164 m_start_time = std::chrono::steady_clock::now().time_since_epoch();
165 m_should_stop = true;
166 }
167
168 void stop()
169 {
170 m_should_stop = true;
171 }
172
173 double time_since_start() const
174 {
175 return static_cast<double>((std::chrono::steady_clock::now().time_since_epoch() - m_start_time).count()) / 1e9;
176 }
177
178 bool isActive() const
179 {
180 return m_is_active;
181 }
182
183 double angleX() const
184 {
185 return amplitude_x * glm::sin(frequency * time_since_start());
186 }
187
188 double angleY() const
189 {
190 return amplitude_y * glm::sin(frequency * time_since_start() * 2.0);
191 }
192
193 double angleZ() const
194 {
195 return amplitude_z * glm::sin(frequency * time_since_start() * 2.0);
196 }
197
198 void update()
199 {
200 if (time_since_start() >= duration_s)
201 {
202 if (m_should_stop)
203 {
204 m_is_active = false;
205 m_should_stop = false;
206 }
207 else
208 {
209 m_start_time = std::chrono::steady_clock::now().time_since_epoch();
210 }
211 }
212 }
213
214 private:
215
216 bool m_is_active = false;
217 bool m_should_stop = false;
218 std::chrono::nanoseconds m_start_time = std::chrono::nanoseconds(0);
219
220 constexpr static inline double amplitude_x = 1.0;
221 constexpr static inline double amplitude_y = -0.8;
222 constexpr static inline double amplitude_z = 0.3;
223
224 constexpr static inline double duration_s = 0.2;
225
226 constexpr static inline double frequency = glm::pi<double>() / duration_s;
227
228 };
229
230};
Definition: Model.hpp:154
double time_since_start() const
Definition: Model.hpp:173
double angleY() const
Definition: Model.hpp:188
double angleX() const
Definition: Model.hpp:183
void update()
Definition: Model.hpp:198
double angleZ() const
Definition: Model.hpp:193
void stop()
Definition: Model.hpp:168
void start()
Definition: Model.hpp:161
bool isActive() const
Definition: Model.hpp:178
Definition: Model.hpp:86
void update()
Definition: Model.hpp:121
void stop()
Definition: Model.hpp:101
void start()
Definition: Model.hpp:93
double time_since_start() const
Definition: Model.hpp:106
double angle() const
Definition: Model.hpp:116
bool isActive() const
Definition: Model.hpp:111
Definition: Model.hpp:14
PlayerModel()
Definition: Model.hpp:57
static constexpr glm::dvec3 arm_size
Definition: Model.hpp:71
static constexpr glm::vec3 right_leg_pos
Definition: Model.hpp:81
static constexpr double unit
Definition: Model.hpp:66
static constexpr glm::dvec3 chest_size
Definition: Model.hpp:68
PlayerModel & operator=(PlayerModel &other)=delete
static constexpr glm::vec3 left_leg_pos
Definition: Model.hpp:80
static constexpr glm::dvec3 head_size
Definition: Model.hpp:69
static constexpr glm::vec3 right_arm_pos
Definition: Model.hpp:83
PlayerModel & operator=(PlayerModel &&other)=delete
PlayerModel(PlayerModel &&other)=delete
static constexpr glm::vec3 head_pos
Definition: Model.hpp:79
~PlayerModel()
Definition: Model.hpp:58
static constexpr glm::dvec3 leg_size
Definition: Model.hpp:70
static constexpr glm::vec3 chest_pos
Definition: Model.hpp:78
static constexpr double player_height
Definition: Model.hpp:65
static constexpr glm::vec3 left_arm_pos
Definition: Model.hpp:82
PlayerModel(PlayerModel &other)=delete
Definition: Model.hpp:19
glm::vec3 normal
Definition: Model.hpp:21
static std::vector< VkVertexInputAttributeDescription > getAttributeDescriptions()
Definition: Model.hpp:34
glm::vec2 texCoord
Definition: Model.hpp:22
static VkVertexInputBindingDescription getBindingDescription()
Definition: Model.hpp:24
glm::vec3 pos
Definition: Model.hpp:20