Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
GrassLoader.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------------------
2Copyright (c) 2006 John Judnich
3
4This 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.
5Permission 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:
6 1. 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.
7 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
8 3. This notice may not be removed or altered from any source distribution.
9-------------------------------------------------------------------------------------*/
10
11#ifndef __GrassLoader_H__
12#define __GrassLoader_H__
13
14#include "PagedGeometry.h"
15#include "PropertyMaps.h"
16#include "RandomTable.h"
17
18#include "OgrePrerequisites.h"
19#include "OgreMaterial.h"
20#include "OgrePixelFormat.h"
21#include "OgreStringConverter.h"
22
23
24namespace Forests {
25
26class GrassLayer;
27
50{
51public:
56
70 GrassLayer *addLayer(const Ogre::String &material);
71
75 void deleteLayer(GrassLayer *layer);
76
81 inline std::list<GrassLayer*> &getLayerList() { return layerList; }
82
94 inline void setWindDirection(Ogre::Vector3 &dir) { windDir = dir; }
95
99 inline Ogre::Vector3 &getWindDirection() { return windDir; }
100
113 inline void setDensityFactor(float density) { densityFactor = density; }
114
118 inline float getDensityFactor() { return densityFactor; }
119
135 inline void setRenderQueueGroup(Ogre::uint8 queueID) { renderQueue = queueID; }
136
168 void setHeightFunction(Ogre::Real (*heightFunction)(Ogre::Real x, Ogre::Real z, void *userData), void *userData = NULL) {
170 heightFunctionUserData = userData;
171 }
172
174 void loadPage(PageInfo &page);
176 void unloadPage(PageInfo &page);
178 void frameUpdate();
179
180 static float getRangeRandom(float start, float end);
181
182
183private:
184 friend class GrassLayer;
185
186 //Helper functions
187 Ogre::Mesh *generateGrass_QUAD(PageInfo &page, GrassLayer *layer, const float *grassPositions, unsigned int grassCount);
188 Ogre::Mesh *generateGrass_CROSSQUADS(PageInfo &page, GrassLayer *layer, const float *grassPositions, unsigned int grassCount);
189 Ogre::Mesh *generateGrass_SPRITE(PageInfo &page, GrassLayer *layer, const float *grassPositions, unsigned int grassCount);
190
191 //List of grass types
192 std::list<GrassLayer*> layerList;
193
194 //Height data
195 Ogre::Real (*heightFunction)(Ogre::Real x, Ogre::Real z, void *userData); //Pointer to height function
197
198 //Misc.
200 Ogre::uint8 renderQueue;
202
203 // random table
205
206 //Animation
207 Ogre::Timer windTimer;
208 Ogre::Vector3 windDir;
209 unsigned long lastTime;
210
211 static unsigned long GUID;
212 static inline Ogre::String getUniqueID()
213 {
214 return "GrassLDR" + Ogre::StringConverter::toString(++GUID);
215 }
216};
217
229{
230public:
232 void setMaterialName(const Ogre::String &matName);
233
235 void setMinimumSize(float width, float height);
236
238 void setMaximumSize(float width, float height);
239
241 void setDensity(float density) { this->density = density; }
242
259
269 void setMaxSlope(const float maxSlopeRatio) { maxSlope = maxSlopeRatio; }
270
271 void setMaxSlope(Ogre::Radian maxSlopeAngle) {
272 if (maxSlopeAngle > Ogre::Degree(89.99f))
273 maxSlopeAngle = Ogre::Degree(89.99f);
274 if (maxSlopeAngle < Ogre::Degree(0))
275 maxSlopeAngle = Ogre::Degree(0);
276
277 maxSlope = Ogre::Math::Tan(maxSlopeAngle);
278 }
279
286 Ogre::Real getMaxSlope() const { return maxSlope; }
287
307 void setDensityMap(const Ogre::String &mapFile, MapChannel channel = CHANNEL_COLOR);
308
316 void setDensityMap(Ogre::TexturePtr map, MapChannel channel = CHANNEL_COLOR);
317
328 void setDensityMapFilter(MapFilter filter);
329
350 void setColorMap(const Ogre::String &mapFile, MapChannel channel = CHANNEL_COLOR);
351
359 void setColorMap(Ogre::TexturePtr map, MapChannel channel = CHANNEL_COLOR);
360
371 void setColorMapFilter(MapFilter filter);
372
384 void setMapBounds(const TBounds &bounds)
385 {
386 mapBounds = bounds;
387 }
388
399
409 ColorMap* getColorMap() const { return colorMap; }
410
412 bool getBlendValue() const { return blend;};
413
416
418 bool getLightingEnabled() const { return lighting; }
419
421 const TBounds& getMapBounds() const { return mapBounds; }
422
438 void setRenderTechnique(GrassTechnique style, bool blendBase = false);
439
449
456 void setAnimationEnabled(bool enabled);
457
458
461 void setLightingEnabled(bool enabled);
462
467 void setSwayLength(Ogre::Real mag) { animMag = mag; }
468
470 void setSwaySpeed(Ogre::Real speed) { animSpeed = speed; }
471
480 void setSwayDistribution(Ogre::Real freq) { animFreq = freq; }
481
482private:
483 friend class GrassLoader;
484
487
489 ~GrassLayer();
490
491 //Updates the vertex shader used by this layer based on the animate enable status
492 void _updateShaders();
493
494 //Used by GrassLoader::loadPage() - populates an array with a uniform distribution of grass
495 //Returns the final number of grasses, which will always be <= grassCount
496 unsigned int _populateGrassList_Uniform(PageInfo page, float *posBuff, unsigned int grassCount);
497
498 //Used by GrassLoader::loadPage() - populates an array of grass positions based on the density map
499 //Returns the final number of grasses, which will always be <= grassCount
500 unsigned int _populateGrassList_UnfilteredDM(PageInfo page, float *posBuff, unsigned int grassCount);
501
502 //Variation of _populateGrassList(), using bilinear filtering on the density map lookups
503 //Returns the final number of grasses, which will always be <= grassCount
504 unsigned int _populateGrassList_BilinearDM(PageInfo page, float *posBuff, unsigned int grassCount);
505
506
508
509 //Grass material/shape properties
510 Ogre::MaterialPtr material;
511 Ogre::Real density;
512 Ogre::Real minWidth, maxWidth;
513 Ogre::Real minHeight, maxHeight;
514
515 Ogre::Real minY, maxY;
516 Ogre::Real maxSlope;
517
520
521 //Property maps
523
526
529
530 //Grass shader properties
533
534 //Current frame of animation for this layer
535 Ogre::Real waveCount;
536
538};
539
540
549{
550public:
551 void init(PagedGeometry *geom, const Ogre::Any &data);
552 ~GrassPage();
553
554 void addEntity(Ogre::Entity *ent, const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, const Ogre::Vector3 &scale, const Ogre::ColourValue &color);
555 void removeEntities();
556 void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist) {}
557 void setVisible(bool visible);
558
559private:
560 Ogre::SceneManager *sceneMgr;
561 Ogre::SceneNode *rootNode;
562
563 std::list<Ogre::SceneNode*> nodeList;
564
565 static unsigned long GUID;
566 static inline Ogre::String getUniqueID()
567 {
568 return "GrassPage" + Ogre::StringConverter::toString(++GUID);
569 }
570};
571
572
573}
574
575#endif
A 2D greyscale image that is assigned to a certain region of your world to represent color levels.
A 2D greyscale image that is assigned to a certain region of your world to represent density levels.
This base-class can be extended to provide different ways of representing entities.
A data structure giving you full control over grass properties.
void setColorMapFilter(MapFilter filter)
Sets the filtering mode used for color maps.
GrassTechnique renderTechnique
ColorMap * getColorMap() const
Gets a pointer to the color map being used.
DensityMap * getDensityMap() const
Gets a pointer to the density map being used.
const TBounds & getMapBounds() const
Gets the map bounds.
void setMaxSlope(const float maxSlopeRatio)
Set the maximum slope a grass of blade can be placed on.
void setMaximumSize(float width, float height)
Sets the maximum size that grass quads/billboards will be.
Ogre::MaterialPtr material
void setMaxSlope(Ogre::Radian maxSlopeAngle)
void setDensityMapFilter(MapFilter filter)
Sets the filtering mode used for density maps.
MapFilter colorMapFilter
void setColorMap(const Ogre::String &mapFile, MapChannel channel=CHANNEL_COLOR)
Sets the color map used for this grass layer.
bool getBlendValue() const
Gives back the value of blending.
void setSwayDistribution(Ogre::Real freq)
Sets the smooth distribution (positional phase shift) of the grass swaying animation.
MapFilter densityMapFilter
Ogre::Real getMaxSlope() const
Get the maximum slope a grass blade can be placed on (as set by setMaxSlope()).
void setLightingEnabled(bool enabled)
Enables/disables lighting on this layer.
void setRenderTechnique(GrassTechnique style, bool blendBase=false)
Sets the technique used to render this grass layer.
unsigned int _populateGrassList_BilinearDM(PageInfo page, float *posBuff, unsigned int grassCount)
GrassLoader * parent
void setSwaySpeed(Ogre::Real speed)
Sets the sway speed of the grass (measured in "sways-per-second")
void setMapBounds(const TBounds &bounds)
Sets the boundaries of the density/color maps.
unsigned int _populateGrassList_UnfilteredDM(PageInfo page, float *posBuff, unsigned int grassCount)
void setMinimumSize(float width, float height)
Sets the minimum size that grass quads/billboards will be.
~GrassLayer()
Do not delete a GrassLayer yourself - the GrassLoader will do this automatically when it's deleted.
void setDensity(float density)
Sets the maximum density (measured in grass quads/billboards per square unit) of grass.
void setFadeTechnique(FadeTechnique style)
Sets the technique used when fading out distant grass.
DensityMap * densityMap
PagedGeometry * geom
FadeTechnique fadeTechnique
GrassLoader * getParentGrassLoader() const
Gets the parent grassLoader of this layer.
void setHeightRange(float minHeight, float maxHeight=0)
Sets a minimum / maximum height where grass may appear.
void setAnimationEnabled(bool enabled)
Enables/disables animation on this layer.
void setDensityMap(const Ogre::String &mapFile, MapChannel channel=CHANNEL_COLOR)
Sets the density map used for this grass layer.
unsigned int _populateGrassList_Uniform(PageInfo page, float *posBuff, unsigned int grassCount)
void setColorMap(Ogre::TexturePtr map, MapChannel channel=CHANNEL_COLOR)
Sets the color map used for this grass layer.
bool getLightingEnabled() const
Gives back if lighting is enabled for grass.
void setDensityMap(Ogre::TexturePtr map, MapChannel channel=CHANNEL_COLOR)
Sets the density map used for this grass layer.
void setMaterialName(const Ogre::String &matName)
Sets the material that is applied to all grass billboards/quads.
void setSwayLength(Ogre::Real mag)
Sets how far grass should sway back and forth.
A PageLoader-derived object you can use with PagedGeometry to produce realistic grass.
Definition GrassLoader.h:50
Ogre::Timer windTimer
static unsigned long GUID
Ogre::uint8 renderQueue
void setWindDirection(Ogre::Vector3 &dir)
Sets the global wind direction for this GrassLoader.
Definition GrassLoader.h:94
Ogre::Vector3 windDir
void deleteLayer(GrassLayer *layer)
Removes and deletes a grass layer from the scene.
static Ogre::String getUniqueID()
Ogre::Real(* heightFunction)(Ogre::Real x, Ogre::Real z, void *userData)
void setRenderQueueGroup(Ogre::uint8 queueID)
Sets the render queue group the grass will be rendered through.
void setDensityFactor(float density)
Sets the global density factor for this GrassLoader.
Ogre::Vector3 & getWindDirection()
Returns the global wind direction for this GrassLoader.
Definition GrassLoader.h:99
void frameUpdate()
INTERNAL FUNCTION - DO NOT USE.
Ogre::Mesh * generateGrass_QUAD(PageInfo &page, GrassLayer *layer, const float *grassPositions, unsigned int grassCount)
Ogre::Mesh * generateGrass_CROSSQUADS(PageInfo &page, GrassLayer *layer, const float *grassPositions, unsigned int grassCount)
Ogre::Mesh * generateGrass_SPRITE(PageInfo &page, GrassLayer *layer, const float *grassPositions, unsigned int grassCount)
std::list< GrassLayer * > & getLayerList()
Returns a list of added grass layers.
Definition GrassLoader.h:81
static float getRangeRandom(float start, float end)
void unloadPage(PageInfo &page)
INTERNAL FUNCTION - DO NOT USE.
void loadPage(PageInfo &page)
INTERNAL FUNCTION - DO NOT USE.
void setHeightFunction(Ogre::Real(*heightFunction)(Ogre::Real x, Ogre::Real z, void *userData), void *userData=NULL)
Sets the height function used to calculate grass Y coordinates.
GrassLayer * addLayer(const Ogre::String &material)
Adds a grass layer to the scene.
std::list< GrassLayer * > layerList
RandomTable * rTable
PagedGeometry * geom
unsigned long lastTime
float getDensityFactor()
Returns the global density factor for this GrassLoader.
A custom page type designed specifically for use with GrassLoader.
Ogre::SceneNode * rootNode
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.
void setFade(bool enabled, Ogre::Real visibleDist, Ogre::Real invisibleDist)
Sets fade behavior for this page.
std::list< Ogre::SceneNode * > nodeList
Ogre::SceneManager * sceneMgr
void removeEntities()
Remove all geometry/entities from the page completely.
void init(PagedGeometry *geom, const Ogre::Any &data)
Prepare a geometry page for use.
void setVisible(bool visible)
Toggle the entire page's visibility.
static Ogre::String getUniqueID()
static unsigned long GUID
A class which you extend to provide a callback function for loading entities.
A class providing highly optimized methods for rendering massive amounts of geometry.
MapFilter
Specifies the filtering method used to interpret property maps.
GrassTechnique
A technique used to render grass.
Ogre::TRect< Ogre::Real > TBounds
Define TBounds as a TRect using Real numeric units.
FadeTechnique
A technique used to fade grass into the distance.
MapChannel
Specifies which color channel(s) to extract from an image.
@ CHANNEL_COLOR
Use the image's full RGB color information.
Useful page information supplied to a pageloader.