Pyrogenesis  trunk
ModelAbstract.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 #ifndef INCLUDED_MODELABSTRACT
19 #define INCLUDED_MODELABSTRACT
20 
23 #include "ps/Shapes.h"
25 
26 class CModel;
27 class CModelDecal;
29 
30 /**
31  * Abstract base class for graphical objects that are used by units,
32  * or as props attached to other CModelAbstract objects.
33  * This includes meshes, terrain decals, and sprites.
34  * These objects exist in a tree hierarchy.
35  */
37 {
39 
40 public:
41 
42  /**
43  * Describes a custom selection shape to be used for a model's selection box instead of the default
44  * recursive bounding boxes.
45  */
47  {
48  enum EType {
49  /// The selection shape is determined by an oriented box of custom, user-specified size.
50  BOX,
51  /// The selection shape is determined by a cylinder of custom, user-specified size.
53  };
54 
55  EType m_Type; ///< Type of shape.
56  float m_Size0; ///< Box width if @ref BOX, or radius if @ref CYLINDER
57  float m_Size1; ///< Box depth if @ref BOX, or radius if @ref CYLINDER
58  float m_Height; ///< Box height if @ref BOX, cylinder height if @ref CYLINDER
59  };
60 
61 public:
62 
64  : m_Parent(NULL), m_PositionValid(false), m_ShadingColor(1, 1, 1, 1), m_PlayerID(INVALID_PLAYER),
66  { }
67 
69  {
70  delete m_CustomSelectionShape; // allocated and set externally by CCmpVisualActor, but our responsibility to clean up
71  }
72 
73  virtual CModelAbstract* Clone() const = 0;
74 
75  /// Dynamic cast
76  virtual CModel* ToCModel() { return NULL; }
77 
78  /// Dynamic cast
79  virtual CModelDecal* ToCModelDecal() { return NULL; }
80 
81  /// Dynamic cast
82  virtual CModelParticleEmitter* ToCModelParticleEmitter() { return NULL; }
83 
84  // (This dynamic casting is a bit ugly, but we won't have many subclasses
85  // and this seems the easiest way to integrate with other code that wants
86  // type-specific processing)
87 
88  /// Calls SetDirty on this model and all child objects.
89  virtual void SetDirtyRec(int dirtyflags) = 0;
90 
91  /// Returns world space bounds of this object and all child objects.
92  virtual const CBoundingBoxAligned GetWorldBoundsRec() { return GetWorldBounds(); } // default implementation
93 
94  /**
95  * Returns the world-space selection box of this model. Used primarily for hittesting against against a selection ray. The
96  * returned selection box may be empty to indicate that it does not wish to participate in the selection process.
97  */
98  virtual const CBoundingBoxOriented& GetSelectionBox();
99 
100  virtual void InvalidateBounds()
101  {
102  m_BoundsValid = false;
103  // a call to this method usually means that the model's transform has changed, i.e. it has moved or rotated, so we'll also
104  // want to update the selection box accordingly regardless of the shape it is built from.
105  m_SelectionBoxValid = false;
106  }
107 
108  /// Sets a custom selection shape as described by a @p descriptor. Argument may be NULL
109  /// if you wish to keep the default behaviour of using the recursively-calculated bounding boxes.
111  {
112  if (m_CustomSelectionShape != descriptor)
113  {
114  m_CustomSelectionShape = descriptor;
115  m_SelectionBoxValid = false; // update the selection box when it is next requested
116  }
117  }
118 
119  /**
120  * Returns the (object-space) bounds that should be used to construct a selection box for this model and its children.
121  * May return an empty bound to indicate that this model and its children should not be selectable themselves, or should
122  * not be included in its parent model's selection box. This method is used for constructing the default selection boxes,
123  * as opposed to any boxes of custom shape specified by @ref m_CustomSelectionShape.
124  *
125  * If you wish your model type to be included in selection boxes, override this method and have it return the object-space
126  * bounds of itself, augmented recursively (via this method) with the object-space selection bounds of its children.
127  */
129 
130  /**
131  * Called when terrain has changed in the given inclusive bounds.
132  * Might call SetDirty if the change affects this model.
133  */
134  virtual void SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) = 0;
135 
136  /**
137  * Called when the entity tries to set some variable to affect the display of this model
138  * and/or its child objects.
139  */
140  virtual void SetEntityVariable(const std::string& UNUSED(name), float UNUSED(value)) { }
141 
142  /**
143  * Ensure that both the transformation and the bone matrices are correct for this model and all its props.
144  */
145  virtual void ValidatePosition() = 0;
146 
147  /**
148  * Mark this model's position and bone matrices, and all props' positions as invalid.
149  */
150  virtual void InvalidatePosition() = 0;
151 
152  virtual void SetPlayerID(player_id_t id) { m_PlayerID = id; }
153 
154  // get the model's player ID; initial default is INVALID_PLAYER
155  virtual player_id_t GetPlayerID() const { return m_PlayerID; }
156 
157  virtual void SetShadingColor(const CColor& color) { m_ShadingColor = color; }
158  virtual CColor GetShadingColor() const { return m_ShadingColor; }
159 
160 protected:
161  void CalcSelectionBox();
162 
163 public:
164  /// If non-null, points to the model that we are attached to.
166 
167  /// True if both transform and and bone matrices are valid.
169 
171 
172  /// Modulating color
174 
175 protected:
176 
177  /// Selection box for this model.
179 
180  /// Is the current selection box valid?
182 
183  /// Pointer to a descriptor for a custom-defined selection box shape. If no custom selection box is required, this is NULL
184  /// and the standard recursive-bounding-box-based selection box is used. Otherwise, a custom selection box described by this
185  /// field will be used.
186  /// @see SetCustomSelectionShape
188 
189 };
190 
191 #endif // INCLUDED_MODELABSTRACT
player_id_t m_PlayerID
Definition: ModelAbstract.h:170
void CalcSelectionBox()
Definition: ModelAbstract.cpp:34
The selection shape is determined by a cylinder of custom, user-specified size.
Definition: ModelAbstract.h:52
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
float m_Size0
Box width if BOX, or radius if CYLINDER.
Definition: ModelAbstract.h:56
virtual void SetPlayerID(player_id_t id)
Definition: ModelAbstract.h:152
virtual CModelParticleEmitter * ToCModelParticleEmitter()
Dynamic cast.
Definition: ModelAbstract.h:82
float m_Height
Box height if BOX, cylinder height if CYLINDER.
Definition: ModelAbstract.h:58
static const CBoundingBoxAligned EMPTY
Definition: BoundingBoxAligned.h:165
float m_Size1
Box depth if BOX, or radius if CYLINDER.
Definition: ModelAbstract.h:57
virtual void InvalidatePosition()=0
Mark this model&#39;s position and bone matrices, and all props&#39; positions as invalid.
CColor m_ShadingColor
Modulating color.
Definition: ModelAbstract.h:173
virtual void SetShadingColor(const CColor &color)
Definition: ModelAbstract.h:157
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.
virtual const CBoundingBoxOriented & GetSelectionBox()
Returns the world-space selection box of this model.
Definition: ModelAbstract.cpp:24
void SetCustomSelectionShape(CustomSelectionShape *descriptor)
Sets a custom selection shape as described by a descriptor.
Definition: ModelAbstract.h:110
virtual CModelAbstract * Clone() const =0
bool m_PositionValid
True if both transform and and bone matrices are valid.
Definition: ModelAbstract.h:168
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
Describes a custom selection shape to be used for a model&#39;s selection box instead of the default recu...
Definition: ModelAbstract.h:46
virtual void SetDirtyRec(int dirtyflags)=0
Calls SetDirty on this model and all child objects.
bool m_BoundsValid
Remembers whether any bounds need to be recalculated.
Definition: RenderableObject.h:160
CustomSelectionShape * m_CustomSelectionShape
Pointer to a descriptor for a custom-defined selection box shape.
Definition: ModelAbstract.h:187
~CModelAbstract()
Definition: ModelAbstract.h:68
EType
Definition: ModelAbstract.h:48
CModelAbstract()
Definition: ModelAbstract.h:63
intptr_t ssize_t
Definition: wposix_types.h:82
virtual player_id_t GetPlayerID() const
Definition: ModelAbstract.h:155
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
Definition: Decal.h:50
virtual const CBoundingBoxAligned GetObjectSelectionBoundsRec()
Returns the (object-space) bounds that should be used to construct a selection box for this model and...
Definition: ModelAbstract.h:128
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:36
Definition: BoundingBoxAligned.h:35
CBoundingBoxOriented m_SelectionBox
Selection box for this model.
Definition: ModelAbstract.h:178
Definition: RenderableObject.h:54
virtual void InvalidateBounds()
Marks the bounds as invalid.
Definition: ModelAbstract.h:100
virtual CModel * ToCModel()
Dynamic cast.
Definition: ModelAbstract.h:76
Definition: BoundingBoxOriented.h:30
Particle emitter model, for attaching emitters as props on other models.
Definition: ParticleEmitter.h:176
Definition: Model.h:48
virtual CColor GetShadingColor() const
Definition: ModelAbstract.h:158
virtual const CBoundingBoxAligned GetWorldBoundsRec()
Returns world space bounds of this object and all child objects.
Definition: ModelAbstract.h:92
bool m_SelectionBoxValid
Is the current selection box valid?
Definition: ModelAbstract.h:181
const CBoundingBoxAligned & GetWorldBounds()
Returns the world-space axis-aligned bounds of this object.
Definition: RenderableObject.h:105
EType m_Type
Type of shape.
Definition: ModelAbstract.h:55
static const player_id_t INVALID_PLAYER
Definition: Player.h:26
The selection shape is determined by an oriented box of custom, user-specified size.
Definition: ModelAbstract.h:50
virtual CModelDecal * ToCModelDecal()
Dynamic cast.
Definition: ModelAbstract.h:79
CModelAbstract * m_Parent
If non-null, points to the model that we are attached to.
Definition: ModelAbstract.h:165
virtual void ValidatePosition()=0
Ensure that both the transformation and the bone matrices are correct for this model and all its prop...
NONCOPYABLE(CModelAbstract)