Cafu Engine
Loader_mdl_hl2_mdl.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_HL2_MDL_FILE_FORMAT_HPP_INCLUDED
8 #define CAFU_HL2_MDL_FILE_FORMAT_HPP_INCLUDED
9 
10 #include "Loader_mdl_hl2_vvd.hpp"
11 #include "Loader_mdl_hl2_vtx.hpp"
12 #include "Loader_mdl_hl2_anim.hpp"
13 #include "Math3D/Angles.hpp"
14 #include "Math3D/Quaternion.hpp"
15 
16 
17 namespace HL2mdl
18 {
19  using namespace cf::math;
20  class StudioModelT;
21 
22 
23  /// This class describes a mesh of a model.
24  struct StudioMeshT
25  {
26  uint32_t Material; ///< See LoaderHL2mdlT::Load(Skins, ...) for details!
27  int32_t ModelOffset; ///< Points back to the parent StudioModelT. Usually negative.
28 
29  uint32_t NumVertices; ///< The number of unique vertices (along with normals, tangents and texcoords) in this mesh.
30  uint32_t VerticesOffset; ///< The first vertex of the mesh, relative to the first vertex of the model.
31 
32  uint32_t NumFlexes; ///< Flexes are for animating vertices (unrelated to / independent from bones).
33  uint32_t FlexesOffset;
34 
35  uint32_t MaterialType;
36  uint32_t MaterialParam;
37 
38  uint32_t MeshID; ///< A unique ID for this mesh.
39  Vector3fT Center;
40 
41  uint32_t Unused1;
42  uint32_t NumLODVertices[HL2mdl_MAX_NUM_LODS];
43  uint32_t Unused2[8];
44 
45 
46  public:
47 
48  const StudioModelT* GetModel() const { return (StudioModelT*)(((uint8_t*)this) + ModelOffset); }
49  const StudioVertexT* GetVertices() const;
50  const Vector4D* GetTangents() const;
51 
52  std::ostream& print(std::ostream& os, const char* indent) const;
53  };
54 
55 
56  /// This class describes a model of a body part.
58  {
59  public:
60 
61  char Name[64];
62  uint32_t Type;
63  float BoundingRadius;
64 
65  uint32_t NumMeshes; ///< LOD is implemented at mesh level, not at model level.
66  uint32_t MeshesOffset;
67 
68  uint32_t NumVertices; ///< The number of unique vertices (along with normals, tangents and texcoords) in this mesh.
69  uint32_t VertOffset; ///< The byte-offset into the VVD file to the first vertex. See GetVertices() for details.
70  uint32_t TangentsOffset; ///< As VertOffset, but for tangents.
71 
72  uint32_t NumAttachments;
73  uint32_t AttachmentsOffset;
74 
75  uint32_t NumEyeballs;
76  uint32_t EyeballsOffset;
77 
78  const StudioVertexT* m_Vertices; ///< = VertexHeader->GetVertices();
79  const Vector4D* m_Tangents; ///< = VertexHeader->GetTangents();
80 
81  #if UINTPTR_MAX == UINT32_MAX
82  uint32_t Unused[8]; ///< Pointers are 32 bits wide.
83  #else
84  uint32_t Unused[6]; ///< Pointers are 64 bits wide.
85  #endif
86 
87 
88  public:
89 
90  const StudioMeshT* GetMeshes() const
91  {
92  return (StudioMeshT*)(((uint8_t*)this) + MeshesOffset);
93  }
94 
95  const StudioVertexT* GetVertices() const
96  {
97  assert(VertOffset % sizeof(StudioVertexT) == 0);
98 
99  return m_Vertices ? m_Vertices + (VertOffset / sizeof(StudioVertexT)) : NULL;
100  }
101 
102  const Vector4D* GetTangents() const
103  {
104  assert(TangentsOffset % sizeof(Vector4D) == 0);
105 
106  return m_Tangents ? m_Tangents + (TangentsOffset / sizeof(Vector4D)) : NULL;
107  }
108 
109  std::ostream& print(std::ostream& os, const char* indent) const;
110  };
111 
112 
113  inline const StudioVertexT* StudioMeshT::GetVertices() const
114  {
115  return GetModel()->GetVertices() + VerticesOffset;
116  }
117 
118  inline const Vector4D* StudioMeshT::GetTangents() const
119  {
120  return GetModel()->GetTangents() + VerticesOffset;
121  }
122 
123 
124  /// This class describes a body part.
125  /// A body part contains one or several models that are mutually exclusive, e.g.
126  /// "empty holster", "holster with gun", "walkie-talkie", "empty/nothing".
127  /// Bodies ("body variants") can be constructed from all such possible combinations.
128  /// The related hierarchy is:
129  ///
130  /// body variant // a result of the combinations below
131  /// body part // all needed
132  /// model // mutually exclusive
133  /// meshes // all needed
134  ///
136  {
137  uint32_t NameOffset;
138  uint32_t NumModels;
139  uint32_t Base; ///< For the i-th body ("body variant"), use from this body part the `(i / Base) % NumModels`-th model.
140  uint32_t ModelsOffset;
141 
142 
143  public:
144 
145  const char* GetName() const { return ((char*)this) + NameOffset; }
146  StudioModelT* GetModels() const { return (StudioModelT*)(((uint8_t*)this) + ModelsOffset); }
147 
148  std::ostream& print(std::ostream& os, const char* indent) const;
149  };
150 
151 
152  /// Used in StudioBoneT.
154  {
155  public:
156 
157  Matrix3x4fT() { }
158 
159  /// Computes M*v, where M is this matrix.
160  /// The w-component of v is assumed to be 1 (v being a point, not a direction vector).
161  /// The fourth, missing row of M is assumed to be (0 0 0 1).
162  /// That means that both the rotation (and scale) *and* the translation of M is applied to v.
163  /// @param v A point.
164  /// @return M*v. The w-component of the returned vector is implied to be 1.
165  Vector3fT Mul1(const Vector3fT& v) const
166  {
167  return Vector3fT(m[0][0]*v.x + m[0][1]*v.y + m[0][2]*v.z + m[0][3],
168  m[1][0]*v.x + m[1][1]*v.y + m[1][2]*v.z + m[1][3],
169  m[2][0]*v.x + m[2][1]*v.y + m[2][2]*v.z + m[2][3]);
170  }
171 
172 
173  private:
174 
175  float m[3][4];
176  };
177 
178 
179  /// This class describes a bone.
180  /// The entirety of the bones define the skeleton of the model.
181  struct StudioBoneT
182  {
183  uint32_t NameOffset;
184  int32_t Parent;
185  int32_t BoneController[6]; ///< Indices to the bone controllers of this bone, -1 means "none".
186 
187  Vector3fT Pos; ///< The bone's position in the default pose.
188  QuaternionfT Quat; ///< The bone's orientation in the default pose.
189  AnglesfT Angles; ///< The bone's rotation in the default pose (Euler angles in radians, not degrees).
190 
191  Vector3fT PosScale; ///< Compression scale.
192  Vector3fT RotScale;
193 
194  Matrix3x4fT PoseToBone; ///< Transforms a (default pose's) mesh vertex from Model Space to Bone Space.
195  QuaternionfT Alignment;
196  uint32_t Flags;
197  uint32_t ProcType;
198  uint32_t ProcOffset;
199  uint32_t PhysicsBone;
200  uint32_t SurfacePropNameOffset;
201  uint32_t Contents;
202  uint32_t Unused[8];
203 
204 
205  public:
206 
207  const char* GetName() const { return ((char*)this) + NameOffset; }
208  const char* GetSurfacePropName() const { return ((char*)this) + SurfacePropNameOffset; }
209 
210  std::ostream& print(std::ostream& os, const char* indent) const;
211  };
212 
213 
215  {
216  uint32_t NameOffset;
217  uint32_t Flags;
218  uint32_t Unknown;
219  uint32_t Unused[13];
220 
221 
222  public:
223 
224  const char* GetName() const { return ((char*)this) + NameOffset; }
225 
226  std::ostream& print(std::ostream& os, const char* indent) const;
227  };
228 
229 
230  /// This is the header of an MDL model file.
232  {
233  public:
234 
235  static const uint32_t FILE_ID_IDST; ///< The ID of a MDL studio model file.
236  static const uint32_t FILE_VERSION; ///< The currently supported file format version.
237 
238  uint32_t ID; ///< Must be FILE_ID_IDST.
239  uint32_t Version; ///< Must be FILE_VERSION.
240  uint32_t Checksum; ///< Must match the checksum in VertexHeaderT.
241  char Name[64]; ///< The (relative) file name of this model.
242  uint32_t FileSize; ///< The size in bytes of the MDL file.
243 
244  Vector3fT EyePos; ///< Ideal eye position.
245  Vector3fT IllumPos; ///< Illumination center.
246  BoundingBox3fT HullBB; ///< The ideal movement hull size.
247  BoundingBox3fT ViewBB; ///< The bounding box for visual clipping, possibly empty (0, 0, 0) (0, 0, 0).
248  uint32_t Flags;
249 
250  uint32_t NumBones; ///< The number of StudioBoneT instances.
251  uint32_t BonesOffset; ///< The byte offset to the first bone, relative to "this".
252 
253  uint32_t NumBoneControllers; ///< The number of StudioBoneControllerT instances.
254  uint32_t BoneControllersOffset; ///< The byte offset to the first bone controller, relative to "this".
255 
256  uint32_t NumHitboxSets;
257  uint32_t HitboxSetsOffset;
258 
259  uint32_t NumLocalAnimDescs; ///< Animation/pose descriptions stored locally (in this MDL file).
260  uint32_t LocalAnimDescsOffset;
261 
262  uint32_t NumLocalSequDescs; ///< Sequence descriptions stored locally (in this MDL file).
263  uint32_t LocalSequDescsOffset;
264 
265  uint32_t ActivityListVersion;
266  uint32_t EventsIndexed;
267 
268  uint32_t NumTextures; ///< The names of all materials (VMT files) used in this model. The VMT scripts in turn point to the texture images (VTF).
269  uint32_t TexturesOffset;
270 
271  uint32_t NumTexturePaths; ///< The search paths to be used for the VMT material script files.
272  uint32_t TexturePathsOffset;
273 
274  uint32_t NumSkinRefs;
275  uint32_t NumSkinFamilies;
276  uint32_t SkinsOffset;
277 
278  uint32_t NumBodyParts; ///< The number of StudioBodyPartT instances.
279  uint32_t BodyPartsOffset; ///< The byte offset to the first body part, relative to "this".
280 
281  uint32_t NumLocalAttachments;
282  uint32_t LocalAttachmentsOffset;
283 
284  uint32_t NumLocalNodes; ///< Nodes form a graph, transitioning from animation node to animation node.
285  uint32_t LocalNodeOffset;
286  uint32_t LocalNodenameOffset;
287 
288  uint32_t NumFlexDesc;
289  uint32_t FlexDescOffset;
290 
291  uint32_t NumFlexControllers;
292  uint32_t FlexControllerOffset;
293 
294  uint32_t NumFlexRules;
295  uint32_t FlexRuleOffset;
296 
297  uint32_t NumIKChains;
298  uint32_t IKChainOffset;
299 
300  uint32_t NumMouths;
301  uint32_t MouthOffset;
302 
303  uint32_t NumLocalPoseParameters;
304  uint32_t LocalPoseParamOffset;
305 
306  uint32_t SurfacePropOffset;
307 
308  uint32_t KeyValueOffset;
309  uint32_t KeyValueSize;
310 
311  uint32_t NumLocalIKAutoPlayLocks;
312  uint32_t LocalIKAutoPlayLockOffset;
313 
314  float Mass;
315  uint32_t Contents;
316 
317  uint32_t NumIncludeModels; ///< These represent external models with additional resources, e.g. animations.
318  uint32_t IncludeModelOffset;
319 
320 
321  public:
322 
323  const StudioBoneT* GetBones() const { return (StudioBoneT*)(((uint8_t*)this) + BonesOffset); }
324 
325  const StudioAnimDescT* GetLocalAnimDescs() const { return (StudioAnimDescT*)(((uint8_t*)this) + LocalAnimDescsOffset); };
326  const StudioSequDescT* GetLocalSequDescs() const { return (StudioSequDescT*)(((uint8_t*)this) + LocalSequDescsOffset); };
327 
328  const StudioAnimDescT* GetAnimDesc(uint32_t i) const
329  {
330  if (NumIncludeModels == 0) return GetLocalAnimDescs() + i;
331 
332  // We don't support virtual models.
333  assert(false);
334  return NULL;
335  }
336 
337  const StudioTextureT* GetTextures() const { return (StudioTextureT*)(((uint8_t*)this) + TexturesOffset); }
338  const char* GetTexturePath(uint32_t i) const { return ((char*)this) + *((int*)(((uint8_t*)this) + TexturePathsOffset) + i); }
339  const uint16_t* GetSkinRefs() const { return (uint16_t*)(((uint8_t*)this) + SkinsOffset); }
340 
341  const StudioBodyPartT* GetBodyParts() const { return (StudioBodyPartT*)(((uint8_t*)this) + BodyPartsOffset); }
342  };
343 }
344 
345 #endif
const Vector4D * m_Tangents
= VertexHeader->GetTangents();
Definition: Loader_mdl_hl2_mdl.hpp:79
This class describes a body part.
Definition: Loader_mdl_hl2_mdl.hpp:135
static const uint32_t FILE_VERSION
The currently supported file format version.
Definition: Loader_mdl_hl2_mdl.hpp:236
BoundingBox3fT ViewBB
The bounding box for visual clipping, possibly empty (0, 0, 0) (0, 0, 0).
Definition: Loader_mdl_hl2_mdl.hpp:247
Vector3fT IllumPos
Illumination center.
Definition: Loader_mdl_hl2_mdl.hpp:245
uint32_t NumBones
The number of StudioBoneT instances.
Definition: Loader_mdl_hl2_mdl.hpp:250
uint32_t VerticesOffset
The first vertex of the mesh, relative to the first vertex of the model.
Definition: Loader_mdl_hl2_mdl.hpp:30
uint32_t MeshID
A unique ID for this mesh.
Definition: Loader_mdl_hl2_mdl.hpp:38
uint32_t BonesOffset
The byte offset to the first bone, relative to "this".
Definition: Loader_mdl_hl2_mdl.hpp:251
This is the header of an MDL model file.
Definition: Loader_mdl_hl2_mdl.hpp:231
Vector3fT Mul1(const Vector3fT &v) const
Computes M*v, where M is this matrix.
Definition: Loader_mdl_hl2_mdl.hpp:165
uint32_t Base
For the i-th body ("body variant"), use from this body part the (i / Base) % NumModels-th model...
Definition: Loader_mdl_hl2_mdl.hpp:139
Definition: Loader_mdl_hl2_vvd.hpp:29
uint32_t NumBodyParts
The number of StudioBodyPartT instances.
Definition: Loader_mdl_hl2_mdl.hpp:278
Definition: Loader_mdl_hl1.h:175
int32_t ModelOffset
Points back to the parent StudioModelT. Usually negative.
Definition: Loader_mdl_hl2_mdl.hpp:27
uint32_t TangentsOffset
As VertOffset, but for tangents.
Definition: Loader_mdl_hl2_mdl.hpp:70
Definition: Loader_mdl_hl2_mdl.hpp:214
uint32_t NumTexturePaths
The search paths to be used for the VMT material script files.
Definition: Loader_mdl_hl2_mdl.hpp:271
uint32_t NumBoneControllers
The number of StudioBoneControllerT instances.
Definition: Loader_mdl_hl2_mdl.hpp:253
uint32_t NumLocalSequDescs
Sequence descriptions stored locally (in this MDL file).
Definition: Loader_mdl_hl2_mdl.hpp:262
uint32_t Version
Must be FILE_VERSION.
Definition: Loader_mdl_hl2_mdl.hpp:239
T y
The y-component of this vector.
Definition: Vector3.hpp:41
uint32_t NumTextures
The names of all materials (VMT files) used in this model. The VMT scripts in turn point to the textu...
Definition: Loader_mdl_hl2_mdl.hpp:268
uint32_t Material
See LoaderHL2mdlT::Load(Skins, ...) for details!
Definition: Loader_mdl_hl2_mdl.hpp:26
Definition: Loader_mdl_hl1.h:185
BoundingBox3fT HullBB
The ideal movement hull size.
Definition: Loader_mdl_hl2_mdl.hpp:246
Definition: Loader_mdl_hl2_anim.hpp:136
uint32_t NumLocalAnimDescs
Animation/pose descriptions stored locally (in this MDL file).
Definition: Loader_mdl_hl2_mdl.hpp:259
AnglesfT Angles
The bone's rotation in the default pose (Euler angles in radians, not degrees).
Definition: Loader_mdl_hl2_mdl.hpp:189
uint32_t NumLocalNodes
Nodes form a graph, transitioning from animation node to animation node.
Definition: Loader_mdl_hl2_mdl.hpp:284
uint32_t ID
Must be FILE_ID_IDST.
Definition: Loader_mdl_hl2_mdl.hpp:238
This class describes a mesh of a model.
Definition: Loader_mdl_hl2_mdl.hpp:24
uint32_t NumVertices
The number of unique vertices (along with normals, tangents and texcoords) in this mesh...
Definition: Loader_mdl_hl2_mdl.hpp:29
uint32_t NumIncludeModels
These represent external models with additional resources, e.g. animations.
Definition: Loader_mdl_hl2_mdl.hpp:317
uint32_t VertOffset
The byte-offset into the VVD file to the first vertex. See GetVertices() for details.
Definition: Loader_mdl_hl2_mdl.hpp:69
uint32_t NumVertices
The number of unique vertices (along with normals, tangents and texcoords) in this mesh...
Definition: Loader_mdl_hl2_mdl.hpp:68
uint32_t NumMeshes
LOD is implemented at mesh level, not at model level.
Definition: Loader_mdl_hl2_mdl.hpp:65
Definition: Loader_mdl_hl2_vvd.hpp:43
const StudioVertexT * m_Vertices
= VertexHeader->GetVertices();
Definition: Loader_mdl_hl2_mdl.hpp:78
Vector3fT EyePos
Ideal eye position.
Definition: Loader_mdl_hl2_mdl.hpp:244
Vector3fT PosScale
Compression scale.
Definition: Loader_mdl_hl2_mdl.hpp:191
QuaternionfT Quat
The bone's orientation in the default pose.
Definition: Loader_mdl_hl2_mdl.hpp:188
uint32_t NumFlexes
Flexes are for animating vertices (unrelated to / independent from bones).
Definition: Loader_mdl_hl2_mdl.hpp:32
T z
The z-component of this vector.
Definition: Vector3.hpp:42
Definition: Loader_mdl_hl1.h:194
static const uint32_t FILE_ID_IDST
The ID of a MDL studio model file.
Definition: Loader_mdl_hl2_mdl.hpp:235
Matrix3x4fT PoseToBone
Transforms a (default pose's) mesh vertex from Model Space to Bone Space.
Definition: Loader_mdl_hl2_mdl.hpp:194
T x
The x-component of this vector.
Definition: Vector3.hpp:40
Vector3fT Pos
The bone's position in the default pose.
Definition: Loader_mdl_hl2_mdl.hpp:187
Used in StudioBoneT.
Definition: Loader_mdl_hl2_mdl.hpp:153
uint32_t Checksum
Must match the checksum in VertexHeaderT.
Definition: Loader_mdl_hl2_mdl.hpp:240
This class describes a model of a body part.
Definition: Loader_mdl_hl2_mdl.hpp:57
uint32_t FileSize
The size in bytes of the MDL file.
Definition: Loader_mdl_hl2_mdl.hpp:242
uint32_t BodyPartsOffset
The byte offset to the first body part, relative to "this".
Definition: Loader_mdl_hl2_mdl.hpp:279
uint32_t BoneControllersOffset
The byte offset to the first bone controller, relative to "this".
Definition: Loader_mdl_hl2_mdl.hpp:254
This class describes a bone.
Definition: Loader_mdl_hl2_mdl.hpp:181