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