LCOV - code coverage report
Current view: top level - source/graphics - ModelAbstract.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 13 27 48.1 %
Date: 2026-12-03 01:19:39 Functions: 4 15 26.7 %

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

Generated by: LCOV version 1.16