Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Model.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/*
19 * Mesh object with texture and skinning information
20 */
21
22#ifndef INCLUDED_MODEL
23#define INCLUDED_MODEL
24
25#include "graphics/Material.h"
28
29#include <vector>
30
31struct SPropPoint;
32class CObjectEntry;
33class CSkeletonAnim;
35class CSimulation2;
36
37// Holds world information for a particular instance of a model in the game.
38class CModel : public CModelAbstract
39{
41
42public:
43 struct Prop
44 {
45 float m_MinHeight{0.0f};
46 float m_MaxHeight{0.0f};
47
48 /**
49 * Location of the prop point within its parent model, relative to either a bone in the parent model or to the
50 * parent model's origin. See the documentation for @ref SPropPoint for more details.
51 * @see SPropPoint
52 */
53 const SPropPoint* m_Point{nullptr};
54
55 /**
56 * Pointer to the model associated with this prop. Note that the transform matrix held by this model is the full object-to-world
57 * space transform, taking into account all parent model positioning (see @ref CModel::ValidatePosition for positioning logic).
58 * @see CModel::ValidatePosition
59 */
60 std::unique_ptr<CModelAbstract> m_Model;
62
63 bool m_Hidden{false}; ///< Should this prop be temporarily removed from rendering?
64 bool m_Selectable{true}; /// < should this prop count in the selection size?
65 };
66
67public:
68 CModel(const CSimulation2& simulation, const CMaterial& material, const CModelDefPtr& modeldef);
69 ~CModel() override;
70
71 /// Dynamic cast
72 CModel* ToCModel() override
73 {
74 return this;
75 }
76
77 // update this model's state; 'time' is the absolute time since the start of the animation, in MS
78 void UpdateTo(float time);
79
80 // get the model's geometry data
82
83 // set the model's player ID, recursively through props
84 void SetPlayerID(player_id_t id) override;
85 // set the models mod color
86 void SetShadingColor(const CColor& color) override;
87 // get the model's material
88 const CMaterial& GetMaterial() { return m_Material; }
89
90 // set the given animation as the current animation on this model
91 bool SetAnimation(CSkeletonAnim* anim, bool once = false);
92
93 // get the currently playing animation, if any
94 CSkeletonAnim* GetAnimation() const { return m_Anim; }
95
96 // set the animation state to be the same as from another; both models should
97 // be compatible types (same type of skeleton)
98 void CopyAnimationFrom(CModel* source);
99
100 // set object flags
101 void SetFlags(int flags) { m_Flags=flags; }
102 // get object flags
103 int GetFlags() const { return m_Flags; }
104
105 // add object flags, recursively through props
106 void AddFlagsRec(int flags);
107 // remove shadow casting and receiving, recursively through props
108 // TODO: replace with more generic shader define + flags setting
109 void RemoveShadowsRec();
110
111 void SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) override
112 {
113 for (size_t i = 0; i < m_Props.size(); ++i)
114 m_Props[i].m_Model->SetTerrainDirty(i0, j0, i1, j1);
115 }
116
117 void SetEntityVariable(const std::string& name, float value) override
118 {
119 for (size_t i = 0; i < m_Props.size(); ++i)
120 m_Props[i].m_Model->SetEntityVariable(name, value);
121 }
122
123 // --- WORLD/OBJECT SPACE BOUNDS -----------------------------------------------------------------
124
125 /// Overridden to calculate both the world-space and object-space bounds of this model, and stores the result in
126 /// m_Bounds and m_ObjectBounds, respectively.
127 void CalcBounds() override;
128
129 /// Returns the object-space bounds for this model, excluding its children.
131 {
132 RecalculateBoundsIfNecessary(); // recalculates both object-space and world-space bounds if necessary
133 return m_ObjectBounds;
134 }
135
136 const CBoundingBoxAligned GetWorldBoundsRec() override; // reimplemented here
137
138 /// Auxiliary method; calculates object space bounds of this model, based solely on vertex positions, and stores
139 /// the result in m_ObjectBounds. Called by CalcBounds (instead of CalcAnimatedObjectBounds) if it has been determined
140 /// that the object-space bounds are static.
142
143 /// Auxiliary method; calculate object-space bounds encompassing all vertex positions for given animation, and stores
144 /// the result in m_ObjectBounds. Called by CalcBounds (instead of CalcStaticBounds) if it has been determined that the
145 /// object-space bounds need to take animations into account.
147
148 // --- SELECTION BOX/BOUNDS ----------------------------------------------------------------------
149
150 /// Reimplemented here since proper models should participate in selection boxes.
152
153 /**
154 * Set transform of this object.
155 *
156 * @note In order to ensure that all child props are updated properly,
157 * you must call ValidatePosition().
158 */
159 void SetTransform(const CMatrix3D& transform) override;
160
161 /**
162 * Return whether this is a skinned/skeletal model. If it is, Get*BoneMatrices()
163 * will return valid non-NULL arrays.
164 */
165 bool IsSkinned() { return (m_BoneMatrices != NULL); }
166
167 // return the models bone matrices; 16-byte aligned for SSE reads
169 {
171 return m_BoneMatrices;
172 }
173
174 /**
175 * Add a prop to the model on the given point.
176 */
177 void AddProp(const SPropPoint* point, std::unique_ptr<CModelAbstract> model, CObjectEntry* objectentry, float minHeight = 0.f, float maxHeight = 0.f, bool selectable = true);
178
179 /**
180 * Add a prop to the model on the given point, and treat it as the ammo prop.
181 * The prop will be hidden by default.
182 */
183 void AddAmmoProp(const SPropPoint* point, std::unique_ptr<CModelAbstract> model, CObjectEntry* objectentry);
184
185 /**
186 * Show the ammo prop (if any), and hide any other props on that prop point.
187 */
188 void ShowAmmoProp();
189
190 /**
191 * Hide the ammo prop (if any), and show any other props on that prop point.
192 */
193 void HideAmmoProp();
194
195 /**
196 * Find the first prop used for ammo, by this model or its own props.
197 */
199
200 // return prop list
201 std::vector<Prop>& GetProps() { return m_Props; }
202 const std::vector<Prop>& GetProps() const { return m_Props; }
203
204 // return a clone of this model
205 std::unique_ptr<CModelAbstract> Clone() const override;
206
207 /**
208 * Ensure that both the transformation and the bone
209 * matrices are correct for this model and all its props.
210 */
211 void ValidatePosition() override;
212
213 /**
214 * Mark this model's position and bone matrices,
215 * and all props' positions as invalid.
216 */
217 void InvalidatePosition() override;
218
219private:
220 // Needed for terrain aligned props
222
223 // object flags
224 int m_Flags{0};
225 // model's material
227 // pointer to the model's raw 3d data
229 // object space bounds of model - accounts for bounds of all possible animations
230 // that can play on a model. Not always up-to-date - currently CalcBounds()
231 // updates it when necessary.
233 // animation currently playing on this model, if any
235 // time (in MS) into the current animation
236 float m_AnimTime{0.0f};
237
238 /**
239 * Current state of all bones on this model; null if associated modeldef isn't skeletal.
240 * Props may attach to these bones by means of the SPropPoint::m_BoneIndex field; in this case their
241 * transformation matrix held is relative to the bone transformation (see @ref SPropPoint and
242 * @ref CModel::ValidatePosition).
243 *
244 * @see SPropPoint
245 */
247 // list of current props on model
248 std::vector<Prop> m_Props;
249
250 /**
251 * The prop point to which the ammo prop is attached, or NULL if none
252 */
254
255 /**
256 * If m_AmmoPropPoint is not NULL, then the index in m_Props of the ammo prop
257 */
259};
260
261#endif // INCLUDED_MODEL
std::shared_ptr< CModelDef > CModelDefPtr
Definition: MeshManager.h:27
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
Definition: BoundingBoxAligned.h:34
Definition: Material.h:29
Definition: Matrix3D.h:34
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:50
bool m_PositionValid
True if both transform and and bone matrices are valid.
Definition: ModelAbstract.h:182
Definition: Model.h:39
std::vector< Prop > m_Props
Definition: Model.h:248
const CSimulation2 & m_Simulation
Definition: Model.h:221
void CalcStaticObjectBounds()
Auxiliary method; calculates object space bounds of this model, based solely on vertex positions,...
Definition: Model.cpp:92
bool SetAnimation(CSkeletonAnim *anim, bool once=false)
Definition: Model.cpp:334
CModel * ToCModel() override
Dynamic cast.
Definition: Model.h:72
float m_AnimTime
Definition: Model.h:236
CSkeletonAnim * m_Anim
Definition: Model.h:234
void HideAmmoProp()
Hide the ammo prop (if any), and show any other props on that prop point.
Definition: Model.cpp:430
CModel(const CSimulation2 &simulation, const CMaterial &material, const CModelDefPtr &modeldef)
Definition: Model.cpp:40
void UpdateTo(float time)
Definition: Model.cpp:169
void CalcBounds() override
Overridden to calculate both the world-space and object-space bounds of this model,...
Definition: Model.cpp:67
void CopyAnimationFrom(CModel *source)
Definition: Model.cpp:374
std::unique_ptr< CModelAbstract > Clone() const override
Definition: Model.cpp:466
void RemoveShadowsRec()
Definition: Model.cpp:509
void CalcAnimatedObjectBounds(CSkeletonAnimDef *anim, CBoundingBoxAligned &result)
Auxiliary method; calculate object-space bounds encompassing all vertex positions for given animation...
Definition: Model.cpp:100
void SetPlayerID(player_id_t id) override
Definition: Model.cpp:524
void SetTransform(const CMatrix3D &transform) override
Set transform of this object.
Definition: Model.cpp:488
void ValidatePosition() override
Ensure that both the transformation and the bone matrices are correct for this model and all its prop...
Definition: Model.cpp:196
NONCOPYABLE(CModel)
CBoundingBoxAligned m_ObjectBounds
Definition: Model.h:232
void AddAmmoProp(const SPropPoint *point, std::unique_ptr< CModelAbstract > model, CObjectEntry *objectentry)
Add a prop to the model on the given point, and treat it as the ammo prop.
Definition: Model.cpp:403
const CMatrix3D * GetAnimatedBoneMatrices()
Definition: Model.h:168
~CModel() override
Definition: Model.cpp:60
int GetFlags() const
Definition: Model.h:103
CMaterial m_Material
Definition: Model.h:226
const std::vector< Prop > & GetProps() const
Definition: Model.h:202
CSkeletonAnim * GetAnimation() const
Definition: Model.h:94
const SPropPoint * m_AmmoPropPoint
The prop point to which the ammo prop is attached, or NULL if none.
Definition: Model.h:253
bool IsSkinned()
Return whether this is a skinned/skeletal model.
Definition: Model.h:165
void AddFlagsRec(int flags)
Definition: Model.cpp:497
void SetFlags(int flags)
Definition: Model.h:101
const CBoundingBoxAligned GetObjectSelectionBoundsRec() override
Reimplemented here since proper models should participate in selection boxes.
Definition: Model.cpp:115
CMatrix3D * m_BoneMatrices
Current state of all bones on this model; null if associated modeldef isn't skeletal.
Definition: Model.h:246
void InvalidatePosition() override
Mark this model's position and bone matrices, and all props' positions as invalid.
Definition: Model.cpp:186
const CModelDefPtr & GetModelDef()
Definition: Model.h:81
const CModelDefPtr m_pModelDef
Definition: Model.h:228
CModelAbstract * FindFirstAmmoProp()
Find the first prop used for ammo, by this model or its own props.
Definition: Model.cpp:445
const CBoundingBoxAligned GetWorldBoundsRec() override
Returns world space bounds of this object and all child objects.
Definition: Model.cpp:107
std::vector< Prop > & GetProps()
Definition: Model.h:201
size_t m_AmmoLoadedProp
If m_AmmoPropPoint is not NULL, then the index in m_Props of the ammo prop.
Definition: Model.h:258
const CMaterial & GetMaterial()
Definition: Model.h:88
void SetShadingColor(const CColor &color) override
Definition: Model.cpp:532
int m_Flags
Definition: Model.h:224
const CBoundingBoxAligned & GetObjectBounds()
Returns the object-space bounds for this model, excluding its children.
Definition: Model.h:130
void ShowAmmoProp()
Show the ammo prop (if any), and hide any other props on that prop point.
Definition: Model.cpp:415
void AddProp(const SPropPoint *point, std::unique_ptr< 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:385
void SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) override
Called when terrain has changed in the given inclusive bounds.
Definition: Model.h:111
void SetEntityVariable(const std::string &name, float value) override
Called when the entity tries to set some variable to affect the display of this model and/or its chil...
Definition: Model.h:117
Definition: ObjectEntry.h:38
void RecalculateBoundsIfNecessary()
Factored out so subclasses don't need to repeat this if they want to add additional getters for bound...
Definition: RenderableObject.h:130
Public API for simulation system.
Definition: Simulation2.h:47
Definition: SkeletonAnimDef.h:48
Definition: SkeletonAnim.h:33
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:277
static size_t m_Model
Definition: x86_x64.cpp:211
Definition: Color.h:43
Definition: Model.h:44
bool m_Hidden
Should this prop be temporarily removed from rendering?
Definition: Model.h:63
float m_MaxHeight
Definition: Model.h:46
std::unique_ptr< CModelAbstract > m_Model
Pointer to the model associated with this prop.
Definition: Model.h:60
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:53
CObjectEntry * m_ObjectEntry
Definition: Model.h:61
float m_MinHeight
Definition: Model.h:45
bool m_Selectable
Definition: Model.h:64
Describes the position of a prop point within its parent model.
Definition: ModelDef.h:55
intptr_t ssize_t
Definition: wposix_types.h:82