Modifications to Grass

novaumas

23-10-2008 14:50:32

Hi!

I recently posted about The Teddy Incident on the main Ogre forums, and said we used paged geometry.

I'd like to thank you for the awesome lib, we had it completely integrated into our game in just a day :)

I had to modify it a little bit to make it work with our needs, and I'd like to share the changes. [b]Here are all the changes in a single patch file.[/b]

The first difference with the use that I've commonly seen around here, is that we didn't use any terrain. We used plain meshes for the terrain.

That meant creating a custom height function, of course, but when we did so we stumbled on the following issue, depending on what type of grass we chose.



To solve this I made some slight changes to the grass generation function from grass loader, mainly adding a parameter so that the two ground vertices of a blade of grass have a maximum height difference.

here are the changes:

Index: GrassLoader.h
===================================================================
--- GrassLoader.h (revision 2499)
+++ GrassLoader.h (working copy)
@@ -169,7 +169,18 @@
heightFunctionUserData = userData;
}

+ /**
+ Set the maximum slope a grass of blade can be placed on.
+ Measured as the height difference between the two opposite sides of a grass blade.
+ */
+ void setMaxSlope( const float maxSlope_ ) { maxSlope = maxSlope_; }

+ /**
+ Get the maximum slope a grass blade can be placed on.
+ Measured as the height difference between the two opposite sides of a grass blade.
+ */
+ float getMaxSlope() const { return maxSlope; }
+
/** INTERNAL FUNCTION - DO NOT USE */
void loadPage(PageInfo &page);
/** INTERNAL FUNCTION - DO NOT USE */
@@ -197,6 +208,9 @@
Ogre::uint8 renderQueue;
float densityFactor;

+ // maximum slope in which a grass blade can be placed.
+ float maxSlope;
+
//Animation
Ogre::Timer windTimer;
Ogre::Vector3 windDir;



Index: GrassLoader.cpp
===================================================================
--- GrassLoader.cpp (revision 2499)
+++ GrassLoader.cpp (working copy)
@@ -54,6 +54,8 @@

windTimer.reset();
lastTime = 0;
+
+ maxSlope = 50;
}

GrassLoader::~GrassLoader()
@@ -260,6 +262,14 @@
if (heightFunction){
y1 = heightFunction(x1, z1, heightFunctionUserData);
y2 = heightFunction(x2, z2, heightFunctionUserData);
+
+ if ( getMaxSlope() < abs( y1 - y2 ) )
+ {
+ // degenerate the face
+ x2 = x1;
+ y2 = y1;
+ z2 = z1;
+ }
} else {
y1 = 0;
y2 = 0;
@@ -398,6 +408,13 @@
if (heightFunction){
y1 = heightFunction(x1, z1, heightFunctionUserData);
y2 = heightFunction(x2, z2, heightFunctionUserData);
+ if ( getMaxSlope() < abs( y1 - y2 ) )
+ {
+ // degenerate the face
+ x2 = x1;
+ y2 = y1;
+ z2 = z1;
+ }
} else {
y1 = 0;
y2 = 0;
@@ -434,6 +451,13 @@
if (heightFunction){
y3 = heightFunction(x3, z3, heightFunctionUserData);
y4 = heightFunction(x4, z4, heightFunctionUserData);
+ if ( getMaxSlope() < abs( y3 - y4 ) )
+ {
+ // degenerate the face
+ x4 = x3;
+ y4 = y3;
+ z4 = z3;
+ }
} else {
y3 = 0;
y4 = 0;


Also, the square dimensions check for the paged geometry gave us some trouble due to float precission issues. A slight change to the check function made it work flawlessly.


Index: PagedGeometry.cpp
===================================================================
--- PagedGeometry.cpp (revision 2499)
+++ PagedGeometry.cpp (working copy)
@@ -166,7 +166,7 @@
{
if (!managerList.empty())
OGRE_EXCEPT(0, "PagedGeometry::setBounds() cannot be called after detail levels have been added. Call removeDetailLevels() first.", "PagedGeometry::setBounds()");
- if (bounds.width() != bounds.height())
+ if ( ! Ogre::Math::RealEqual( bounds.width(), bounds.height(), 0.01 ) )
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Bounds must be square", "PagedGeometry::setBounds()");
if (bounds.width() <= 0 || bounds.height() <=0)
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Bounds must have positive width and height", "PagedGeometry::setBounds()");

JohnJ

25-10-2008 00:06:02

Thanks for the patch!

Another possible solution to the problem would be to use density maps to remove grass from the cliffs, but obviously this solution is better. Should be very useful to people using non-2D terrains.

milamber08

09-11-2008 15:17:33

@john_j - Will this modification make it into the mainstream Pagedgeometry library? Seems like one of those sensible little items that most people would use.

JohnJ

10-11-2008 04:43:40

Yes, I plan to merge this with the official version soon.

Edit: Done.