Pyrogenesis trunk
RenderableObject.h
Go to the documentation of this file.
1/* Copyright (C) 2022 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 * Base class for renderable objects
20 */
21
22#ifndef INCLUDED_RENDERABLEOBJECT
23#define INCLUDED_RENDERABLEOBJECT
24
25
27#include "maths/Matrix3D.h"
28
29
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)
34
35
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
41{
42public:
44 virtual ~CRenderData() {}
45
47};
48
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
54{
56
57public:
58 // constructor
60 {
62 }
63 // destructor
64 virtual ~CRenderableObject() { delete m_RenderData; }
65
66 // set object transform
67 virtual void SetTransform(const CMatrix3D& transform)
68 {
69 if (m_Transform == transform)
70 return;
71 // store transform, calculate inverse
72 m_Transform=transform;
74 // normal recalculation likely required on transform change; flag it
76 // need to rebuild world space bounds
78 }
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; }
83
84 // mark some part of the renderdata as dirty, and requiring
85 // an update on next render
86 void SetDirty(u32 dirtyflags)
87 {
88 if (m_RenderData)
89 m_RenderData->m_UpdateFlags |= dirtyflags;
90 }
91
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.
97 *
98 * @see m_BoundsValid
99 * @see RecalculateBoundsIfNecessary
100 */
101 virtual void CalcBounds() = 0;
102
103 /// Returns the world-space axis-aligned bounds of this object.
105 {
107 return m_WorldBounds;
108 }
109
110 /**
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.
114 */
115 virtual void InvalidateBounds() { m_BoundsValid = false; }
116
117 // Set the object renderdata and free previous renderdata, if any.
118 void SetRenderData(CRenderData* renderdata)
119 {
120 delete m_RenderData;
121 m_RenderData = renderdata;
122 }
123
124 /// Return object renderdata - can be null if renderer hasn't yet created the renderdata
126
127protected:
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).
131 {
132 if (!m_BoundsValid) {
133 CalcBounds();
134 m_BoundsValid = true;
135 }
136 }
137
138protected:
139 /// World-space bounds of this object
141 // local->world space transform
143 // world->local space transform
145 // object renderdata
147
148 /**
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).
154 *
155 * @see CalcBounds
156 * @see InvalidateBounds
157 * @see RecalculateBoundsIfNecessary
158 */
160};
161
162#endif
#define RENDERDATA_UPDATE_VERTICES
Definition: RenderableObject.h:32
Definition: BoundingBoxAligned.h:34
Definition: Matrix3D.h:34
void GetInverse(CMatrix3D &dst) const
Definition: Matrix3D.cpp:303
void SetIdentity()
Definition: Matrix3D.cpp:30
Definition: RenderableObject.h:41
int m_UpdateFlags
Definition: RenderableObject.h:46
virtual ~CRenderData()
Definition: RenderableObject.h:44
CRenderData()
Definition: RenderableObject.h:43
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
void SetDirty(u32 dirtyflags)
Definition: RenderableObject.h:86
virtual void SetTransform(const CMatrix3D &transform)
Definition: RenderableObject.h:67
CBoundingBoxAligned m_WorldBounds
World-space bounds of this object.
Definition: RenderableObject.h:140
CRenderData * GetRenderData()
Return object renderdata - can be null if renderer hasn't yet created the renderdata.
Definition: RenderableObject.h:125
CMatrix3D m_InvTransform
Definition: RenderableObject.h:144
NONCOPYABLE(CRenderableObject)
CRenderableObject()
Definition: RenderableObject.h:59
virtual ~CRenderableObject()
Definition: RenderableObject.h:64
CMatrix3D m_Transform
Definition: RenderableObject.h:142
virtual void InvalidateBounds()
Marks the bounds as invalid.
Definition: RenderableObject.h:115
const CMatrix3D & GetInvTransform() const
Definition: RenderableObject.h:82
void SetRenderData(CRenderData *renderdata)
Definition: RenderableObject.h:118
virtual void CalcBounds()=0
(Re)calculates and stores any bounds or bound-dependent data for this object.
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
const CMatrix3D & GetTransform() const
Definition: RenderableObject.h:80
CRenderData * m_RenderData
Definition: RenderableObject.h:146
uint32_t u32
Definition: types.h:39