Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ModelDef.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * Defines a raw 3d model.
20 */
21
22#ifndef INCLUDED_MODELDEF
23#define INCLUDED_MODELDEF
24
25#include "lib/code_annotation.h"
28#include "maths/Matrix3D.h"
29#include "maths/Quaternion.h"
30#include "maths/Vector3D.h"
31#include "ps/CStr.h"
32
33#include <cstring>
34#include <map>
35#include <unordered_map>
36#include <vector>
37
38class CBoneState;
40class CVector2D;
41template <typename T> class VertexArrayIterator;
42
43/**
44 * Describes the position of a prop point within its parent model. A prop point is the location within a parent model
45 * where the prop's origin will be attached.
46 *
47 * A prop point is specified by its transformation matrix (or separately by its position and rotation), which
48 * can be relative to either the parent model's origin, or one of the parent's bones. If the parent model is boned,
49 * then the @ref m_BoneIndex field may specify a bone to which the transformation matrix is relative (see
50 * @ref CModel::m_BoneMatrices). Otherwise, the transformation matrix is assumed to be relative to the parent model's
51 * origin.
52 *
53 * @see CModel::m_BoneMatrices
54 */
56{
57 /// Name of the prop point
58 CStr m_Name;
59
60 /**
61 * Position of the point within the parent model, relative to either the parent model's origin or one of the parent
62 * model's bones if applicable. Also specified as part of @ref m_Transform.
63 * @see m_Transform
64 */
66
67 /**
68 * Rotation of the prop model that will be attached at this point. Also specified as part of @ref m_Transform.
69 * @see m_Transform
70 */
72
73 /**
74 * Object to parent space transformation. Combines both @ref m_Position and @ref m_Rotation in a single
75 * transformation matrix. This transformation is relative to either the parent model's origin, or one of its
76 * bones, depending on whether it is skeletal. If relative to a bone, then the bone in the parent model to
77 * which this transformation is relative may be found by m_BoneIndex.
78 * @see m_Position, m_Rotation
79 */
81
82 /**
83 * Index of parent bone to which this prop point is relative, if any. The value 0xFF specifies that either the parent
84 * model is unboned, or that this prop point is relative to the parent model's origin rather than one if its bones.
85 */
86 std::uint8_t m_BoneIndex;
87};
88
89///////////////////////////////////////////////////////////////////////////////
90// SVertexBlend: structure containing the necessary data for blending vertices
91// with multiple bones
93{
94 enum { SIZE = 4 };
95 // index of the influencing bone, or 0xff if none
96 std::uint8_t m_Bone[SIZE];
97 // weight of the influence; all weights sum to 1
98 float m_Weight[SIZE];
99
100 bool operator==(const SVertexBlend& o) const
101 {
102 return !memcmp(m_Bone, o.m_Bone, sizeof(m_Bone)) && !memcmp(m_Weight, o.m_Weight, sizeof(m_Weight));
103 }
104};
105
106///////////////////////////////////////////////////////////////////////////////
107// SModelVertex: structure containing per-vertex data
109{
110 // vertex position
112 // vertex normal
114 // vertex blend data
116};
117
118
119///////////////////////////////////////////////////////////////////////////////
120// SModelFace: structure containing per-face data
122{
123 // indices of the 3 vertices on this face
124 std::uint16_t m_Verts[3];
125};
126
127
128////////////////////////////////////////////////////////////////////////////////////////
129// CModelDefRPrivate
131{
132public:
134 virtual ~CModelDefRPrivate() { }
135};
136
137
138////////////////////////////////////////////////////////////////////////////////////////
139// CModelDef: a raw 3D model; describes the vertices, faces, skinning and skeletal
140// information of a model
142{
144
145public:
146 // current file version given to saved animations
147 enum { FILE_VERSION = 3 };
148 // supported file read version - files with a version less than this will be rejected
149 enum { FILE_READ_VERSION = 1 };
150
151
152public:
153 CModelDef();
154 ~CModelDef();
155
156 // model I/O functions
157
158 static void Save(const VfsPath& filename,const CModelDef* mdef);
159
160 /**
161 * Loads a PMD file.
162 * @param filename VFS path of .pmd file to load
163 * @param name arbitrary name to give the model for debugging purposes (usually pathname)
164 * @return the model - always non-NULL
165 * @throw PSERROR_File if it can't load the model
166 */
167 static CModelDef* Load(const VfsPath& filename, const VfsPath& name);
168
169public:
170 // accessor: get vertex data
171 size_t GetNumVertices() const { return m_NumVertices; }
173
174 // accessor: get number of UV sets
175 size_t GetNumUVsPerVertex() const { return m_NumUVsPerVertex; }
176
177 const std::vector<CVector2D>& GetUVCoordinates() const { return m_UVCoordinates; }
178
179 // accessor: get face data
180 size_t GetNumFaces() const { return m_NumFaces; }
181 SModelFace* GetFaces() const { return m_pFaces; }
182
183 // accessor: get bone data
184 size_t GetNumBones() const { return m_NumBones; }
185 CBoneState* GetBones() const { return m_Bones; }
187
188 // accessor: get blend data
189 size_t GetNumBlends() const { return m_NumBlends; }
190 SVertexBlend* GetBlends() const { return m_pBlends; }
191 size_t* GetBlendIndices() const { return m_pBlendIndices; }
192
193 // find and return pointer to prop point matching given name; return
194 // null if no match (case insensitive search)
195 const SPropPoint* FindPropPoint(const char* name) const;
196
197 /**
198 * @param anim may be null
199 */
200 void GetMaxBounds(CSkeletonAnimDef* anim, bool loop, CBoundingBoxAligned& result);
201
202 /**
203 * Transform the given vertex's position from the bind pose into the new pose.
204 *
205 * @return new world-space vertex coordinates
206 */
207 static CVector3D SkinPoint(const SModelVertex& vtx,
208 const CMatrix3D newPoseMatrices[]);
209
210 /**
211 * Transform the given vertex's normal from the bind pose into the new pose.
212 *
213 * @return new world-space vertex normal
214 */
215 static CVector3D SkinNormal(const SModelVertex& vtx,
216 const CMatrix3D newPoseMatrices[]);
217
218 /**
219 * Transform vertices' positions and normals.
220 * (This is equivalent to looping over SkinPoint and SkinNormal,
221 * but slightly more efficient.)
222 */
223 static void(*SkinPointsAndNormals)(
224 size_t numVertices,
225 const VertexArrayIterator<CVector3D>& Position,
227 const SModelVertex* vertices,
228 const size_t* blendIndices,
229 const CMatrix3D newPoseMatrices[]);
230
231 /**
232 * Blend bone matrices together to fill bone palette.
233 */
234 void BlendBoneMatrices(CMatrix3D boneMatrices[]);
235
236 /**
237 * Register renderer private data. Use the key to
238 * distinguish between private data used by different render paths.
239 * The private data will be managed by this CModelDef object:
240 * It will be deleted when CModelDef is destructed or when private
241 * data is registered using the same key.
242 *
243 * @param key The opaque key that is used to identify the caller.
244 * The given private data can be retrieved by passing key to GetRenderData.
245 * @param data The private data.
246 *
247 * postconditions : data is bound to the lifetime of this CModelDef
248 * object.
249 */
250 void SetRenderData(const void* key, CModelDefRPrivate* data);
251
252 // accessor: render data
253 CModelDefRPrivate* GetRenderData(const void* key) const;
254
255 // accessor: get model name (for debugging)
256 const VfsPath& GetName() const { return m_Name; }
257
258public:
259 // vertex data
262 std::vector<CVector2D> m_UVCoordinates;
263 size_t m_NumUVsPerVertex; // number of UV pairs per vertex
264 // face data
267 // bone data - default model pose
271 // blend data
275 // prop point data
276 std::vector<SPropPoint> m_PropPoints;
277
278private:
279 VfsPath m_Name; // filename
280
281 // Maximal bounding box of this mesh for a given animation.
282 std::unordered_map<std::uint32_t, CBoundingBoxAligned> m_MaxBoundsPerAnimDef;
283
284 // renderdata shared by models of the same modeldef,
285 // by render path
286 typedef std::map<const void*, CModelDefRPrivate*> RenderDataMap;
288};
289
290/**
291 * Detects CPU caps and activates the best possible codepath.
292 */
293extern void ModelDefActivateFastImpl();
294
295#endif
296
void ModelDefActivateFastImpl()
Detects CPU caps and activates the best possible codepath.
Definition: ModelDef.cpp:533
Definition: SkeletonAnimDef.h:38
Definition: BoundingBoxAligned.h:35
Definition: Matrix3D.h:36
Definition: ModelDef.h:131
CModelDefRPrivate()
Definition: ModelDef.h:133
virtual ~CModelDefRPrivate()
Definition: ModelDef.h:134
Definition: ModelDef.h:142
const VfsPath & GetName() const
Definition: ModelDef.h:256
CMatrix3D * m_InverseBindBoneMatrices
Definition: ModelDef.h:270
SModelVertex * m_pVertices
Definition: ModelDef.h:261
const std::vector< CVector2D > & GetUVCoordinates() const
Definition: ModelDef.h:177
void BlendBoneMatrices(CMatrix3D boneMatrices[])
Blend bone matrices together to fill bone palette.
Definition: ModelDef.cpp:264
std::map< const void *, CModelDefRPrivate * > RenderDataMap
Definition: ModelDef.h:286
size_t m_NumBlends
Definition: ModelDef.h:272
void SetRenderData(const void *key, CModelDefRPrivate *data)
Register renderer private data.
Definition: ModelDef.cpp:514
size_t m_NumUVsPerVertex
Definition: ModelDef.h:263
std::unordered_map< std::uint32_t, CBoundingBoxAligned > m_MaxBoundsPerAnimDef
Definition: ModelDef.h:282
static void(* SkinPointsAndNormals)(size_t numVertices, const VertexArrayIterator< CVector3D > &Position, const VertexArrayIterator< CVector3D > &Normal, const SModelVertex *vertices, const size_t *blendIndices, const CMatrix3D newPoseMatrices[])
Transform vertices' positions and normals.
Definition: ModelDef.h:223
CMatrix3D * GetInverseBindBoneMatrices()
Definition: ModelDef.h:186
const SPropPoint * FindPropPoint(const char *name) const
Definition: ModelDef.cpp:309
SModelFace * GetFaces() const
Definition: ModelDef.h:181
VfsPath m_Name
Definition: ModelDef.h:279
CBoneState * GetBones() const
Definition: ModelDef.h:185
size_t m_NumBones
Definition: ModelDef.h:268
SVertexBlend * m_pBlends
Definition: ModelDef.h:273
RenderDataMap m_RenderData
Definition: ModelDef.h:287
@ FILE_VERSION
Definition: ModelDef.h:147
static void Save(const VfsPath &filename, const CModelDef *mdef)
Definition: ModelDef.cpp:481
size_t GetNumVertices() const
Definition: ModelDef.h:171
~CModelDef()
Definition: ModelDef.cpp:295
CModelDef()
Definition: ModelDef.cpp:286
std::vector< CVector2D > m_UVCoordinates
Definition: ModelDef.h:262
size_t GetNumFaces() const
Definition: ModelDef.h:180
static CVector3D SkinNormal(const SModelVertex &vtx, const CMatrix3D newPoseMatrices[])
Transform the given vertex's normal from the bind pose into the new pose.
Definition: ModelDef.cpp:94
NONCOPYABLE(CModelDef)
size_t m_NumVertices
Definition: ModelDef.h:260
SModelFace * m_pFaces
Definition: ModelDef.h:266
size_t GetNumBlends() const
Definition: ModelDef.h:189
size_t * m_pBlendIndices
Definition: ModelDef.h:274
std::vector< SPropPoint > m_PropPoints
Definition: ModelDef.h:276
size_t GetNumBones() const
Definition: ModelDef.h:184
size_t GetNumUVsPerVertex() const
Definition: ModelDef.h:175
void GetMaxBounds(CSkeletonAnimDef *anim, bool loop, CBoundingBoxAligned &result)
Definition: ModelDef.cpp:39
static CModelDef * Load(const VfsPath &filename, const VfsPath &name)
Loads a PMD file.
Definition: ModelDef.cpp:319
static CVector3D SkinPoint(const SModelVertex &vtx, const CMatrix3D newPoseMatrices[])
Transform the given vertex's position from the bind pose into the new pose.
Definition: ModelDef.cpp:81
size_t * GetBlendIndices() const
Definition: ModelDef.h:191
SModelVertex * GetVertices() const
Definition: ModelDef.h:172
SVertexBlend * GetBlends() const
Definition: ModelDef.h:190
CBoneState * m_Bones
Definition: ModelDef.h:269
@ FILE_READ_VERSION
Definition: ModelDef.h:149
CModelDefRPrivate * GetRenderData(const void *key) const
Definition: ModelDef.cpp:523
size_t m_NumFaces
Definition: ModelDef.h:265
Definition: Quaternion.h:26
Definition: SkeletonAnimDef.h:51
Definition: Vector2D.h:32
Definition: Vector3D.h:32
Definition: path.h:84
Definition: VertexArray.h:37
@ Normal
Definition: CCmpRangeManager.cpp:242
Definition: ModelDef.h:122
std::uint16_t m_Verts[3]
Definition: ModelDef.h:124
Definition: ModelDef.h:109
CVector3D m_Norm
Definition: ModelDef.h:113
CVector3D m_Coords
Definition: ModelDef.h:111
SVertexBlend m_Blend
Definition: ModelDef.h:115
Describes the position of a prop point within its parent model.
Definition: ModelDef.h:56
CStr m_Name
Name of the prop point.
Definition: ModelDef.h:58
CVector3D m_Position
Position of the point within the parent model, relative to either the parent model's origin or one of...
Definition: ModelDef.h:65
CQuaternion m_Rotation
Rotation of the prop model that will be attached at this point.
Definition: ModelDef.h:71
std::uint8_t m_BoneIndex
Index of parent bone to which this prop point is relative, if any.
Definition: ModelDef.h:86
CMatrix3D m_Transform
Object to parent space transformation.
Definition: ModelDef.h:80
Definition: ModelDef.h:93
bool operator==(const SVertexBlend &o) const
Definition: ModelDef.h:100
std::uint8_t m_Bone[SIZE]
Definition: ModelDef.h:96
float m_Weight[SIZE]
Definition: ModelDef.h:98
@ SIZE
Definition: ModelDef.h:94
pthread_key_t key
Definition: wpthread.cpp:141