Pyrogenesis  trunk
ModelDef.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 "ps/CStr.h"
26 #include "maths/Vector2D.h"
27 #include "maths/Vector3D.h"
28 #include "maths/Quaternion.h"
29 #include "lib/file/vfs/vfs_path.h"
30 #include "renderer/VertexArray.h"
31 #include <map>
32 #include <cstring>
33 
34 class CBoneState;
35 
36 /**
37  * Describes the position of a prop point within its parent model. A prop point is the location within a parent model
38  * where the prop's origin will be attached.
39  *
40  * A prop point is specified by its transformation matrix (or separately by its position and rotation), which
41  * can be relative to either the parent model's origin, or one of the parent's bones. If the parent model is boned,
42  * then the @ref m_BoneIndex field may specify a bone to which the transformation matrix is relative (see
43  * @ref CModel::m_BoneMatrices). Otherwise, the transformation matrix is assumed to be relative to the parent model's
44  * origin.
45  *
46  * @see CModel::m_BoneMatrices
47  */
48 struct SPropPoint
49 {
50  /// Name of the prop point
51  CStr m_Name;
52 
53  /**
54  * Position of the point within the parent model, relative to either the parent model's origin or one of the parent
55  * model's bones if applicable. Also specified as part of @ref m_Transform.
56  * @see m_Transform
57  */
59 
60  /**
61  * Rotation of the prop model that will be attached at this point. Also specified as part of @ref m_Transform.
62  * @see m_Transform
63  */
65 
66  /**
67  * Object to parent space transformation. Combines both @ref m_Position and @ref m_Rotation in a single
68  * transformation matrix. This transformation is relative to either the parent model's origin, or one of its
69  * bones, depending on whether it is skeletal. If relative to a bone, then the bone in the parent model to
70  * which this transformation is relative may be found by m_BoneIndex.
71  * @see m_Position, m_Rotation
72  */
74 
75  /**
76  * Index of parent bone to which this prop point is relative, if any. The value 0xFF specifies that either the parent
77  * model is unboned, or that this prop point is relative to the parent model's origin rather than one if its bones.
78  */
80 };
81 
82 ///////////////////////////////////////////////////////////////////////////////
83 // SVertexBlend: structure containing the necessary data for blending vertices
84 // with multiple bones
86 {
87  enum { SIZE = 4 };
88  // index of the influencing bone, or 0xff if none
89  u8 m_Bone[SIZE];
90  // weight of the influence; all weights sum to 1
91  float m_Weight[SIZE];
92 
93  bool operator==(const SVertexBlend& o) const
94  {
95  return !memcmp(m_Bone, o.m_Bone, sizeof(m_Bone)) && !memcmp(m_Weight, o.m_Weight, sizeof(m_Weight));
96  }
97 };
98 
99 ///////////////////////////////////////////////////////////////////////////////
100 // SModelVertex: structure containing per-vertex data
102 {
103  // vertex position
105  // vertex normal
107  // vertex UVs
108  std::vector<float> m_UVs;
109  // vertex blend data
111 };
112 
113 
114 ///////////////////////////////////////////////////////////////////////////////
115 // SModelFace: structure containing per-face data
117 {
118  // indices of the 3 vertices on this face
119  u16 m_Verts[3];
120 };
121 
122 
123 ////////////////////////////////////////////////////////////////////////////////////////
124 // CModelDefRPrivate
126 {
127 public:
129  virtual ~CModelDefRPrivate() { }
130 };
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////////////
134 // CModelDef: a raw 3D model; describes the vertices, faces, skinning and skeletal
135 // information of a model
137 {
139 
140 public:
141  // current file version given to saved animations
142  enum { FILE_VERSION = 3 };
143  // supported file read version - files with a version less than this will be rejected
144  enum { FILE_READ_VERSION = 1 };
145 
146 
147 public:
148  CModelDef();
149  ~CModelDef();
150 
151  // model I/O functions
152 
153  static void Save(const VfsPath& filename,const CModelDef* mdef);
154 
155  /**
156  * Loads a PMD file.
157  * @param filename VFS path of .pmd file to load
158  * @param name arbitrary name to give the model for debugging purposes (usually pathname)
159  * @return the model - always non-NULL
160  * @throw PSERROR_File if it can't load the model
161  */
162  static CModelDef* Load(const VfsPath& filename, const VfsPath& name);
163 
164 public:
165  // accessor: get vertex data
166  size_t GetNumVertices() const { return m_NumVertices; }
167  SModelVertex* GetVertices() const { return m_pVertices; }
168 
169  // accessor: get number of UV sets
170  size_t GetNumUVsPerVertex() const { return m_NumUVsPerVertex; }
171 
172  // accessor: get face data
173  size_t GetNumFaces() const { return m_NumFaces; }
174  SModelFace* GetFaces() const { return m_pFaces; }
175 
176  // accessor: get bone data
177  size_t GetNumBones() const { return m_NumBones; }
178  CBoneState* GetBones() const { return m_Bones; }
179  CMatrix3D* GetInverseBindBoneMatrices() { return m_InverseBindBoneMatrices; }
180 
181  // accessor: get blend data
182  size_t GetNumBlends() const { return m_NumBlends; }
183  SVertexBlend* GetBlends() const { return m_pBlends; }
184  size_t* GetBlendIndices() const { return m_pBlendIndices; }
185 
186  // find and return pointer to prop point matching given name; return
187  // null if no match (case insensitive search)
188  const SPropPoint* FindPropPoint(const char* name) const;
189 
190  /**
191  * Transform the given vertex's position from the bind pose into the new pose.
192  *
193  * @return new world-space vertex coordinates
194  */
195  static CVector3D SkinPoint(const SModelVertex& vtx,
196  const CMatrix3D newPoseMatrices[]);
197 
198  /**
199  * Transform the given vertex's normal from the bind pose into the new pose.
200  *
201  * @return new world-space vertex normal
202  */
203  static CVector3D SkinNormal(const SModelVertex& vtx,
204  const CMatrix3D newPoseMatrices[]);
205 
206  /**
207  * Transform vertices' positions and normals.
208  * (This is equivalent to looping over SkinPoint and SkinNormal,
209  * but slightly more efficient.)
210  */
211  static void SkinPointsAndNormals(
212  size_t numVertices,
213  const VertexArrayIterator<CVector3D>& Position,
215  const SModelVertex* vertices,
216  const size_t* blendIndices,
217  const CMatrix3D newPoseMatrices[]);
218 
219 #if HAVE_SSE
220  /**
221  * SSE-optimised version of SkinPointsAndNormals.
222  */
223  static void SkinPointsAndNormals_SSE(
224  size_t numVertices,
225  const VertexArrayIterator<CVector3D>& Position,
226  const VertexArrayIterator<CVector3D>& Normal,
227  const SModelVertex* vertices,
228  const size_t* blendIndices,
229  const CMatrix3D newPoseMatrices[]);
230 #endif
231 
232  /**
233  * Blend bone matrices together to fill bone palette.
234  */
235  void BlendBoneMatrices(CMatrix3D boneMatrices[]);
236 
237  /**
238  * Register renderer private data. Use the key to
239  * distinguish between private data used by different render paths.
240  * The private data will be managed by this CModelDef object:
241  * It will be deleted when CModelDef is destructed or when private
242  * data is registered using the same key.
243  *
244  * @param key The opaque key that is used to identify the caller.
245  * The given private data can be retrieved by passing key to GetRenderData.
246  * @param data The private data.
247  *
248  * postconditions : data is bound to the lifetime of this CModelDef
249  * object.
250  */
251  void SetRenderData(const void* key, CModelDefRPrivate* data);
252 
253  // accessor: render data
254  CModelDefRPrivate* GetRenderData(const void* key) const;
255 
256  // accessor: get model name (for debugging)
257  const VfsPath& GetName() const { return m_Name; }
258 
259 public:
260  // vertex data
263  size_t m_NumUVsPerVertex; // number of UV pairs per vertex
264  // face data
265  size_t m_NumFaces;
267  // bone data - default model pose
268  size_t m_NumBones;
271  // blend data
272  size_t m_NumBlends;
275  // prop point data
276  std::vector<SPropPoint> m_PropPoints;
277 
278 private:
279  VfsPath m_Name; // filename
280 
281  // renderdata shared by models of the same modeldef,
282  // by render path
283  typedef std::map<const void*, CModelDefRPrivate*> RenderDataMap;
284  RenderDataMap m_RenderData;
285 };
286 
287 #endif
288 
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:217
u8 m_BoneIndex
Index of parent bone to which this prop point is relative, if any.
Definition: ModelDef.h:79
CQuaternion m_Rotation
Rotation of the prop model that will be attached at this point.
Definition: ModelDef.h:64
SModelFace * m_pFaces
Definition: ModelDef.h:266
Describes the position of a prop point within its parent model.
Definition: ModelDef.h:48
RenderDataMap m_RenderData
Definition: ModelDef.h:284
CVector3D m_Norm
Definition: ModelDef.h:106
bool operator==(const SVertexBlend &o) const
Definition: ModelDef.h:93
SModelVertex * m_pVertices
Definition: ModelDef.h:262
uint16_t u16
Definition: types.h:38
float m_Weight[SIZE]
Definition: ModelDef.h:91
SModelVertex * GetVertices() const
Definition: ModelDef.h:167
Definition: SkeletonAnimDef.h:31
Definition: Vector3D.h:28
size_t GetNumBones() const
Definition: ModelDef.h:177
Definition: ModelDef.h:125
virtual ~CModelDefRPrivate()
Definition: ModelDef.h:129
std::vector< SPropPoint > m_PropPoints
Definition: ModelDef.h:276
Definition: ModelDef.h:136
size_t m_NumBones
Definition: ModelDef.h:268
uint8_t u8
Definition: types.h:37
size_t GetNumVertices() const
Definition: ModelDef.h:166
size_t m_NumFaces
Definition: ModelDef.h:265
SVertexBlend * m_pBlends
Definition: ModelDef.h:273
Status Save(const CStrW &name, const CStrW &description, CSimulation2 &simulation, const shared_ptr< ScriptInterface::StructuredClone > &guiMetadataClone)
Create new saved game archive with given name and simulation data.
Definition: SavedGame.cpp:57
Definition: ModelDef.h:101
Definition: Matrix3D.h:33
CBoneState * GetBones() const
Definition: ModelDef.h:178
u8 m_Bone[SIZE]
Definition: ModelDef.h:89
CMatrix3D * GetInverseBindBoneMatrices()
Definition: ModelDef.h:179
VfsPath m_Name
Definition: ModelDef.h:279
Definition: path.h:77
size_t m_NumBlends
Definition: ModelDef.h:272
size_t * m_pBlendIndices
Definition: ModelDef.h:274
pthread_key_t key
Definition: wpthread.cpp:140
size_t GetNumBlends() const
Definition: ModelDef.h:182
size_t m_NumVertices
Definition: ModelDef.h:261
CStr m_Name
Name of the prop point.
Definition: ModelDef.h:51
static Status Load(const OsPath &pathname, void *buf, size_t size, const Parameters &p=Parameters(), const CompletedHook &completedHook=CompletedHook(), const IssueHook &issueHook=IssueHook())
Definition: io.h:337
CMatrix3D * m_InverseBindBoneMatrices
Definition: ModelDef.h:270
Definition: ModelDef.h:85
Definition: ModelDef.h:116
CMatrix3D m_Transform
Object to parent space transformation.
Definition: ModelDef.h:73
CModelDefRPrivate()
Definition: ModelDef.h:128
std::map< const void *, CModelDefRPrivate * > RenderDataMap
Definition: ModelDef.h:283
SModelFace * GetFaces() const
Definition: ModelDef.h:174
Definition: Quaternion.h:24
CVector3D m_Coords
Definition: ModelDef.h:104
SVertexBlend m_Blend
Definition: ModelDef.h:110
CVector3D m_Position
Position of the point within the parent model, relative to either the parent model&#39;s origin or one of...
Definition: ModelDef.h:58
Definition: VertexArray.h:25
size_t * GetBlendIndices() const
Definition: ModelDef.h:184
size_t GetNumFaces() const
Definition: ModelDef.h:173
const VfsPath & GetName() const
Definition: ModelDef.h:257
size_t m_NumUVsPerVertex
Definition: ModelDef.h:263
size_t GetNumUVsPerVertex() const
Definition: ModelDef.h:170
CBoneState * m_Bones
Definition: ModelDef.h:269
Definition: CCmpRangeManager.cpp:198
SVertexBlend * GetBlends() const
Definition: ModelDef.h:183
std::vector< float > m_UVs
Definition: ModelDef.h:108