~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/environment/FoliageLoader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: FoliageLoader
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2008
 
8
//
 
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.
 
13
// 
 
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.
 
18
// 
 
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.//
 
22
//
 
23
#ifdef HAVE_CONFIG_H
 
24
#include "config.h"
 
25
#endif
 
26
 
 
27
#include "FoliageLoader.h"
 
28
#include "../terrain/TerrainLayerDefinition.h"
 
29
 
 
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>
 
38
 
 
39
 
 
40
#include <Ogre.h>
 
41
 
 
42
using namespace EmberOgre::Terrain;
 
43
namespace EmberOgre {
 
44
 
 
45
namespace Environment {
 
46
 
 
47
FoliageLoader::FoliageLoader(const Terrain::TerrainLayerDefinition& terrainLayerDefinition, const Terrain::TerrainFoliageDefinition& foliageDefinition)
 
48
: mTerrainLayerDefinition(terrainLayerDefinition)
 
49
, mFoliageDefinition(foliageDefinition)
 
50
, mMinScale(1)
 
51
, mMaxScale(1)
 
52
{
 
53
        Ogre::SceneManager* sceneMgr = EmberOgre::getSingleton().getSceneManager();
 
54
        mEntity = sceneMgr->createEntity(std::string("shrubbery_") + mFoliageDefinition.getPlantType(), mFoliageDefinition.getParameter("mesh"));
 
55
        
 
56
        mMinScale = atof(mFoliageDefinition.getParameter("minScale").c_str());
 
57
        mMaxScale = atof(mFoliageDefinition.getParameter("maxScale").c_str());
 
58
 
 
59
}
 
60
 
 
61
 
 
62
FoliageLoader::~FoliageLoader()
 
63
{
 
64
}
 
65
 
 
66
void FoliageLoader::loadPage(::PagedGeometry::PageInfo &page)
 
67
{
 
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());
 
72
 
 
73
        TerrainPosition wfPos(Ogre2Atlas_TerrainPosition(page.centerPoint));
 
74
        TerrainPage* terrainPage = terrainGenerator->getTerrainPageAtPosition(wfPos);
 
75
        if (terrainPage) {
 
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;
 
79
                
 
80
                unsigned char threshold(100);
 
81
                if (mFoliageDefinition.getParameter("threshold") != "") {
 
82
                        threshold = static_cast<unsigned char>(atoi(mFoliageDefinition.getParameter("threshold").c_str()));
 
83
                }
 
84
                
 
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);
 
88
                        
 
89
                        float scale = Ogre::Math::RangeRandom(mMinScale, mMaxScale);
 
90
                        pos2D.x = pos.x;
 
91
                        pos2D.y = pos.z;
 
92
//                      terrainGenerator->getShadowColourAt(pos2D, colour);
 
93
                        
 
94
                        //Get rotation
 
95
                        Ogre::Degree angle(Ogre::Math::RangeRandom(0, 360.0f));
 
96
                        Ogre::Quaternion rot(angle, Ogre::Vector3::UNIT_Y);
 
97
                        
 
98
 
 
99
                        addEntity(mEntity, pos, rot, Ogre::Vector3(scale,scale,scale), colour);
 
100
                }
 
101
        }
 
102
}
 
103
 
 
104
 
 
105
}
 
106
 
 
107
}