Cafu Engine
Loader_mdl_hl2_vvd.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_VVD_VERTEX_DATA_HPP_INCLUDED
8 #define CAFU_HL2_MDL_VVD_VERTEX_DATA_HPP_INCLUDED
9 
10 #include "Math3D/Vector3.hpp"
11 #include "Templates/Array.hpp"
12 
13 #if defined(_WIN32) && _MSC_VER<1600
14 #include "pstdint.h" // Paul Hsieh's portable implementation of the stdint.h header.
15 #else
16 #include <stdint.h>
17 #endif
18 
19 
20 #define HL2mdl_MAX_NUM_BONES_PER_VERT 3
21 #define HL2mdl_MAX_NUM_LODS 8
22 
23 
24 namespace HL2mdl
25 {
26  typedef float Vector2D[2];
27 
28 
29  struct Vector4D
30  {
31  float v[4];
32  };
33 
34 
36  {
37  float Weight[HL2mdl_MAX_NUM_BONES_PER_VERT];
38  uint8_t Bone[HL2mdl_MAX_NUM_BONES_PER_VERT];
39  uint8_t NumBones;
40  };
41 
42 
44  {
45  StudioBoneWeightT BoneWeights;
46  Vector3fT Pos;
47  Vector3fT Normal;
48  Vector2D TexCoord;
49 
50  std::ostream& print(std::ostream& os, const char* indent) const;
51  };
52 
53 
54  struct FixupT
55  {
56  uint32_t LOD; ///< Can use this to skip higher LODs.
57  uint32_t StartVertex; ///< Begin of the "fixup strip".
58  uint32_t NumVertices; ///< Length of the "fixup strip".
59 
60  std::ostream& print(std::ostream& os, const char* indent) const;
61  };
62 
63 
64  /// This is the header of a VVD vertex data file.
66  {
67  public:
68 
69  static const uint32_t FILE_ID_IDSV; ///< The ID of a VVD file with normal vertex data.
70  static const uint32_t FILE_ID_IDCV; ///< The ID of a VVD file with compressed ("thin") vertex data.
71  static const uint32_t FILE_VERSION; ///< The currently supported file format version.
72 
73  uint32_t ID; ///< Must be FILE_ID_IDSV or FILE_ID_IDCV.
74  uint32_t Version; ///< Must be FILE_VERSION.
75  uint32_t Checksum; ///< Must match the checksum in StudioHeaderT.
76  uint32_t NumLODs; ///< The number of available detail levels.
77  uint32_t NumLODVertices[HL2mdl_MAX_NUM_LODS]; ///< The cumulated number of vertices for the given and all lower detail levels.
78  uint32_t NumFixups; ///< The number of FixupT instances.
79  uint32_t FixupsOffset; ///< The byte offset to the first fixup, relative to "this".
80  uint32_t VerticesOffset; ///< The byte offset to the first vertex, relative to "this".
81  uint32_t TangentsOffset; ///< The byte offset to the first tangent, relative to "this".
82 
83 
84  public:
85 
86  StudioVertexT* GetVertices() const
87  {
88  return ID == FILE_ID_IDSV && VerticesOffset != 0 ? (StudioVertexT*)((uint8_t*)this + VerticesOffset) : NULL;
89  }
90 
91  Vector4D* GetTangents() const
92  {
93  return ID == FILE_ID_IDSV && TangentsOffset != 0 ? (Vector4D*)((uint8_t*)this + TangentsOffset) : NULL;
94  }
95 
96  const FixupT* GetFixups() const
97  {
98  return (FixupT*)((uint8_t*)this + FixupsOffset);
99  }
100 
101  void FixData()
102  {
103  // This is not efficient, but this is just model importer code anyway.
104  ArrayT<StudioVertexT> NewVertices;
105  ArrayT<Vector4D> NewTangents;
106 
107  for (uint32_t i = 0; i < NumFixups; i++)
108  {
109  const FixupT& Fixup = GetFixups()[i];
110 
111  if (GetVertices())
112  {
113  const StudioVertexT* Vertices = GetVertices() + Fixup.StartVertex;
114 
115  for (uint32_t VertNr = 0; VertNr < Fixup.NumVertices; VertNr++)
116  NewVertices.PushBack(Vertices[VertNr]);
117  }
118 
119  if (GetTangents())
120  {
121  const Vector4D* Tangents = GetTangents() + Fixup.StartVertex;
122 
123  for (uint32_t VertNr = 0; VertNr < Fixup.NumVertices; VertNr++)
124  NewTangents.PushBack(Tangents[VertNr]);
125  }
126  }
127 
128  for (uint32_t VertNr = 0; VertNr < NewVertices.Size(); VertNr++)
129  GetVertices()[VertNr] = NewVertices[VertNr];
130 
131  for (uint32_t VertNr = 0; VertNr < NewTangents.Size(); VertNr++)
132  GetTangents()[VertNr] = NewTangents[VertNr];
133 
134  NumFixups = 0;
135  }
136 
137  std::ostream& print(std::ostream& os, const char* indent) const;
138  };
139 }
140 
141 #endif
uint32_t NumLODs
The number of available detail levels.
Definition: Loader_mdl_hl2_vvd.hpp:76
uint32_t Version
Must be FILE_VERSION.
Definition: Loader_mdl_hl2_vvd.hpp:74
uint32_t TangentsOffset
The byte offset to the first tangent, relative to "this".
Definition: Loader_mdl_hl2_vvd.hpp:81
Definition: Loader_mdl_hl2_vvd.hpp:29
unsigned long Size() const
Get size of array.
Definition: Array.hpp:138
uint32_t StartVertex
Begin of the "fixup strip".
Definition: Loader_mdl_hl2_vvd.hpp:57
static const uint32_t FILE_VERSION
The currently supported file format version.
Definition: Loader_mdl_hl2_vvd.hpp:71
Definition: Loader_mdl_hl2_vvd.hpp:35
uint32_t NumLODVertices[HL2mdl_MAX_NUM_LODS]
The cumulated number of vertices for the given and all lower detail levels.
Definition: Loader_mdl_hl2_vvd.hpp:77
static const uint32_t FILE_ID_IDCV
The ID of a VVD file with compressed ("thin") vertex data.
Definition: Loader_mdl_hl2_vvd.hpp:70
uint32_t NumFixups
The number of FixupT instances.
Definition: Loader_mdl_hl2_vvd.hpp:78
static const uint32_t FILE_ID_IDSV
The ID of a VVD file with normal vertex data.
Definition: Loader_mdl_hl2_vvd.hpp:69
uint32_t VerticesOffset
The byte offset to the first vertex, relative to "this".
Definition: Loader_mdl_hl2_vvd.hpp:80
uint32_t LOD
Can use this to skip higher LODs.
Definition: Loader_mdl_hl2_vvd.hpp:56
uint32_t Checksum
Must match the checksum in StudioHeaderT.
Definition: Loader_mdl_hl2_vvd.hpp:75
Definition: Loader_mdl_hl2_vvd.hpp:43
This is the header of a VVD vertex data file.
Definition: Loader_mdl_hl2_vvd.hpp:65
uint32_t ID
Must be FILE_ID_IDSV or FILE_ID_IDCV.
Definition: Loader_mdl_hl2_vvd.hpp:73
Definition: Loader_mdl_hl2_vvd.hpp:54
uint32_t FixupsOffset
The byte offset to the first fixup, relative to "this".
Definition: Loader_mdl_hl2_vvd.hpp:79
uint32_t NumVertices
Length of the "fixup strip".
Definition: Loader_mdl_hl2_vvd.hpp:58
Definition: Renderer.hpp:16