/src/assimp/code/AssetLib/MDL/HalfLife/HL1MDLLoader.cpp
Line | Count | Source |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2026, assimp team |
7 | | |
8 | | All rights reserved. |
9 | | |
10 | | Redistribution and use of this software in source and binary forms, |
11 | | with or without modification, are permitted provided that the following |
12 | | conditions are met: |
13 | | |
14 | | * Redistributions of source code must retain the above |
15 | | copyright notice, this list of conditions and the |
16 | | following disclaimer. |
17 | | |
18 | | * Redistributions in binary form must reproduce the above |
19 | | copyright notice, this list of conditions and the |
20 | | following disclaimer in the documentation and/or other |
21 | | materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the assimp team, nor the names of its |
24 | | contributors may be used to endorse or promote products |
25 | | derived from this software without specific prior |
26 | | written permission of the assimp team. |
27 | | |
28 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
29 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
31 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
32 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
33 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
34 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
35 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
36 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
37 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
38 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | --------------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /** @file HL1MDLLoader.cpp |
43 | | * @brief Implementation for the Half-Life 1 MDL loader. |
44 | | */ |
45 | | |
46 | | #include "HL1MDLLoader.h" |
47 | | #include "HL1ImportDefinitions.h" |
48 | | #include "HL1MeshTrivert.h" |
49 | | #include "UniqueNameGenerator.h" |
50 | | |
51 | | #include <assimp/BaseImporter.h> |
52 | | #include <assimp/StringUtils.h> |
53 | | #include <assimp/ai_assert.h> |
54 | | #include <assimp/qnan.h> |
55 | | #include <assimp/DefaultLogger.hpp> |
56 | | #include <assimp/Importer.hpp> |
57 | | |
58 | | #include <iomanip> |
59 | | #include <sstream> |
60 | | #include <map> |
61 | | #include <limits> |
62 | | #include <cstring> |
63 | | #include <memory> |
64 | | |
65 | | #ifdef MDL_HALFLIFE_LOG_WARN_HEADER |
66 | | #undef MDL_HALFLIFE_LOG_WARN_HEADER |
67 | | #endif |
68 | 548 | #define MDL_HALFLIFE_LOG_HEADER "[Half-Life 1 MDL] " |
69 | | #include "LogFunctions.h" |
70 | | |
71 | | namespace Assimp { |
72 | | namespace MDL { |
73 | | namespace HalfLife { |
74 | | |
75 | | #ifdef _MSC_VER |
76 | | # pragma warning(disable : 4706) |
77 | | #endif // _MSC_VER |
78 | | |
79 | | // ------------------------------------------------------------------------------------------------ |
80 | 125k | static void validate_index(int index, int count, const char *name) { |
81 | 125k | if (index < 0 || index >= count) { |
82 | 283 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid ", name, " index"); |
83 | 283 | } |
84 | 125k | } |
85 | | |
86 | | // ------------------------------------------------------------------------------------------------ |
87 | 19.5k | static void validate_count(int count, int limit, const char *name) { |
88 | 19.5k | if (count < 0 || count > limit) { |
89 | 109 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid ", name, " count"); |
90 | 109 | } |
91 | 19.5k | } |
92 | | |
93 | | // ------------------------------------------------------------------------------------------------ |
94 | 2.13k | static void validate_non_negative_count(int count, const char *name) { |
95 | 2.13k | if (count < 0) { |
96 | 6 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid ", name, " count"); |
97 | 6 | } |
98 | 2.13k | } |
99 | | |
100 | | // ------------------------------------------------------------------------------------------------ |
101 | 58.2k | static void validate_bone_index(int bone_index, size_t bone_count) { |
102 | 58.2k | if (bone_index < 0 || static_cast<size_t>(bone_index) >= bone_count) { |
103 | 13 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid bone index"); |
104 | 13 | } |
105 | 58.2k | } |
106 | | |
107 | | // ------------------------------------------------------------------------------------------------ |
108 | 67.6k | static int advance_mesh_command_offset(int offset, size_t bytes) { |
109 | 67.6k | if (offset < 0) { |
110 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid mesh command offset"); |
111 | 0 | } |
112 | | |
113 | 67.6k | const auto max_int = static_cast<size_t>(std::numeric_limits<int>::max()); |
114 | 67.6k | if (bytes > max_int || static_cast<size_t>(offset) > max_int - bytes) { |
115 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid mesh command offset"); |
116 | 0 | } |
117 | | |
118 | 67.6k | return offset + static_cast<int>(bytes); |
119 | 67.6k | } |
120 | | |
121 | | // ------------------------------------------------------------------------------------------------ |
122 | 1.53k | static int validate_texture_pixel_count(const Texture_HL1 &texture) { |
123 | 1.53k | if (texture.width <= 0 || texture.height <= 0 || |
124 | 1.51k | texture.width > std::numeric_limits<int>::max() / texture.height) { |
125 | 33 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid texture dimensions"); |
126 | 33 | } |
127 | | |
128 | 1.50k | const int pixel_count = texture.width * texture.height; |
129 | 1.50k | if (static_cast<size_t>(pixel_count) > AI_MAX_ALLOC(aiTexel)) { |
130 | 2 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid texture dimensions"); |
131 | 2 | } |
132 | | |
133 | 1.49k | return pixel_count; |
134 | 1.50k | } |
135 | | |
136 | | // ------------------------------------------------------------------------------------------------ |
137 | 1.47k | static int texture_palette_offset(const Texture_HL1 &texture, int pixel_count) { |
138 | 1.47k | if (texture.index < 0 || texture.index > std::numeric_limits<int>::max() - pixel_count) { |
139 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid texture data offset"); |
140 | 0 | } |
141 | | |
142 | 1.47k | return texture.index + pixel_count; |
143 | 1.47k | } |
144 | | |
145 | | // ------------------------------------------------------------------------------------------------ |
146 | 9.62k | static void validate_mesh_command_vertex_count(size_t count) { |
147 | 9.62k | if (count < 3 || count > static_cast<size_t>(AI_MDL_HL1_MAX_TRIANGLES + 2)) { |
148 | 3 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid mesh command vertex count"); |
149 | 3 | } |
150 | 9.62k | } |
151 | | |
152 | | // ------------------------------------------------------------------------------------------------ |
153 | | HL1MDLLoader::HL1MDLLoader( |
154 | | aiScene *scene, |
155 | | IOSystem *io, |
156 | | const unsigned char *buffer, |
157 | | size_t buffer_length, |
158 | | const std::string &file_path, |
159 | | const HL1ImportSettings &import_settings) : |
160 | 1.12k | scene_(scene), |
161 | 1.12k | io_(io), |
162 | 1.12k | buffer_(HL1DataBuffer::view(buffer, buffer_length)), |
163 | 1.12k | file_path_(file_path), |
164 | 1.12k | import_settings_(import_settings), |
165 | 1.12k | header_(nullptr), |
166 | 1.12k | texture_header_(nullptr), |
167 | 1.12k | anim_headers_(), |
168 | 1.12k | texture_buffer_(), |
169 | 1.12k | anim_buffers_(), |
170 | 1.12k | num_sequence_groups_(0), |
171 | 1.12k | rootnode_children_(), |
172 | 1.12k | unique_name_generator_(), |
173 | 1.12k | unique_sequence_names_(), |
174 | 1.12k | unique_sequence_groups_names_(), |
175 | 1.12k | temp_bones_(), |
176 | 1.12k | num_blend_controllers_(0), |
177 | 1.12k | total_models_(0) { |
178 | 1.12k | load_file(); |
179 | 1.12k | } |
180 | | |
181 | | // ------------------------------------------------------------------------------------------------ |
182 | 433 | HL1MDLLoader::~HL1MDLLoader() { |
183 | 433 | release_resources(); |
184 | 433 | } |
185 | | |
186 | | // ------------------------------------------------------------------------------------------------ |
187 | 1.55k | void HL1MDLLoader::release_resources() { |
188 | | // Root has some children nodes. so let's proceed them |
189 | 1.55k | if (!rootnode_children_.empty()) { |
190 | | // Here, it means that the nodes were not added to the |
191 | | // scene root node. We still have to delete them. |
192 | 1.59k | for (auto it = rootnode_children_.begin(); it != rootnode_children_.end(); ++it) { |
193 | 1.15k | if (*it) { |
194 | 1.15k | delete *it; |
195 | 1.15k | } |
196 | 1.15k | } |
197 | | // Ensure this happens only once. |
198 | 434 | rootnode_children_.clear(); |
199 | 434 | } |
200 | 1.55k | } |
201 | | |
202 | | // ------------------------------------------------------------------------------------------------ |
203 | 1.12k | void HL1MDLLoader::load_file() { |
204 | 1.12k | try { |
205 | 1.12k | header_ = get_buffer_data<Header_HL1>(0, 1); |
206 | 1.12k | validate_header(header_, false); |
207 | | |
208 | | // Create the root scene node. |
209 | 1.12k | scene_->mRootNode = new aiNode(AI_MDL_HL1_NODE_ROOT); |
210 | | |
211 | 1.12k | load_texture_file(); |
212 | | |
213 | 1.12k | if (import_settings_.read_animations) { |
214 | 1.03k | load_sequence_groups_files(); |
215 | 1.03k | } |
216 | | |
217 | 1.12k | read_textures(); |
218 | 1.12k | read_skins(); |
219 | | |
220 | 1.12k | read_bones(); |
221 | 1.12k | read_meshes(); |
222 | | |
223 | 1.12k | if (import_settings_.read_animations) { |
224 | 662 | read_sequence_groups_info(); |
225 | 662 | read_animations(); |
226 | 662 | read_sequence_infos(); |
227 | 662 | if (import_settings_.read_sequence_transitions) |
228 | 496 | read_sequence_transitions(); |
229 | 662 | } |
230 | | |
231 | 1.12k | if (import_settings_.read_attachments) { |
232 | 493 | read_attachments(); |
233 | 493 | } |
234 | | |
235 | 1.12k | if (import_settings_.read_hitboxes) { |
236 | 467 | read_hitboxes(); |
237 | 467 | } |
238 | | |
239 | 1.12k | if (import_settings_.read_bone_controllers) { |
240 | 449 | read_bone_controllers(); |
241 | 449 | } |
242 | | |
243 | 1.12k | read_global_info(); |
244 | | |
245 | 1.12k | if (!header_->numbodyparts) { |
246 | | // This could be an MDL external texture file. In this case, |
247 | | // add this flag to allow the scene to be loaded even if it |
248 | | // has no meshes. |
249 | 26 | scene_->mFlags |= AI_SCENE_FLAGS_INCOMPLETE; |
250 | 26 | } |
251 | | |
252 | | // Append children to root node. |
253 | 1.12k | if (rootnode_children_.size()) { |
254 | 433 | scene_->mRootNode->addChildren( |
255 | 433 | static_cast<unsigned int>(rootnode_children_.size()), |
256 | 433 | rootnode_children_.data()); |
257 | | |
258 | | // Clear the list of nodes so they will not be destroyed |
259 | | // when resources are released. |
260 | 433 | rootnode_children_.clear(); |
261 | 433 | } |
262 | | |
263 | 1.12k | release_resources(); |
264 | | |
265 | 1.12k | } catch (...) { |
266 | 687 | release_resources(); |
267 | 687 | throw; |
268 | 687 | } |
269 | 1.12k | } |
270 | | |
271 | | // ------------------------------------------------------------------------------------------------ |
272 | 2.17k | void HL1MDLLoader::validate_header(const Header_HL1 *header, bool is_texture_header) { |
273 | 2.17k | if (is_texture_header) { |
274 | | // Every single Half-Life model is assumed to have at least one texture. |
275 | 1.05k | if (!header->numtextures) { |
276 | 4 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "There are no textures in the file"); |
277 | 4 | } |
278 | | |
279 | 1.04k | validate_count(header->numtextures, AI_MDL_HL1_MAX_TEXTURES, "texture"); |
280 | 1.04k | validate_count(header->numskinfamilies, AI_MDL_HL1_MAX_SKIN_FAMILIES, "skin family"); |
281 | 1.04k | validate_count(header->numskinref, AI_MDL_HL1_MAX_TEXTURES, "skin reference"); |
282 | | |
283 | 1.12k | } else { |
284 | | |
285 | 1.12k | validate_count(header->numbodyparts, AI_MDL_HL1_MAX_BODYPARTS, "bodypart"); |
286 | 1.12k | validate_count(header->numbones, AI_MDL_HL1_MAX_BONES, "bone"); |
287 | 1.12k | validate_count(header->numbonecontrollers, AI_MDL_HL1_MAX_BONE_CONTROLLERS, "bone controller"); |
288 | 1.12k | validate_non_negative_count(header->numhitboxes, "hitbox"); |
289 | 1.12k | validate_count(header->numseq, AI_MDL_HL1_MAX_SEQUENCES, "sequence"); |
290 | 1.12k | validate_count(header->numseqgroups, AI_MDL_HL1_MAX_SEQUENCE_GROUPS, "sequence group"); |
291 | 1.12k | validate_count(header->numattachments, AI_MDL_HL1_MAX_ATTACHMENTS, "attachment"); |
292 | 1.12k | validate_non_negative_count(header->numtransitions, "transition"); |
293 | 1.12k | } |
294 | 2.17k | } |
295 | | |
296 | | // ------------------------------------------------------------------------------------------------ |
297 | | /* |
298 | | Load textures. |
299 | | |
300 | | There are two ways for textures to be stored in a Half-Life model: |
301 | | |
302 | | 1. Directly in the MDL file (filePath) or |
303 | | 2. In an external MDL file. |
304 | | |
305 | | Due to the way StudioMDL works (tool used to compile SMDs into MDLs), |
306 | | it is assumed that an external texture file follows the naming |
307 | | convention: <YourModelName>T.mdl. Note the extra (T) at the end of the |
308 | | model name. |
309 | | |
310 | | .e.g For a given model named MyModel.mdl |
311 | | |
312 | | The external texture file name would be MyModelT.mdl |
313 | | */ |
314 | 1.05k | void HL1MDLLoader::load_texture_file() { |
315 | 1.05k | if (header_->numtextures == 0) { |
316 | | // Load an external MDL texture file. |
317 | 4 | std::string texture_file_path = |
318 | 4 | DefaultIOSystem::absolutePath(file_path_) + io_->getOsSeparator() + |
319 | 4 | DefaultIOSystem::completeBaseName(file_path_) + "T." + |
320 | 4 | BaseImporter::GetExtension(file_path_); |
321 | | |
322 | 4 | load_file_into_buffer<Header_HL1>(texture_file_path, texture_buffer_); |
323 | 1.04k | } else { |
324 | | // Model has no external texture file. This means the texture is stored inside the main MDL file. |
325 | 1.04k | texture_buffer_ = HL1DataBuffer::view(buffer_); |
326 | 1.04k | } |
327 | | |
328 | 1.05k | texture_header_ = get_texture_buffer_data<Header_HL1>(0, 1); |
329 | | |
330 | | // Validate texture header. |
331 | 1.05k | validate_header(texture_header_, true); |
332 | 1.05k | } |
333 | | |
334 | | // ------------------------------------------------------------------------------------------------ |
335 | | /* |
336 | | Load sequence group files if any. |
337 | | |
338 | | Due to the way StudioMDL works (tool used to compile SMDs into MDLs), |
339 | | it is assumed that a sequence group file follows the naming |
340 | | convention: <YourModelName>0X.mdl. Note the extra (0X) at the end of |
341 | | the model name, where (X) is the sequence group. |
342 | | |
343 | | .e.g For a given model named MyModel.mdl |
344 | | |
345 | | Sequence group 1 => MyModel01.mdl |
346 | | Sequence group 2 => MyModel02.mdl |
347 | | Sequence group X => MyModel0X.mdl |
348 | | |
349 | | */ |
350 | 1.03k | void HL1MDLLoader::load_sequence_groups_files() { |
351 | 1.03k | if (header_->numseqgroups <= 1) { |
352 | 873 | return; |
353 | 873 | } |
354 | | |
355 | 161 | num_sequence_groups_ = header_->numseqgroups; |
356 | | |
357 | 161 | anim_buffers_.resize(num_sequence_groups_); |
358 | 161 | anim_headers_.resize(num_sequence_groups_, nullptr); |
359 | | |
360 | 161 | std::string file_path_without_extension = |
361 | 161 | DefaultIOSystem::absolutePath(file_path_) + |
362 | 161 | io_->getOsSeparator() + |
363 | 161 | DefaultIOSystem::completeBaseName(file_path_); |
364 | | |
365 | 3.38k | for (int i = 1; i < num_sequence_groups_; ++i) { |
366 | 3.22k | std::stringstream ss; |
367 | 3.22k | ss << file_path_without_extension; |
368 | 3.22k | ss << std::setw(2) << std::setfill('0') << i; |
369 | 3.22k | ss << '.' << BaseImporter::GetExtension(file_path_); |
370 | | |
371 | 3.22k | std::string sequence_file_path = ss.str(); |
372 | | |
373 | 3.22k | load_file_into_buffer<SequenceHeader_HL1>(sequence_file_path, anim_buffers_[i]); |
374 | | |
375 | 3.22k | anim_headers_[i] = get_anim_buffer_data<SequenceHeader_HL1>(i, 0, 1); |
376 | 3.22k | } |
377 | 161 | } |
378 | | |
379 | | // ------------------------------------------------------------------------------------------------ |
380 | | // Read an MDL texture. |
381 | | void HL1MDLLoader::read_texture(const Texture_HL1 *ptexture, |
382 | | const uint8_t *data, const uint8_t *pal, aiTexture *pResult, |
383 | 1.47k | aiColor3D &last_palette_color) { |
384 | 1.47k | pResult->mFilename = ptexture->name; |
385 | 1.47k | pResult->mWidth = static_cast<unsigned int>(ptexture->width); |
386 | 1.47k | pResult->mHeight = static_cast<unsigned int>(ptexture->height); |
387 | 1.47k | pResult->achFormatHint[0] = 'r'; |
388 | 1.47k | pResult->achFormatHint[1] = 'g'; |
389 | 1.47k | pResult->achFormatHint[2] = 'b'; |
390 | 1.47k | pResult->achFormatHint[3] = 'a'; |
391 | 1.47k | pResult->achFormatHint[4] = '8'; |
392 | 1.47k | pResult->achFormatHint[5] = '8'; |
393 | 1.47k | pResult->achFormatHint[6] = '8'; |
394 | 1.47k | pResult->achFormatHint[7] = '8'; |
395 | 1.47k | pResult->achFormatHint[8] = '\0'; |
396 | | |
397 | 1.47k | const size_t num_pixels = pResult->mWidth * pResult->mHeight; |
398 | 1.47k | aiTexel *out = pResult->pcData = new aiTexel[num_pixels]; |
399 | | |
400 | | // Convert indexed 8 bit to 32 bit RGBA. |
401 | 2.94M | for (size_t i = 0; i < num_pixels; ++i, ++out) { |
402 | 2.94M | out->r = pal[data[i] * 3]; |
403 | 2.94M | out->g = pal[data[i] * 3 + 1]; |
404 | 2.94M | out->b = pal[data[i] * 3 + 2]; |
405 | 2.94M | out->a = 255; |
406 | 2.94M | } |
407 | | |
408 | | // Get the last palette color. |
409 | 1.47k | last_palette_color.r = pal[255 * 3]; |
410 | 1.47k | last_palette_color.g = pal[255 * 3 + 1]; |
411 | 1.47k | last_palette_color.b = pal[255 * 3 + 2]; |
412 | 1.47k | } |
413 | | |
414 | | // ------------------------------------------------------------------------------------------------ |
415 | 1.03k | void HL1MDLLoader::read_textures() { |
416 | 1.03k | scene_->mTextures = new aiTexture *[texture_header_->numtextures]; |
417 | 1.03k | scene_->mMaterials = new aiMaterial *[texture_header_->numtextures]; |
418 | | |
419 | 1.03k | const Texture_HL1 *ptexture = get_texture_buffer_data<Texture_HL1>(texture_header_->textureindex, texture_header_->numtextures); |
420 | | |
421 | 2.56k | for (int i = 0; i < texture_header_->numtextures; ++i) { |
422 | 1.53k | scene_->mTextures[i] = new aiTexture(); |
423 | 1.53k | ++scene_->mNumTextures; |
424 | | |
425 | 1.53k | const int pixel_count = validate_texture_pixel_count(ptexture[i]); |
426 | 1.53k | const uint8_t *data = get_texture_buffer_data<uint8_t>(ptexture[i].index, pixel_count); |
427 | 1.53k | const uint8_t *pal = get_texture_buffer_data<uint8_t>(texture_palette_offset(ptexture[i], pixel_count), 256 * 3); |
428 | | |
429 | 1.53k | aiColor3D last_palette_color; |
430 | 1.53k | read_texture(&ptexture[i], data, pal, scene_->mTextures[i], last_palette_color); |
431 | | |
432 | 1.53k | aiMaterial *scene_material = new aiMaterial(); |
433 | 1.53k | scene_->mMaterials[i] = scene_material; |
434 | 1.53k | ++scene_->mNumMaterials; |
435 | | |
436 | 1.53k | const aiTextureType texture_type = aiTextureType_DIFFUSE; |
437 | 1.53k | aiString texture_name(ptexture[i].name); |
438 | 1.53k | scene_material->AddProperty(&texture_name, AI_MATKEY_TEXTURE(texture_type, 0)); |
439 | | |
440 | | // Is this a chrome texture? |
441 | 1.53k | int chrome = ptexture[i].flags & AI_MDL_HL1_STUDIO_NF_CHROME ? 1 : 0; |
442 | 1.53k | scene_material->AddProperty(&chrome, 1, AI_MDL_HL1_MATKEY_CHROME(texture_type, 0)); |
443 | | |
444 | 1.53k | if (ptexture[i].flags & AI_MDL_HL1_STUDIO_NF_FLATSHADE) { |
445 | | // Flat shading. |
446 | 124 | const aiShadingMode shading_mode = aiShadingMode_Flat; |
447 | 124 | scene_material->AddProperty(&shading_mode, 1, AI_MATKEY_SHADING_MODEL); |
448 | 124 | } |
449 | | |
450 | 1.53k | if (ptexture[i].flags & AI_MDL_HL1_STUDIO_NF_ADDITIVE) { |
451 | | // Additive texture. |
452 | 687 | const aiBlendMode blend_mode = aiBlendMode_Additive; |
453 | 687 | scene_material->AddProperty(&blend_mode, 1, AI_MATKEY_BLEND_FUNC); |
454 | 847 | } else if (ptexture[i].flags & AI_MDL_HL1_STUDIO_NF_MASKED) { |
455 | | // Texture with 1 bit alpha test. |
456 | 51 | const aiTextureFlags use_alpha = aiTextureFlags_UseAlpha; |
457 | 51 | scene_material->AddProperty(&use_alpha, 1, AI_MATKEY_TEXFLAGS(texture_type, 0)); |
458 | 51 | scene_material->AddProperty(&last_palette_color, 1, AI_MATKEY_COLOR_TRANSPARENT); |
459 | 51 | } |
460 | 1.53k | } |
461 | 1.03k | } |
462 | | |
463 | | // ------------------------------------------------------------------------------------------------ |
464 | 958 | void HL1MDLLoader::read_skins() { |
465 | | // Read skins, if any. |
466 | 958 | if (texture_header_->numskinfamilies <= 1) { |
467 | 921 | return; |
468 | 921 | } |
469 | | |
470 | | // Pointer to base texture index. |
471 | 37 | const short *default_skin_ptr = get_texture_buffer_data<short>( |
472 | 37 | texture_header_->skinindex, |
473 | 37 | texture_header_->numskinref); |
474 | | |
475 | | // Start at first replacement skin. |
476 | 37 | const short *replacement_skin_ptr = get_texture_buffer_data<short>( |
477 | 37 | texture_header_->skinindex + texture_header_->numskinref * sizeof(short), |
478 | 37 | (texture_header_->numskinfamilies - 1) * texture_header_->numskinref); |
479 | | |
480 | 375 | for (int i = 1; i < texture_header_->numskinfamilies; ++i, replacement_skin_ptr += texture_header_->numskinref) { |
481 | 947 | for (int j = 0; j < texture_header_->numskinref; ++j) { |
482 | 609 | const short default_texture = default_skin_ptr[j]; |
483 | 609 | const short replacement_texture = replacement_skin_ptr[j]; |
484 | 609 | if (default_texture != replacement_texture) { |
485 | 38 | validate_index(default_texture, texture_header_->numtextures, "skin texture"); |
486 | 38 | validate_index(replacement_texture, texture_header_->numtextures, "skin texture"); |
487 | | |
488 | | // Save replacement textures. |
489 | 38 | aiString skinMaterialId(scene_->mTextures[replacement_texture]->mFilename); |
490 | 38 | scene_->mMaterials[default_texture]->AddProperty(&skinMaterialId, AI_MATKEY_TEXTURE_DIFFUSE(i)); |
491 | 38 | } |
492 | 609 | } |
493 | 338 | } |
494 | 37 | } |
495 | | |
496 | | // ------------------------------------------------------------------------------------------------ |
497 | 944 | void HL1MDLLoader::read_bones() { |
498 | 944 | if (!header_->numbones) { |
499 | 14 | return; |
500 | 14 | } |
501 | | |
502 | 930 | const Bone_HL1 *pbone = get_buffer_data<Bone_HL1>(header_->boneindex, header_->numbones); |
503 | | |
504 | 930 | std::vector<std::string> unique_bones_names(header_->numbones); |
505 | 6.25k | for (int i = 0; i < header_->numbones; ++i) { |
506 | 5.32k | unique_bones_names[i] = pbone[i].name; |
507 | 5.32k | } |
508 | | |
509 | | // Ensure bones have unique names. |
510 | 930 | unique_name_generator_.set_template_name("Bone"); |
511 | 930 | unique_name_generator_.make_unique(unique_bones_names); |
512 | | |
513 | 3.55k | for (int i = 0; i < header_->numbones; ++i) { |
514 | 2.62k | if (pbone[i].parent == -1) { |
515 | 1.02k | continue; |
516 | 1.02k | } |
517 | | |
518 | 1.59k | validate_index(pbone[i].parent, header_->numbones, "bone parent"); |
519 | 1.59k | if (pbone[i].parent >= i) { |
520 | 4 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid bone parent index"); |
521 | 4 | } |
522 | 1.59k | } |
523 | | |
524 | 926 | temp_bones_.resize(header_->numbones); |
525 | | |
526 | | // Create the main 'bones' node that will contain all MDL root bones. |
527 | 926 | aiNode *bones_node = new aiNode(AI_MDL_HL1_NODE_BONES); |
528 | 926 | rootnode_children_.push_back(bones_node); |
529 | | |
530 | | // Store roots bones IDs temporarily. |
531 | 926 | std::vector<int> roots; |
532 | | |
533 | | // Create bone matrices in local space. |
534 | 3.15k | for (int i = 0; i < header_->numbones; ++i) { |
535 | 2.23k | aiNode *bone_node = temp_bones_[i].node = new aiNode(unique_bones_names[i]); |
536 | | |
537 | 2.23k | aiVector3D angles(pbone[i].value[3], pbone[i].value[4], pbone[i].value[5]); |
538 | 2.23k | temp_bones_[i].absolute_transform = bone_node->mTransformation = |
539 | 2.23k | aiMatrix4x4(aiVector3D(1), aiQuaternion(angles.y, angles.z, angles.x), |
540 | 2.23k | aiVector3D(pbone[i].value[0], pbone[i].value[1], pbone[i].value[2])); |
541 | | |
542 | 2.23k | if (pbone[i].parent == -1) { |
543 | 945 | bone_node->mParent = bones_node; |
544 | 945 | roots.push_back(i); // This bone has no parent. Add it to the roots list. |
545 | 1.28k | } else { |
546 | 1.28k | validate_index(pbone[i].parent, header_->numbones, "bone parent"); |
547 | 1.28k | bone_node->mParent = temp_bones_[pbone[i].parent].node; |
548 | 1.28k | temp_bones_[pbone[i].parent].children.push_back(i); // Add this bone to the parent bone's children list. |
549 | | |
550 | 1.28k | temp_bones_[i].absolute_transform = |
551 | 1.28k | temp_bones_[pbone[i].parent].absolute_transform * bone_node->mTransformation; |
552 | 1.28k | } |
553 | | |
554 | 2.23k | temp_bones_[i].offset_matrix = temp_bones_[i].absolute_transform; |
555 | 2.23k | temp_bones_[i].offset_matrix.Inverse(); |
556 | 2.23k | } |
557 | | |
558 | | // Allocate memory for each MDL root bone. |
559 | 926 | bones_node->mNumChildren = static_cast<unsigned int>(roots.size()); |
560 | 926 | bones_node->mChildren = new aiNode *[bones_node->mNumChildren](); |
561 | | |
562 | | // Build all bones children hierarchy starting from each MDL root bone. |
563 | 1.87k | for (size_t i = 0; i < roots.size(); ++i) |
564 | 945 | { |
565 | 945 | const TempBone &root_bone = temp_bones_[roots[i]]; |
566 | 945 | bones_node->mChildren[i] = root_bone.node; |
567 | 945 | build_bone_children_hierarchy(root_bone); |
568 | 945 | } |
569 | 926 | } |
570 | | |
571 | | void HL1MDLLoader::build_bone_children_hierarchy(const TempBone &bone) |
572 | 2.23k | { |
573 | 2.23k | if (bone.children.empty()) |
574 | 1.04k | return; |
575 | | |
576 | 1.18k | aiNode* bone_node = bone.node; |
577 | 1.18k | bone_node->mNumChildren = static_cast<unsigned int>(bone.children.size()); |
578 | 1.18k | bone_node->mChildren = new aiNode *[bone_node->mNumChildren](); |
579 | | |
580 | | // Build each child bone's hierarchy recursively. |
581 | 2.47k | for (size_t i = 0; i < bone.children.size(); ++i) |
582 | 1.28k | { |
583 | 1.28k | const TempBone &child_bone = temp_bones_[bone.children[i]]; |
584 | 1.28k | bone_node->mChildren[i] = child_bone.node; |
585 | 1.28k | build_bone_children_hierarchy(child_bone); |
586 | 1.28k | } |
587 | 1.18k | } |
588 | | |
589 | | // ------------------------------------------------------------------------------------------------ |
590 | | /* |
591 | | Read meshes. |
592 | | |
593 | | Half-Life MDLs are structured such that each MDL |
594 | | contains one or more 'bodypart(s)', which contain one |
595 | | or more 'model(s)', which contains one or more mesh(es). |
596 | | |
597 | | * Bodyparts are used to group models that may be replaced |
598 | | in the game .e.g a character could have a 'heads' group, |
599 | | 'torso' group, 'shoes' group, with each group containing |
600 | | different 'model(s)'. |
601 | | |
602 | | * Models, also called 'sub models', contain vertices as |
603 | | well as a reference to each mesh used by the sub model. |
604 | | |
605 | | * Meshes contain a list of tris, also known as 'triverts'. |
606 | | Each tris contains the following information: |
607 | | |
608 | | 1. The index of the position to use for the vertex. |
609 | | 2. The index of the normal to use for the vertex. |
610 | | 3. The S coordinate to use for the vertex UV. |
611 | | 4. The T coordinate ^ |
612 | | |
613 | | These tris represent the way to represent the triangles |
614 | | for each mesh. Depending on how the tool compiled the MDL, |
615 | | those triangles were saved as strips and or fans. |
616 | | |
617 | | NOTE: Each tris is NOT unique. This means that you |
618 | | might encounter the same vertex index but with a different |
619 | | normal index, S coordinate, T coordinate. |
620 | | |
621 | | In addition, each mesh contains the texture's index. |
622 | | |
623 | | ------------------------------------------------------ |
624 | | With the details above, there are several things to |
625 | | take into consideration. |
626 | | |
627 | | * The Half-Life models store the vertices by sub model |
628 | | rather than by mesh. Due to Assimp's structure, it |
629 | | is necessary to remap each model vertex to be used |
630 | | per mesh. Unfortunately, this has the consequence |
631 | | to duplicate vertices. |
632 | | |
633 | | * Because the mesh triangles are comprised of strips and |
634 | | fans, it is necessary to convert each primitive to |
635 | | triangles, respectively (3 indices per face). |
636 | | */ |
637 | 867 | void HL1MDLLoader::read_meshes() { |
638 | 867 | if (!header_->numbodyparts) { |
639 | 43 | return; |
640 | 43 | } |
641 | | |
642 | 824 | int total_verts = 0; |
643 | 824 | int total_triangles = 0; |
644 | 824 | total_models_ = 0; |
645 | | |
646 | 824 | const Bodypart_HL1 *pbodypart = get_buffer_data<Bodypart_HL1>(header_->bodypartindex, header_->numbodyparts); |
647 | 824 | const Model_HL1 *pmodel = nullptr; |
648 | 824 | const Mesh_HL1 *pmesh = nullptr; |
649 | | |
650 | 824 | const Texture_HL1 *ptexture = get_texture_buffer_data<Texture_HL1>(texture_header_->textureindex, texture_header_->numtextures); |
651 | 824 | const short *pskinref = get_texture_buffer_data<short>(texture_header_->skinindex, texture_header_->numskinref); |
652 | | |
653 | 824 | scene_->mNumMeshes = 0; |
654 | | |
655 | 824 | std::vector<std::string> unique_bodyparts_names; |
656 | 824 | unique_bodyparts_names.resize(header_->numbodyparts); |
657 | | |
658 | | // Count the number of meshes. |
659 | | |
660 | 2.23k | for (int i = 0; i < header_->numbodyparts; ++i, ++pbodypart) { |
661 | 1.41k | unique_bodyparts_names[i] = pbodypart->name; |
662 | 1.41k | validate_count(pbodypart->nummodels, AI_MDL_HL1_MAX_MODELS, "bodypart model"); |
663 | 1.41k | if (total_models_ > AI_MDL_HL1_MAX_MODELS - pbodypart->nummodels) { |
664 | 2 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid model count"); |
665 | 2 | } |
666 | | |
667 | 1.41k | pmodel = get_buffer_data<Model_HL1>(pbodypart->modelindex, pbodypart->nummodels); |
668 | 3.12k | for (int j = 0; j < pbodypart->nummodels; ++j, ++pmodel) { |
669 | 1.71k | validate_count(pmodel->nummesh, AI_MDL_HL1_MAX_MESHES, "mesh"); |
670 | 1.71k | validate_count(pmodel->numverts, AI_MDL_HL1_MAX_VERTICES, "vertex"); |
671 | 1.71k | validate_count(pmodel->numnorms, AI_MDL_HL1_MAX_VERTICES, "normal"); |
672 | | |
673 | 1.71k | if (scene_->mNumMeshes > AI_MDL_HL1_MAX_MESHES - static_cast<unsigned int>(pmodel->nummesh)) { |
674 | 2 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid mesh count"); |
675 | 2 | } |
676 | 1.71k | if (total_verts > AI_MDL_HL1_MAX_VERTICES - pmodel->numverts) { |
677 | 2 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid vertex count"); |
678 | 2 | } |
679 | | |
680 | 1.71k | scene_->mNumMeshes += static_cast<unsigned int>(pmodel->nummesh); |
681 | 1.71k | total_verts += pmodel->numverts; |
682 | 1.71k | } |
683 | | |
684 | 1.40k | total_models_ += pbodypart->nummodels; |
685 | 1.40k | } |
686 | | |
687 | | // Display limit infos. |
688 | 818 | if (total_verts > AI_MDL_HL1_MAX_VERTICES) { |
689 | 0 | log_warning_limit_exceeded<AI_MDL_HL1_MAX_VERTICES>(total_verts, "vertices"); |
690 | 0 | } |
691 | | |
692 | 818 | if (scene_->mNumMeshes > AI_MDL_HL1_MAX_MESHES) { |
693 | 0 | log_warning_limit_exceeded<AI_MDL_HL1_MAX_MESHES>(scene_->mNumMeshes, "meshes"); |
694 | 0 | } |
695 | | |
696 | 818 | if (total_models_ > AI_MDL_HL1_MAX_MODELS) { |
697 | 0 | log_warning_limit_exceeded<AI_MDL_HL1_MAX_MODELS>(total_models_, "models"); |
698 | 0 | } |
699 | | |
700 | | // Ensure bodyparts have unique names. |
701 | 818 | unique_name_generator_.set_template_name("Bodypart"); |
702 | 818 | unique_name_generator_.make_unique(unique_bodyparts_names); |
703 | | |
704 | | // Now do the same for each model. |
705 | 818 | pbodypart = get_buffer_data<Bodypart_HL1>(header_->bodypartindex, header_->numbodyparts); |
706 | | |
707 | | // Prepare template name for bodypart models. |
708 | 818 | std::vector<std::string> unique_models_names; |
709 | 818 | unique_models_names.resize(total_models_); |
710 | | |
711 | 818 | unsigned int model_index = 0; |
712 | | |
713 | 2.15k | for (int i = 0; i < header_->numbodyparts; ++i, ++pbodypart) { |
714 | 1.33k | pmodel = get_buffer_data<Model_HL1>(pbodypart->modelindex, pbodypart->nummodels); |
715 | 2.98k | for (int j = 0; j < pbodypart->nummodels; ++j, ++pmodel, ++model_index) |
716 | 1.64k | unique_models_names[model_index] = pmodel->name; |
717 | 1.33k | } |
718 | | |
719 | 818 | unique_name_generator_.set_template_name("Model"); |
720 | 818 | unique_name_generator_.make_unique(unique_models_names); |
721 | | |
722 | 818 | unsigned int mesh_index = 0; |
723 | | |
724 | 818 | scene_->mMeshes = new aiMesh *[scene_->mNumMeshes](); |
725 | | |
726 | 818 | pbodypart = get_buffer_data<Bodypart_HL1>(header_->bodypartindex, header_->numbodyparts); |
727 | | |
728 | | /* Create a node that will represent the mesh hierarchy. |
729 | | |
730 | | <MDL_bodyparts> |
731 | | | |
732 | | +-- bodypart --+-- model -- [mesh index, mesh index, ...] |
733 | | | | |
734 | | | +-- model -- [mesh index, mesh index, ...] |
735 | | | | |
736 | | | ... |
737 | | | |
738 | | |-- bodypart -- ... |
739 | | | |
740 | | ... |
741 | | */ |
742 | 818 | aiNode *bodyparts_node = new aiNode(AI_MDL_HL1_NODE_BODYPARTS); |
743 | 818 | rootnode_children_.push_back(bodyparts_node); |
744 | 818 | bodyparts_node->mNumChildren = static_cast<unsigned int>(header_->numbodyparts); |
745 | 818 | bodyparts_node->mChildren = new aiNode *[bodyparts_node->mNumChildren](); |
746 | 818 | aiNode **bodyparts_node_ptr = bodyparts_node->mChildren; |
747 | | |
748 | | // The following variables are defined here so they don't have |
749 | | // to be recreated every iteration. |
750 | | |
751 | | // Model_HL1 vertices, in bind pose space. |
752 | 818 | std::vector<aiVector3D> bind_pose_vertices; |
753 | | |
754 | | // Model_HL1 normals, in bind pose space. |
755 | 818 | std::vector<aiVector3D> bind_pose_normals; |
756 | | |
757 | | // Used to contain temporary information for building a mesh. |
758 | 818 | std::vector<HL1MeshTrivert> triverts; |
759 | | |
760 | 818 | std::vector<short> tricmds; |
761 | | |
762 | | // Which triverts to use for the mesh. |
763 | 818 | std::vector<short> mesh_triverts_indices; |
764 | | |
765 | 818 | std::vector<HL1MeshFace> mesh_faces; |
766 | | |
767 | | /* triverts that have the same vertindex, but have different normindex,s,t values. |
768 | | Similar triverts are mapped from vertindex to a list of similar triverts. */ |
769 | 818 | std::map<short, std::set<short>> triverts_similars; |
770 | | |
771 | | // triverts per bone. |
772 | 818 | std::map<int, std::set<short>> bone_triverts; |
773 | | |
774 | | /** This function adds a trivert index to the list of triverts per bone. |
775 | | * \param[in] bone The bone that affects the trivert at index \p trivert_index. |
776 | | * \param[in] trivert_index The trivert index. |
777 | | */ |
778 | 37.6k | auto AddTrivertToBone = [&](int bone, short trivert_index) { |
779 | 37.6k | if (bone_triverts.count(bone) == 0) |
780 | 2.43k | bone_triverts.insert({ bone, std::set<short>{ trivert_index }}); |
781 | 35.1k | else |
782 | 35.1k | bone_triverts[bone].insert(trivert_index); |
783 | 37.6k | }; |
784 | | |
785 | | /** This function creates and appends a new trivert to the list of triverts. |
786 | | * \param[in] trivert The trivert to use as a prototype. |
787 | | * \param[in] bone The bone that affects \p trivert. |
788 | | */ |
789 | 12.2k | auto AddSimilarTrivert = [&](const Trivert &trivert, const int bone) { |
790 | 12.2k | HL1MeshTrivert new_trivert(trivert); |
791 | 12.2k | new_trivert.localindex = static_cast<short>(mesh_triverts_indices.size()); |
792 | | |
793 | 12.2k | short new_trivert_index = static_cast<short>(triverts.size()); |
794 | | |
795 | 12.2k | if (triverts_similars.count(trivert.vertindex) == 0) |
796 | 6.15k | triverts_similars.insert({ trivert.vertindex, std::set<short>{ new_trivert_index }}); |
797 | 6.06k | else |
798 | 6.06k | triverts_similars[trivert.vertindex].insert(new_trivert_index); |
799 | | |
800 | 12.2k | triverts.push_back(new_trivert); |
801 | | |
802 | 12.2k | mesh_triverts_indices.push_back(new_trivert_index); |
803 | 12.2k | tricmds.push_back(new_trivert.localindex); |
804 | 12.2k | AddTrivertToBone(bone, new_trivert.localindex); |
805 | 12.2k | }; |
806 | | |
807 | 818 | model_index = 0; |
808 | | |
809 | 2.08k | for (int i = 0; i < header_->numbodyparts; ++i, ++pbodypart, ++bodyparts_node_ptr) { |
810 | 1.27k | pmodel = get_buffer_data<Model_HL1>(pbodypart->modelindex, pbodypart->nummodels); |
811 | | |
812 | | // Create bodypart node for the mesh tree hierarchy. |
813 | 1.27k | aiNode *bodypart_node = (*bodyparts_node_ptr) = new aiNode(unique_bodyparts_names[i]); |
814 | 1.27k | bodypart_node->mParent = bodyparts_node; |
815 | 1.27k | bodypart_node->mMetaData = aiMetadata::Alloc(1); |
816 | 1.27k | bodypart_node->mMetaData->Set(0, "Base", pbodypart->base); |
817 | | |
818 | 1.27k | bodypart_node->mNumChildren = static_cast<unsigned int>(pbodypart->nummodels); |
819 | 1.27k | bodypart_node->mChildren = new aiNode *[bodypart_node->mNumChildren](); |
820 | 1.27k | aiNode **bodypart_models_ptr = bodypart_node->mChildren; |
821 | | |
822 | 2.80k | for (int j = 0; j < pbodypart->nummodels; |
823 | 1.53k | ++j, ++pmodel, ++bodypart_models_ptr, ++model_index) { |
824 | | |
825 | 1.53k | pmesh = get_buffer_data<Mesh_HL1>(pmodel->meshindex, pmodel->nummesh); |
826 | | |
827 | 1.53k | const uint8_t *pvertbone = get_buffer_data<uint8_t>(pmodel->vertinfoindex, pmodel->numverts); |
828 | 1.53k | const uint8_t *pnormbone = get_buffer_data<uint8_t>(pmodel->norminfoindex, pmodel->numnorms); |
829 | 1.53k | const uint8_t *pstudioverts = get_buffer_data<uint8_t>(pmodel->vertindex, pmodel->numverts * static_cast<int>(sizeof(vec3_t))); |
830 | 1.53k | const uint8_t *pstudionorms = get_buffer_data<uint8_t>(pmodel->normindex, pmodel->numnorms * static_cast<int>(sizeof(vec3_t))); |
831 | | |
832 | | // Each vertex and normal is in local space, so transform |
833 | | // each of them to bring them in bind pose. |
834 | 1.53k | bind_pose_vertices.resize(pmodel->numverts); |
835 | 1.53k | bind_pose_normals.resize(pmodel->numnorms); |
836 | 33.9k | for (size_t k = 0; k < bind_pose_vertices.size(); ++k) { |
837 | 32.3k | validate_bone_index(pvertbone[k], temp_bones_.size()); |
838 | 32.3k | vec3_t vert; |
839 | 32.3k | ::memcpy(vert, pstudioverts + k * sizeof(vec3_t), sizeof(vec3_t)); |
840 | 32.3k | bind_pose_vertices[k] = temp_bones_[pvertbone[k]].absolute_transform * aiVector3D(vert[0], vert[1], vert[2]); |
841 | 32.3k | } |
842 | 27.4k | for (size_t k = 0; k < bind_pose_normals.size(); ++k) { |
843 | 25.8k | validate_bone_index(pnormbone[k], temp_bones_.size()); |
844 | 25.8k | vec3_t norm; |
845 | 25.8k | ::memcpy(norm, pstudionorms + k * sizeof(vec3_t), sizeof(vec3_t)); |
846 | | // Compute the normal matrix to transform the normal into bind pose, |
847 | | // without affecting its length. |
848 | 25.8k | const aiMatrix4x4 normal_matrix = aiMatrix4x4(temp_bones_[pnormbone[k]].absolute_transform).Inverse().Transpose(); |
849 | 25.8k | bind_pose_normals[k] = normal_matrix * aiVector3D(norm[0], norm[1], norm[2]); |
850 | 25.8k | } |
851 | | |
852 | | // Create model node for the mesh tree hierarchy. |
853 | 1.53k | aiNode *model_node = (*bodypart_models_ptr) = new aiNode(unique_models_names[model_index]); |
854 | 1.53k | model_node->mParent = bodypart_node; |
855 | 1.53k | model_node->mNumMeshes = static_cast<unsigned int>(pmodel->nummesh); |
856 | 1.53k | model_node->mMeshes = new unsigned int[model_node->mNumMeshes]; |
857 | 1.53k | unsigned int *model_meshes_ptr = model_node->mMeshes; |
858 | | |
859 | 3.27k | for (int k = 0; k < pmodel->nummesh; ++k, ++pmesh, ++mesh_index, ++model_meshes_ptr) { |
860 | 1.73k | *model_meshes_ptr = mesh_index; |
861 | | |
862 | | // Read triverts. |
863 | 1.73k | int tricmds_offset = pmesh->triindex; |
864 | 1.73k | validate_index(pmesh->skinref, texture_header_->numskinref, "skin reference"); |
865 | 1.73k | const int texture_index = pskinref[pmesh->skinref]; |
866 | 1.73k | validate_index(texture_index, texture_header_->numtextures, "texture"); |
867 | 1.73k | float texcoords_s_scale = 1.0f / (float)ptexture[texture_index].width; |
868 | 1.73k | float texcoords_t_scale = 1.0f / (float)ptexture[texture_index].height; |
869 | | |
870 | | // Reset the data for the upcoming mesh. |
871 | 1.73k | triverts.clear(); |
872 | 1.73k | triverts.resize(pmodel->numverts); |
873 | 1.73k | mesh_triverts_indices.clear(); |
874 | 1.73k | mesh_faces.clear(); |
875 | 1.73k | triverts_similars.clear(); |
876 | 1.73k | bone_triverts.clear(); |
877 | | |
878 | 1.73k | int l; |
879 | 11.4k | while ((l = *get_buffer_data<short>(tricmds_offset, 1))) { |
880 | 9.72k | tricmds_offset = advance_mesh_command_offset(tricmds_offset, sizeof(short)); |
881 | 9.72k | bool is_triangle_fan = false; |
882 | | |
883 | 9.72k | if (l < 0) { |
884 | 632 | l = -l; |
885 | 632 | is_triangle_fan = true; |
886 | 632 | } |
887 | | |
888 | | // Clear the list of tris for the upcoming tris. |
889 | 9.72k | tricmds.clear(); |
890 | | |
891 | 67.7k | for (; l > 0; --l, tricmds_offset = advance_mesh_command_offset(tricmds_offset, sizeof(Trivert))) { |
892 | 58.0k | const Trivert *input_trivert = get_buffer_data<Trivert>(tricmds_offset, 1); |
893 | 58.0k | validate_index(input_trivert->vertindex, pmodel->numverts, "vertex"); |
894 | 58.0k | validate_index(input_trivert->normindex, pmodel->numnorms, "normal"); |
895 | 58.0k | const int bone = pvertbone[input_trivert->vertindex]; |
896 | | |
897 | 58.0k | HL1MeshTrivert *private_trivert = &triverts[input_trivert->vertindex]; |
898 | 58.0k | if (private_trivert->localindex == -1) { |
899 | | // First time referenced. |
900 | 25.3k | *private_trivert = *input_trivert; |
901 | 25.3k | private_trivert->localindex = static_cast<short>(mesh_triverts_indices.size()); |
902 | 25.3k | mesh_triverts_indices.push_back(input_trivert->vertindex); |
903 | 25.3k | tricmds.push_back(private_trivert->localindex); |
904 | 25.3k | AddTrivertToBone(bone, private_trivert->localindex); |
905 | 32.6k | } else if (*private_trivert == *input_trivert) { |
906 | | // Exists and is the same. |
907 | 20.2k | tricmds.push_back(private_trivert->localindex); |
908 | 20.2k | } else { |
909 | | // No similar trivert associated to the trivert currently processed. |
910 | 12.4k | if (triverts_similars.count(input_trivert->vertindex) == 0) |
911 | 6.15k | AddSimilarTrivert(*input_trivert, bone); |
912 | 6.26k | else { |
913 | | // Search in the list of similar triverts to see if the |
914 | | // trivert in process is already registered. |
915 | 6.26k | short similar_index = -1; |
916 | 6.26k | for (auto it = triverts_similars[input_trivert->vertindex].cbegin(); |
917 | 35.4k | similar_index == -1 && it != triverts_similars[input_trivert->vertindex].cend(); |
918 | 29.1k | ++it) { |
919 | 29.1k | if (triverts[*it] == *input_trivert) |
920 | 96 | similar_index = *it; |
921 | 29.1k | } |
922 | | |
923 | | // If a similar trivert has been found, reuse it. |
924 | | // Otherwise, add it. |
925 | 6.26k | if (similar_index == -1) |
926 | 6.06k | AddSimilarTrivert(*input_trivert, bone); |
927 | 197 | else |
928 | 197 | tricmds.push_back(triverts[similar_index].localindex); |
929 | 6.26k | } |
930 | 12.4k | } |
931 | 58.0k | } |
932 | | |
933 | | // Build mesh faces. |
934 | 9.72k | validate_mesh_command_vertex_count(tricmds.size()); |
935 | 9.72k | const int num_faces = static_cast<int>(tricmds.size() - 2); |
936 | 9.72k | if (total_triangles > AI_MDL_HL1_MAX_TRIANGLES - num_faces) { |
937 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid triangle count"); |
938 | 0 | } |
939 | 9.72k | mesh_faces.reserve(num_faces); |
940 | | |
941 | 9.72k | if (is_triangle_fan) { |
942 | 4.54k | for (int faceIdx = 0; faceIdx < num_faces; ++faceIdx) { |
943 | 3.91k | mesh_faces.push_back(HL1MeshFace{ |
944 | 3.91k | tricmds[0], |
945 | 3.91k | tricmds[faceIdx + 1], |
946 | 3.91k | tricmds[faceIdx + 2] }); |
947 | 3.91k | } |
948 | 9.10k | } else { |
949 | 42.8k | for (int faceIdx = 0; faceIdx < num_faces; ++faceIdx) { |
950 | 33.7k | if (faceIdx & 1) { |
951 | | // Preserve winding order. |
952 | 14.7k | mesh_faces.push_back(HL1MeshFace{ |
953 | 14.7k | tricmds[faceIdx + 1], |
954 | 14.7k | tricmds[faceIdx], |
955 | 14.7k | tricmds[faceIdx + 2] }); |
956 | 18.9k | } else { |
957 | 18.9k | mesh_faces.push_back(HL1MeshFace{ |
958 | 18.9k | tricmds[faceIdx], |
959 | 18.9k | tricmds[faceIdx + 1], |
960 | 18.9k | tricmds[faceIdx + 2] }); |
961 | 18.9k | } |
962 | 33.7k | } |
963 | 9.10k | } |
964 | | |
965 | 9.72k | total_triangles += num_faces; |
966 | 9.72k | } |
967 | | |
968 | | // Create the scene mesh. |
969 | 1.73k | aiMesh *scene_mesh = scene_->mMeshes[mesh_index] = new aiMesh(); |
970 | 1.73k | scene_mesh->mPrimitiveTypes = aiPrimitiveType::aiPrimitiveType_TRIANGLE; |
971 | 1.73k | scene_mesh->mMaterialIndex = texture_index; |
972 | | |
973 | 1.73k | scene_mesh->mNumVertices = static_cast<unsigned int>(mesh_triverts_indices.size()); |
974 | | |
975 | 1.73k | if (scene_mesh->mNumVertices) { |
976 | 1.59k | scene_mesh->mVertices = new aiVector3D[scene_mesh->mNumVertices]; |
977 | 1.59k | scene_mesh->mNormals = new aiVector3D[scene_mesh->mNumVertices]; |
978 | | |
979 | 1.59k | scene_mesh->mNumUVComponents[0] = 2; |
980 | 1.59k | scene_mesh->mTextureCoords[0] = new aiVector3D[scene_mesh->mNumVertices]; |
981 | | |
982 | | // Add vertices. |
983 | 32.9k | for (unsigned int v = 0; v < scene_mesh->mNumVertices; ++v) { |
984 | 31.3k | const HL1MeshTrivert *pTrivert = &triverts[mesh_triverts_indices[v]]; |
985 | 31.3k | scene_mesh->mVertices[v] = bind_pose_vertices[pTrivert->vertindex]; |
986 | 31.3k | scene_mesh->mNormals[v] = bind_pose_normals[pTrivert->normindex]; |
987 | 31.3k | scene_mesh->mTextureCoords[0][v] = aiVector3D( |
988 | 31.3k | pTrivert->s * texcoords_s_scale, |
989 | 31.3k | pTrivert->t * -texcoords_t_scale, 0); |
990 | 31.3k | } |
991 | | |
992 | | // Add face and indices. |
993 | 1.59k | scene_mesh->mNumFaces = static_cast<unsigned int>(mesh_faces.size()); |
994 | 1.59k | scene_mesh->mFaces = new aiFace[scene_mesh->mNumFaces]; |
995 | | |
996 | 34.3k | for (unsigned int f = 0; f < scene_mesh->mNumFaces; ++f) { |
997 | 32.7k | aiFace *face = &scene_mesh->mFaces[f]; |
998 | 32.7k | face->mNumIndices = 3; |
999 | 32.7k | face->mIndices = new unsigned int[3]; |
1000 | 32.7k | face->mIndices[0] = mesh_faces[f].v2; |
1001 | 32.7k | face->mIndices[1] = mesh_faces[f].v1; |
1002 | 32.7k | face->mIndices[2] = mesh_faces[f].v0; |
1003 | 32.7k | } |
1004 | | |
1005 | | // Add mesh bones. |
1006 | 1.59k | scene_mesh->mNumBones = static_cast<unsigned int>(bone_triverts.size()); |
1007 | 1.59k | scene_mesh->mBones = new aiBone *[scene_mesh->mNumBones]; |
1008 | | |
1009 | 1.59k | aiBone **scene_bone_ptr = scene_mesh->mBones; |
1010 | | |
1011 | 1.59k | for (auto bone_it = bone_triverts.cbegin(); |
1012 | 3.74k | bone_it != bone_triverts.cend(); |
1013 | 2.14k | ++bone_it, ++scene_bone_ptr) { |
1014 | 2.14k | const int bone_index = bone_it->first; |
1015 | | |
1016 | 2.14k | aiBone *scene_bone = (*scene_bone_ptr) = new aiBone(); |
1017 | 2.14k | scene_bone->mName = temp_bones_[bone_index].node->mName; |
1018 | | |
1019 | 2.14k | scene_bone->mOffsetMatrix = temp_bones_[bone_index].offset_matrix; |
1020 | | |
1021 | 2.14k | auto vertex_ids = bone_triverts.at(bone_index); |
1022 | | |
1023 | | // Add vertex weight per bone. |
1024 | 2.14k | scene_bone->mNumWeights = static_cast<unsigned int>(vertex_ids.size()); |
1025 | 2.14k | aiVertexWeight *vertex_weight_ptr = scene_bone->mWeights = new aiVertexWeight[scene_bone->mNumWeights]; |
1026 | | |
1027 | 2.14k | for (auto vertex_it = vertex_ids.begin(); |
1028 | 33.4k | vertex_it != vertex_ids.end(); |
1029 | 31.3k | ++vertex_it, ++vertex_weight_ptr) { |
1030 | 31.3k | vertex_weight_ptr->mVertexId = *vertex_it; |
1031 | 31.3k | vertex_weight_ptr->mWeight = 1.0f; |
1032 | 31.3k | } |
1033 | 2.14k | } |
1034 | 1.59k | } |
1035 | 1.73k | } |
1036 | 1.53k | } |
1037 | 1.27k | } |
1038 | | |
1039 | 818 | if (total_triangles > AI_MDL_HL1_MAX_TRIANGLES) { |
1040 | 0 | log_warning_limit_exceeded<AI_MDL_HL1_MAX_TRIANGLES>(total_triangles, "triangles"); |
1041 | 0 | } |
1042 | 818 | } |
1043 | | |
1044 | | // ------------------------------------------------------------------------------------------------ |
1045 | 654 | void HL1MDLLoader::read_animations() { |
1046 | 654 | if (!header_->numseq) { |
1047 | 18 | return; |
1048 | 18 | } |
1049 | | |
1050 | 636 | const SequenceDesc_HL1 *pseqdesc = get_buffer_data<SequenceDesc_HL1>(header_->seqindex, header_->numseq); |
1051 | 636 | const SequenceGroup_HL1 *pseqgroup = nullptr; |
1052 | 636 | const AnimValueOffset_HL1 *panim = nullptr; |
1053 | 636 | const AnimValue_HL1 *panimvalue = nullptr; |
1054 | | |
1055 | 636 | unique_sequence_names_.resize(header_->numseq); |
1056 | 2.19k | for (int i = 0; i < header_->numseq; ++i) |
1057 | 1.56k | unique_sequence_names_[i] = pseqdesc[i].label; |
1058 | | |
1059 | | // Ensure sequences have unique names. |
1060 | 636 | unique_name_generator_.set_template_name("Sequence"); |
1061 | 636 | unique_name_generator_.make_unique(unique_sequence_names_); |
1062 | | |
1063 | 636 | scene_->mNumAnimations = 0; |
1064 | | |
1065 | 636 | int highest_num_blend_animations = SequenceBlendMode_HL1::NoBlend; |
1066 | | |
1067 | | // Count the total number of animations. |
1068 | 1.93k | for (int i = 0; i < header_->numseq; ++i, ++pseqdesc) { |
1069 | 1.33k | if (int num_blend_controllers = 0; !get_num_blend_controllers(pseqdesc->numblends, num_blend_controllers)) { |
1070 | 34 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid sequence blend count"); |
1071 | 34 | } |
1072 | 1.29k | validate_count(pseqdesc->numframes, AI_MDL_HL1_MAX_SEQUENCE_FRAMES, "sequence frame"); |
1073 | | |
1074 | 1.29k | scene_->mNumAnimations += pseqdesc->numblends; |
1075 | 1.29k | highest_num_blend_animations = std::max(pseqdesc->numblends, highest_num_blend_animations); |
1076 | 1.29k | } |
1077 | | |
1078 | | // Get the number of available blend controllers for global info. |
1079 | 602 | get_num_blend_controllers(highest_num_blend_animations, num_blend_controllers_); |
1080 | | |
1081 | 602 | pseqdesc = get_buffer_data<SequenceDesc_HL1>(header_->seqindex, header_->numseq); |
1082 | | |
1083 | 602 | auto scene_animations = std::make_unique<aiAnimation *[]>(scene_->mNumAnimations); |
1084 | 602 | scene_->mAnimations = scene_animations.release(); |
1085 | 602 | aiAnimation **scene_animations_ptr = scene_->mAnimations; |
1086 | | |
1087 | 1.79k | for (int sequence = 0; sequence < header_->numseq; ++sequence, ++pseqdesc) { |
1088 | 1.18k | validate_index(pseqdesc->seqgroup, header_->numseqgroups, "sequence group"); |
1089 | 1.18k | pseqgroup = get_buffer_data<SequenceGroup_HL1>(header_->seqgroupindex + pseqdesc->seqgroup * sizeof(SequenceGroup_HL1), 1); |
1090 | | |
1091 | 1.18k | const HL1DataBuffer *anim_buffer = nullptr; |
1092 | 1.18k | if (pseqdesc->seqgroup == 0) { |
1093 | 946 | anim_buffer = &buffer_; |
1094 | 946 | panim = get_buffer_data<AnimValueOffset_HL1>(pseqgroup->unused2 + pseqdesc->animindex, pseqdesc->numblends * header_->numbones); |
1095 | 946 | } else { |
1096 | 242 | anim_buffer = &anim_buffers_[pseqdesc->seqgroup]; |
1097 | 242 | panim = get_anim_buffer_data<AnimValueOffset_HL1>(pseqdesc->seqgroup, pseqdesc->animindex, pseqdesc->numblends * header_->numbones); |
1098 | 242 | } |
1099 | | |
1100 | 41.9k | auto get_anim_value = [anim_buffer](const AnimValueOffset_HL1 *anim, unsigned short offset) { |
1101 | 41.9k | if (!anim_buffer->ContainsBytes(static_cast<const uint8_t *>(static_cast<const void *>(anim)), |
1102 | 41.9k | static_cast<size_t>(offset) + sizeof(AnimValue_HL1))) { |
1103 | 17 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid animation value offset"); |
1104 | 17 | } |
1105 | | |
1106 | 41.9k | const auto *bytes = static_cast<const uint8_t *>(static_cast<const void *>(anim)); |
1107 | 41.9k | return reinterpret_cast<const AnimValue_HL1 *>(bytes + offset); // NOSONAR: MDL animation offsets address packed file data. |
1108 | 41.9k | }; |
1109 | | |
1110 | 2.34k | for (int blend = 0; blend < pseqdesc->numblends; ++blend, ++scene_animations_ptr) { |
1111 | | |
1112 | 1.15k | const Bone_HL1 *pbone = get_buffer_data<Bone_HL1>(header_->boneindex, header_->numbones); |
1113 | | |
1114 | 1.15k | aiAnimation *scene_animation = (*scene_animations_ptr) = new aiAnimation(); |
1115 | | |
1116 | 1.15k | scene_animation->mName = unique_sequence_names_[sequence]; |
1117 | 1.15k | scene_animation->mTicksPerSecond = pseqdesc->fps; |
1118 | 1.15k | scene_animation->mDuration = static_cast<double>(pseqdesc->fps) * pseqdesc->numframes; |
1119 | 1.15k | scene_animation->mNumChannels = static_cast<unsigned int>(header_->numbones); |
1120 | 1.15k | scene_animation->mChannels = new aiNodeAnim *[scene_animation->mNumChannels](); |
1121 | | |
1122 | 3.02k | for (int bone = 0; bone < header_->numbones; bone++, ++pbone, ++panim) { |
1123 | 1.87k | aiNodeAnim *node_anim = scene_animation->mChannels[bone] = new aiNodeAnim(); |
1124 | 1.87k | node_anim->mNodeName = temp_bones_[bone].node->mName; |
1125 | | |
1126 | 1.87k | node_anim->mNumPositionKeys = pseqdesc->numframes; |
1127 | 1.87k | node_anim->mNumRotationKeys = node_anim->mNumPositionKeys; |
1128 | 1.87k | node_anim->mNumScalingKeys = 0; |
1129 | | |
1130 | 1.87k | node_anim->mPositionKeys = new aiVectorKey[node_anim->mNumPositionKeys]; |
1131 | 1.87k | node_anim->mRotationKeys = new aiQuatKey[node_anim->mNumRotationKeys]; |
1132 | | |
1133 | 21.7k | for (int frame = 0; frame < pseqdesc->numframes; ++frame) { |
1134 | 19.8k | aiVectorKey *position_key = &node_anim->mPositionKeys[frame]; |
1135 | 19.8k | aiQuatKey *rotation_key = &node_anim->mRotationKeys[frame]; |
1136 | | |
1137 | 19.8k | aiVector3D angle1; |
1138 | 79.3k | for (int j = 0; j < 3; ++j) { |
1139 | 59.5k | if (panim->offset[j + 3] != 0) { |
1140 | | // Read compressed rotation delta. |
1141 | 39.8k | panimvalue = get_anim_value(panim, panim->offset[j + 3]); |
1142 | 39.8k | extract_anim_value(panimvalue, *anim_buffer, frame, pbone->scale[j + 3], angle1[j]); |
1143 | 39.8k | } |
1144 | | |
1145 | | // Add the default rotation value. |
1146 | 59.5k | angle1[j] += pbone->value[j + 3]; |
1147 | | |
1148 | 59.5k | if (panim->offset[j] != 0) { |
1149 | | // Read compressed position delta. |
1150 | 2.08k | panimvalue = get_anim_value(panim, panim->offset[j]); |
1151 | 2.08k | extract_anim_value(panimvalue, *anim_buffer, frame, pbone->scale[j], position_key->mValue[j]); |
1152 | 2.08k | } |
1153 | | |
1154 | | // Add the default position value. |
1155 | 59.5k | position_key->mValue[j] += pbone->value[j]; |
1156 | 59.5k | } |
1157 | | |
1158 | 19.8k | position_key->mTime = rotation_key->mTime = static_cast<double>(frame); |
1159 | | /* The Half-Life engine uses X as forward, Y as left, Z as up. Therefore, |
1160 | | pitch,yaw,roll is represented as (YZX). */ |
1161 | 19.8k | rotation_key->mValue = aiQuaternion(angle1.y, angle1.z, angle1.x); |
1162 | 19.8k | rotation_key->mValue.Normalize(); |
1163 | 19.8k | } |
1164 | 1.87k | } |
1165 | 1.15k | } |
1166 | 1.18k | } |
1167 | 602 | } |
1168 | | |
1169 | | // ------------------------------------------------------------------------------------------------ |
1170 | 662 | void HL1MDLLoader::read_sequence_groups_info() { |
1171 | 662 | if (!header_->numseqgroups) { |
1172 | 13 | return; |
1173 | 13 | } |
1174 | | |
1175 | 649 | aiNode *sequence_groups_node = new aiNode(AI_MDL_HL1_NODE_SEQUENCE_GROUPS); |
1176 | 649 | rootnode_children_.push_back(sequence_groups_node); |
1177 | | |
1178 | 649 | sequence_groups_node->mNumChildren = static_cast<unsigned int>(header_->numseqgroups); |
1179 | 649 | sequence_groups_node->mChildren = new aiNode *[sequence_groups_node->mNumChildren](); |
1180 | | |
1181 | 649 | const SequenceGroup_HL1 *pseqgroup = get_buffer_data<SequenceGroup_HL1>(header_->seqgroupindex, header_->numseqgroups); |
1182 | | |
1183 | 649 | unique_sequence_groups_names_.resize(header_->numseqgroups); |
1184 | 3.71k | for (int i = 0; i < header_->numseqgroups; ++i) { |
1185 | 3.06k | unique_sequence_groups_names_[i] = pseqgroup[i].label; |
1186 | 3.06k | } |
1187 | | |
1188 | | // Ensure sequence groups have unique names. |
1189 | 649 | unique_name_generator_.set_template_name("SequenceGroup"); |
1190 | 649 | unique_name_generator_.make_unique(unique_sequence_groups_names_); |
1191 | | |
1192 | 3.71k | for (int i = 0; i < header_->numseqgroups; ++i, ++pseqgroup) { |
1193 | 3.06k | aiNode *sequence_group_node = sequence_groups_node->mChildren[i] = new aiNode(unique_sequence_groups_names_[i]); |
1194 | 3.06k | sequence_group_node->mParent = sequence_groups_node; |
1195 | | |
1196 | 3.06k | aiMetadata *md = sequence_group_node->mMetaData = aiMetadata::Alloc(1); |
1197 | 3.06k | if (i == 0) { |
1198 | | /* StudioMDL does not write the file name for the default sequence group, |
1199 | | so we will write it. */ |
1200 | 641 | md->Set(0, "File", aiString(file_path_)); |
1201 | 2.42k | } else { |
1202 | 2.42k | md->Set(0, "File", aiString(pseqgroup->name)); |
1203 | 2.42k | } |
1204 | 3.06k | } |
1205 | 649 | } |
1206 | | |
1207 | | // ------------------------------------------------------------------------------------------------ |
1208 | 513 | void HL1MDLLoader::read_sequence_infos() { |
1209 | 513 | if (!header_->numseq) { |
1210 | 18 | return; |
1211 | 18 | } |
1212 | | |
1213 | 495 | const SequenceDesc_HL1 *pseqdesc = get_buffer_data<SequenceDesc_HL1>(header_->seqindex, header_->numseq); |
1214 | | |
1215 | 495 | aiNode *sequence_infos_node = new aiNode(AI_MDL_HL1_NODE_SEQUENCE_INFOS); |
1216 | 495 | rootnode_children_.push_back(sequence_infos_node); |
1217 | | |
1218 | 495 | sequence_infos_node->mNumChildren = static_cast<unsigned int>(header_->numseq); |
1219 | 495 | sequence_infos_node->mChildren = new aiNode *[sequence_infos_node->mNumChildren](); |
1220 | | |
1221 | 495 | std::vector<aiNode *> sequence_info_node_children; |
1222 | | |
1223 | 495 | int animation_index = 0; |
1224 | 1.53k | for (int i = 0; i < header_->numseq; ++i, ++pseqdesc) { |
1225 | 1.03k | validate_index(pseqdesc->seqgroup, header_->numseqgroups, "sequence group"); |
1226 | 1.03k | validate_index(pseqdesc->motionbone, header_->numbones, "motion bone"); |
1227 | 1.03k | validate_count(pseqdesc->numframes, AI_MDL_HL1_MAX_SEQUENCE_FRAMES, "sequence frame"); |
1228 | 1.03k | validate_count(pseqdesc->numevents, AI_MDL_HL1_MAX_EVENTS, "animation event"); |
1229 | | |
1230 | 1.03k | int num_blend_controllers = 0; |
1231 | 1.03k | if (!get_num_blend_controllers(pseqdesc->numblends, num_blend_controllers)) { |
1232 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid sequence blend count"); |
1233 | 0 | } |
1234 | | |
1235 | | // Clear the list of children for the upcoming sequence info node. |
1236 | 1.03k | sequence_info_node_children.clear(); |
1237 | | |
1238 | 1.03k | aiNode *sequence_info_node = sequence_infos_node->mChildren[i] = new aiNode(unique_sequence_names_[i]); |
1239 | 1.03k | sequence_info_node->mParent = sequence_infos_node; |
1240 | | |
1241 | | // Setup sequence info node Metadata. |
1242 | 1.03k | aiMetadata *md = sequence_info_node->mMetaData = aiMetadata::Alloc(16); |
1243 | 1.03k | md->Set(0, "AnimationIndex", animation_index); |
1244 | 1.03k | animation_index += pseqdesc->numblends; |
1245 | | |
1246 | | // Reference the sequence group by name. This allows us to search a particular |
1247 | | // sequence group by name using aiNode(s). |
1248 | 1.03k | md->Set(1, "SequenceGroup", aiString(unique_sequence_groups_names_[pseqdesc->seqgroup])); |
1249 | 1.03k | md->Set(2, "FramesPerSecond", pseqdesc->fps); |
1250 | 1.03k | md->Set(3, "NumFrames", pseqdesc->numframes); |
1251 | 1.03k | md->Set(4, "NumBlends", pseqdesc->numblends); |
1252 | 1.03k | md->Set(5, "Activity", pseqdesc->activity); |
1253 | 1.03k | md->Set(6, "ActivityWeight", pseqdesc->actweight); |
1254 | 1.03k | md->Set(7, "MotionFlags", pseqdesc->motiontype); |
1255 | 1.03k | md->Set(8, "MotionBone", temp_bones_[pseqdesc->motionbone].node->mName); |
1256 | 1.03k | md->Set(9, "LinearMovement", aiVector3D(pseqdesc->linearmovement[0], pseqdesc->linearmovement[1], pseqdesc->linearmovement[2])); |
1257 | 1.03k | md->Set(10, "BBMin", aiVector3D(pseqdesc->bbmin[0], pseqdesc->bbmin[1], pseqdesc->bbmin[2])); |
1258 | 1.03k | md->Set(11, "BBMax", aiVector3D(pseqdesc->bbmax[0], pseqdesc->bbmax[1], pseqdesc->bbmax[2])); |
1259 | 1.03k | md->Set(12, "EntryNode", pseqdesc->entrynode); |
1260 | 1.03k | md->Set(13, "ExitNode", pseqdesc->exitnode); |
1261 | 1.03k | md->Set(14, "NodeFlags", pseqdesc->nodeflags); |
1262 | 1.03k | md->Set(15, "Flags", pseqdesc->flags); |
1263 | | |
1264 | 1.03k | if (import_settings_.read_blend_controllers && num_blend_controllers) { |
1265 | | // Read blend controllers info. |
1266 | 5 | aiNode *blend_controllers_node = new aiNode(AI_MDL_HL1_NODE_BLEND_CONTROLLERS); |
1267 | 5 | sequence_info_node_children.push_back(blend_controllers_node); |
1268 | 5 | blend_controllers_node->mParent = sequence_info_node; |
1269 | 5 | blend_controllers_node->mNumChildren = static_cast<unsigned int>(num_blend_controllers); |
1270 | 5 | blend_controllers_node->mChildren = new aiNode *[blend_controllers_node->mNumChildren](); |
1271 | | |
1272 | 11 | for (unsigned int j = 0; j < blend_controllers_node->mNumChildren; ++j) { |
1273 | 6 | aiNode *blend_controller_node = blend_controllers_node->mChildren[j] = new aiNode(); |
1274 | 6 | blend_controller_node->mParent = blend_controllers_node; |
1275 | | |
1276 | 6 | aiMetadata *metaData = blend_controller_node->mMetaData = aiMetadata::Alloc(3); |
1277 | 6 | metaData->Set(0, "Start", pseqdesc->blendstart[j]); |
1278 | 6 | metaData->Set(1, "End", pseqdesc->blendend[j]); |
1279 | 6 | metaData->Set(2, "MotionFlags", pseqdesc->blendtype[j]); |
1280 | 6 | } |
1281 | 5 | } |
1282 | | |
1283 | 1.03k | if (import_settings_.read_animation_events && pseqdesc->numevents) { |
1284 | | // Read animation events. |
1285 | | |
1286 | 59 | const AnimEvent_HL1 *pevent = get_buffer_data<AnimEvent_HL1>(pseqdesc->eventindex, pseqdesc->numevents); |
1287 | | |
1288 | 59 | aiNode *pEventsNode = new aiNode(AI_MDL_HL1_NODE_ANIMATION_EVENTS); |
1289 | 59 | sequence_info_node_children.push_back(pEventsNode); |
1290 | 59 | pEventsNode->mParent = sequence_info_node; |
1291 | 59 | pEventsNode->mNumChildren = static_cast<unsigned int>(pseqdesc->numevents); |
1292 | 59 | auto event_children = std::make_unique<aiNode *[]>(pEventsNode->mNumChildren); |
1293 | 59 | pEventsNode->mChildren = event_children.release(); |
1294 | | |
1295 | 1.91k | for (unsigned int j = 0; j < pEventsNode->mNumChildren; ++j, ++pevent) { |
1296 | 1.85k | aiNode *pEvent = pEventsNode->mChildren[j] = new aiNode(); |
1297 | 1.85k | pEvent->mParent = pEventsNode; |
1298 | | |
1299 | 1.85k | aiMetadata *metaData = pEvent->mMetaData = aiMetadata::Alloc(3); |
1300 | 1.85k | metaData->Set(0, "Frame", pevent->frame); |
1301 | 1.85k | metaData->Set(1, "ScriptEvent", pevent->event); |
1302 | 1.85k | metaData->Set(2, "Options", aiString(pevent->options)); |
1303 | 1.85k | } |
1304 | 59 | } |
1305 | | |
1306 | 1.03k | if (sequence_info_node_children.size()) { |
1307 | 57 | sequence_info_node->addChildren( |
1308 | 57 | static_cast<unsigned int>(sequence_info_node_children.size()), |
1309 | 57 | sequence_info_node_children.data()); |
1310 | 57 | } |
1311 | 1.03k | } |
1312 | 495 | } |
1313 | | |
1314 | | // ------------------------------------------------------------------------------------------------ |
1315 | 496 | void HL1MDLLoader::read_sequence_transitions() { |
1316 | 496 | if (!header_->numtransitions) { |
1317 | 455 | return; |
1318 | 455 | } |
1319 | | |
1320 | | // Read sequence transition graph. |
1321 | 41 | aiNode *transition_graph_node = new aiNode(AI_MDL_HL1_NODE_SEQUENCE_TRANSITION_GRAPH); |
1322 | 41 | rootnode_children_.push_back(transition_graph_node); |
1323 | | |
1324 | 41 | if (header_->numtransitions > std::numeric_limits<int>::max() / header_->numtransitions) { |
1325 | 2 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid transition count"); |
1326 | 2 | } |
1327 | | |
1328 | 39 | const int num_transition_entries = header_->numtransitions * header_->numtransitions; |
1329 | 39 | const uint8_t *ptransitions = get_buffer_data<uint8_t>(header_->transitionindex, num_transition_entries); |
1330 | 39 | aiMetadata *md = aiMetadata::Alloc(static_cast<unsigned int>(num_transition_entries)); |
1331 | 39 | transition_graph_node->mMetaData = md; |
1332 | 26.1k | for (unsigned int i = 0; i < md->mNumProperties; ++i) |
1333 | 26.1k | md->Set(i, std::to_string(i), static_cast<int>(ptransitions[i])); |
1334 | 39 | } |
1335 | | |
1336 | 493 | void HL1MDLLoader::read_attachments() { |
1337 | 493 | if (!header_->numattachments) { |
1338 | 458 | return; |
1339 | 458 | } |
1340 | | |
1341 | 35 | const Attachment_HL1 *pattach = get_buffer_data<Attachment_HL1>(header_->attachmentindex, header_->numattachments); |
1342 | | |
1343 | 35 | aiNode *attachments_node = new aiNode(AI_MDL_HL1_NODE_ATTACHMENTS); |
1344 | 35 | rootnode_children_.push_back(attachments_node); |
1345 | 35 | attachments_node->mNumChildren = static_cast<unsigned int>(header_->numattachments); |
1346 | 35 | attachments_node->mChildren = new aiNode *[attachments_node->mNumChildren](); |
1347 | | |
1348 | 103 | for (int i = 0; i < header_->numattachments; ++i, ++pattach) { |
1349 | 68 | validate_index(pattach->bone, header_->numbones, "attachment bone"); |
1350 | | |
1351 | 68 | aiNode *attachment_node = attachments_node->mChildren[i] = new aiNode(); |
1352 | 68 | attachment_node->mParent = attachments_node; |
1353 | 68 | attachment_node->mMetaData = aiMetadata::Alloc(2); |
1354 | 68 | attachment_node->mMetaData->Set(0, "Position", aiVector3D(pattach->org[0], pattach->org[1], pattach->org[2])); |
1355 | | // Reference the bone by name. This allows us to search a particular |
1356 | | // bone by name using aiNode(s). |
1357 | 68 | attachment_node->mMetaData->Set(1, "Bone", temp_bones_[pattach->bone].node->mName); |
1358 | 68 | } |
1359 | 35 | } |
1360 | | |
1361 | | // ------------------------------------------------------------------------------------------------ |
1362 | 467 | void HL1MDLLoader::read_hitboxes() { |
1363 | 467 | if (!header_->numhitboxes) { |
1364 | 384 | return; |
1365 | 384 | } |
1366 | | |
1367 | 83 | const Hitbox_HL1 *phitbox = get_buffer_data<Hitbox_HL1>(header_->hitboxindex, header_->numhitboxes); |
1368 | | |
1369 | 83 | aiNode *hitboxes_node = new aiNode(AI_MDL_HL1_NODE_HITBOXES); |
1370 | 83 | rootnode_children_.push_back(hitboxes_node); |
1371 | 83 | hitboxes_node->mNumChildren = static_cast<unsigned int>(header_->numhitboxes); |
1372 | 83 | hitboxes_node->mChildren = new aiNode *[hitboxes_node->mNumChildren](); |
1373 | | |
1374 | 209 | for (int i = 0; i < header_->numhitboxes; ++i, ++phitbox) { |
1375 | 126 | validate_index(phitbox->bone, header_->numbones, "hitbox bone"); |
1376 | | |
1377 | 126 | aiNode *hitbox_node = hitboxes_node->mChildren[i] = new aiNode(); |
1378 | 126 | hitbox_node->mParent = hitboxes_node; |
1379 | | |
1380 | 126 | aiMetadata *md = hitbox_node->mMetaData = aiMetadata::Alloc(4); |
1381 | | // Reference the bone by name. This allows us to search a particular |
1382 | | // bone by name using aiNode(s). |
1383 | 126 | md->Set(0, "Bone", temp_bones_[phitbox->bone].node->mName); |
1384 | 126 | md->Set(1, "HitGroup", phitbox->group); |
1385 | 126 | md->Set(2, "BBMin", aiVector3D(phitbox->bbmin[0], phitbox->bbmin[1], phitbox->bbmin[2])); |
1386 | 126 | md->Set(3, "BBMax", aiVector3D(phitbox->bbmax[0], phitbox->bbmax[1], phitbox->bbmax[2])); |
1387 | 126 | } |
1388 | 83 | } |
1389 | | |
1390 | | // ------------------------------------------------------------------------------------------------ |
1391 | 449 | void HL1MDLLoader::read_bone_controllers() { |
1392 | 449 | if (!header_->numbonecontrollers) { |
1393 | 423 | return; |
1394 | 423 | } |
1395 | | |
1396 | 26 | const BoneController_HL1 *pbonecontroller = get_buffer_data<BoneController_HL1>( |
1397 | 26 | header_->bonecontrollerindex, |
1398 | 26 | header_->numbonecontrollers); |
1399 | | |
1400 | 26 | aiNode *bones_controller_node = new aiNode(AI_MDL_HL1_NODE_BONE_CONTROLLERS); |
1401 | 26 | rootnode_children_.push_back(bones_controller_node); |
1402 | 26 | bones_controller_node->mNumChildren = static_cast<unsigned int>(header_->numbonecontrollers); |
1403 | 26 | bones_controller_node->mChildren = new aiNode *[bones_controller_node->mNumChildren](); |
1404 | | |
1405 | 71 | for (int i = 0; i < header_->numbonecontrollers; ++i, ++pbonecontroller) { |
1406 | 45 | validate_index(pbonecontroller->bone, header_->numbones, "bone controller bone"); |
1407 | | |
1408 | 45 | aiNode *bone_controller_node = bones_controller_node->mChildren[i] = new aiNode(); |
1409 | 45 | bone_controller_node->mParent = bones_controller_node; |
1410 | | |
1411 | 45 | aiMetadata *md = bone_controller_node->mMetaData = aiMetadata::Alloc(5); |
1412 | | // Reference the bone by name. This allows us to search a particular |
1413 | | // bone by name using aiNode(s). |
1414 | 45 | md->Set(0, "Bone", temp_bones_[pbonecontroller->bone].node->mName); |
1415 | 45 | md->Set(1, "MotionFlags", pbonecontroller->type); |
1416 | 45 | md->Set(2, "Start", pbonecontroller->start); |
1417 | 45 | md->Set(3, "End", pbonecontroller->end); |
1418 | 45 | md->Set(4, "Channel", pbonecontroller->index); |
1419 | 45 | } |
1420 | 26 | } |
1421 | | |
1422 | | // ------------------------------------------------------------------------------------------------ |
1423 | 433 | void HL1MDLLoader::read_global_info() { |
1424 | 433 | aiNode *global_info_node = new aiNode(AI_MDL_HL1_NODE_GLOBAL_INFO); |
1425 | 433 | rootnode_children_.push_back(global_info_node); |
1426 | | |
1427 | 433 | aiMetadata *md = global_info_node->mMetaData = aiMetadata::Alloc(import_settings_.read_misc_global_info ? 16 : 11); |
1428 | 433 | md->Set(0, "Version", AI_MDL_HL1_VERSION); |
1429 | 433 | md->Set(1, "NumBodyparts", header_->numbodyparts); |
1430 | 433 | md->Set(2, "NumModels", total_models_); |
1431 | 433 | md->Set(3, "NumBones", header_->numbones); |
1432 | 433 | md->Set(4, "NumAttachments", import_settings_.read_attachments ? header_->numattachments : 0); |
1433 | 433 | md->Set(5, "NumSkinFamilies", texture_header_->numskinfamilies); |
1434 | 433 | md->Set(6, "NumHitboxes", import_settings_.read_hitboxes ? header_->numhitboxes : 0); |
1435 | 433 | md->Set(7, "NumBoneControllers", import_settings_.read_bone_controllers ? header_->numbonecontrollers : 0); |
1436 | 433 | md->Set(8, "NumSequences", import_settings_.read_animations ? header_->numseq : 0); |
1437 | 433 | md->Set(9, "NumBlendControllers", import_settings_.read_blend_controllers ? num_blend_controllers_ : 0); |
1438 | 433 | md->Set(10, "NumTransitionNodes", import_settings_.read_sequence_transitions ? header_->numtransitions : 0); |
1439 | | |
1440 | 433 | if (import_settings_.read_misc_global_info) { |
1441 | 433 | md->Set(11, "EyePosition", aiVector3D(header_->eyeposition[0], header_->eyeposition[1], header_->eyeposition[2])); |
1442 | 433 | md->Set(12, "HullMin", aiVector3D(header_->min[0], header_->min[1], header_->min[2])); |
1443 | 433 | md->Set(13, "HullMax", aiVector3D(header_->max[0], header_->max[1], header_->max[2])); |
1444 | 433 | md->Set(14, "CollisionMin", aiVector3D(header_->bbmin[0], header_->bbmin[1], header_->bbmin[2])); |
1445 | 433 | md->Set(15, "CollisionMax", aiVector3D(header_->bbmax[0], header_->bbmax[1], header_->bbmax[2])); |
1446 | 433 | } |
1447 | 433 | } |
1448 | | |
1449 | | // ------------------------------------------------------------------------------------------------ |
1450 | | /** @brief This method reads a compressed anim value. |
1451 | | * |
1452 | | * @note The structure of this method is taken from HL2 source code. |
1453 | | * Although this is from HL2, it's implementation is almost identical |
1454 | | * to code found in HL1 SDK. See HL1 and HL2 SDKs for more info. |
1455 | | * |
1456 | | * source: |
1457 | | * HL1 source code. |
1458 | | * file: studio_render.cpp |
1459 | | * function(s): CalcBoneQuaternion and CalcBonePosition |
1460 | | * |
1461 | | * HL2 source code. |
1462 | | * file: bone_setup.cpp |
1463 | | * function(s): ExtractAnimValue |
1464 | | */ |
1465 | | void HL1MDLLoader::extract_anim_value( |
1466 | | const AnimValue_HL1 *panimvalue, |
1467 | | const HL1DataBuffer &anim_buffer, |
1468 | 41.9k | int frame, float bone_scale, ai_real &value) { |
1469 | 41.9k | int k = frame; |
1470 | | |
1471 | | // find span of values that includes the frame we want |
1472 | 54.2k | while (true) { |
1473 | 54.2k | if (!anim_buffer.Contains(panimvalue, 1) || !panimvalue->num.total) { |
1474 | 32 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid animation value"); |
1475 | 32 | } |
1476 | 54.2k | if (panimvalue->num.total > k) { |
1477 | 41.9k | break; |
1478 | 41.9k | } |
1479 | | |
1480 | 12.3k | k -= panimvalue->num.total; |
1481 | 12.3k | if (!anim_buffer.Contains(panimvalue, static_cast<size_t>(panimvalue->num.valid) + 2)) { |
1482 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid animation value"); |
1483 | 0 | } |
1484 | | |
1485 | 12.3k | panimvalue += panimvalue->num.valid + 1; |
1486 | 12.3k | } |
1487 | | |
1488 | 41.9k | if (!anim_buffer.Contains(panimvalue, 1)) { |
1489 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid animation value"); |
1490 | 0 | } |
1491 | | |
1492 | | // Bah, missing blend! |
1493 | 41.9k | const size_t value_index = (panimvalue->num.valid > k) ? static_cast<size_t>(k + 1) : panimvalue->num.valid; |
1494 | 41.9k | if (!anim_buffer.Contains(panimvalue, value_index + 1)) { |
1495 | 0 | throw DeadlyImportError(MDL_HALFLIFE_LOG_HEADER "Invalid animation value"); |
1496 | 0 | } |
1497 | | |
1498 | 41.9k | value = panimvalue[value_index].value * bone_scale; |
1499 | 41.9k | } |
1500 | | |
1501 | | // ------------------------------------------------------------------------------------------------ |
1502 | | // Get the number of blend controllers. |
1503 | 2.94k | bool HL1MDLLoader::get_num_blend_controllers(const int num_blend_animations, int &num_blend_controllers) { |
1504 | | |
1505 | 2.94k | switch (num_blend_animations) { |
1506 | 2.88k | case SequenceBlendMode_HL1::NoBlend: |
1507 | 2.88k | num_blend_controllers = 0; |
1508 | 2.88k | return true; |
1509 | 14 | case SequenceBlendMode_HL1::TwoWayBlending: |
1510 | 14 | num_blend_controllers = 1; |
1511 | 14 | return true; |
1512 | 20 | case SequenceBlendMode_HL1::FourWayBlending: |
1513 | 20 | num_blend_controllers = 2; |
1514 | 20 | return true; |
1515 | 34 | default: |
1516 | 34 | num_blend_controllers = 0; |
1517 | | ASSIMP_LOG_WARN(MDL_HALFLIFE_LOG_HEADER "Unsupported number of blend animations (", num_blend_animations, ")"); |
1518 | 34 | return false; |
1519 | 2.94k | } |
1520 | 2.94k | } |
1521 | | |
1522 | | } // namespace HalfLife |
1523 | | } // namespace MDL |
1524 | | } // namespace Assimp |