Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
ImpostorPage.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------------------
2Copyright (c) 2006 John Judnich
3Modified 2008 by Erik Hjortsberg (erik.hjortsberg@iteam.se)
4
5This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
71. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
82. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
93. This notice may not be removed or altered from any source distribution.
10-------------------------------------------------------------------------------------*/
11
12//ImpostorPage.h
13//ImposterPage is an extension to PagedGeometry which displays entities as imposters.
14//-------------------------------------------------------------------------------------
15
16#ifndef __ImpostorPage_H__
17#define __ImpostorPage_H__
18
19#include "PagedGeometry.h"
20#include "StaticBillboardSet.h"
21
22#include <OgrePrerequisites.h>
23#include <OgreTextureManager.h>
24#include <OgreRenderTexture.h>
25
26#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
27// linux memory fix
28#include <memory>
29#endif
30
31//The number of angle increments around the yaw axis to render impostor "snapshots" of trees
32#define IMPOSTOR_YAW_ANGLES 8
33
34//The number of angle increments around the pitch axis to render impostor "snapshots" of trees
35#define IMPOSTOR_PITCH_ANGLES 4
36
37//When IMPOSTOR_RENDER_ABOVE_ONLY is defined, impostor images will only be rendered from angles around and
38//above entities. If this is disabled, bottom views of the entities will be rendered to the impostor atlas
39//and therefore allow those angles to be viewed from a distance. However, this requires the IMPOSTOR_PITCH_ANGLES
40//to be doubled to maintain an equal level of impostor angle correctness compared to when impostors are rendered
41//from above only.
42#define IMPOSTOR_RENDER_ABOVE_ONLY
43
44//When IMPOSTOR_FILE_SAVE is defined, impostor textures will be read and saved to disc; if not, they will stay
45//in memory and need to be regenerated each time the application is run (remove or comment out the line below if this
46//is desired)
47#define IMPOSTOR_FILE_SAVE
48
49namespace Forests
50{
51 // Forward declaration
52 class ImpostorBatch;
53 class ImpostorTexture;
54
61
62 //--------------------------------------------------------------------------
85 {
86 typedef std::map<Ogre::String, ImpostorBatch*> TImpostorBatchs;
87
88 public:
91
92 void init(PagedGeometry *geom, const Ogre::Any &data);
93
94 void setRegion(Ogre::Real left, Ogre::Real top, Ogre::Real right, Ogre::Real bottom);
95 void addEntity(Ogre::Entity *ent, const Ogre::Vector3 &position, const Ogre::Quaternion &rotation,
96 const Ogre::Vector3 &scale, const Ogre::ColourValue &color);
97
98 void build();
99 void removeEntities();
100 void setVisible(bool visible);
101 void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist);
102 void update();
103
105 void setBlendMode(ImpostorBlendMode blendMode) { m_blendMode = blendMode; }
107
109 ImpostorBatch* getImpostorBatch(const Ogre::String &key) const
110 {
111 TImpostorBatchs::const_iterator it = m_mapImpostorBatches.find(key);
112 return it != m_mapImpostorBatches.end() ? it->second : NULL;
113 }
114
116 bool injectImpostorBatch(const Ogre::String &key, ImpostorBatch *injectedBatch)
117 {
118 return injectedBatch ?
119 m_mapImpostorBatches.insert(TImpostorBatchs::value_type(key, injectedBatch)).second : false;
120 }
121
122
123 // Static functions
124
136 static void setImpostorResolution(Ogre::uint pixels) { s_nImpostorResolution = pixels; }
137 static Ogre::uint getImpostorResolution() { return s_nImpostorResolution; }
138
157 static void setImpostorBackgroundColor(const Ogre::ColourValue &color)
158 {
161 }
162
164 static const Ogre::ColourValue& getImpostorBackgroundColor() { return s_clrImpostorBackground; }
165
181 static void setImpostorPivot(Ogre::BillboardOrigin origin)
182 {
183 if (origin != Ogre::BBO_CENTER && origin != Ogre::BBO_BOTTOM_CENTER)
184 {
185 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS,
186 "Invalid origin - only BBO_CENTER and BBO_BOTTOM_CENTER is supported",
187 "ImpostorPage::setImpostorPivot()");
188 }
189
190 s_impostorPivot = origin;
191 }
192
194 static Ogre::BillboardOrigin getImpostorPivot() { return s_impostorPivot; }
195
210 static void regenerate(Ogre::Entity *ent);
211
222 static void regenerateAll();
223
224
225 protected:
226 Ogre::SceneManager* m_pSceneMgr;
229 Ogre::uint m_nInstanceID;
230 Ogre::uint m_nAveCount;
231 Ogre::Vector3 m_vecCenter;
232 Ogre::Timer m_timerUpdate;
234
235 private:
236
237 static Ogre::uint s_nImpostorResolution;
238 static Ogre::uint s_nSelfInstances;
239 static Ogre::uint s_nUpdateInstanceID;
240 static Ogre::ColourValue s_clrImpostorBackground;
241 static Ogre::BillboardOrigin s_impostorPivot;
242 }; // end class ImpostorPage
243
244
245 //-------------------------------------------------------------------------------------
246 //This is used internally by ImpostorPage to store a "batch" of impostors. Similar
247 //impostors are all batched into ImpostorBatch'es, which contain a BillboardSet (where
248 //the actual billboards are), and a pointer to an existing ImpostorTexture.
250 {
251 public:
252 static ImpostorBatch *getBatch(ImpostorPage *group, Ogre::Entity *entity);
254
255 inline void build()
256 {
257 bbset->build();
258 }
259
260 inline void clear()
261 {
262 bbset->clear();
263 }
264
265 inline void setVisible(bool visible)
266 {
267 bbset->setVisible(visible);
268 }
269
270 inline void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist)
271 {
272 bbset->setFade(enabled, visibleDist, invisibleDist);
273 }
274
276 void setBillboardOrigin(Ogre::BillboardOrigin origin);
278 void addBillboard(const Ogre::Vector3 &position, const Ogre::Quaternion &rotation,
279 const Ogre::Vector3 &scale, const Ogre::ColourValue &color = Ogre::ColourValue::White);
281 void setAngle(Ogre::Real pitchDeg, Ogre::Real yawDeg);
282
283 static Ogre::String generateEntityKey(Ogre::Entity *entity);
284
285 protected:
286 ImpostorBatch(ImpostorPage *group, Ogre::Entity *entity);
287
290
291 Ogre::Vector3 entityBBCenter;
292
294
295 Ogre::uint16 pitchIndex, yawIndex;
296
297 static unsigned long GUID;
298 static inline Ogre::String getUniqueID(const Ogre::String &prefix)
299 {
300 return prefix + Ogre::StringConverter::toString(++GUID);
301 }
302 };
303
304 //--------------------------------------------------------------------------
307 class ImpostorTextureResourceLoader : public Ogre::ManualResourceLoader
308 {
309 public:
315
316
321 virtual void loadResource (Ogre::Resource *resource);
322 protected:
324 };
325
326 //-------------------------------------------------------------------------------------
327 //This is used internally by ImpostorPage. An ImpostorTexture is actually multiple
328 //images of an entity from various rotations. ImpostorTextures are applied
329 //to billboards to create the effect of 3D shapes, when in reality they are simply
330 //flat billboards.
332 {
333 friend class ImpostorBatch;
335
336 public:
340 static ImpostorTexture *getTexture(ImpostorPage *group, Ogre::Entity *entity);
341
346 static void removeTexture(ImpostorTexture* Texture);
347
348 void regenerate();
349 static void regenerateAll();
350
352
353 protected:
354 ImpostorTexture(ImpostorPage *group, Ogre::Entity *entity);
355
356 void renderTextures(bool force); // Renders the impostor texture grid
357 void updateMaterials(); // Updates the materials to use the latest rendered impostor texture grid
358
359 static std::map<Ogre::String, ImpostorTexture *> selfList;
360 Ogre::SceneManager *sceneMgr;
361 Ogre::Entity *entity;
362 Ogre::String entityKey;
364
366 Ogre::TexturePtr texture;
367
368 Ogre::ResourceHandle sourceMesh;
369 Ogre::AxisAlignedBox boundingBox;
371 Ogre::Vector3 entityCenter;
372
373 static unsigned long GUID;
374 static inline Ogre::String getUniqueID(const Ogre::String &prefix)
375 {
376 return prefix + Ogre::StringConverter::toString(++GUID);
377 }
378
379 //This will only be used when IMPOSTOR_FILE_SAVE is set to 0
380 std::auto_ptr<ImpostorTextureResourceLoader> loader;
381 };
382
383
384
385 //-------------------------------------------------------------------------------------
386 //This is an inline function from ImposterBatch that had to be defined down below the
387 //ImpostorTexture class, because it uses it.
388 inline void ImpostorBatch::addBillboard(const Ogre::Vector3 &position, const Ogre::Quaternion &rotation,
389 const Ogre::Vector3 &scale, const Ogre::ColourValue &color)
390 {
391 //float degrees = (Math::ACos(rotation.w)*2.0f).valueDegrees();
392 const Ogre::Vector3 zVector = rotation * Ogre::Vector3::UNIT_Z;
393 float degrees = (float)Ogre::Math::ATan2(zVector.x, zVector.z).valueDegrees();
394 if (degrees < 0.f)
395 degrees += 360.f;
396
397 int n = int(IMPOSTOR_YAW_ANGLES * (degrees / 360.0f) + 0.5f);
398 Ogre::uint16 texCoordIndx = (IMPOSTOR_YAW_ANGLES - n) % IMPOSTOR_YAW_ANGLES;
399
400 bbset->createBillboard(position + (rotation * entityBBCenter) * scale,
401 float(m_pTexture->entityDiameter * 0.5f * (scale.x + scale.z)),
402 float(m_pTexture->entityDiameter * scale.y),
403 color, texCoordIndx);
404 }
405
406} // end namespace
407
408#endif
#define IMPOSTOR_PITCH_ANGLES
#define IMPOSTOR_YAW_ANGLES
This base-class can be extended to provide different ways of representing entities.
void setVisible(bool visible)
static Ogre::String generateEntityKey(Ogre::Entity *entity)
static unsigned long GUID
ImpostorPage * igroup
void addBillboard(const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, const Ogre::Vector3 &scale, const Ogre::ColourValue &color=Ogre::ColourValue::White)
StaticBillboardSet * bbset
void setBillboardOrigin(Ogre::BillboardOrigin origin)
static Ogre::String getUniqueID(const Ogre::String &prefix)
ImpostorTexture * m_pTexture
void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist)
void setAngle(Ogre::Real pitchDeg, Ogre::Real yawDeg)
static ImpostorBatch * getBatch(ImpostorPage *group, Ogre::Entity *entity)
Ogre::Vector3 entityBBCenter
The ImpostorPage class renders entities as impostors (billboard images that look just like the real e...
static void setImpostorBackgroundColor(const Ogre::ColourValue &color)
Sets the background color used when rendering impostor images.
void update()
Do whatever needs to be done to keep the page geometry up-to-date.
static Ogre::uint s_nImpostorResolution
void build()
Perform any final steps to make added entities appear in the scene.
void setBlendMode(ImpostorBlendMode blendMode)
ImpostorBlendMode m_blendMode
PagedGeometry * m_pPagedGeom
static void setImpostorPivot(Ogre::BillboardOrigin origin)
Sets the billboard pivot point used when rendering camera-facing impostors.
static void regenerate(Ogre::Entity *ent)
Regenerates the impostor texture for the specified entity.
static void regenerateAll()
Regenerates all impostor textures currently being used in the scene.
void addEntity(Ogre::Entity *ent, const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, const Ogre::Vector3 &scale, const Ogre::ColourValue &color)
Add an entity to the page, at the specified position, rotation, and scale.
TImpostorBatchs m_mapImpostorBatches
void setRegion(Ogre::Real left, Ogre::Real top, Ogre::Real right, Ogre::Real bottom)
Prepare a geometry page for entities.
std::map< Ogre::String, ImpostorBatch * > TImpostorBatchs
static Ogre::uint s_nSelfInstances
static void setImpostorResolution(Ogre::uint pixels)
Sets the resolution for single impostor images.
static Ogre::uint getImpostorResolution()
ImpostorBlendMode getBlendMode() const
Ogre::Vector3 m_vecCenter
void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist)
Sets fade behavior for this page.
Ogre::SceneManager * m_pSceneMgr
~ImpostorPage()
Destructor.
ImpostorBatch * getImpostorBatch(const Ogre::String &key) const
static Ogre::BillboardOrigin getImpostorPivot()
static Ogre::uint s_nUpdateInstanceID
void setVisible(bool visible)
Toggle the entire page's visibility.
Ogre::Timer m_timerUpdate
static Ogre::BillboardOrigin s_impostorPivot
bool injectImpostorBatch(const Ogre::String &key, ImpostorBatch *injectedBatch)
ImpostorPage()
Default constructor.
void removeEntities()
Remove all geometry/entities from the page completely.
static Ogre::ColourValue s_clrImpostorBackground
static const Ogre::ColourValue & getImpostorBackgroundColor()
void init(PagedGeometry *geom, const Ogre::Any &data)
Prepare a geometry page for use.
PagedGeometry * getParentPagedGeometry() const
Ogre::ResourceHandle sourceMesh
Ogre::MaterialPtr material[IMPOSTOR_PITCH_ANGLES][IMPOSTOR_YAW_ANGLES]
static Ogre::String getUniqueID(const Ogre::String &prefix)
static ImpostorTexture * getTexture(ImpostorPage *group, Ogre::Entity *entity)
Returns a pointer to an ImpostorTexture for the specified entity.
static unsigned long GUID
static std::map< Ogre::String, ImpostorTexture * > selfList
static void removeTexture(ImpostorTexture *Texture)
remove created texture, note that all of the ImposterTextures must be deleted at once,...
Ogre::TexturePtr texture
Ogre::SceneManager * sceneMgr
void renderTextures(bool force)
std::auto_ptr< ImpostorTextureResourceLoader > loader
Ogre::AxisAlignedBox boundingBox
Responsible for making sure that the texture is rerendered when the texture resource needs to be relo...
virtual void loadResource(Ogre::Resource *resource)
At load time the texture will be rerendered.
A class providing highly optimized methods for rendering massive amounts of geometry.
A faster alternative to Ogre's built-in BillboardSet class.
void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist)
Enables/disables distance fade-out for this billboard set.
void build()
Performs final steps required for the created billboards to appear in the scene.
void setVisible(bool visible)
Sets whether or not this StaticBillboardSet will be rendered.
void createBillboard(const Ogre::Vector3 &position, float xScale=1.0f, float yScale=1.0f, const Ogre::ColourValue &color=Ogre::ColourValue::White, Ogre::uint16 texcoordIndexU=0, Ogre::uint16 texcoordIndexV=0)
Adds a billboard to the StaticBillboardSet at the specified position.
void clear()
Deletes all billboards from the scene.
ImpostorBlendMode
Blend modes used by ImpostorPage::setBlendMode()
@ ALPHA_BLEND_IMPOSTOR
@ ALPHA_REJECT_IMPOSTOR