tdev
28-07-2008 07:24:04
so i had a problem to face:
add a decent amount of trees to a 130x130 km terrain. If you do this with the default TreeLoader2D and addTree for like 30 million times, you will have to wait a long time and the memory consumption will be great. So i derived a new TreeLoader from the original class, which adds new trees to each loaded page dynamically.
What do you think about it?
Also, after flying around a bit in the terrain, the whole thing slows down a lot. What could be the cause?
to be able to derivated, change private: to protected: in treeloader2d.h (why is this no default?)
add a decent amount of trees to a 130x130 km terrain. If you do this with the default TreeLoader2D and addTree for like 30 million times, you will have to wait a long time and the memory consumption will be great. So i derived a new TreeLoader from the original class, which adds new trees to each loaded page dynamically.
What do you think about it?
Also, after flying around a bit in the terrain, the whole thing slows down a lot. What could be the cause?
#ifndef __RandomTreeLoader_H__
#define __RandomTreeLoader_H__
#include "PagedGeometry.h"
#include "PropertyMaps.h"
#include "TreeLoader2D.h"
#include <OgrePrerequisites.h>
namespace Forests {
class TreeIterator3D;
class TreeIterator2D;
class RandomTreeLoader : public TreeLoader2D
{
public:
RandomTreeLoader(PagedGeometry *geom, const TBounds &bounds) : TreeLoader2D(geom, bounds)
{
}
~RandomTreeLoader()
{
}
void loadPage(PageInfo &page)
{
//Calculate x/z indexes for the grid array
page.xIndex -= Math::Floor(gridBounds.left / pageSize);
page.zIndex -= Math::Floor(gridBounds.top / pageSize);
//Check if the requested page is in bounds
if (page.xIndex < 0 || page.zIndex < 0 || page.xIndex >= pageGridX || page.zIndex >= pageGridZ)
return;
// just take the first tree registered as an example
Entity *entity = pageGridList.begin()->first;
std::vector<TreeDef> *pageGrid = pageGridList.begin()->second;
std::vector<TreeDef> &treeList = _getGridPage(pageGrid, page.xIndex, page.zIndex);
// example values
minimumScale=0.07;
maximumScale=0.13;
// if there are no tress in the page to be loaded, add some randomly
if(treeList.size() == 0)
{
// only add trees if there are none in this page...
for (int n=0; n < pageSize/10; n++)
{
//Create the new tree
Real xrel = Math::RangeRandom(0, pageSize);
Real zrel = Math::RangeRandom(0, pageSize);
Degree yaw(Math::RangeRandom(0, 360));
Real scale = Math::RangeRandom(minimumScale, maximumScale);
TreeDef tree;
tree.xPos = 65535 * (xrel - (page.xIndex * pageSize)) / pageSize;
tree.zPos = 65535 * (zrel - (page.zIndex * pageSize)) / pageSize;
tree.rotation = 255 * (yaw.valueDegrees() / 360.0f);
tree.scale = 255 * ((scale - minimumScale) / maximumScale);
treeList.push_back(tree);
}
}
// now load page as normal
TreeLoader2D::loadPage(page);
}
};
}
#endif
to be able to derivated, change private: to protected: in treeloader2d.h (why is this no default?)