Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Model.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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  * Mesh object with texture and skinning information
20  */
21 
22 #ifndef INCLUDED_MODEL
23 #define INCLUDED_MODEL
24 
25 #include <vector>
26 
27 #include "graphics/Texture.h"
28 #include "graphics/Material.h"
29 #include "graphics/MeshManager.h"
30 #include "graphics/ModelAbstract.h"
31 #include "ps/Overlay.h"
32 
33 struct SPropPoint;
34 class CObjectEntry;
35 class CSkeletonAnim;
36 class CSkeletonAnimDef;
38 class CSimulation2;
39 
40 #define MODELFLAG_CASTSHADOWS (1<<0)
41 #define MODELFLAG_NOLOOPANIMATION (1<<1)
42 #define MODELFLAG_SILHOUETTE_DISPLAY (1<<2)
43 #define MODELFLAG_SILHOUETTE_OCCLUDER (1<<3)
44 #define MODELFLAG_IGNORE_LOS (1<<4)
45 #define MODELFLAG_FILTERED (1<<5) // used internally by renderer
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 // CModel: basically, a mesh object - holds the texturing and skinning
49 // information for a model in game
50 class CModel : public CModelAbstract
51 {
53 
54 public:
55  struct Prop
56  {
57  Prop() : m_minHeight(0.f), m_maxHeight(0.f), m_Point(0), m_Model(0), m_ObjectEntry(0), m_Hidden(false) {}
58 
59  float m_minHeight;
60  float m_maxHeight;
61 
62  /**
63  * Location of the prop point within its parent model, relative to either a bone in the parent model or to the
64  * parent model's origin. See the documentation for @ref SPropPoint for more details.
65  * @see SPropPoint
66  */
68 
69  /**
70  * Pointer to the model associated with this prop. Note that the transform matrix held by this model is the full object-to-world
71  * space transform, taking into account all parent model positioning (see @ref CModel::ValidatePosition for positioning logic).
72  * @see CModel::ValidatePosition
73  */
76 
77  bool m_Hidden; ///< Should this prop be temporarily removed from rendering?
78  };
79 
80 public:
81  // constructor
82  CModel(CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation);
83  // destructor
84  ~CModel();
85 
86 
87  /// Dynamic cast
88  virtual CModel* ToCModel()
89  {
90  return this;
91  }
92 
93  // setup model from given geometry
94  bool InitModel(const CModelDefPtr& modeldef);
95  // update this model's state; 'time' is the absolute time since the start of the animation, in MS
96  void UpdateTo(float time);
97 
98  // get the model's geometry data
99  const CModelDefPtr& GetModelDef() { return m_pModelDef; }
100 
101  // set the model's material
102  void SetMaterial(const CMaterial &material);
103  // set the model's player ID, recursively through props
104  void SetPlayerID(player_id_t id);
105  // set the models mod color
106  virtual void SetShadingColor(const CColor& colour);
107  // get the model's material
109 
110  // set the given animation as the current animation on this model
111  bool SetAnimation(CSkeletonAnim* anim, bool once = false);
112 
113  // get the currently playing animation, if any
114  CSkeletonAnim* GetAnimation() const { return m_Anim; }
115 
116  // set the animation state to be the same as from another; both models should
117  // be compatible types (same type of skeleton)
118  void CopyAnimationFrom(CModel* source);
119 
120  // set object flags
121  void SetFlags(int flags) { m_Flags=flags; }
122  // get object flags
123  int GetFlags() const { return m_Flags; }
124  // add object flags, recursively through props
125  void AddFlagsRec(int flags);
126  // remove shadow casting and receiving, recursively through props
127  // TODO: replace with more generic shader define + flags setting
128  void RemoveShadowsRec();
129 
130  // recurse down tree setting dirty bits
131  virtual void SetDirtyRec(int dirtyflags) {
132  SetDirty(dirtyflags);
133  for (size_t i=0;i<m_Props.size();i++) {
134  m_Props[i].m_Model->SetDirtyRec(dirtyflags);
135  }
136  }
137 
138  virtual void SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
139  {
140  for (size_t i = 0; i < m_Props.size(); ++i)
141  m_Props[i].m_Model->SetTerrainDirty(i0, j0, i1, j1);
142  }
143 
144  virtual void SetEntityVariable(const std::string& name, float value)
145  {
146  for (size_t i = 0; i < m_Props.size(); ++i)
147  m_Props[i].m_Model->SetEntityVariable(name, value);
148  }
149 
150  // --- WORLD/OBJECT SPACE BOUNDS -----------------------------------------------------------------
151 
152  /// Overridden to calculate both the world-space and object-space bounds of this model, and stores the result in
153  /// m_Bounds and m_ObjectBounds, respectively.
154  virtual void CalcBounds();
155 
156  /// Returns the object-space bounds for this model, excluding its children.
158  {
159  RecalculateBoundsIfNecessary(); // recalculates both object-space and world-space bounds if necessary
160  return m_ObjectBounds;
161  }
162 
163  virtual const CBoundingBoxAligned GetWorldBoundsRec(); // reimplemented here
164 
165  /// Auxiliary method; calculates object space bounds of this model, based solely on vertex positions, and stores
166  /// the result in m_ObjectBounds. Called by CalcBounds (instead of CalcAnimatedObjectBounds) if it has been determined
167  /// that the object-space bounds are static.
168  void CalcStaticObjectBounds();
169 
170  /// Auxiliary method; calculate object-space bounds encompassing all vertex positions for given animation, and stores
171  /// the result in m_ObjectBounds. Called by CalcBounds (instead of CalcStaticBounds) if it has been determined that the
172  /// object-space bounds need to take animations into account.
174 
175  // --- SELECTION BOX/BOUNDS ----------------------------------------------------------------------
176 
177  /// Reimplemented here since proper models should participate in selection boxes.
179 
180  /**
181  * Set transform of this object.
182  *
183  * @note In order to ensure that all child props are updated properly,
184  * you must call ValidatePosition().
185  */
186  virtual void SetTransform(const CMatrix3D& transform);
187 
188  /**
189  * Return whether this is a skinned/skeletal model. If it is, Get*BoneMatrices()
190  * will return valid non-NULL arrays.
191  */
192  bool IsSkinned() { return (m_BoneMatrices != NULL); }
193 
194  // return the models bone matrices; 16-byte aligned for SSE reads
197  return m_BoneMatrices;
198  }
199 
200  /**
201  * Load raw animation frame animation from given file, and build an
202  * animation specific to this model.
203  * @param pathname animation file to load
204  * @param name animation name (e.g. "idle")
205  * @param speed animation speed as a factor of the default animation speed
206  * @param actionpos offset of 'action' event, in range [0, 1]
207  * @param actionpos2 offset of 'action2' event, in range [0, 1]
208  * @param sound offset of 'sound' event, in range [0, 1]
209  * @return new animation, or NULL on error
210  */
211  CSkeletonAnim* BuildAnimation(const VfsPath& pathname, const CStr& name, float speed, float actionpos, float actionpos2, float soundpos);
212 
213  /**
214  * Add a prop to the model on the given point.
215  */
216  void AddProp(const SPropPoint* point, CModelAbstract* model, CObjectEntry* objectentry, float minHeight = 0.f, float maxHeight = 0.f);
217 
218  /**
219  * Add a prop to the model on the given point, and treat it as the ammo prop.
220  * The prop will be hidden by default.
221  */
222  void AddAmmoProp(const SPropPoint* point, CModelAbstract* model, CObjectEntry* objectentry);
223 
224  /**
225  * Show the ammo prop (if any), and hide any other props on that prop point.
226  */
227  void ShowAmmoProp();
228 
229  /**
230  * Hide the ammo prop (if any), and show any other props on that prop point.
231  */
232  void HideAmmoProp();
233 
234  /**
235  * Find the first prop used for ammo, by this model or its own props.
236  */
238 
239  // return prop list
240  std::vector<Prop>& GetProps() { return m_Props; }
241  const std::vector<Prop>& GetProps() const { return m_Props; }
242 
243  // return a clone of this model
244  virtual CModelAbstract* Clone() const;
245 
246  /**
247  * Ensure that both the transformation and the bone
248  * matrices are correct for this model and all its props.
249  */
250  virtual void ValidatePosition();
251 
252  /**
253  * Mark this model's position and bone matrices,
254  * and all props' positions as invalid.
255  */
256  virtual void InvalidatePosition();
257 
258 private:
259  // delete anything allocated by the model
260  void ReleaseData();
261 
262  // Needed for terrain aligned props
264 
265  // object flags
266  int m_Flags;
267  // model's material
269  // pointer to the model's raw 3d data
271  // object space bounds of model - accounts for bounds of all possible animations
272  // that can play on a model. Not always up-to-date - currently CalcBounds()
273  // updates it when necessary.
275  // animation currently playing on this model, if any
277  // time (in MS) into the current animation
278  float m_AnimTime;
279 
280  /**
281  * Current state of all bones on this model; null if associated modeldef isn't skeletal.
282  * Props may attach to these bones by means of the SPropPoint::m_BoneIndex field; in this case their
283  * transformation matrix held is relative to the bone transformation (see @ref SPropPoint and
284  * @ref CModel::ValidatePosition).
285  *
286  * @see SPropPoint
287  */
289  // list of current props on model
290  std::vector<Prop> m_Props;
291 
292  /**
293  * The prop point to which the ammo prop is attached, or NULL if none
294  */
296 
297  /**
298  * If m_AmmoPropPoint is not NULL, then the index in m_Props of the ammo prop
299  */
301 
302  // manager object which can load animations for us
304 };
305 
306 #endif
CSkeletonAnimManager & m_SkeletonAnimManager
Definition: Model.h:303
void HideAmmoProp()
Hide the ammo prop (if any), and show any other props on that prop point.
Definition: Model.cpp:555
Describes the position of a prop point within its parent model.
Definition: ModelDef.h:48
float m_minHeight
Definition: Model.h:59
NONCOPYABLE(CModel)
virtual void SetEntityVariable(const std::string &name, float value)
Called when the entity tries to set some variable to affect the display of this model and/or its chil...
Definition: Model.h:144
CMatrix3D * m_BoneMatrices
Current state of all bones on this model; null if associated modeldef isn&#39;t skeletal.
Definition: Model.h:288
virtual const CBoundingBoxAligned GetWorldBoundsRec()
Returns world space bounds of this object and all child objects.
Definition: Model.cpp:195
Definition: Overlay.h:34
std::vector< Prop > m_Props
Definition: Model.h:290
void UpdateTo(float time)
Definition: Model.cpp:292
const std::vector< Prop > & GetProps() const
Definition: Model.h:241
void ReleaseData()
Definition: Model.cpp:63
bool m_PositionValid
True if both transform and and bone matrices are valid.
std::vector< Prop > & GetProps()
Definition: Model.h:240
Public API for simulation system.
Definition: Simulation2.h:46
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
const CMatrix3D * GetAnimatedBoneMatrices()
Definition: Model.h:195
CSkeletonAnim * BuildAnimation(const VfsPath &pathname, const CStr &name, float speed, float actionpos, float actionpos2, float soundpos)
Load raw animation frame animation from given file, and build an animation specific to this model...
Definition: Model.cpp:258
bool SetAnimation(CSkeletonAnim *anim, bool once=false)
Definition: Model.cpp:449
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
virtual void CalcBounds()
Overridden to calculate both the world-space and object-space bounds of this model, and stores the result in m_Bounds and m_ObjectBounds, respectively.
Definition: Model.cpp:105
void RemoveShadowsRec()
Definition: Model.cpp:639
float m_maxHeight
Definition: Model.h:60
int m_Flags
Definition: Model.h:266
bool IsSkinned()
Return whether this is a skinned/skeletal model.
Definition: Model.h:192
bool InitModel(const CModelDefPtr &modeldef)
Definition: Model.cpp:76
Definition: path.h:75
void ShowAmmoProp()
Show the ammo prop (if any), and hide any other props on that prop point.
Definition: Model.cpp:540
virtual CModelAbstract * Clone() const
Definition: Model.cpp:591
virtual void ValidatePosition()
Ensure that both the transformation and the bone matrices are correct for this model and all its prop...
Definition: Model.cpp:319
CModelAbstract * m_Model
Pointer to the model associated with this prop.
Definition: Model.h:74
void SetDirty(u32 dirtyflags)
virtual void SetTransform(const CMatrix3D &transform)
Set transform of this object.
Definition: Model.cpp:615
void AddProp(const SPropPoint *point, CModelAbstract *model, CObjectEntry *objectentry, float minHeight=0.f, float maxHeight=0.f)
Add a prop to the model on the given point.
Definition: Model.cpp:511
size_t m_AmmoLoadedProp
If m_AmmoPropPoint is not NULL, then the index in m_Props of the ammo prop.
Definition: Model.h:300
virtual const CBoundingBoxAligned GetObjectSelectionBoundsRec()
Reimplemented here since proper models should participate in selection boxes.
Definition: Model.cpp:203
void AddAmmoProp(const SPropPoint *point, CModelAbstract *model, CObjectEntry *objectentry)
Add a prop to the model on the given point, and treat it as the ammo prop.
Definition: Model.cpp:528
virtual CModel * ToCModel()
Dynamic cast.
Definition: Model.h:88
void CalcAnimatedObjectBounds(CSkeletonAnimDef *anim, CBoundingBoxAligned &result)
Auxiliary method; calculate object-space bounds encompassing all vertex positions for given animation...
Definition: Model.cpp:144
virtual void SetDirtyRec(int dirtyflags)
Calls SetDirty on this model and all child objects.
Definition: Model.h:131
intptr_t ssize_t
Definition: wposix_types.h:82
CModelAbstract * FindFirstAmmoProp()
Find the first prop used for ammo, by this model or its own props.
Definition: Model.cpp:570
const CBoundingBoxAligned & GetObjectBounds()
Returns the object-space bounds for this model, excluding its children.
Definition: Model.h:157
const SPropPoint * m_Point
Location of the prop point within its parent model, relative to either a bone in the parent model or ...
Definition: Model.h:67
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:36
CModel(CSkeletonAnimManager &skeletonAnimManager, CSimulation2 &simulation)
Definition: Model.cpp:47
static size_t model
Definition: x86_x64.cpp:211
CBoundingBoxAligned m_ObjectBounds
Definition: Model.h:274
~CModel()
Definition: Model.cpp:56
virtual void SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
Called when terrain has changed in the given inclusive bounds.
Definition: Model.h:138
void RecalculateBoundsIfNecessary()
Factored out so subclasses don&#39;t need to repeat this if they want to add additional getters for bound...
void CalcStaticObjectBounds()
Auxiliary method; calculates object space bounds of this model, based solely on vertex positions...
Definition: Model.cpp:130
Definition: Model.h:50
const CModelDefPtr & GetModelDef()
Definition: Model.h:99
CMaterial & GetMaterial()
Definition: Model.h:108
CSkeletonAnim * m_Anim
Definition: Model.h:276
CMaterial m_Material
Definition: Model.h:268
void SetPlayerID(player_id_t id)
Definition: Model.cpp:660
int GetFlags() const
Definition: Model.h:123
CSimulation2 & m_Simulation
Definition: Model.h:263
CObjectEntry * m_ObjectEntry
Definition: Model.h:75
void SetFlags(int flags)
Definition: Model.h:121
const SPropPoint * m_AmmoPropPoint
The prop point to which the ammo prop is attached, or NULL if none.
Definition: Model.h:295
CSkeletonAnim * GetAnimation() const
Definition: Model.h:114
void CopyAnimationFrom(CModel *source)
Definition: Model.cpp:496
virtual void SetShadingColor(const CColor &colour)
Definition: Model.cpp:668
virtual void InvalidatePosition()
Mark this model&#39;s position and bone matrices, and all props&#39; positions as invalid.
Definition: Model.cpp:309
bool m_Hidden
Should this prop be temporarily removed from rendering?
Definition: Model.h:77
float m_AnimTime
Definition: Model.h:278
void SetMaterial(const CMaterial &material)
Definition: Model.cpp:655
boost::shared_ptr< CModelDef > CModelDefPtr
Definition: MeshManager.h:27
CModelDefPtr m_pModelDef
Definition: Model.h:270
void AddFlagsRec(int flags)
Definition: Model.cpp:624