/src/client/characters.cc
Line | Count | Source |
1 | | #include "client/characters.h" |
2 | | |
3 | | #include <external/cgltf.h> |
4 | | #include <raymath.h> |
5 | | |
6 | | #include <algorithm> |
7 | | #include <array> |
8 | | #include <cctype> |
9 | | #include <cmath> |
10 | | #include <string> |
11 | | #include <string_view> |
12 | | #include <vector> |
13 | | |
14 | | #include "absl/strings/str_cat.h" |
15 | | #include "client/figures.h" |
16 | | #include "client/rig.h" |
17 | | #include "content/figures.h" |
18 | | #include "content/rig.h" |
19 | | |
20 | | namespace nanommo { |
21 | | |
22 | | namespace { |
23 | | |
24 | | // In the protocol's order of motions, with attac for the skeleton's attack, |
25 | | // whose name raylib cuts at 31 characters. |
26 | | constexpr std::array<const char*, 5> kClips = {"idle", "walk", "run", "attac", |
27 | | "death"}; |
28 | | |
29 | | constexpr std::string_view kRightHand = "RightHand"; |
30 | | constexpr std::string_view kLeftFoot = "LeftFoot"; |
31 | | constexpr std::string_view kRightFoot = "RightFoot"; |
32 | | |
33 | | // The middle of the fist, from the wrist: along the fingers, and a little |
34 | | // towards the palm, in metres. |
35 | | constexpr Vector3 kFist = {-0.06f, -0.01f, 0.0f}; |
36 | | |
37 | | // The light a blow's flash gives off at its brightest, on top of the sun's, |
38 | | // and a copy left behind's: enough to bloom. |
39 | | constexpr float kFlash = 2.5f; |
40 | | constexpr float kGhostGlow = 1.5f; |
41 | | |
42 | | // raylib cuts clip names at 31 characters, so the skeleton's run ends in |
43 | | // Runni, which still has the word run in it. |
44 | 450 | bool HasWord(std::string name, std::string_view word) { |
45 | 13.8k | std::ranges::transform(name, name.begin(), [](unsigned char c) { |
46 | 13.8k | return static_cast<char>(std::tolower(c)); |
47 | 13.8k | }); |
48 | 450 | return name.contains(word); |
49 | 450 | } |
50 | | |
51 | | // raylib samples glTF clips 60 times a second, and the last frame is the |
52 | | // first again, so a loop skips it, and the death stops just before it. |
53 | 18 | float FrameAt(const ModelAnimation& clip, float time, bool loops) { |
54 | 18 | const int frames = clip.keyframeCount > 1 ? clip.keyframeCount - 1 : 10 ; |
55 | 18 | if (!loops) { |
56 | 3 | return std::min(time * 60.0f, static_cast<float>(frames - 1)); |
57 | 3 | } |
58 | 15 | return std::fmod(time * 60.0f, static_cast<float>(frames)); |
59 | 18 | } |
60 | | |
61 | | // glTF stores a matrix a column at a time, and raylib's Matrix lists its |
62 | | // fields a row at a time. |
63 | 432 | Matrix FromGltf(const std::array<float, 16>& m) { |
64 | 432 | return {m[0], m[4], m[8], m[12], m[1], m[5], m[9], m[13], |
65 | 432 | m[2], m[6], m[10], m[14], m[3], m[7], m[11], m[15]}; |
66 | 432 | } |
67 | | |
68 | | // raylib takes the pose the bones rest in as the one the mesh was bound in, |
69 | | // but the skeleton's arms rest about 70 degrees away from it. glTF keeps the |
70 | | // bind pose as each bone's inverse bind matrix, so we read it from those. |
71 | 18 | void ReadBindPose(const std::string& path, Model& model) { |
72 | 18 | cgltf_options options{}; |
73 | 18 | cgltf_data* data = nullptr; |
74 | 18 | if (cgltf_parse_file(&options, path.c_str(), &data) != cgltf_result_success) { |
75 | 0 | return; |
76 | 0 | } |
77 | 18 | const cgltf_node* mesh = nullptr; |
78 | 378 | for (cgltf_size i = 0; i < data->nodes_count && mesh == nullptr360 ; ++i360 ) { |
79 | 360 | if (data->nodes[i].skin != nullptr) { |
80 | 18 | mesh = &data->nodes[i]; |
81 | 18 | } |
82 | 360 | } |
83 | 18 | if (mesh != nullptr && mesh->skin->inverse_bind_matrices != nullptr && |
84 | 18 | cgltf_load_buffers(&options, data, path.c_str()) == |
85 | 18 | cgltf_result_success) { |
86 | 18 | std::array<float, 16> world{}; |
87 | 18 | cgltf_node_transform_world(mesh, world.data()); |
88 | | // raylib moves the mesh to where its node puts it in the world as it |
89 | | // loads it, so the bind pose goes there too. |
90 | 234 | for (int i = 0; i < static_cast<int>(model.skeleton.boneCount); ++i216 ) { |
91 | 216 | std::array<float, 16> inverse{}; |
92 | 216 | if (cgltf_accessor_read_float(mesh->skin->inverse_bind_matrices, i, |
93 | 216 | inverse.data(), inverse.size())) { |
94 | 216 | const Matrix bind = |
95 | 216 | MatrixMultiply(MatrixInvert(FromGltf(inverse)), FromGltf(world)); |
96 | 216 | Transform& pose = model.skeleton.bindPose[i]; |
97 | 216 | MatrixDecompose(bind, &pose.translation, &pose.rotation, &pose.scale); |
98 | 216 | } |
99 | 216 | } |
100 | 18 | } |
101 | 18 | cgltf_free(data); |
102 | 18 | } |
103 | | |
104 | | } // namespace |
105 | | |
106 | 18 | std::string MonstersFolder() { |
107 | 18 | return std::string(GetApplicationDirectory()) + "monsters"; |
108 | 18 | } |
109 | | |
110 | 54 | std::string WeaponsFolder() { |
111 | 54 | return std::string(GetApplicationDirectory()) + "weapons"; |
112 | 54 | } |
113 | | |
114 | 0 | std::string ContentFolder() { |
115 | 0 | return std::string(GetApplicationDirectory()) + "content"; |
116 | 0 | } |
117 | | |
118 | | Characters::Characters(const rig::Rig& rig, Shader shader, |
119 | | const figures::Figures& figures) |
120 | 12 | : figures_(figures), |
121 | 12 | shader_(shader), |
122 | 12 | emission_(GetShaderLocation(shader, "emission")) { |
123 | | // The rig as it is, without a build. |
124 | 24 | for (int build = 0; build < std::max(rig.builds_size(), 1); ++build12 ) { |
125 | 18 | const std::vector<rig::Joint> joints = Skeleton(rig, build); |
126 | 18 | if (joints.empty()) { |
127 | 6 | return; |
128 | 6 | } |
129 | 12 | Character character{.model = RigModel(joints)}; |
130 | | // The body first, which the death keeps out of the ground. |
131 | 12 | Dress(character, figures); |
132 | 12 | character.clips = Bake(rig, character.model, &character.clip_count); |
133 | 12 | if (character.clip_count < 4) { |
134 | 0 | TraceLog(LOG_WARNING, "the rig doesn't have the legs it needs to move"); |
135 | 0 | Unload(character); |
136 | 0 | return; |
137 | 0 | } |
138 | | // A player's swing is a skill, with a move of its own, so it stands |
139 | | // without one. |
140 | 12 | character.clip_for = {0, 1, 2, 0, 3}; |
141 | 12 | Ready(character, shader); |
142 | 12 | characters_.push_back(character); |
143 | 12 | } |
144 | 12 | } |
145 | | |
146 | | Characters::Characters(const std::string& folder, |
147 | | const std::vector<std::string>& files, float height, |
148 | | Shader shader) |
149 | 18 | : shader_(shader), emission_(GetShaderLocation(shader, "emission")) { |
150 | 18 | for (const std::string& file : files) { |
151 | 18 | const std::string path = absl::StrCat(folder, "/", file); |
152 | 18 | if (!FileExists(path.c_str())) { |
153 | 0 | continue; |
154 | 0 | } |
155 | 18 | Character character{.model = LoadModel(path.c_str())}; |
156 | 18 | ReadBindPose(path, character.model); |
157 | 18 | character.clips = LoadModelAnimations(path.c_str(), &character.clip_count); |
158 | | |
159 | 108 | for (std::size_t motion = 0; motion < kClips.size(); ++motion90 ) { |
160 | 90 | int found = -1; |
161 | 540 | for (int i = 0; i < character.clip_count; ++i450 ) { |
162 | 450 | if (HasWord(character.clips[i].name, kClips[motion]) && |
163 | 450 | IsModelAnimationValid(character.model, character.clips[i])72 ) { |
164 | 72 | found = i; |
165 | 72 | } |
166 | 450 | } |
167 | 90 | character.clip_for[motion] = found; |
168 | 90 | } |
169 | | // Without a walk, a character walks with its run, played slower. The |
170 | | // skeleton's feet keep up with our walk at 0.4, and with our run at 0.7. |
171 | 18 | auto& [idle, walk, run, attack, death] = character.clip_for; |
172 | 18 | if (walk < 0 && run >= 0) { |
173 | 18 | walk = run; |
174 | 18 | character.speed[protocol::MOTION_WALK] = 0.4f; |
175 | 18 | character.speed[protocol::MOTION_RUN] = 0.7f; |
176 | 18 | } |
177 | | // Without an attack, it stands as it strikes. |
178 | 18 | if (attack < 0) { |
179 | 0 | attack = idle; |
180 | 0 | } |
181 | 18 | if (idle < 0 || walk < 0 || run < 0 || death < 0) { |
182 | 0 | TraceLog(LOG_WARNING, "%s doesn't have the clips we need", file.c_str()); |
183 | 0 | UnloadModelAnimations(character.clips, character.clip_count); |
184 | 0 | UnloadModel(character.model); |
185 | 0 | continue; |
186 | 0 | } |
187 | 18 | Ready(character, shader); |
188 | | // Sized by the model's own mesh, so a figure is as tall as the model. |
189 | 18 | const BoundingBox box = GetModelBoundingBox(character.model); |
190 | 18 | character.scale = height / (box.max.y - box.min.y); |
191 | 18 | character.hull = character.model; |
192 | 18 | characters_.push_back(character); |
193 | 18 | } |
194 | 18 | } |
195 | | |
196 | 30 | void Characters::Ready(Character& character, Shader shader) { |
197 | 78 | for (int i = 0; i < character.model.materialCount; ++i48 ) { |
198 | 48 | character.model.materials[i].shader = shader; |
199 | 48 | } |
200 | 30 | int left_foot = -1; |
201 | 30 | int right_foot = -1; |
202 | 534 | for (int i = 0; i < static_cast<int>(character.model.skeleton.boneCount); |
203 | 504 | ++i) { |
204 | 504 | const std::string_view bone = character.model.skeleton.bones[i].name; |
205 | 504 | if (bone == kRightHand) { |
206 | 12 | character.hand = i; |
207 | 492 | } else if (bone == kLeftFoot) { |
208 | 12 | left_foot = i; |
209 | 480 | } else if (bone == kRightFoot) { |
210 | 12 | right_foot = i; |
211 | 12 | } |
212 | 504 | } |
213 | 30 | for (const protocol::Motion motion : |
214 | 60 | {protocol::MOTION_WALK, protocol::MOTION_RUN}) { |
215 | 60 | character.footfalls[motion] = Footfalls( |
216 | 60 | character.clips[character.clip_for[motion]], left_foot, right_foot); |
217 | 60 | } |
218 | 30 | character.joints = FindJoints(character.model); |
219 | 30 | } |
220 | | |
221 | 12 | void Characters::Dress(Character& character, const figures::Figures& figures) { |
222 | 12 | character.hull = character.model; |
223 | 12 | if (figures.body().empty()) { |
224 | 0 | return; |
225 | 0 | } |
226 | 12 | const float metre = 1.0f / character.scale; |
227 | 12 | const std::vector<figures::Part> body = Unfold(figures.body()); |
228 | 12 | nanommo::Dress(character.model, body, metre); |
229 | | // The hull shares the model's bones, so the clips pose it too. |
230 | 12 | character.hull = character.model; |
231 | 12 | character.hull.meshes = static_cast<Mesh*>(MemAlloc(sizeof(Mesh))); |
232 | 12 | character.hull.meshes[0] = Hull(character.model, body, metre); |
233 | 72 | for (int i = 0; i < figures.hairs_size(); ++i60 ) { |
234 | 60 | if (figures.hairs(i).parts().empty()) { |
235 | 10 | continue; |
236 | 10 | } |
237 | | // Copies of the model, with the hair for their meshes, so they move with |
238 | | // its bones. |
239 | 50 | const std::vector<figures::Part> parts = Unfold(figures.hairs(i).parts()); |
240 | 50 | Garment hair{.model = character.model, .hull = character.model}; |
241 | 50 | hair.model.meshes = static_cast<Mesh*>(MemAlloc(sizeof(Mesh))); |
242 | 50 | hair.model.meshes[0] = Parts(character.model, parts, metre); |
243 | 50 | hair.hull.meshes = static_cast<Mesh*>(MemAlloc(sizeof(Mesh))); |
244 | 50 | hair.hull.meshes[0] = Hull(character.model, parts, metre); |
245 | 50 | character.hairs[i] = hair; |
246 | 50 | } |
247 | 12 | character.dressed = true; |
248 | 12 | } |
249 | | |
250 | 30 | Characters::~Characters() { |
251 | 30 | for (Character& character : characters_) { |
252 | 30 | Unload(character); |
253 | 30 | } |
254 | 30 | for (auto& [id, weapon] : weapons_) { |
255 | 12 | UnloadModel(weapon.model); |
256 | 12 | } |
257 | 30 | } |
258 | | |
259 | 30 | void Characters::Unload(Character& character) { |
260 | 68 | for (auto& [id, garment] : character.garments) { |
261 | 68 | UnloadMesh(garment.model.meshes[0]); |
262 | 68 | MemFree(garment.model.meshes); |
263 | 68 | UnloadMesh(garment.hull.meshes[0]); |
264 | 68 | MemFree(garment.hull.meshes); |
265 | 68 | } |
266 | 50 | for (auto& [i, hair] : character.hairs) { |
267 | 50 | UnloadMesh(hair.model.meshes[0]); |
268 | 50 | MemFree(hair.model.meshes); |
269 | 50 | UnloadMesh(hair.hull.meshes[0]); |
270 | 50 | MemFree(hair.hull.meshes); |
271 | 50 | } |
272 | 30 | if (character.dressed) { |
273 | 12 | UnloadMesh(character.hull.meshes[0]); |
274 | 12 | MemFree(character.hull.meshes); |
275 | 12 | } |
276 | 30 | UnloadModelAnimations(character.clips, character.clip_count); |
277 | 30 | UnloadModel(character.model); |
278 | 30 | } |
279 | | |
280 | 54 | void Characters::Hold(uint32_t id, const items::Look& look, Shader shader) { |
281 | 54 | const std::string path = WeaponsFolder() + "/" + look.model(); |
282 | 54 | if (look.model().empty() || !FileExists(path.c_str())12 ) { |
283 | 42 | return; |
284 | 42 | } |
285 | 12 | Model weapon = LoadModel(path.c_str()); |
286 | | // glTF colours are linear, and our shader writes them out as they are, so |
287 | | // we turn them into sRGB, or the sword comes out nearly black. |
288 | 84 | for (int i = 0; i < weapon.materialCount; ++i72 ) { |
289 | 72 | weapon.materials[i].shader = shader; |
290 | 72 | Color& color = weapon.materials[i].maps[MATERIAL_MAP_DIFFUSE].color; |
291 | 72 | const Vector4 linear = ColorNormalize(color); |
292 | 72 | color = ColorFromNormalized({std::pow(linear.x, 1.0f / 2.2f), |
293 | 72 | std::pow(linear.y, 1.0f / 2.2f), |
294 | 72 | std::pow(linear.z, 1.0f / 2.2f), linear.w}); |
295 | | // A tint makes another item of the same model, such as the Ash Blade of |
296 | | // the sword. |
297 | 72 | if (look.has_tint()) { |
298 | 48 | color = ColorTint(color, GetColor(look.tint())); |
299 | 48 | } |
300 | 72 | } |
301 | 12 | const BoundingBox box = GetModelBoundingBox(weapon); |
302 | 12 | const float height = box.max.y - box.min.y; |
303 | 12 | const float scale = look.length() / height; |
304 | 12 | const Matrix sized = MatrixMultiply( |
305 | 12 | MatrixTranslate(0.0f, -(box.min.y + look.grip() * height), 0.0f), |
306 | 12 | MatrixScale(scale, scale, scale)); |
307 | | // The rig's right hand rests with its fingers out to the right, the palm |
308 | | // down and the thumb ahead. The fist holds the blade out past the thumb, |
309 | | // leaning 20 degrees towards the fingers. |
310 | 12 | const Matrix along = MatrixMultiply(MatrixRotateX(90.0f * DEG2RAD), |
311 | 12 | MatrixRotateY(-20.0f * DEG2RAD)); |
312 | 12 | const Matrix in_hand = |
313 | 12 | MatrixMultiply(along, MatrixTranslate(kFist.x, kFist.y, kFist.z)); |
314 | | // The blade runs up the middle of the model, from the grip to the top. |
315 | 12 | const float middle_x = (box.min.x + box.max.x) / 2.0f; |
316 | 12 | const float middle_z = (box.min.z + box.max.z) / 2.0f; |
317 | 12 | weapons_[id] = { |
318 | 12 | .model = weapon, |
319 | 12 | .grip = MatrixMultiply(sized, in_hand), |
320 | 12 | .hilt = {middle_x, box.min.y + 1.5f * look.grip() * height, middle_z}, |
321 | 12 | .tip = {middle_x, box.max.y, middle_z}}; |
322 | 12 | } |
323 | | |
324 | 54 | void Characters::Wear(uint32_t id, const items::Look& look) { |
325 | 54 | if (look.parts().empty()) { |
326 | 20 | return; |
327 | 20 | } |
328 | 34 | const std::vector<figures::Part> parts = Unfold(look.parts()); |
329 | 68 | for (Character& character : characters_) { |
330 | 68 | const float metre = 1.0f / character.scale; |
331 | | // Copies of the model, with the item's parts for meshes, so they move |
332 | | // with its bones. |
333 | 68 | Garment garment{ |
334 | 68 | .model = character.model, .hull = character.model, .dyed = look.dyed()}; |
335 | 68 | garment.model.meshes = static_cast<Mesh*>(MemAlloc(sizeof(Mesh))); |
336 | 68 | garment.model.meshes[0] = Parts(character.model, parts, metre); |
337 | 68 | garment.hull.meshes = static_cast<Mesh*>(MemAlloc(sizeof(Mesh))); |
338 | 68 | garment.hull.meshes[0] = Hull(character.model, parts, metre); |
339 | 68 | character.garments[id] = garment; |
340 | 68 | } |
341 | 34 | } |
342 | | |
343 | 64 | void Characters::Learn(uint32_t skill, const skills::Look& look) { |
344 | 64 | if (!look.move().keys().empty()) { |
345 | 64 | moves_[skill] = look.move(); |
346 | 64 | } |
347 | 64 | } |
348 | | |
349 | | // A skill plays its move, over the character standing. |
350 | | Characters::Pose Characters::PoseFor(const Character& character, |
351 | 18 | protocol::Motion motion, float time) { |
352 | 18 | if (motion == protocol::MOTION_SKILL) { |
353 | 0 | motion = protocol::MOTION_IDLE; |
354 | 0 | } |
355 | 18 | const int index = motion >= 0 && motion < std::ssize(kClips) ? motion : 00 ; |
356 | 18 | const ModelAnimation& clip = character.clips[character.clip_for[index]]; |
357 | 18 | return {&clip, FrameAt(clip, time * character.speed[index], |
358 | 18 | index != protocol::MOTION_DEAD)}; |
359 | 18 | } |
360 | | |
361 | 1 | std::optional<Gait> Characters::GaitOf(const Figure& figure) const { |
362 | 1 | if (characters_.empty() || (figure.to != protocol::MOTION_WALK && |
363 | 1 | figure.to != protocol::MOTION_RUN0 )) { |
364 | 0 | return std::nullopt; |
365 | 0 | } |
366 | 1 | const Character& character = For(figure); |
367 | 1 | const ModelAnimation& clip = character.clips[character.clip_for[figure.to]]; |
368 | | // As FrameAt plays it, sixty frames a second, and the last is the first. |
369 | 1 | const float frames = static_cast<float>(std::max(clip.keyframeCount - 1, 1)); |
370 | 1 | return Gait{ |
371 | 1 | .loops = figure.to_time * character.speed[figure.to] * 60.0f / frames, |
372 | 1 | .footfalls = character.footfalls[figure.to]}; |
373 | 1 | } |
374 | | |
375 | 10 | const Characters::Character& Characters::For(const Figure& figure) const { |
376 | | // A player who chose their build has it. An NPC looks the same wherever it |
377 | | // stands, so it goes by its kind, and anyone else by their id. |
378 | 10 | uint32_t which = figure.npc ? figure.kind0 : figure.id; |
379 | 10 | if (figure.features && figure.features->build0 ) { |
380 | 0 | which = *figure.features->build; |
381 | 0 | } |
382 | 10 | return characters_[which % characters_.size()]; |
383 | 10 | } |
384 | | |
385 | | void Characters::PoseAs(const Character& character, |
386 | 9 | const Figure& figure) const { |
387 | 9 | const Pose from = PoseFor(character, figure.from, figure.from_time); |
388 | 9 | const Pose to = PoseFor(character, figure.to, figure.to_time); |
389 | | // Players share the model's bones, so we pose them right before each draw. |
390 | 9 | UpdateModelAnimationEx(character.model, *from.clip, from.frame, *to.clip, |
391 | 9 | to.frame, figure.blend); |
392 | 9 | const auto move = moves_.find(figure.to_skill); |
393 | 9 | if (figure.to == protocol::MOTION_SKILL && move != moves_.end()0 ) { |
394 | 0 | Model model = character.model; |
395 | 0 | Apply(BendAt(move->second, figure.to_time * 1000.0f), character.joints, |
396 | 0 | 1.0f / character.scale, model); |
397 | 0 | } |
398 | 9 | } |
399 | | |
400 | | // Turned, then tipped over towards its lean's way, about its feet, and up |
401 | | // off the ground by its lift. |
402 | 9 | Matrix Characters::Placed(const Character& character, const Figure& figure) { |
403 | 9 | const float scale = character.scale; |
404 | 9 | const Vector3 across{std::cos(figure.lean_yaw), 0.0f, |
405 | 9 | -std::sin(figure.lean_yaw)}; |
406 | 9 | return MatrixMultiply( |
407 | 9 | MatrixMultiply(MatrixMultiply(MatrixScale(scale, scale, scale), |
408 | 9 | MatrixRotateY(figure.yaw)), |
409 | 9 | MatrixRotate(across, figure.lean)), |
410 | 9 | MatrixTranslate(figure.position.x, figure.position.y + figure.lift, |
411 | 9 | figure.position.z)); |
412 | 9 | } |
413 | | |
414 | 0 | Matrix Characters::InHand(const Character& character, const Matrix& grip) { |
415 | 0 | const Transform& hand = character.model.currentPose[character.hand]; |
416 | 0 | const Matrix bone = MatrixMultiply( |
417 | 0 | MatrixMultiply(MatrixScale(hand.scale.x, hand.scale.y, hand.scale.z), |
418 | 0 | QuaternionToMatrix(hand.rotation)), |
419 | 0 | MatrixTranslate(hand.translation.x, hand.translation.y, |
420 | 0 | hand.translation.z)); |
421 | 0 | return MatrixMultiply(grip, bone); |
422 | 0 | } |
423 | | |
424 | | std::optional<std::pair<Vector3, Vector3>> Characters::Blade( |
425 | 0 | const Figure& figure) const { |
426 | 0 | const auto held = weapons_.find(figure.weapon); |
427 | 0 | if (characters_.empty() || held == weapons_.end()) { |
428 | 0 | return std::nullopt; |
429 | 0 | } |
430 | 0 | const Character& character = For(figure); |
431 | 0 | if (character.hand < 0) { |
432 | 0 | return std::nullopt; |
433 | 0 | } |
434 | 0 | PoseAs(character, figure); |
435 | 0 | const Matrix world = MatrixMultiply(InHand(character, held->second.grip), |
436 | 0 | Placed(character, figure)); |
437 | 0 | return std::pair{Vector3Transform(held->second.hilt, world), |
438 | 0 | Vector3Transform(held->second.tip, world)}; |
439 | 0 | } |
440 | | |
441 | | void Characters::Draw(const Figure& figure, Color color, |
442 | 9 | const Outline* outline) const { |
443 | 9 | const Character& character = For(figure); |
444 | 9 | PoseAs(character, figure); |
445 | 9 | const Matrix placed = Placed(character, figure); |
446 | | // A blow just landed lights it up. |
447 | 9 | const float flash = figure.flash * kFlash; |
448 | 9 | SetShaderValue(shader_, emission_, &flash, SHADER_UNIFORM_FLOAT); |
449 | | // Each with its outline first, at the figure's place, its turn and its |
450 | | // scale. |
451 | 20 | auto draw = [&](Model model, const Model& hull, Color tint) { |
452 | 20 | if (outline != nullptr) { |
453 | 0 | DrawOutline(hull, placed, outline->skinned); |
454 | 0 | } |
455 | 20 | model.transform = MatrixMultiply(model.transform, placed); |
456 | 20 | DrawModel(model, {0.0f, 0.0f, 0.0f}, 1.0f, tint); |
457 | 20 | }; |
458 | 9 | if (!character.dressed) { |
459 | 1 | draw(character.model, character.hull, color); |
460 | 8 | } else { |
461 | 8 | const Features features = |
462 | 8 | figure.features ? *figure.features0 : FeaturesOf(figures_, figure.id); |
463 | 8 | draw(character.model, character.hull, GetColor(features.skin)); |
464 | 8 | const auto hair = character.hairs.find(features.hair); |
465 | 8 | if (hair != character.hairs.end()) { |
466 | 4 | draw(hair->second.model, hair->second.hull, |
467 | 4 | GetColor(features.hair_color)); |
468 | 4 | } |
469 | 8 | } |
470 | | // What it wears has colours of its own, over the figure's, or takes on |
471 | | // the colour it's given. |
472 | 9 | for (const uint32_t item : figure.wearing) { |
473 | 7 | const auto worn = character.garments.find(item); |
474 | 7 | if (worn != character.garments.end()) { |
475 | 7 | draw(worn->second.model, worn->second.hull, |
476 | 7 | worn->second.dyed ? color6 : WHITE); |
477 | 7 | } |
478 | 7 | } |
479 | | |
480 | 9 | const auto held = weapons_.find(figure.weapon); |
481 | 9 | if (held != weapons_.end() && character.hand >= 00 ) { |
482 | 0 | Model weapon = held->second.model; |
483 | 0 | weapon.transform = |
484 | 0 | MatrixMultiply(InHand(character, held->second.grip), placed); |
485 | 0 | if (outline != nullptr) { |
486 | 0 | DrawOutline(weapon, MatrixIdentity(), outline->still); |
487 | 0 | } |
488 | 0 | DrawModel(weapon, {0.0f, 0.0f, 0.0f}, 1.0f, WHITE); |
489 | 0 | } |
490 | 9 | constexpr float kNone = 0.0f; |
491 | 9 | SetShaderValue(shader_, emission_, &kNone, SHADER_UNIFORM_FLOAT); |
492 | 9 | } |
493 | | |
494 | | // The body alone, in the colour, its weapon too, lit so that it glows. |
495 | 0 | void Characters::DrawGhost(const Figure& figure, Color color) const { |
496 | 0 | const Character& character = For(figure); |
497 | 0 | PoseAs(character, figure); |
498 | 0 | const Matrix placed = Placed(character, figure); |
499 | 0 | SetShaderValue(shader_, emission_, &kGhostGlow, SHADER_UNIFORM_FLOAT); |
500 | 0 | Model body = character.model; |
501 | 0 | body.transform = MatrixMultiply(body.transform, placed); |
502 | 0 | DrawModel(body, {0.0f, 0.0f, 0.0f}, 1.0f, color); |
503 | 0 | const auto held = weapons_.find(figure.weapon); |
504 | 0 | if (held != weapons_.end() && character.hand >= 0) { |
505 | 0 | Model weapon = held->second.model; |
506 | 0 | weapon.transform = |
507 | 0 | MatrixMultiply(InHand(character, held->second.grip), placed); |
508 | 0 | DrawModel(weapon, {0.0f, 0.0f, 0.0f}, 1.0f, color); |
509 | 0 | } |
510 | 0 | constexpr float kNone = 0.0f; |
511 | 0 | SetShaderValue(shader_, emission_, &kNone, SHADER_UNIFORM_FLOAT); |
512 | 0 | } |
513 | | |
514 | | } // namespace nanommo |