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

« back to all changes in this revision

Viewing changes to src/components/ogre/environment/Forest.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: Forest
 
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 "Forest.h"
 
28
#include "EmberEntityLoader.h"
 
29
 
 
30
#include "framework/LoggingInstance.h"
 
31
#include "services/config/ConfigService.h"
 
32
#include "pagedgeometry/include/PagedGeometry.h"
 
33
#include "pagedgeometry/include/TreeLoader3D.h"
 
34
#include "pagedgeometry/include/BatchPage.h"
 
35
#include "pagedgeometry/include/ImpostorPage.h"
 
36
#include "pagedgeometry/include/DummyPage.h"
 
37
#include "pagedgeometry/include/PassiveEntityPage.h"
 
38
#include "pagedgeometry/include/BatchedGeometry.h"
 
39
 
 
40
#include "../MathConverter.h"
 
41
#include "../AvatarCamera.h"
 
42
#include "../EmberOgre.h"
 
43
#include "../terrain/TerrainInfo.h"
 
44
#include "../terrain/TerrainGenerator.h"
 
45
#include "../terrain/ISceneManagerAdapter.h"
 
46
 
 
47
namespace EmberOgre {
 
48
 
 
49
namespace Environment {
 
50
 
 
51
Forest::Forest()
 
52
: mTrees(0)
 
53
, mTreeLoader(0)
 
54
, mEntityLoader(0)
 
55
{
 
56
        Ogre::Root::getSingleton().addFrameListener(this);
 
57
}
 
58
 
 
59
 
 
60
Forest::~Forest()
 
61
{
 
62
        delete mEntityLoader;
 
63
        delete mTreeLoader;
 
64
        delete mTrees;
 
65
        Ogre::Root::getSingleton().removeFrameListener(this);
 
66
}
 
67
 
 
68
void Forest::initialize()
 
69
{
 
70
        S_LOG_INFO("Initializing forest.");
 
71
        const WFMath::AxisBox<2>& worldSize = EmberOgre::getSingleton().getTerrainGenerator()->getTerrainInfo().getWorldSizeInIndices();
 
72
        if (worldSize.upperBound(0) - worldSize.lowerBound(0) > 0 && worldSize.upperBound(1) - worldSize.lowerBound(1) > 0) {
 
73
                
 
74
                Ogre::Camera& camera = EmberOgre::getSingleton().getMainCamera()->getCamera();
 
75
                
 
76
                mTrees = new PagedGeometry::PagedGeometry();
 
77
                mTrees->setCamera(&camera);     //Set the camera so PagedGeometry knows how to calculate LODs
 
78
                mTrees->setPageSize(64);        //Set the size of each page of geometry
 
79
                
 
80
                ::PagedGeometry::TBounds ogreBounds(Atlas2Ogre(worldSize));
 
81
                if (ogreBounds.width() != ogreBounds.height()) {
 
82
                        if (ogreBounds.width() > ogreBounds.height()) {
 
83
                                float difference = ogreBounds.width() - ogreBounds.height();
 
84
                                ogreBounds.bottom += difference;
 
85
                        } else {
 
86
                                float difference = ogreBounds.height() - ogreBounds.width();
 
87
                                ogreBounds.right += difference;
 
88
                        }
 
89
                }
 
90
                mTrees->setBounds(ogreBounds);
 
91
        //      mTrees->addDetailLevel<PagedGeometry::BatchPage>(150, 50);              //Use batches up to 150 units away, and fade for 30 more units
 
92
        //  mTrees->addDetailLevel<PagedGeometry::DummyPage>(100, 0);           //Use batches up to 150 units away, and fade for 30 more units  
 
93
                mTrees->addDetailLevel<PagedGeometry::PassiveEntityPage>(150, 0);               //Use standard entities up to 150 units away, and don't fade since the PassiveEntityPage doesn't support this (yet)
 
94
                mTrees->addDetailLevel<PagedGeometry::ImpostorPage>(500, 50);   //Use impostors up to 400 units, and for for 50 more units
 
95
        
 
96
                //Create a new TreeLoader2D object
 
97
                mEntityLoader = new EmberEntityLoader(*mTrees, 64);
 
98
        //      mTreeLoader = new PagedGeometry::TreeLoader3D(mTrees, Atlas2Ogre(worldSize));
 
99
                mTrees->setPageLoader(mEntityLoader);   //Assign the "treeLoader" to be used to load geometry for the PagedGeometry instance    
 
100
        }
 
101
}
 
102
 
 
103
void Forest::addTree(Ogre::Entity *entity, const Ogre::Vector3 &position, Ogre::Degree yaw, Ogre::Real scale)
 
104
{
 
105
        return;
 
106
        if (mTreeLoader && mTrees)
 
107
        {
 
108
                S_LOG_VERBOSE("Adding tree of entity type " << entity->getMesh()->getName() << " to position x: " << position.x << " y: " << position.y << " z: " << position.z << " and scale " << scale);
 
109
                try {
 
110
                        mTreeLoader->addTree(entity, position, yaw, scale);
 
111
                } catch (const std::exception& ex)
 
112
                {
 
113
                        S_LOG_FAILURE("Error when adding tree: " << ex.what());
 
114
                }
 
115
        } else {
 
116
                S_LOG_WARNING("Could not add tree before the forest has been initialized.");
 
117
        }
 
118
}
 
119
 
 
120
bool Forest::frameStarted(const Ogre::FrameEvent & evt)
 
121
{
 
122
        if (mTrees) {
 
123
                try {
 
124
                        mTrees->update();
 
125
                } catch (const Ogre::Exception& ex)
 
126
                {
 
127
                        S_LOG_FAILURE("Error when updating forest. Will disable forest.\n"<< ex.what());
 
128
                        delete mTreeLoader;
 
129
                        delete mEntityLoader;
 
130
                        delete mTrees;
 
131
                        mTrees = 0;
 
132
                        mTreeLoader = 0;
 
133
                        mEntityLoader = 0;
 
134
                }
 
135
        }
 
136
        return true;
 
137
}
 
138
 
 
139
void Forest::addEmberEntity(EmberPhysicalEntity * entity)
 
140
{
 
141
        if (mEntityLoader) {
 
142
                mEntityLoader->addEmberEntity(entity);
 
143
        }
 
144
}
 
145
 
 
146
void Forest::removeEmberEntity(EmberPhysicalEntity * entity)
 
147
{
 
148
        if (mEntityLoader) {
 
149
                mEntityLoader->removeEmberEntity(entity);
 
150
        }
 
151
}
 
152
 
 
153
}
 
154
 
 
155
}
 
156
 
 
157