2
// C++ Implementation: FoliageLoader
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2008
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 2 of the License, or
12
// (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
27
#include "FoliageLoader.h"
28
#include "../terrain/TerrainLayerDefinition.h"
30
#include "../MathConverter.h"
31
#include "../EmberOgre.h"
32
#include "../terrain/TerrainGenerator.h"
33
#include "../terrain/TerrainPageFoliage.h"
34
#include "../terrain/TerrainPage.h"
35
#include "../terrain/TerrainLayerDefinition.h"
36
#include "framework/LoggingInstance.h"
37
#include <wfmath/intersect.h>
42
using namespace EmberOgre::Terrain;
45
namespace Environment {
47
FoliageLoader::FoliageLoader(const Terrain::TerrainLayerDefinition& terrainLayerDefinition, const Terrain::TerrainFoliageDefinition& foliageDefinition)
48
: mTerrainLayerDefinition(terrainLayerDefinition)
49
, mFoliageDefinition(foliageDefinition)
53
Ogre::SceneManager* sceneMgr = EmberOgre::getSingleton().getSceneManager();
54
mEntity = sceneMgr->createEntity(std::string("shrubbery_") + mFoliageDefinition.getPlantType(), mFoliageDefinition.getParameter("mesh"));
56
mMinScale = atof(mFoliageDefinition.getParameter("minScale").c_str());
57
mMaxScale = atof(mFoliageDefinition.getParameter("maxScale").c_str());
62
FoliageLoader::~FoliageLoader()
66
void FoliageLoader::loadPage(::PagedGeometry::PageInfo &page)
68
///make these static for fast lookup
69
static Ogre::Vector2 pos2D;
70
static Ogre::ColourValue colour(1,1,1,1);
71
static Terrain::TerrainGenerator* terrainGenerator(EmberOgre::getSingleton().getTerrainGenerator());
73
TerrainPosition wfPos(Ogre2Atlas_TerrainPosition(page.centerPoint));
74
TerrainPage* terrainPage = terrainGenerator->getTerrainPageAtPosition(wfPos);
76
Ogre::TRect<float> ogrePageExtent = Atlas2Ogre(terrainPage->getExtent());
77
Ogre::TRect<float> adjustedBounds = Ogre::TRect<float>(page.bounds.left - ogrePageExtent.left, page.bounds.top - ogrePageExtent.top, page.bounds.right - ogrePageExtent.left, page.bounds.bottom - ogrePageExtent.top);
78
TerrainPageFoliage::PlantStore plants;
80
unsigned char threshold(100);
81
if (mFoliageDefinition.getParameter("threshold") != "") {
82
threshold = static_cast<unsigned char>(atoi(mFoliageDefinition.getParameter("threshold").c_str()));
85
terrainPage->getPageFoliage()->getPlantsForArea(mTerrainLayerDefinition, threshold, mFoliageDefinition.getPlantType(), adjustedBounds, plants);
86
for (TerrainPageFoliage::PlantStore::const_iterator I = plants.begin(); I != plants.end(); ++I) {
87
Ogre::Vector3 pos(I->x + ogrePageExtent.left, terrainGenerator->getHeight(TerrainPosition(I->x + ogrePageExtent.left, -(I->y + ogrePageExtent.top))), I->y + ogrePageExtent.top);
89
float scale = Ogre::Math::RangeRandom(mMinScale, mMaxScale);
92
// terrainGenerator->getShadowColourAt(pos2D, colour);
95
Ogre::Degree angle(Ogre::Math::RangeRandom(0, 360.0f));
96
Ogre::Quaternion rot(angle, Ogre::Vector3::UNIT_Y);
99
addEntity(mEntity, pos, rot, Ogre::Vector3(scale,scale,scale), colour);