[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / RenderableObject.h
blobeff28897ddcae7c60f6400f08bca5e3f6cead5c8
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * Base class for renderable objects
22 #ifndef INCLUDED_RENDERABLEOBJECT
23 #define INCLUDED_RENDERABLEOBJECT
26 #include "maths/BoundingBoxAligned.h"
27 #include "maths/Matrix3D.h"
30 // dirty flags - used as notification to the renderer that some bit of data
31 // need updating
32 #define RENDERDATA_UPDATE_VERTICES (1<<1)
33 #define RENDERDATA_UPDATE_INDICES (1<<2)
36 ///////////////////////////////////////////////////////////////////////////////
37 // CRenderData: base class of all the renderer's renderdata classes - the
38 // derived class stores necessary information for rendering an object of a
39 // particular type
40 class CRenderData
42 public:
43 CRenderData() : m_UpdateFlags(0) {}
44 virtual ~CRenderData() {}
46 int m_UpdateFlags;
49 ///////////////////////////////////////////////////////////////////////////////
50 // CRenderableObject: base class of all renderable objects - patches, models,
51 // sprites, etc; stores position and bound information, and a pointer to
52 // some renderdata necessary for the renderer to actually render it
53 class CRenderableObject
55 NONCOPYABLE(CRenderableObject);
57 public:
58 // constructor
59 CRenderableObject() : m_RenderData(0), m_BoundsValid(false)
61 m_Transform.SetIdentity();
63 // destructor
64 virtual ~CRenderableObject() { delete m_RenderData; }
66 // set object transform
67 virtual void SetTransform(const CMatrix3D& transform)
69 if (m_Transform == transform)
70 return;
71 // store transform, calculate inverse
72 m_Transform=transform;
73 m_Transform.GetInverse(m_InvTransform);
74 // normal recalculation likely required on transform change; flag it
75 SetDirty(RENDERDATA_UPDATE_VERTICES);
76 // need to rebuild world space bounds
77 InvalidateBounds();
79 // get object to world space transform
80 const CMatrix3D& GetTransform() const { return m_Transform; }
81 // get world to object space transform
82 const CMatrix3D& GetInvTransform() const { return m_InvTransform; }
84 // mark some part of the renderdata as dirty, and requiring
85 // an update on next render
86 void SetDirty(u32 dirtyflags)
88 if (m_RenderData)
89 m_RenderData->m_UpdateFlags |= dirtyflags;
92 /**
93 * (Re)calculates and stores any bounds or bound-dependent data for this object. At this abstraction level, this is only the world-space
94 * bounds stored in @ref m_WorldBounds; subclasses may use this method to (re)compute additional bounds if necessary, or any data that
95 * depends on the bounds. Whenever bound-dependent data is requested through a public interface, @ref RecalculateBoundsIfNecessary should
96 * be called first to ensure bound correctness, which will in turn call this method if it turns out that they're outdated.
98 * @see m_BoundsValid
99 * @see RecalculateBoundsIfNecessary
101 virtual void CalcBounds() = 0;
103 /// Returns the world-space axis-aligned bounds of this object.
104 const CBoundingBoxAligned& GetWorldBounds()
106 RecalculateBoundsIfNecessary();
107 return m_WorldBounds;
111 * Marks the bounds as invalid. This will trigger @ref RecalculateBoundsIfNecessary to recompute any bound-related data the next time
112 * any bound-related data is requested through a public interface -- at least, if you've made sure to call it before returning the
113 * stored data.
115 virtual void InvalidateBounds() { m_BoundsValid = false; }
117 // Set the object renderdata and free previous renderdata, if any.
118 void SetRenderData(CRenderData* renderdata)
120 delete m_RenderData;
121 m_RenderData = renderdata;
124 /// Return object renderdata - can be null if renderer hasn't yet created the renderdata
125 CRenderData* GetRenderData() { return m_RenderData; }
127 protected:
128 /// Factored out so subclasses don't need to repeat this if they want to add additional getters for bounds-related methods
129 /// (since they'll have to make sure to recalc the bounds if necessary before they return it).
130 void RecalculateBoundsIfNecessary()
132 if (!m_BoundsValid) {
133 CalcBounds();
134 m_BoundsValid = true;
138 protected:
139 /// World-space bounds of this object
140 CBoundingBoxAligned m_WorldBounds;
141 // local->world space transform
142 CMatrix3D m_Transform;
143 // world->local space transform
144 CMatrix3D m_InvTransform;
145 // object renderdata
146 CRenderData* m_RenderData;
149 * Remembers whether any bounds need to be recalculated. Subclasses that add any data that depends on the bounds should
150 * take care to consider the validity of the bounds and recalculate their data when necessary -- overriding @ref CalcBounds
151 * to do so would be a good idea, since it's already set up to be called by @ref RecalculateBoundsIfNecessary whenever the
152 * bounds are marked as invalid. The latter should then be called before returning any bounds or bounds-derived data through
153 * a public interface (see the implementation of @ref GetWorldBounds for an example).
155 * @see CalcBounds
156 * @see InvalidateBounds
157 * @see RecalculateBoundsIfNecessary
159 bool m_BoundsValid;
162 #endif