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