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