Botcraft 26.3
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
31 namespace
32 {
33 glm::mat4 ComputeModelMatrix(const FaceTransformation& transformations, const Orientation orientation);
34 }
35
37 {
38
39 }
40
41 Face::Face(const FaceTransformation& transformations, const Orientation orientation)
42 {
43 const glm::mat4 m = ComputeModelMatrix(transformations, orientation);
44
45 // Check scales for back culling of flat plane
46 unsigned int display_back_face = 1;
47 for (int i = 0; i < transformations.scales.size(); ++i)
48 {
49 if (abs(transformations.scales[i]->axis_x) < 0.001f ||
50 abs(transformations.scales[i]->axis_y) < 0.001f ||
51 abs(transformations.scales[i]->axis_z) < 0.001f)
52 {
53 display_back_face = 0;
54 }
55 }
56
57 unsigned char texture_rotation = transformations.rotation & 0x03;
58
59 const float* model_ptr = glm::value_ptr(m);
60
61 for (int i = 0; i < 16; ++i)
62 {
63 model_matrix[i] = model_ptr[i];
64 }
65
67 static_cast<float>(transformations.offset_x1),
68 static_cast<float>(transformations.offset_y2),
69 static_cast<float>(transformations.offset_x2),
70 static_cast<float>(transformations.offset_y1)
71 };
73
74 texture_data = 0;
75 texture_data = (texture_rotation << 3) | (display_back_face << 2);
76 }
77
78 void Face::SetTransparencyData(const Transparency transparency)
79 {
80 // 0 0 means opaque
81 // 0 1 means fully transparent
82 // 1 1 means partially transparent
83 switch (transparency)
84 {
86 texture_data &= ~3UL;
87 break;
89 texture_data |= 1UL;
90 texture_data &= ~2UL;
91 break;
93 texture_data |= 3UL;
94 break;
95 }
96 }
97
99 {
100 switch (texture_data & 3UL)
101 {
102 case 0:
104 case 1:
105 return Transparency::Total;
106 case 3:
108 default:
110 }
111 }
112
113 void Face::SetDisplayBackface(const bool display_backface)
114 {
115 if (display_backface)
116 {
117 texture_data |= 1Ul << 2;
118 }
119 else
120 {
121 texture_data &= ~(1U << 2);
122 }
123 }
124
125 const std::array<float, 16>& Face::GetMatrix() const
126 {
127 return model_matrix;
128 }
129
130 std::array<float, 16>& Face::GetMatrix()
131 {
132 return model_matrix;
133 }
134
135 void Face::SetTextureMultipliers(const std::array<unsigned int, 2>& mult)
136 {
137 texture_multipliers = mult;
138 }
139
140 const std::array<float, 4>& Face::GetTextureCoords(const bool overlay) const
141 {
142 if (overlay)
143 {
145 }
146 return texture_coords;
147 }
148
149 void Face::SetTextureCoords(const std::array<float, 4>& coords, const bool overlay)
150 {
151 if (overlay)
152 {
153 texture_coords_overlay = coords;
154
155 texture_data |= 1Ul << 5;
156 }
157 else
158 {
159 texture_coords = coords;
160 }
161 }
162
163 void Face::UpdateMatrix(const FaceTransformation& transformations, const Orientation orientation)
164 {
165 const glm::mat4 m = ComputeModelMatrix(transformations, orientation);
166
167 const float* model_ptr = glm::value_ptr(m);
168 for (int i = 0; i < 16; ++i)
169 {
170 model_matrix[i] = model_ptr[i];
171 }
172 }
173
174 namespace
175 {
176 glm::mat4 ComputeModelMatrix(const FaceTransformation& transformations, const Orientation orientation)
177 {
178 IMatrix model;
179 for (const TransformationPtr& t : transformations.translations)
180 {
181 t->ApplyTransformation(model);
182 }
183 for (const TransformationPtr& r : transformations.rotations)
184 {
185 r->ApplyTransformation(model);
186 }
187 for (const ScalePtr& s : transformations.scales)
188 {
189 s->ApplyTransformation(model);
190 }
191
192 // Apply the transformations to get the good face from the base one
193 switch (orientation)
194 {
197 // No rotation needed
198 break;
199 case Orientation::Top:
200 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f));
201 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
202 break;
204 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
205 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
206 break;
208 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
209 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
210 break;
212 model.m = glm::rotate(model.m, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
213 model.m = glm::rotate(model.m, glm::radians(180.0f), glm::vec3(0.0f, 1.0f, 0.0f));
214 break;
216 model.m = glm::rotate(model.m, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
217 break;
218 }
219
220 return model.m;
221 }
222 }
223 } // Renderer
224} // Botcraft
std::array< float, 4 > texture_coords_overlay
Definition Face.hpp:57
void SetTransparencyData(const Transparency transparency)
Definition Face.cpp:78
const std::array< float, 16 > & GetMatrix() const
Definition Face.cpp:125
std::array< unsigned int, 2 > texture_multipliers
Definition Face.hpp:64
void SetTextureMultipliers(const std::array< unsigned int, 2 > &mult)
Definition Face.cpp:135
static const std::vector< float > base_face
Definition Face.hpp:48
const std::array< float, 4 > & GetTextureCoords(const bool overlay) const
Definition Face.cpp:140
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:113
void SetTextureCoords(const std::array< float, 4 > &coords, const bool overlay)
Definition Face.cpp:149
Transparency GetTransparencyData() const
Definition Face.cpp:98
void UpdateMatrix(const FaceTransformation &transformations, const Orientation orientation)
Definition Face.cpp:163
Transparency
Transparency values for textures.
Definition Enums.hpp:9
std::shared_ptr< Scale > ScalePtr
std::shared_ptr< Transformation > TransformationPtr
IMatrix(const glm::mat4 &m_=glm::mat4(1.0f))
Definition Face.cpp:15