Botcraft 1.21.4
Loading...
Searching...
No Matches
Face.cpp
Go to the documentation of this file.
3
4#include <glm/glm.hpp>
5#include <glm/gtc/matrix_transform.hpp>
6#include <glm/gtc/type_ptr.hpp>
7
8namespace Botcraft
9{
10 namespace Renderer
11 {
12 //To avoid including glm in public header
13 struct IMatrix
14 {
15 IMatrix(const glm::mat4& m_ = glm::mat4(1.0f))
16 {
17 m = m_;
18 }
19
20 glm::mat4 m;
21 };
22
23 const std::vector<float> Face::base_face = {
24 // x y z
25 -0.9999f, -0.9999f, -0.9999f,
26 0.9999f, -0.9999f, -0.9999f,
27 -0.9999f, -0.9999f, 0.9999f,
28 0.9999f, -0.9999f, 0.9999f
29 };
30
32 {
33
34 }
35
36 Face::Face(const FaceTransformation& transformations, const Orientation orientation)
37 {
38 IMatrix model;
39
40 //Apply the translations of the model
41 for (int i = 0; i < transformations.translations.size(); ++i)
42 {
43 transformations.translations[i]->ApplyTransformation(model);
44 }
45
46 //Apply the rotations
47 for (int i = 0; i < transformations.rotations.size(); ++i)
48 {
49 transformations.rotations[i]->ApplyTransformation(model);
50 }
51
52 //Apply the scales
53 unsigned int display_back_face = 1;
54 for (int i = 0; i < transformations.scales.size(); ++i)
55 {
56 transformations.scales[i]->ApplyTransformation(model);
57 if (abs(transformations.scales[i]->axis_x) < 0.001f ||
58 abs(transformations.scales[i]->axis_y) < 0.001f ||
59 abs(transformations.scales[i]->axis_z) < 0.001f)
60 {
61 display_back_face = 0;
62 }
63 }
64
65 //Apply the transformations to get the good face from the base one
66 switch (orientation)
67 {
70 // No rotation needed
71 break;
73 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f));
74 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
75 break;
77 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
78 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
79 break;
81 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
82 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
83 break;
85 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
86 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
87 break;
89 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
90 break;
91 }
92
93 unsigned char texture_rotation = transformations.rotation;
94 while (texture_rotation < 0)
95 {
96 texture_rotation += 4;
97 }
98 while (texture_rotation > 3)
99 {
100 texture_rotation -= 4;
101 }
102
103 const float* model_ptr = glm::value_ptr(model.m);
104
105 for (int i = 0; i < 16; ++i)
106 {
107 model_matrix[i] = model_ptr[i];
108 }
109
110 texture_coords = {
111 static_cast<float>(transformations.offset_x1),
112 static_cast<float>(transformations.offset_y2),
113 static_cast<float>(transformations.offset_x2),
114 static_cast<float>(transformations.offset_y1)
115 };
117
118 texture_data = 0;
119 texture_data = (texture_rotation << 3) | (display_back_face << 2);
120 }
121
123 {
124 // 0 0 means opaque
125 // 0 1 means fully transparent
126 // 1 1 means partially transparent
127 switch (transparency)
128 {
130 texture_data &= ~3UL;
131 break;
133 texture_data |= 1UL;
134 texture_data &= ~2UL;
135 break;
137 texture_data |= 3UL;
138 break;
139 }
140 }
141
143 {
144 switch (texture_data & 3UL)
145 {
146 case 0:
148 case 1:
149 return Transparency::Total;
150 case 3:
152 default:
154 }
155 }
156
157 void Face::SetDisplayBackface(const bool display_backface)
158 {
159 if (display_backface)
160 {
161 texture_data |= 1Ul << 2;
162 }
163 else
164 {
165 texture_data &= (~1Ul) << 2;
166 }
167 }
168
169 const std::array<float, 16>& Face::GetMatrix() const
170 {
171 return model_matrix;
172 }
173
174 std::array<float, 16>& Face::GetMatrix()
175 {
176 return model_matrix;
177 }
178
179 void Face::SetTextureMultipliers(const std::array<unsigned int, 2>& mult)
180 {
181 texture_multipliers = mult;
182 }
183
184 const std::array<float, 4>& Face::GetTextureCoords(const bool overlay) const
185 {
186 if (overlay)
187 {
189 }
190 return texture_coords;
191 }
192
193 void Face::SetTextureCoords(const std::array<float, 4>& coords, const bool overlay)
194 {
195 if (overlay)
196 {
197 texture_coords_overlay = coords;
198
199 texture_data |= 1Ul << 5;
200 }
201 else
202 {
203 texture_coords = coords;
204 }
205 }
206
207 void Face::UpdateMatrix(const FaceTransformation& transformations, const Orientation orientation)
208 {
209 IMatrix model;
210
211 //Apply the translations of the model
212 for (int i = 0; i < transformations.translations.size(); ++i)
213 {
214 transformations.translations[i]->ApplyTransformation(model);
215 }
216
217 //Apply the rotations
218 for (int i = 0; i < transformations.rotations.size(); ++i)
219 {
220 transformations.rotations[i]->ApplyTransformation(model);
221 }
222
223 //Apply the scales
224 for (int i = 0; i < transformations.scales.size(); ++i)
225 {
226 transformations.scales[i]->ApplyTransformation(model);
227 }
228
229 //Apply the transformations to get the good face from the base one
230 switch (orientation)
231 {
234 // No rotation needed
235 case Orientation::Top:
236 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f));
237 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
238 break;
240 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
241 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
242 break;
244 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
245 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
246 break;
248 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
249 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
250 break;
252 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
253 break;
254 }
255
256 const float* model_ptr = glm::value_ptr(model.m);
257
258 for (int i = 0; i < 16; ++i)
259 {
260 model_matrix[i] = model_ptr[i];
261 }
262 }
263 } // Renderer
264} // Botcraft
std::array< float, 4 > texture_coords_overlay
Definition Face.hpp:57
void SetTransparencyData(const Transparency transparency)
Definition Face.cpp:122
const std::array< float, 16 > & GetMatrix() const
Definition Face.cpp:169
std::array< unsigned int, 2 > texture_multipliers
Definition Face.hpp:64
void SetTextureMultipliers(const std::array< unsigned int, 2 > &mult)
Definition Face.cpp:179
static const std::vector< float > base_face
Definition Face.hpp:48
const std::array< float, 4 > & GetTextureCoords(const bool overlay) const
Definition Face.cpp:184
unsigned int texture_data
Definition Face.hpp:61
std::array< float, 4 > texture_coords
Definition Face.hpp:56
std::array< float, 16 > model_matrix
Definition Face.hpp:52
void SetDisplayBackface(const bool display_backface)
Definition Face.cpp:157
void SetTextureCoords(const std::array< float, 4 > &coords, const bool overlay)
Definition Face.cpp:193
Transparency GetTransparencyData() const
Definition Face.cpp:142
void UpdateMatrix(const FaceTransformation &transformations, const Orientation orientation)
Definition Face.cpp:207
Transparency
Transparency values for textures.
Definition Enums.hpp:9
std::vector< TransformationPtr > rotations
std::vector< TransformationPtr > translations
IMatrix(const glm::mat4 &m_=glm::mat4(1.0f))
Definition Face.cpp:15