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